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

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

Finally committing the (many) changes to the GLI to use the new metadata code... I hope this doesn't have too many bugs in it and committing it now doesn't stuff anyone up! (Katherine said I could commit it, so blame her if anything goes wrong).

  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 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 if(sources != null && sources.size() > 1) {
221 Collections.sort(sources);
222 }
223 }
224 return sources;
225 }
226
227 public boolean isAssigned() {
228 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
229 }
230
231 public void setAssigned(boolean assigned) {
232 if(element != null) {
233 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
234 }
235 }
236
237 public void setElement(Element element) {
238 this.element = element;
239 this.level = -1;
240 this.id = null;
241 this.sources = null;
242 }
243
244 /** Method to set the level of this index which can only be used for the default index.
245 * @param new_level the new level as an int
246 */
247 public void setLevel(int new_level) {
248 System.err.println("SetLevel(" + new_level + ")");
249 if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
250 element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[new_level]);
251 this.id = null; // Regenerate ID.
252 this.level = new_level;
253 }
254 else {
255 DebugStream.println("Error! Called setLevel() of index other than the default.");
256 }
257 }
258
259 /** Method to set the sources for this index which can only be used for the default index.
260 * @param sources an ArrayList of source names
261 */
262 public void setSources(ArrayList sources) {
263 if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
264 // Erase old sources
265 XMLTools.clear(element);
266 // For each entry in the sources array add a new content element.
267 int size = sources.size();
268 for(int i = 0; i < size; i++) {
269 Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT);
270 Object source_object = sources.get(i);
271 if (source_object instanceof MetadataElement) {
272 String name = ((MetadataElement) source_object).getFullName();
273 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, name);
274 name = null;
275 }
276 else {
277 //DebugStream.println("Found String as source: " + source_object.toString());
278 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, source_object.toString());
279 }
280 source_object = null;
281 element.appendChild(content_element);
282 content_element = null;
283 }
284 this.id = null; // Regenerate ID.
285 this.sources = sources;
286 }
287 else {
288 DebugStream.println("Error! Called setSource() of index other than the default.");
289 }
290 }
291
292 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
293 * @return A <strong>String</strong> containing the information of this class.
294 */
295 public String toString() {
296 StringBuffer text_buffer = new StringBuffer("");
297 // Generate language dependant id (include extracted metadata namespace)
298 // Write level information, if any.
299 int level = getLevel();
300 if(0 <= level && level < 3) {
301 text_buffer.append(LEVEL[level]);
302 text_buffer.append(StaticStrings.COLON_CHARACTER);
303 }
304 // Write data information. Retrieve each of the content sources and add them in a comma separated list.
305 ArrayList sources = getSources();
306 int sources_size = sources.size();
307 for(int i = 0; i < sources_size; i++) {
308 String source_name = (sources.get(i)).toString();
309 text_buffer.append(source_name);
310 if(i < sources_size - 1) {
311 text_buffer.append(StaticStrings.COMMA_CHARACTER);
312 }
313 }
314 sources = null;
315 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + getID(), false);
316 if(metadatum != null) {
317 text_buffer.append(" \"");
318 text_buffer.append(metadatum.getValue(CollectionMeta.TEXT));
319 text_buffer.append("\"");
320 }
321 return text_buffer.toString();
322 }
323}
Note: See TracBrowser for help on using the repository browser.