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 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import java.util.*;
|
---|
30 | import org.greenstone.gatherer.DebugStream;
|
---|
31 | import org.greenstone.gatherer.Gatherer;
|
---|
32 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
33 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
34 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
35 | import org.greenstone.gatherer.util.XMLTools;
|
---|
36 | import 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 | */
|
---|
42 | public 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 | this.element = CollectionConfiguration.createElement(StaticStrings.INDEX_ELEMENT);
|
---|
69 | // For each source add a content element
|
---|
70 | int size = sources.size();
|
---|
71 | for(int i = 0; i < size; i++) {
|
---|
72 | Element content_element = CollectionConfiguration.createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
73 | Object source_object = sources.get(i);
|
---|
74 | if (source_object instanceof MetadataElement) {
|
---|
75 | // System.err.println("Constructing new Index with MetadataElement source...");
|
---|
76 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, ((MetadataElement) source_object).getFullName());
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | // System.err.println("Constructing new Index with String source...");
|
---|
80 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
|
---|
81 | }
|
---|
82 | element.appendChild(content_element);
|
---|
83 | content_element = null;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Constructor for a newly assigned index with specified level (old skool). */
|
---|
88 | public Index(int level, ArrayList sources) {
|
---|
89 | this(sources);
|
---|
90 | this.level = level;
|
---|
91 | element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[level]);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public Index(String level, ArrayList sources) {
|
---|
95 | this(sources);
|
---|
96 | for(int i = 0; i < LEVEL.length; i++) {
|
---|
97 | if(LEVEL[i].equalsIgnoreCase(level)) {
|
---|
98 | this.level = i;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[this.level]);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Method to compare two indexes.
|
---|
105 | * @param object The other index as an <strong>Object</strong>.
|
---|
106 | * @return An <i>int</i> which indicates how the indexes compare.
|
---|
107 | * @see java.lang.String
|
---|
108 | */
|
---|
109 | public int compareTo(Object object) {
|
---|
110 | if(object == null) {
|
---|
111 | return -1;
|
---|
112 | }
|
---|
113 | String id = getID();
|
---|
114 | return id.compareTo(((Index)object).getID());
|
---|
115 | }
|
---|
116 |
|
---|
117 | public DOMProxyListEntry create(Element element) {
|
---|
118 | return new Index(element);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Method to test for the equality of two indexes.
|
---|
122 | * @param object The other index as an <strong>Object</strong>.
|
---|
123 | * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
|
---|
124 | */
|
---|
125 | public boolean equals(Object object) {
|
---|
126 | return (compareTo(object) == 0);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public Element getElement() {
|
---|
130 | return element;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Method to get the value of level.
|
---|
134 | * @return the level as a int
|
---|
135 | */
|
---|
136 | public int getLevel() {
|
---|
137 | if(level == -1 && element != null) {
|
---|
138 | String level_str = element.getAttribute(StaticStrings.LEVEL_ATTRIBUTE);
|
---|
139 | for(int i = 0; level == -1 && i < LEVEL.length; i++) {
|
---|
140 | if(level_str.equals(LEVEL[i])) {
|
---|
141 | level = i;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | level_str = null;
|
---|
145 | }
|
---|
146 | return level;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public String getID() {
|
---|
150 | if(element == null) {
|
---|
151 | id="";
|
---|
152 | }
|
---|
153 | else if(id == null) {
|
---|
154 | StringBuffer id_buffer = new StringBuffer();
|
---|
155 | // Write level information, if any.
|
---|
156 | int level = getLevel();
|
---|
157 | if(0 <= level && level < 3) {
|
---|
158 | id_buffer.append(LEVEL[level]);
|
---|
159 | id_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
160 | }
|
---|
161 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
162 | ArrayList sources = getSources();
|
---|
163 | int sources_size = sources.size();
|
---|
164 | for(int i = 0; i < sources_size; i++) {
|
---|
165 | Object source_object = sources.get(i);
|
---|
166 | if (source_object instanceof MetadataElement) {
|
---|
167 | String full_element_name = ((MetadataElement)source_object).getFullName();
|
---|
168 | if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && full_element_name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
|
---|
169 | id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
|
---|
170 | }
|
---|
171 | else {
|
---|
172 | id_buffer.append(full_element_name);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | else {
|
---|
176 | id_buffer.append(source_object.toString());
|
---|
177 | }
|
---|
178 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
179 | }
|
---|
180 | sources = null;
|
---|
181 | if (id_buffer.length()==0) {
|
---|
182 | id = "";
|
---|
183 | } else {
|
---|
184 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | return id;
|
---|
188 | }
|
---|
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(StaticStrings.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 metadata_element_name_full = (String) content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
202 | MetadataElement metadata_element = MetadataTools.getMetadataElementWithName(metadata_element_name_full);
|
---|
203 | if (metadata_element != null) {
|
---|
204 | sources.add(metadata_element);
|
---|
205 | }
|
---|
206 | else {
|
---|
207 | sources.add(metadata_element_name_full);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | content_elements = null;
|
---|
211 | }
|
---|
212 | return sources;
|
---|
213 | }
|
---|
214 |
|
---|
215 | public boolean isAssigned() {
|
---|
216 | return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
|
---|
217 | }
|
---|
218 |
|
---|
219 | public void setAssigned(boolean assigned) {
|
---|
220 | if(element != null) {
|
---|
221 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | public void setElement(Element element) {
|
---|
226 | this.element = element;
|
---|
227 | this.level = -1;
|
---|
228 | this.id = null;
|
---|
229 | this.sources = null;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Method to set the level of this index which can only be used for the default index.
|
---|
233 | * @param new_level the new level as an int
|
---|
234 | */
|
---|
235 | public void setLevel(int new_level) {
|
---|
236 | // System.err.println("SetLevel(" + new_level + ")");
|
---|
237 | if(element != null && element.getNodeName().equals(StaticStrings.INDEX_DEFAULT_ELEMENT)) {
|
---|
238 | if (0 <= new_level && new_level < 3) {
|
---|
239 | element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[new_level]);
|
---|
240 | } else {
|
---|
241 | element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, "");
|
---|
242 | }
|
---|
243 | this.id = null; // Regenerate ID.
|
---|
244 | this.level = new_level;
|
---|
245 | }
|
---|
246 | else {
|
---|
247 | DebugStream.println("Error! Called setLevel() of index other than the default.");
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Method to set the sources for this index which can only be used for the default index.
|
---|
252 | * @param sources an ArrayList of source names
|
---|
253 | */
|
---|
254 | public void setSources(ArrayList sources) {
|
---|
255 | if(element != null && element.getNodeName().equals(StaticStrings.INDEX_DEFAULT_ELEMENT)) {
|
---|
256 | // Erase old sources
|
---|
257 | XMLTools.clear(element);
|
---|
258 | // For each entry in the sources array add a new content element.
|
---|
259 | int size = sources.size();
|
---|
260 | for(int i = 0; i < size; i++) {
|
---|
261 | Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
262 | Object source_object = sources.get(i);
|
---|
263 | if (source_object instanceof MetadataElement) {
|
---|
264 | String name = ((MetadataElement) source_object).getFullName();
|
---|
265 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
|
---|
266 | name = null;
|
---|
267 | }
|
---|
268 | else {
|
---|
269 | //DebugStream.println("Found String as source: " + source_object.toString());
|
---|
270 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
|
---|
271 | }
|
---|
272 | source_object = null;
|
---|
273 | element.appendChild(content_element);
|
---|
274 | content_element = null;
|
---|
275 | }
|
---|
276 | this.id = null; // Regenerate ID.
|
---|
277 | this.sources = sources;
|
---|
278 | }
|
---|
279 | else {
|
---|
280 | DebugStream.println("Error! Called setSource() of index other than the default.");
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
|
---|
285 | * @return A <strong>String</strong> containing the information of this class.
|
---|
286 | */
|
---|
287 | public String toString() {
|
---|
288 | StringBuffer text_buffer = new StringBuffer("");
|
---|
289 | // Generate language dependant id (include extracted metadata namespace)
|
---|
290 | // Write level information, if any.
|
---|
291 | int level = getLevel();
|
---|
292 | if(0 <= level && level < 3) {
|
---|
293 | text_buffer.append(LEVEL[level]);
|
---|
294 | text_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
295 | }
|
---|
296 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
297 | ArrayList sources = getSources();
|
---|
298 | int sources_size = sources.size();
|
---|
299 | for(int i = 0; i < sources_size; i++) {
|
---|
300 | String source_name = (sources.get(i)).toString();
|
---|
301 | text_buffer.append(source_name);
|
---|
302 | if(i < sources_size - 1) {
|
---|
303 | text_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
304 | }
|
---|
305 | }
|
---|
306 | sources = null;
|
---|
307 | return text_buffer.toString();
|
---|
308 | }
|
---|
309 | }
|
---|