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

Last change on this file since 5255 was 5255, checked in by jmt12, 21 years ago

Changed calls to CollectionMeta to reflect the two modes of retrieving value - as text, or as greenstone format

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