source: trunk/gli/src/org/greenstone/gatherer/cdm/Index.java@ 8236

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

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