source: trunk/gli/src/org/greenstone/gatherer/cdm/Classifier.java@ 4838

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

2030159: Had confused element name and identifier. Also added ability for ElementWrappers to be stored as Argument values, and thus have identifier appear within GLI while the name is written to the collect.cfg

  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 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
43/* GPL_HEADER */
44package org.greenstone.gatherer.cdm;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Company: The University of Waikato
49 * Written: 01/05/02
50 * Revised: 16/08/02 Optimized and Commented.
51 **************************************************************************************/
52import java.io.Serializable;
53import java.util.ArrayList;
54import org.greenstone.gatherer.cdm.Argument;
55import org.greenstone.gatherer.cdm.ArgumentContainer;
56import org.greenstone.gatherer.cdm.CustomClassifier;
57import org.greenstone.gatherer.cdm.Format;
58/** This class is responsible for storing information from a parsed classinfo.pl call in such a way that it allows easy access to parsed details for the purposes of user design and specification of classifiers.
59 * @author John Thompson, Greenstone Digital Library, University of Waikato
60 * @version 2.3
61 */
62// ####################################################################################
63// Optimization Saving
64// ####################################################################################
65// Vector -> ArrayList (x8) + Processor
66// ####################################################################################
67public class Classifier
68 extends ArrayList
69 implements ArgumentContainer, Comparable, Serializable {
70 /** A reference to the classifier that this one inherits from. */
71 private Classifier super_classifier = null;
72 /** A reference to the manager of this classifier. Only available once the classifiers been assigned, always <i>null</i> for classifiers in reserve. */
73 private ClassifierManager manager = null;
74 /** Custom arguments (ie those unparsable from classinfo.pl) provided to this class. */
75 private String custom = null;
76 /** A description of this classifier. */
77 private String desc = null;
78 /** The name of the classifier as it would appear in the collect.cfg file. */
79 private String name = null;
80 /** A list of format commands that are dependant on this classifier. */
81 private ArrayList dependant_formats = null;
82 /** Default Constructor.
83 */
84 public Classifier() {
85 super();
86 dependant_formats = new ArrayList();
87 }
88 /** Constructor.
89 * @param name The name of this classifier as a <strong>String</strong>.
90 * @param desc A description of this classifier as a <strong>String</strong>.
91 * @param super_classifier The super class of this classifier, as a <strong>Classifier</strong>.
92 */
93 public Classifier(String name, String desc, Classifier super_classifier) {
94 this();
95 this.desc = desc;
96 this.name = name;
97 this.super_classifier = super_classifier;
98 }
99 /** Method to add an argument to this classifier. Only adds the argument if it isn't already present.
100 * @param argument The <strong>Argument</strong> to add.
101 */
102 public void addArgument(Argument argument) {
103 if(!contains(argument)) {
104 add(argument);
105 argument.setOwner(name);
106 }
107 }
108 /** Method to register a dependant format with this classifier. If the classifier changes, this format also needs to be updated.
109 * @param format A <strong>Format</strong> which is dependant on this classifier.
110 */
111 public void addDependantFormat(Format format) {
112 dependant_formats.add(format);
113 }
114 /** Method to compare two classifiers for ordering.
115 * @param object The classifier we are comparing to, as an <strong>Object</strong>.
116 * @return An <i>int</i> specifying the classifier order, using values as set out in String.
117 * @see java.lang.String#compareTo
118 */
119 public int compareTo(Object object) {
120 if(object instanceof Classifier) {
121 Classifier classifier = (Classifier) object;
122 return name.compareTo(classifier.getName());
123 }
124 return name.compareTo(object.toString());
125 }
126 /** This method produces a deep copy of this classifier. Note that this also creates a new copy of each of the super classes of classifiers as well. This is the way it should be, as each assigned classifier may have different values for the higher classifiers (such as BasPlug).
127 * @return A newly created <strong>Classifier</strong> with the same details and Arguments as this one.
128 * @see org.greenstone.gatherer.cdm.Argument
129 */
130 public Classifier copy() {
131 Classifier copy = null;
132 if(super_classifier == null) {
133 copy = new Classifier(name, desc, null);
134 }
135 else {
136 copy = new Classifier(name, desc, super_classifier.copy());
137 }
138 for(int i = 0; i < size(); i++) {
139 copy.addArgument(((Argument)get(i)).copy());
140 }
141 return copy;
142 }
143 /** Method to determine if two classifiers are equal.
144 * @param object The classifier to test against, as an <strong>Object</strong>.
145 * @return <i>true</i> if the classifier names match, <i>false</i> otherwise.
146 * @see org.greenstone.gatherer.cdm.CustomClassifier
147 */
148 public boolean equals(Object object) {
149 if(object instanceof CustomClassifier) {
150 CustomClassifier classifier = (CustomClassifier) object;
151 return (toString().equalsIgnoreCase(classifier.getCommand()));
152 }
153 else {
154 return(toString().equalsIgnoreCase(object.toString()));
155 }
156 }
157 /** Method to retrieve an argument by its name.
158 * @param name The name of the argument as a <strong>String</strong>.
159 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
160 */
161 public Argument getArgument(String name) {
162 // The name given may still include the '-'
163 if(name.startsWith("-")) {
164 name = name.substring(1);
165 }
166 ArrayList arguments = getArguments();
167 for(int i = 0; i < arguments.size(); i++) {
168 Argument argument = (Argument)arguments.get(i);
169 if(argument.getName().equals(name)) {
170 return argument;
171 }
172 }
173 return null;
174 }
175 /** Method to retrieve all of the arguments available to a classifier, including both specific and general ones.
176 * @return A <strong>Hashtable</strong> of arguments, with &lt;name&gt; -&gt; &lt;argument&gt; entries.
177 */
178 public ArrayList getArguments() {
179 ArrayList all_arguments = new ArrayList(this);
180 if(super_classifier != null) {
181 ArrayList super_arguments = super_classifier.getArguments();
182 for(int i = 0; i < super_arguments.size(); i++) {
183 Object argument = super_arguments.get(i);
184 if(!all_arguments.contains(argument)) {
185 all_arguments.add(argument);
186 }
187 }
188 }
189 return all_arguments;
190 }
191 /** Method to retrieve a classifiers custom argument information.
192 * @return The custom arguments as a <strong>String</strong>.
193 */
194 public String getCustom() {
195 return custom;
196 }
197 /** Method to retrieve a classifiers name.
198 * @return A <strong>String</strong> containing the classifiers name.
199 */
200 public String getName() {
201 return name;
202 }
203 /** Method to retrieve the position of this classifier as a special keyword or the form "CL#" where # is the classifiers order in the set of classifiers. Note that if this is called for a Classifier that has never been assigned a position of 'Search' is returned, as this is the only case where we will try to write a Classifier that does not have a manager.
204 * @return A <strong>String</strong> containing the special position keyword.
205 * @see org.greenstone.gatherer.cdm.ClassifierManager
206 */
207 public String getPositionString() {
208 if(manager == null) {
209 return "Search";
210 }
211 return "CL" + (manager.indexOf(this) + 1);
212 }
213 /** Method to set the value of custom.
214 * @param custom The new value of custom as a <strong>String</strong>.
215 */
216 public void setCustom(String custom) {
217 this.custom = custom;
218 }
219 /** Method to set the value of desc.
220 * @param desc The new value of desc as a <strong>String</strong>.
221 */
222 public void setDesc(String desc) {
223 this.desc = desc;
224 }
225 /** Method to set the value of manager.
226 * @param manager The new manager as a <strong>ClassifierManager</strong>.
227 */
228 public void setManager(ClassifierManager manager) {
229 this.manager = manager;
230 }
231 /** Method to set the value of name.
232 * @param name The new value of name as a <strong>String</strong>.
233 */
234 public void setName(String name) {
235 this.name = name;
236 }
237 /** Method to set the value of the super_classifier.
238 * @param super_classifier The new value of super_classifier as a <strong>Classifier</strong>, or <i>null</i> if this class has no inheritance.
239 */
240 public void setSuper(Classifier super_classifier) {
241 this.super_classifier = super_classifier;
242 }
243 /** Method to print out this classifier as it would appear to the user in the interface
244 * @return A <strong>String</strong> containing a single classifier command.
245 */
246 public String toString() {
247 StringBuffer text = new StringBuffer("classify ");
248 text.append(name);
249 text.append(" ");
250 ArrayList arguments = getArguments();
251 for(int i = 0; i < arguments.size(); i++) {
252 Argument argument = (Argument)arguments.get(i);
253 if(argument.isAssigned()) {
254 text.append(argument.toString());
255 text.append(" ");
256 }
257 }
258 if(custom != null) {
259 text.append(custom);
260 }
261 return text.toString();
262 }
263 /** Method to print out this classifier as it would appear as a command within the collection configuration file.
264 * @return A <strong>String</strong> containing a single classifier command.
265 */
266 public String toStringConfig() {
267 StringBuffer text = new StringBuffer("classify ");
268 text.append(name);
269 text.append(" ");
270 ArrayList arguments = getArguments();
271 for(int i = 0; i < arguments.size(); i++) {
272 Argument argument = (Argument)arguments.get(i);
273 if(argument.isAssigned()) {
274 text.append(argument.toStringConfig());
275 text.append(" ");
276 }
277 }
278 if(custom != null) {
279 text.append(custom);
280 }
281 return text.toString();
282 }
283}
284
285
Note: See TracBrowser for help on using the repository browser.