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

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

Sunday's work

  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43package org.greenstone.gatherer.cdm;
44/**************************************************************************************
45 * Title: Gatherer
46 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
47 * Copyright: Copyright (c) 2001
48 * Company: The University of Waikato
49 * Written: 03/05/02
50 * Revised: 17/11/02
51 **************************************************************************************/
52import java.util.Collections;
53import java.util.Comparator;
54import java.util.StringTokenizer;
55import java.util.Vector;
56import org.greenstone.gatherer.cdm.CollectionMeta;
57import org.greenstone.gatherer.msm.MSMUtils;
58import org.greenstone.gatherer.util.Utility;
59import org.w3c.dom.Element;
60/** This class encapsulates a single indexing pair.
61 * @author John Thompson, Greenstone Digital Library, University of Waikato
62 * @version 2.2
63 */
64public class Index
65 implements Comparable {
66 /** A refernce to the main manager for access to other sub-managers. */
67 private CollectionDesignManager manager = null;
68 /** The level of this index. */
69 private int level = 0;
70 /** The sources for data this index is built apon, which are either fully qualified metadata element names or 'text'. */
71 private Vector sources = null;
72 /** An element in the index level enumeration. */
73 static public final int DOCUMENT = 0;
74 /** An element in the index level enumeration. */
75 static public final int PARAGRAPH = 1;
76 /** An element in the index level enumeration. */
77 static public final int SECTION = 2;
78 /** An values of items in the index level enumeration. */
79 static public final String LEVEL[] = {"document","paragraph","section"};
80
81 /** Constructor.
82 * @param level The level of this index as a <strong>String</string>.
83 * @param metadata The fully qualified name of the metadata this index is built on as a <strong>String</strong>, or <i>null</i> in which case the data defaults to "text".
84 */
85 public Index(int level, Vector sources, CollectionDesignManager manager) {
86 this.level = level;
87 this.manager = manager;
88 this.sources = sources;
89 if(this.sources == null) {
90 this.sources = new Vector();
91 this.sources.add("text");
92 }
93 }
94 /** Method to compare two indexes.
95 * @param object The other index as an <strong>Object</strong>.
96 * @return An <i>int</i> which indicates how the indexes compare.
97 * @see java.lang.String
98 */
99 public int compareTo(Object object) {
100 return toString(false).compareTo(object.toString());
101 }
102 /** Method to test for the equality of two indexes.
103 * @param object The other index as an <strong>Object</strong>.
104 * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
105 */
106 public boolean equals(Object object) {
107 if(compareTo(object) == 0) {
108 return true;
109 }
110 return false;
111 }
112 /** Method to get the data source of this index.
113 * @return A <strong>String</string> which is a comma separated list of either fully qualified names of an assigned metadata elements, or "text".
114 */
115 public String getData() {
116 String result = "";
117 Collections.sort(sources, new IndexComparator());
118 for(int i = 0; i < sources.size(); i++) {
119 result = result + sources.get(i).toString();
120 if(i < sources.size() - 1) {
121 result = result + ",";
122 }
123 }
124 return result;
125 }
126 /** Method to get the data source of this index.
127 * @return A <strong>String</string> which is a comma separated list of either fully qualified names of an assigned metadata elements, or "text".
128 * Note, greenstone extracted metadata do not have the ex.
129 */
130 public String getDataConfig() {
131 String result = "";
132 Collections.sort(sources, new IndexComparator());
133 for(int i = 0; i < sources.size(); i++) {
134 String source = sources.get(i).toString();
135 if (source.startsWith(Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP)) {
136 // remove the ex. bit
137 source = source.substring(source.indexOf(MSMUtils.NS_SEP)+1);
138 }
139 result = result + source;
140 if(i < sources.size() - 1) {
141 result = result + ",";
142 }
143 }
144 return result;
145 }
146
147 /** Method to get the value of level.
148 * @return The value of level as a <strong>String</strong>.
149 */
150 public int getLevel() {
151 return level;
152 }
153 /** Method to get the value of metadata.
154 * @return The value of metadata as an <strong>Vector</strong>.
155 */
156 public Vector getMetadata() {
157 return sources;
158 }
159 /** Method to retrieve this indexes name.
160 * @return A <strong>String</strong>.
161 */
162 public String getName() {
163 if(manager != null) {
164 String name = LEVEL[level] + ":" + getData();
165 Language language = manager.languages.getDefaultLanguage();
166 CollectionMeta metadata = manager.collectionmetadatum.getMetadata(name, language, true);
167 if(metadata != null) {
168 return metadata.getValue();
169 }
170 }
171 return "";
172
173 }
174 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
175 * @return A <strong>String</strong> containing the information of this class.
176 */
177 public String toString() {
178 return LEVEL[level] + ":" + getData();
179 }
180 /** Retrieve a textual representation of this index.
181 * @param show_name <i>true</i> if you want the name of this index, <i>false</i> for the gsdl index reference.
182 */
183 public String toString(boolean show_name) {
184 if(show_name) {
185 return getName();
186 }
187 return LEVEL[level] + ":" + getData();
188 }
189
190 /** Retrieve a textual representation of this index, specifically for the configuration files. Extracted metadata is shown to teh user and stored as ex.MD, however in the config file it should have no namespace */
191 public String toStringConfig() {
192
193 return LEVEL[level] + ":" + getDataConfig();
194 }
195
196 /** Method to parse a configuration file index specification into an Index
197 * object. returns null if invalid syntax */
198 public static Index parseIndexConfig(String entry, CollectionDesignManager manager) {
199 // all (mg) indexes must start with level:
200 if(entry.indexOf(":") == -1) {
201 return null;
202 }
203
204 String level_str = entry.substring(0, entry.indexOf(":"));
205 String sources_raw = entry.substring(entry.indexOf(":") + 1);
206 Vector sources = new Vector();
207 StringTokenizer st = new StringTokenizer(sources_raw, ",");
208 while(st.hasMoreTokens()) {
209 String token = st.nextToken();
210 // We may have to replace old : with whatever namespace separator we are using.
211 token = token.replace(':', MSMUtils.NS_SEP);
212 // if there is no namespace, we need to add one, except for text
213 if (token.indexOf(MSMUtils.NS_SEP)==-1) {
214 if (!token.equals("text")) {
215 token = Utility.EXTRACTED_METADATA_NAMESPACE+MSMUtils.NS_SEP+token;
216 }
217 }
218
219 sources.add(token);
220 }
221 Index index = null;
222 for(int i = 0; i < Index.LEVEL.length; i++) {
223 if(level_str.equals(Index.LEVEL[i])) {
224 return new Index(i, sources, manager);
225
226 }
227 }
228 // somethings gone wrong
229 return null;
230
231 }
232
233 /** A custom comparator for comparing Indexes. */
234 private class IndexComparator
235 implements Comparator {
236 /** Method to compare two objects, which may be either indexes or strings.
237 * @param object1 One object as an <strong>Object</strong>.
238 * @param object2 Another object as an <strong>Object</strong>.
239 * @return An <i>int</i> which indicates how they compare.
240 * @see java.lang.String
241 */
242 public int compare(Object object1, Object object2) {
243 if(object1 instanceof Index && object2 instanceof Index) {
244 Index index1 = (Index)object1;
245 Index index2 = (Index)object2;
246 return index1.toString(false).compareTo(index2.toString(false));
247 }
248 else if(object1 instanceof Index) {
249 Index index = (Index) object1;
250 return index.toString(false).compareTo(object2.toString());
251 }
252 else if(object2 instanceof Index) {
253 Index index = (Index) object2;
254 return object1.toString().compareTo(index.toString(false));
255 }
256 return object1.toString().compareTo(object2.toString());
257 }
258 /** Method to test for the equality of two objects, which may be indexes or strings.
259 * @param object Another object as an <strong>Object</strong>.
260 * @return A <i>boolean</i> which is <i>true</i> if the two objects are equal, <i>false</i> otherwise.
261 */
262 public boolean equals(Object object) {
263 if(compareTo(object) == 0) {
264 return true;
265 }
266 return false;
267 }
268 }
269}
Note: See TracBrowser for help on using the repository browser.