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: Xiao F. Yu, 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 org.greenstone.gatherer.DebugStream;
|
---|
30 | import org.greenstone.gatherer.util.Codec;
|
---|
31 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
32 | import org.greenstone.gatherer.util.XMLTools;
|
---|
33 | import org.w3c.dom.*;
|
---|
34 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
35 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * This class is the greenstone 3 version of the Format.java in gs2 for handling
|
---|
39 | * 'gsf' format statements.
|
---|
40 | */
|
---|
41 | public class Format4gs3 implements DOMProxyListEntry
|
---|
42 | {
|
---|
43 |
|
---|
44 | /** The element this format is based on. */
|
---|
45 | private Element element = null;
|
---|
46 | /** We keep a copy of the feature name */
|
---|
47 | private String feature_name = null;
|
---|
48 | /**
|
---|
49 | * The format statement for this feature (also contains classifier options
|
---|
50 | * if its a classifier), used by the format list on the top in GLI.
|
---|
51 | */
|
---|
52 | private String feature_format = null;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The format statement for this feature (does not contain options in the
|
---|
56 | * front), displayed in the format editor at the bottom in GLI. If this
|
---|
57 | * format is not a classifier, feature_format==pure_format
|
---|
58 | */
|
---|
59 | private String pure_format = null;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Wether this feature is about a classifier. The other way to check this is
|
---|
63 | * to see if feature_name starts with "CL"
|
---|
64 | */
|
---|
65 | private static boolean is_classifier = false;
|
---|
66 | /** If this format is about a classifier, this contains all its options */
|
---|
67 | private String classifier_options = "";
|
---|
68 |
|
---|
69 | /** Constructor only to be used during DOMProxyListModel initialization. */
|
---|
70 | public Format4gs3()
|
---|
71 | {
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Constructor for a format-string format command.
|
---|
77 | *
|
---|
78 | * @param element
|
---|
79 | * The <Strong>element</strong> this format object contains.
|
---|
80 | */
|
---|
81 | public Format4gs3(Element element)
|
---|
82 | {
|
---|
83 | this.element = element;
|
---|
84 |
|
---|
85 | is_classifier = element.getParentNode().getNodeName().equals(StaticStrings.CLASSIFY_ELEMENT);
|
---|
86 |
|
---|
87 | if (is_classifier && CollectionDesignManager.classifier_manager != null)
|
---|
88 | {
|
---|
89 | //The getClassifierPosition() method also sets the classifier_options variable.
|
---|
90 | feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element);
|
---|
91 | Classifier classifier = getClassifier(feature_name);
|
---|
92 | //set the classifier_options string
|
---|
93 | if (classifier != null)
|
---|
94 | {
|
---|
95 | classifier_options = classifier.toString();
|
---|
96 | }
|
---|
97 |
|
---|
98 | pure_format = toOneLineFormat(XMLTools.getNodeText(element));
|
---|
99 | feature_format = classifier_options + " " + pure_format;
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | feature_name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
104 | feature_format = toOneLineFormat(XMLTools.getNodeText(element));
|
---|
105 | pure_format = feature_format;
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | //
|
---|
111 | public Format4gs3(String feature_name, String feature_format)
|
---|
112 | {
|
---|
113 | this.feature_name = feature_name;
|
---|
114 | this.feature_format = feature_format;
|
---|
115 | this.pure_format = feature_format;
|
---|
116 |
|
---|
117 | is_classifier = feature_name.startsWith(Classifier.CLASSIFIER_PREFIX);
|
---|
118 |
|
---|
119 | element = CollectionConfiguration.createElement(StaticStrings.FORMAT_STR);
|
---|
120 | if (is_classifier)
|
---|
121 | {
|
---|
122 |
|
---|
123 | //Now get the options of the classifier
|
---|
124 | Classifier classifier = getClassifier(feature_name);
|
---|
125 | //set the classifier_options string
|
---|
126 | classifier_options = classifier.toString();
|
---|
127 |
|
---|
128 | feature_format = classifier_options + " " + feature_format;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | element.setAttribute(StaticStrings.NAME_ATTRIBUTE, feature_name);
|
---|
133 | }
|
---|
134 |
|
---|
135 | XMLTools.setNodeText(element, toOneLineFormat(pure_format));
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | public int getClassifierPosition(Element element)
|
---|
140 | {
|
---|
141 | //we could also here use 'element.getParentNode().getParentNode()' to get this root node
|
---|
142 | Element root = CollectionDesignManager.collect_config.getDocumentElement();
|
---|
143 | NodeList classify_list = root.getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT);
|
---|
144 | int num_classify_elements = classify_list.getLength();
|
---|
145 | for (int i = 0; i < num_classify_elements; i++)
|
---|
146 | {
|
---|
147 | Element classify = (Element) classify_list.item(i);
|
---|
148 | Element format = (Element) XMLTools.getChildByTagName(classify, StaticStrings.FORMAT_STR);
|
---|
149 | if (format == element)
|
---|
150 | {
|
---|
151 | //Now get the options of the classifier
|
---|
152 | Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(i);
|
---|
153 | classifier_options = classifier.toString();
|
---|
154 | // i+1 because java is from 0 up, greenstone starts from 1.
|
---|
155 | return i + 1;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | return -1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | public static Classifier getClassifier(String feature_name)
|
---|
162 | {
|
---|
163 | int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length()));
|
---|
164 | Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(index - 1);
|
---|
165 | return classifier;
|
---|
166 | }
|
---|
167 |
|
---|
168 | //This method is used when the add button is clicked and a new format element for a classifier is added to
|
---|
169 | // the DOM tree, we need to figure out which Classify element to append this format element to.
|
---|
170 | public Element getClassifyElement()
|
---|
171 | {
|
---|
172 |
|
---|
173 | //String name = classifier.getPositionString();
|
---|
174 | int classifier_index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length()));
|
---|
175 | Element root = CollectionDesignManager.collect_config.getDocumentElement();
|
---|
176 | NodeList classify_list = root.getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT);
|
---|
177 |
|
---|
178 | // - 1 because java is from 0 up, greenstone starts from 1.
|
---|
179 | classifier_index = classifier_index - 1;
|
---|
180 | return (Element) classify_list.item(classifier_index);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Method to translate this class information into a line of text as you
|
---|
185 | * would expect on the format list panel
|
---|
186 | *
|
---|
187 | * @return A <strong>String</strong> containing the format command.
|
---|
188 | */
|
---|
189 | public String toString()
|
---|
190 | {
|
---|
191 |
|
---|
192 | return feature_name + " " + toOneLineFormat(feature_format);
|
---|
193 | }
|
---|
194 |
|
---|
195 | public void update()
|
---|
196 | {
|
---|
197 |
|
---|
198 | if (is_classifier)
|
---|
199 | {
|
---|
200 | feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element);
|
---|
201 | Classifier classifier = getClassifier(feature_name);
|
---|
202 | //update the classifier_options string
|
---|
203 | classifier_options = classifier.toString();
|
---|
204 | pure_format = toOneLineFormat(XMLTools.getNodeText(element));
|
---|
205 | feature_format = classifier_options + " " + pure_format;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | public String getFormatedFormat()
|
---|
210 | {
|
---|
211 | return toFormatedFormat(feature_format);
|
---|
212 | }
|
---|
213 |
|
---|
214 | public String getPureFormat()
|
---|
215 | {
|
---|
216 | return toFormatedFormat(pure_format);
|
---|
217 | }
|
---|
218 |
|
---|
219 | public Classifier getClassifier()
|
---|
220 | {
|
---|
221 | if (!isClassifier())
|
---|
222 | {
|
---|
223 |
|
---|
224 | return null;
|
---|
225 | }
|
---|
226 | int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length()));
|
---|
227 | //index-1 because java indexes from 0, but greenstone is from 1.
|
---|
228 | index = index - 1;
|
---|
229 | return CollectionDesignManager.classifier_manager.getClassifier(index);
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void setFeatureName(String name)
|
---|
233 | {
|
---|
234 | feature_name = name;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Set the value of value. Hmmm.
|
---|
239 | *
|
---|
240 | * @param value
|
---|
241 | * The new value from value as a <strong>String</strong>.
|
---|
242 | */
|
---|
243 | public void setFeatureFormat(String value)
|
---|
244 | {
|
---|
245 | if (is_classifier)
|
---|
246 | {
|
---|
247 | feature_format = classifier_options + " " + value;
|
---|
248 | }
|
---|
249 | else
|
---|
250 | {
|
---|
251 | feature_format = value;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | public void setPureFormat(String value)
|
---|
256 | {
|
---|
257 | pure_format = value;
|
---|
258 | setFeatureFormat(value);
|
---|
259 | XMLTools.setNodeText(element, pure_format);
|
---|
260 | }
|
---|
261 |
|
---|
262 | public static String toOneLineFormat(String str)
|
---|
263 | {
|
---|
264 |
|
---|
265 | return str.replaceAll("\n", "").replaceAll("\\s+", " ").replaceAll(" <", "<");
|
---|
266 | }
|
---|
267 |
|
---|
268 | public static String toFormatedFormat(String str)
|
---|
269 | {
|
---|
270 | return str.replaceAll("\\s+", " ").replaceAll("<", "\n<").replaceFirst("\n", "");
|
---|
271 | }
|
---|
272 |
|
---|
273 | public int compareTo(Object object)
|
---|
274 | {
|
---|
275 | if (object == null)
|
---|
276 | { // It seems that occasionally compareTo is called and somehow null ends up in comparison.
|
---|
277 | return -1;
|
---|
278 | }
|
---|
279 | return this.getFeatureName().compareToIgnoreCase(((Format4gs3) object).getFeatureName());
|
---|
280 | }
|
---|
281 |
|
---|
282 | public DOMProxyListEntry create(Element element)
|
---|
283 | {
|
---|
284 | return new Format4gs3(element);
|
---|
285 | }
|
---|
286 |
|
---|
287 | public boolean equals(Object object)
|
---|
288 | {
|
---|
289 | return (compareTo(object) == 0);
|
---|
290 | }
|
---|
291 |
|
---|
292 | public Element getElement()
|
---|
293 | {
|
---|
294 | return element;
|
---|
295 | }
|
---|
296 |
|
---|
297 | public boolean isClassifier()
|
---|
298 | {
|
---|
299 | return feature_name.startsWith(Classifier.CLASSIFIER_PREFIX);
|
---|
300 | }
|
---|
301 |
|
---|
302 | public String getFeatureName()
|
---|
303 | {
|
---|
304 | return feature_name;
|
---|
305 | }
|
---|
306 |
|
---|
307 | //The format appended as a row of string used to display in the format list panel
|
---|
308 | public String getFeatureFormat()
|
---|
309 | {
|
---|
310 | return feature_format;
|
---|
311 | }
|
---|
312 |
|
---|
313 | public void setElement(Element element)
|
---|
314 | {
|
---|
315 | this.element = element;
|
---|
316 | }
|
---|
317 |
|
---|
318 | public boolean isAssigned()
|
---|
319 | {
|
---|
320 | return true;
|
---|
321 | }
|
---|
322 |
|
---|
323 | public void setAssigned(boolean assigned)
|
---|
324 | {
|
---|
325 | }
|
---|
326 |
|
---|
327 | } |
---|