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

Last change on this file since 12080 was 10964, checked in by kjdon, 19 years ago

default index doesn't need a level now, fixed a couple of bugs. index list now displays metadata display name not identifier

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