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

Last change on this file since 4366 was 4366, checked in by kjdon, 21 years ago

re-tabbed the code for java

  • Property svn:keywords set to Author Date Id Revision
File size: 7.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 * <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.Vector;
55import org.greenstone.gatherer.cdm.CollectionMeta;
56import org.greenstone.gatherer.msm.MSMUtils;
57import org.w3c.dom.Element;
58/** This class encapsulates a single indexing pair.
59 * @author John Thompson, Greenstone Digital Library, University of Waikato
60 * @version 2.2
61 */
62public class Index
63 implements Comparable {
64 /** A refernce to the main manager for access to other sub-managers. */
65 private CollectionDesignManager manager = null;
66 /** The level of this index. */
67 private int level = 0;
68 /** The sources for data this index is built apon, which are either fully qualified metadata element names or 'text'. */
69 private Vector sources = null;
70 /** An element in the index level enumeration. */
71 static public final int DOCUMENT = 0;
72 /** An element in the index level enumeration. */
73 static public final int PARAGRAPH = 1;
74 /** An element in the index level enumeration. */
75 static public final int SECTION = 2;
76 /** An values of items in the index level enumeration. */
77 static public final String LEVEL[] = {"document","paragraph","section"};
78 /** Constructor.
79 * @param level The level of this index as a <strong>String</string>.
80 * @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".
81 */
82 public Index(int level, Vector sources, CollectionDesignManager manager) {
83 this.level = level;
84 this.manager = manager;
85 this.sources = sources;
86 if(this.sources == null) {
87 this.sources = new Vector();
88 this.sources.add("text");
89 }
90 }
91 /** Method to compare two indexes.
92 * @param object The other index as an <strong>Object</strong>.
93 * @return An <i>int</i> which indicates how the indexes compare.
94 * @see java.lang.String
95 */
96 public int compareTo(Object object) {
97 if(object instanceof Index) {
98 Index index = (Index) object;
99 return toString(false).compareTo(index.toString(false));
100 }
101 return toString(false).compareTo(object.toString());
102 }
103 /** Method to test for the equality of two indexes.
104 * @param object The other index as an <strong>Object</strong>.
105 * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
106 */
107 public boolean equals(Object object) {
108 if(compareTo(object) == 0) {
109 return true;
110 }
111 return false;
112 }
113 /** Method to get the data source of this index.
114 * @return A <strong>String</string> which is a comma separated list of either fully qualified names of an assigned metadata elements, or "text".
115 */
116 public String getData() {
117 String result = "";
118 Collections.sort(sources, new IndexComparator());
119 for(int i = 0; i < sources.size(); i++) {
120 result = result + sources.get(i).toString();
121 if(i < sources.size() - 1) {
122 result = result + ",";
123 }
124 }
125 return result;
126 }
127 /** Method to get the value of level.
128 * @return The value of level as a <strong>String</strong>.
129 */
130 public int getLevel() {
131 return level;
132 }
133 /** Method to get the value of metadata.
134 * @return The value of metadata as an <strong>Vector</strong>.
135 */
136 public Vector getMetadata() {
137 return sources;
138 }
139 /** Method to retrieve this indexes name.
140 * @return A <strong>String</strong>.
141 */
142 public String getName() {
143 if(manager != null) {
144 String name = LEVEL[level] + ":" + getData();
145 Language language = manager.languages.getDefaultLanguage();
146 CollectionMeta metadata = manager.collectionmetadatum.getMetadata(name, language, true);
147 if(metadata != null) {
148 return metadata.getValue();
149 }
150 }
151 return "";
152 }
153 /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
154 * @return A <strong>String</strong> containing the information of this class.
155 */
156 public String toString() {
157 return LEVEL[level] + ":" + getData() + " \"" + getName() + "\"";
158 }
159 /** Retrieve a textual representation of this index.
160 * @param show_name <i>true</i> if you want the name of this index, <i>false</i> for the gsdl index reference.
161 */
162 public String toString(boolean show_name) {
163 if(show_name) {
164 return getName();
165 }
166 return LEVEL[level] + ":" + getData();
167 }
168 /** A custom comparator for comparing Indexes. */
169 private class IndexComparator
170 implements Comparator {
171 /** Method to compare two objects, which may be either indexes or strings.
172 * @param object1 One object as an <strong>Object</strong>.
173 * @param object2 Another object as an <strong>Object</strong>.
174 * @return An <i>int</i> which indicates how they compare.
175 * @see java.lang.String
176 */
177 public int compare(Object object1, Object object2) {
178 if(object1 instanceof Index && object2 instanceof Index) {
179 Index index1 = (Index)object1;
180 Index index2 = (Index)object2;
181 return index1.toString(false).compareTo(index2.toString(false));
182 }
183 else if(object1 instanceof Index) {
184 Index index = (Index) object1;
185 return index.toString(false).compareTo(object2.toString());
186 }
187 else if(object2 instanceof Index) {
188 Index index = (Index) object2;
189 return object1.toString().compareTo(index.toString(false));
190 }
191 return object1.toString().compareTo(object2.toString());
192 }
193 /** Method to test for the equality of two objects, which may be indexes or strings.
194 * @param object Another object as an <strong>Object</strong>.
195 * @return A <i>boolean</i> which is <i>true</i> if the two objects are equal, <i>false</i> otherwise.
196 */
197 public boolean equals(Object object) {
198 if(compareTo(object) == 0) {
199 return true;
200 }
201 return false;
202 }
203 }
204}
Note: See TracBrowser for help on using the repository browser.