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

Last change on this file since 5859 was 5590, checked in by mdewsnip, 21 years ago

Could it be I've finished adding tooltips?? Why yes, very nearly... and a big "hallelulah" for that.

  • 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.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.msm.MSMUtils;
37import org.greenstone.gatherer.util.StaticStrings;
38import org.greenstone.gatherer.util.Utility;
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[] = {"document","paragraph","section"};
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(Utility.EXTRACTED_METADATA_NAMESPACE)) {
175 id_buffer.append(full_element_name.substring(Utility.EXTRACTED_METADATA_NAMESPACE.length() + 1));
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 Collections.sort(sources);
222 }
223 return sources;
224 }
225
226 public boolean isAssigned() {
227 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
228 }
229
230 public void setAssigned(boolean assigned) {
231 if(element != null) {
232 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
233 }
234 }
235
236 public void setElement(Element element) {
237 this.element = element;
238 this.level = -1;
239 this.id = null;
240 this.sources = null;
241 }
242
243 /** Method to set the level of this index which can only be used for the default index.
244 * @param level the new level as an int
245 */
246 public void setLevel(int new_level) {
247 System.err.println("SetLevel(" + new_level + ")");
248 if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
249 element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[new_level]);
250 this.id = null; // Regenerate ID.
251 this.level = new_level;
252 }
253 else {
254 Gatherer.println("Error! Called setLevel() of index other than the default.");
255 }
256 }
257
258 /** Method to set the sources for this index which can only be used for the default index.
259 * @param sources an ArrayList of source names
260 */
261 public void setSources(ArrayList sources) {
262 if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
263 // Erase old sources
264 MSMUtils.clear(element);
265 // For each entry in the sources array add a new content element.
266 int size = sources.size();
267 for(int i = 0; i < size; i++) {
268 Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT);
269 Object source_object = sources.get(i);
270 if(source_object instanceof ElementWrapper) {
271 //Gatherer.println("Found ElementWrapper as source: " + ((ElementWrapper)source_object).getName());
272 String name = ((ElementWrapper)source_object).getName();
273 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, name);
274 name = null;
275 }
276 else {
277 //Gatherer.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 Gatherer.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.