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

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

Moved clear() and setValue() functions from MSMUtils into XMLTools. Moved NS_SEP string from MSMUtils into StaticStrings.

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