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

Last change on this file since 12750 was 12641, checked in by mdewsnip, 18 years ago

Changed all access to the static strings through CollectionConfiguration to directly use StaticStrings.

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