1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *########################################################################
|
---|
26 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import java.util.*;
|
---|
30 | import org.greenstone.gatherer.DebugStream;
|
---|
31 | import org.greenstone.gatherer.Gatherer;
|
---|
32 | import org.greenstone.gatherer.msm.ElementWrapper;
|
---|
33 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
34 | import org.greenstone.gatherer.util.Utility;
|
---|
35 | import org.greenstone.gatherer.util.XMLTools;
|
---|
36 | import org.w3c.dom.*;
|
---|
37 |
|
---|
38 | /** This class encapsulates a single indexing pair.
|
---|
39 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
40 | * @version 2.3d
|
---|
41 | */
|
---|
42 | public class Index
|
---|
43 | implements Comparable, DOMProxyListEntry {
|
---|
44 | /** An values of items in the index level enumeration. */
|
---|
45 | static public final String LEVEL[] = {CollectionConfiguration.DOCUMENT_STR, CollectionConfiguration.PARAGRAPH_STR, CollectionConfiguration.SECTION_STR};
|
---|
46 |
|
---|
47 | private ArrayList sources = null;
|
---|
48 | /** The level of this index (if old sckool MG). */
|
---|
49 | private int level = -1;
|
---|
50 | /** The element this index is based upon. */
|
---|
51 | private Element element = null;
|
---|
52 | /** The unique, if cryptic, identifier of an index. */
|
---|
53 | private String id = null;
|
---|
54 |
|
---|
55 | /** Default constructor, which should only be used during DOMProxyListModel creation. */
|
---|
56 | public Index() {
|
---|
57 | }
|
---|
58 |
|
---|
59 | /** Constructor. */
|
---|
60 | public Index(Element element) {
|
---|
61 | this.element = element;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Constructor for a newly assigned index. */
|
---|
65 | public Index(ArrayList sources) {
|
---|
66 | this.sources = sources;
|
---|
67 | // Create a new element
|
---|
68 | Document document = CollectionDesignManager.collect_config.document;
|
---|
69 | element = document.createElement(CollectionConfiguration.INDEX_ELEMENT);
|
---|
70 | // For each source add a content element
|
---|
71 | int size = sources.size();
|
---|
72 | for(int i = 0; i < size; i++) {
|
---|
73 | Element content_element = document.createElement(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
74 | Object source_object = sources.get(i);
|
---|
75 | if(source_object instanceof ElementWrapper) {
|
---|
76 | ///DebugStream.println("Found ElementWrapper as source: " + ((ElementWrapper)source_object).getName());
|
---|
77 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, ((ElementWrapper)source_object).getName());
|
---|
78 | }
|
---|
79 | else {
|
---|
80 | ///DebugStream.println("Found String as source: " + source_object.toString());
|
---|
81 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, source_object.toString());
|
---|
82 | }
|
---|
83 | element.appendChild(content_element);
|
---|
84 | content_element = null;
|
---|
85 | }
|
---|
86 | document = null;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Constructor for a newly assigned index with specified level (old skool). */
|
---|
90 | public Index(int level, ArrayList sources) {
|
---|
91 | this(sources);
|
---|
92 | this.level = level;
|
---|
93 | element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[level]);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public Index(String level, ArrayList sources) {
|
---|
97 | this(sources);
|
---|
98 | for(int i = 0; i < LEVEL.length; i++) {
|
---|
99 | if(LEVEL[i].equalsIgnoreCase(level)) {
|
---|
100 | this.level = i;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[this.level]);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Method to compare two indexes.
|
---|
107 | * @param object The other index as an <strong>Object</strong>.
|
---|
108 | * @return An <i>int</i> which indicates how the indexes compare.
|
---|
109 | * @see java.lang.String
|
---|
110 | */
|
---|
111 | public int compareTo(Object object) {
|
---|
112 | if(object == null) {
|
---|
113 | return -1;
|
---|
114 | }
|
---|
115 | String id = getID();
|
---|
116 | return id.compareTo(((Index)object).getID());
|
---|
117 | }
|
---|
118 |
|
---|
119 | public DOMProxyListEntry create(Element element) {
|
---|
120 | return new Index(element);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** Method to test for the equality of two indexes.
|
---|
124 | * @param object The other index as an <strong>Object</strong>.
|
---|
125 | * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
|
---|
126 | */
|
---|
127 | public boolean equals(Object object) {
|
---|
128 | return (compareTo(object) == 0);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public Element getElement() {
|
---|
132 | return element;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Method to get the value of level.
|
---|
136 | * @return the level as a int
|
---|
137 | */
|
---|
138 | public int getLevel() {
|
---|
139 | if(level == -1) {
|
---|
140 | String level_str = element.getAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE);
|
---|
141 | for(int i = 0; level == -1 && i < LEVEL.length; i++) {
|
---|
142 | if(level_str.equals(LEVEL[i])) {
|
---|
143 | level = i;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | level_str = null;
|
---|
147 | }
|
---|
148 | return level;
|
---|
149 | }
|
---|
150 |
|
---|
151 | public String getID() {
|
---|
152 | if(element == null) {
|
---|
153 | id="";
|
---|
154 | }
|
---|
155 | else if(id == null) {
|
---|
156 | StringBuffer id_buffer = new StringBuffer();
|
---|
157 | // Write level information, if any.
|
---|
158 | int level = getLevel();
|
---|
159 | if(0 <= level && level < 3) {
|
---|
160 | id_buffer.append(LEVEL[level]);
|
---|
161 | id_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
162 | }
|
---|
163 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
164 | ArrayList sources = getSources();
|
---|
165 | int sources_size = sources.size();
|
---|
166 | for(int i = 0; i < sources_size; i++) {
|
---|
167 | Object source_object = sources.get(i);
|
---|
168 | // If its an element wrapper use the unique name rather than the
|
---|
169 | if(source_object instanceof ElementWrapper) {
|
---|
170 | String full_element_name = ((ElementWrapper)source_object).getName();
|
---|
171 | if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
|
---|
172 | id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
|
---|
173 | }
|
---|
174 | else {
|
---|
175 | id_buffer.append(full_element_name);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | else {
|
---|
179 | id_buffer.append(source_object.toString());
|
---|
180 | }
|
---|
181 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
182 | }
|
---|
183 | sources = null;
|
---|
184 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
185 | }
|
---|
186 | return id;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Tries to retrieve this indexes name according to the CollectionMetaManager. */
|
---|
190 | public String getName() {
|
---|
191 | CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + getID(), false);
|
---|
192 | if(metadatum != null) {
|
---|
193 | return metadatum.getValue(CollectionMeta.TEXT);
|
---|
194 | }
|
---|
195 | return "";
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** Retrieve the sources of this index.
|
---|
199 | * @return the sources as an ArrayList
|
---|
200 | */
|
---|
201 | public ArrayList getSources() {
|
---|
202 | if(sources == null) {
|
---|
203 | sources = new ArrayList();
|
---|
204 | NodeList content_elements = element.getElementsByTagName(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
205 | int content_elements_length = content_elements.getLength();
|
---|
206 | for(int i = 0; i < content_elements_length; i++) {
|
---|
207 | Element content_element = (Element) content_elements.item(i);
|
---|
208 | String source_str = (String) content_element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
|
---|
209 | ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(source_str);
|
---|
210 | if(element_wrapper != null) {
|
---|
211 | sources.add(element_wrapper);
|
---|
212 | }
|
---|
213 | else {
|
---|
214 | sources.add(source_str);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | content_elements = null;
|
---|
218 | if(sources != null && sources.size() > 1) {
|
---|
219 | Collections.sort(sources);
|
---|
220 | }
|
---|
221 | }
|
---|
222 | return sources;
|
---|
223 | }
|
---|
224 |
|
---|
225 | public boolean isAssigned() {
|
---|
226 | return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
|
---|
227 | }
|
---|
228 |
|
---|
229 | public void setAssigned(boolean assigned) {
|
---|
230 | if(element != null) {
|
---|
231 | element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | public void setElement(Element element) {
|
---|
236 | this.element = element;
|
---|
237 | this.level = -1;
|
---|
238 | this.id = null;
|
---|
239 | this.sources = null;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /** Method to set the level of this index which can only be used for the default index.
|
---|
243 | * @param new_level the new level as an int
|
---|
244 | */
|
---|
245 | public void setLevel(int new_level) {
|
---|
246 | System.err.println("SetLevel(" + new_level + ")");
|
---|
247 | if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
|
---|
248 | element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[new_level]);
|
---|
249 | this.id = null; // Regenerate ID.
|
---|
250 | this.level = new_level;
|
---|
251 | }
|
---|
252 | else {
|
---|
253 | DebugStream.println("Error! Called setLevel() of index other than the default.");
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Method to set the sources for this index which can only be used for the default index.
|
---|
258 | * @param sources an ArrayList of source names
|
---|
259 | */
|
---|
260 | public void setSources(ArrayList sources) {
|
---|
261 | if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
|
---|
262 | // Erase old sources
|
---|
263 | XMLTools.clear(element);
|
---|
264 | // For each entry in the sources array add a new content element.
|
---|
265 | int size = sources.size();
|
---|
266 | for(int i = 0; i < size; i++) {
|
---|
267 | Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
268 | Object source_object = sources.get(i);
|
---|
269 | if(source_object instanceof ElementWrapper) {
|
---|
270 | //DebugStream.println("Found ElementWrapper as source: " + ((ElementWrapper)source_object).getName());
|
---|
271 | String name = ((ElementWrapper)source_object).getName();
|
---|
272 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, name);
|
---|
273 | name = null;
|
---|
274 | }
|
---|
275 | else {
|
---|
276 | //DebugStream.println("Found String as source: " + source_object.toString());
|
---|
277 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, source_object.toString());
|
---|
278 | }
|
---|
279 | source_object = null;
|
---|
280 | element.appendChild(content_element);
|
---|
281 | content_element = null;
|
---|
282 | }
|
---|
283 | this.id = null; // Regenerate ID.
|
---|
284 | this.sources = sources;
|
---|
285 | }
|
---|
286 | else {
|
---|
287 | DebugStream.println("Error! Called setSource() of index other than the default.");
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
|
---|
292 | * @return A <strong>String</strong> containing the information of this class.
|
---|
293 | */
|
---|
294 | public String toString() {
|
---|
295 | StringBuffer text_buffer = new StringBuffer("");
|
---|
296 | // Generate language dependant id (include extracted metadata namespace)
|
---|
297 | // Write level information, if any.
|
---|
298 | int level = getLevel();
|
---|
299 | if(0 <= level && level < 3) {
|
---|
300 | text_buffer.append(LEVEL[level]);
|
---|
301 | text_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
302 | }
|
---|
303 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
304 | ArrayList sources = getSources();
|
---|
305 | int sources_size = sources.size();
|
---|
306 | for(int i = 0; i < sources_size; i++) {
|
---|
307 | String source_name = (sources.get(i)).toString();
|
---|
308 | text_buffer.append(source_name);
|
---|
309 | if(i < sources_size - 1) {
|
---|
310 | text_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | sources = null;
|
---|
314 | CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + getID(), false);
|
---|
315 | if(metadatum != null) {
|
---|
316 | text_buffer.append(" \"");
|
---|
317 | text_buffer.append(metadatum.getValue(CollectionMeta.TEXT));
|
---|
318 | text_buffer.append("\"");
|
---|
319 | }
|
---|
320 | return text_buffer.toString();
|
---|
321 | }
|
---|
322 | }
|
---|