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.Gatherer;
|
---|
31 | import org.greenstone.gatherer.cdm.CollectionConfiguration;
|
---|
32 | import org.greenstone.gatherer.cdm.CollectionDesignManager;
|
---|
33 | import org.greenstone.gatherer.cdm.CollectionMeta;
|
---|
34 | import org.greenstone.gatherer.cdm.DOMProxyListEntry;
|
---|
35 | import org.greenstone.gatherer.msm.ElementWrapper;
|
---|
36 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
37 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
38 | import org.greenstone.gatherer.util.Utility;
|
---|
39 | import 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 | */
|
---|
44 | public 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 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, ((ElementWrapper)source_object).getName());
|
---|
79 | }
|
---|
80 | else {
|
---|
81 | content_element.setAttribute(CollectionConfiguration.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(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[level]);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Method to compare two indexes.
|
---|
97 | * @param object The other index as an <strong>Object</strong>.
|
---|
98 | * @return An <i>int</i> which indicates how the indexes compare.
|
---|
99 | * @see java.lang.String
|
---|
100 | */
|
---|
101 | public int compareTo(Object object) {
|
---|
102 | return id.compareTo(((Index)object).getID());
|
---|
103 | }
|
---|
104 |
|
---|
105 | public DOMProxyListEntry create(Element element) {
|
---|
106 | return new Index(element);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Method to test for the equality of two indexes.
|
---|
110 | * @param object The other index as an <strong>Object</strong>.
|
---|
111 | * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
|
---|
112 | */
|
---|
113 | public boolean equals(Object object) {
|
---|
114 | return (compareTo(object) == 0);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public Element getElement() {
|
---|
118 | return element;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Method to get the value of level.
|
---|
122 | * @return the level as a int
|
---|
123 | */
|
---|
124 | public int getLevel() {
|
---|
125 | if(level == -1) {
|
---|
126 | String level_str = element.getAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE);
|
---|
127 | for(int i = 0; level == -1 && i < LEVEL.length; i++) {
|
---|
128 | if(level_str.equals(LEVEL[i])) {
|
---|
129 | level = i;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | level_str = null;
|
---|
133 | }
|
---|
134 | return level;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public String getID() {
|
---|
138 | if(id == null) {
|
---|
139 | StringBuffer id_buffer = new StringBuffer();
|
---|
140 | // Write level information, if any.
|
---|
141 | int level = getLevel();
|
---|
142 | if(0 <= level && level < 3) {
|
---|
143 | id_buffer.append(LEVEL[level]);
|
---|
144 | id_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
145 | }
|
---|
146 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
147 | ArrayList sources = getSources();
|
---|
148 | int sources_size = sources.size();
|
---|
149 | for(int i = 0; i < sources_size; i++) {
|
---|
150 | Object source_object = sources.get(i);
|
---|
151 | if(source_object instanceof ElementWrapper) {
|
---|
152 | id_buffer.append(((ElementWrapper)source_object).getName());
|
---|
153 | }
|
---|
154 | else {
|
---|
155 | id_buffer.append(source_object.toString());
|
---|
156 | }
|
---|
157 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
158 | }
|
---|
159 | sources = null;
|
---|
160 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
161 | }
|
---|
162 | return id;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /** Retrieve the sources of this index.
|
---|
166 | * @return the sources as an ArrayList
|
---|
167 | */
|
---|
168 | public ArrayList getSources() {
|
---|
169 | if(sources == null) {
|
---|
170 | sources = new ArrayList();
|
---|
171 | NodeList content_elements = element.getElementsByTagName(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
172 | int content_elements_length = content_elements.getLength();
|
---|
173 | for(int i = 0; i < content_elements_length; i++) {
|
---|
174 | Element content_element = (Element) content_elements.item(i);
|
---|
175 | sources.add(content_element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE));
|
---|
176 | }
|
---|
177 | content_elements = null;
|
---|
178 | Collections.sort(sources);
|
---|
179 | }
|
---|
180 | return sources;
|
---|
181 | }
|
---|
182 |
|
---|
183 | public boolean isAssigned() {
|
---|
184 | return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
|
---|
185 | }
|
---|
186 |
|
---|
187 | public void setAssigned(boolean assigned) {
|
---|
188 | if(element != null) {
|
---|
189 | element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void setElement(Element element) {
|
---|
194 | this.element = element;
|
---|
195 | this.level = -1;
|
---|
196 | this.id = null;
|
---|
197 | this.sources = null;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** Method to set the level of this index which can only be used for the default index.
|
---|
201 | * @param level the new level as an int
|
---|
202 | */
|
---|
203 | public void setLevel(int new_level) {
|
---|
204 | if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
|
---|
205 | element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[new_level]);
|
---|
206 | this.id = null; // Regenerate ID.
|
---|
207 | this.level = new_level;
|
---|
208 | }
|
---|
209 | else {
|
---|
210 | Gatherer.println("Error! Called setLevel() of index other than the default.");
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Method to set the sources for this index which can only be used for the default index.
|
---|
215 | * @param sources an ArrayList of source names
|
---|
216 | */
|
---|
217 | public void setSources(ArrayList sources) {
|
---|
218 | if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
|
---|
219 | // Erase old sources
|
---|
220 | MSMUtils.clear(element);
|
---|
221 | // For each entry in the sources array add a new content element.
|
---|
222 | int size = sources.size();
|
---|
223 | for(int i = 0; i < size; i++) {
|
---|
224 | Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
225 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, (String) sources.get(i));
|
---|
226 | element.appendChild(content_element);
|
---|
227 | content_element = null;
|
---|
228 | }
|
---|
229 | this.id = null; // Regenerate ID.
|
---|
230 | this.sources = sources;
|
---|
231 | }
|
---|
232 | else {
|
---|
233 | Gatherer.println("Error! Called setSource() of index other than the default.");
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
|
---|
238 | * @return A <strong>String</strong> containing the information of this class.
|
---|
239 | */
|
---|
240 | public String toString() {
|
---|
241 | String id = getID();
|
---|
242 | StringBuffer text = new StringBuffer(id);
|
---|
243 | CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + id, false);
|
---|
244 | if(metadatum != null) {
|
---|
245 | text.append(" \"");
|
---|
246 | text.append(metadatum.getValue());
|
---|
247 | text.append("\"");
|
---|
248 | }
|
---|
249 | return text.toString();
|
---|
250 | }
|
---|
251 | }
|
---|