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

Last change on this file since 8853 was 8374, checked in by kjdon, 20 years ago

don't sort the list returned from getSources - it contains elements that don't sort, and you get a ClassCastException. they just end up in the same order anyway

  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 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.metadata.MetadataElement;
33import org.greenstone.gatherer.metadata.MetadataSet;
34import org.greenstone.gatherer.metadata.MetadataSetManager;
35import org.greenstone.gatherer.metadata.MetadataTools;
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 MetadataElement) {
79 // System.err.println("Constructing new Index with MetadataElement source...");
80 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, ((MetadataElement) source_object).getFullName());
81 }
82 else {
83 // System.err.println("Constructing new Index with String source...");
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 (source_object instanceof MetadataElement) {
172 String full_element_name = ((MetadataElement)source_object).getFullName();
173 if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
174 id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
175 }
176 else {
177 id_buffer.append(full_element_name);
178 }
179 }
180 else {
181 id_buffer.append(source_object.toString());
182 }
183 id_buffer.append(StaticStrings.COMMA_CHARACTER);
184 }
185 sources = null;
186 id = id_buffer.substring(0, id_buffer.length() - 1);
187 }
188 return id;
189 }
190
191 /** Tries to retrieve this indexes name according to the CollectionMetaManager. */
192 public String getName() {
193 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + getID(), false);
194 if(metadatum != null) {
195 return metadatum.getValue(CollectionMeta.TEXT);
196 }
197 return "";
198 }
199
200 /** Retrieve the sources of this index.
201 * @return the sources as an ArrayList
202 */
203 public ArrayList getSources() {
204 if(sources == null) {
205 sources = new ArrayList();
206 NodeList content_elements = element.getElementsByTagName(CollectionConfiguration.CONTENT_ELEMENT);
207 int content_elements_length = content_elements.getLength();
208 for(int i = 0; i < content_elements_length; i++) {
209 Element content_element = (Element) content_elements.item(i);
210 String metadata_element_name_full = (String) content_element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
211 MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName(metadata_element_name_full);
212 if (metadata_element != null) {
213 sources.add(metadata_element);
214 }
215 else {
216 sources.add(metadata_element_name_full);
217 }
218 }
219 content_elements = null;
220 }
221 return sources;
222 }
223
224 public boolean isAssigned() {
225 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
226 }
227
228 public void setAssigned(boolean assigned) {
229 if(element != null) {
230 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
231 }
232 }
233
234 public void setElement(Element element) {
235 this.element = element;
236 this.level = -1;
237 this.id = null;
238 this.sources = null;
239 }
240
241 /** Method to set the level of this index which can only be used for the default index.
242 * @param new_level the new level as an int
243 */
244 public void setLevel(int new_level) {
245 System.err.println("SetLevel(" + new_level + ")");
246 if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
247 element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[new_level]);
248 this.id = null; // Regenerate ID.
249 this.level = new_level;
250 }
251 else {
252 DebugStream.println("Error! Called setLevel() of index other than the default.");
253 }
254 }
255
256 /** Method to set the sources for this index which can only be used for the default index.
257 * @param sources an ArrayList of source names
258 */
259 public void setSources(ArrayList sources) {
260 if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
261 // Erase old sources
262 XMLTools.clear(element);
263 // For each entry in the sources array add a new content element.
264 int size = sources.size();
265 for(int i = 0; i < size; i++) {
266 Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT);
267 Object source_object = sources.get(i);
268 if (source_object instanceof MetadataElement) {
269 String name = ((MetadataElement) source_object).getFullName();
270 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, name);
271 name = null;
272 }
273 else {
274 //DebugStream.println("Found String as source: " + source_object.toString());
275 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, source_object.toString());
276 }
277 source_object = null;
278 element.appendChild(content_element);
279 content_element = null;
280 }
281 this.id = null; // Regenerate ID.
282 this.sources = sources;
283 }
284 else {
285 DebugStream.println("Error! Called setSource() of index other than the default.");
286 }
287 }
288
289 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
290 * @return A <strong>String</strong> containing the information of this class.
291 */
292 public String toString() {
293 StringBuffer text_buffer = new StringBuffer("");
294 // Generate language dependant id (include extracted metadata namespace)
295 // Write level information, if any.
296 int level = getLevel();
297 if(0 <= level && level < 3) {
298 text_buffer.append(LEVEL[level]);
299 text_buffer.append(StaticStrings.COLON_CHARACTER);
300 }
301 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
302 ArrayList sources = getSources();
303 int sources_size = sources.size();
304 for(int i = 0; i < sources_size; i++) {
305 String source_name = (sources.get(i)).toString();
306 text_buffer.append(source_name);
307 if(i < sources_size - 1) {
308 text_buffer.append(StaticStrings.COMMA_CHARACTER);
309 }
310 }
311 sources = null;
312 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + getID(), false);
313 if(metadatum != null) {
314 text_buffer.append(" \"");
315 text_buffer.append(metadatum.getValue(CollectionMeta.TEXT));
316 text_buffer.append("\"");
317 }
318 return text_buffer.toString();
319 }
320}
Note: See TracBrowser for help on using the repository browser.