source: trunk/gli/src/org/greenstone/gatherer/cdm/Format.java@ 12284

Last change on this file since 12284 was 12280, checked in by kjdon, 18 years ago

no longer display 'format' in the list of assigned formats

  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 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 * 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 */
27package org.greenstone.gatherer.cdm;
28
29import org.greenstone.gatherer.DebugStream;
30import org.greenstone.gatherer.util.Codec;
31import org.greenstone.gatherer.util.XMLTools;
32import org.w3c.dom.*;
33
34
35/** This class encapsulates all the information about a format command from the collection configuration. This includes both the 'general' formatting commands, and the commands targetted at specific classifiers or classification component types. This class is, unfortunately, the most complex in terms of DOM model support, since we have to maintain a live reference to any classifier this format applies to. We temporarily use the feature name as the format name attribute but since the order of classifiers can change, this of course gets horribly out of date. Before the CollectionDesignManager saves the CollectionConfiguration it calls a method in the FormatManager to force all of the Formats to update their name attributes.
36 * @author John Thompson, Greenstone Digital Library, University of Waikato
37 * @version 2.3
38 */
39public class Format
40 implements DOMProxyListEntry {
41
42 /** The default features (not all of these are in the Greenstone Developer's Guide). */
43 static final public String DEFAULT_FEATURES[] = { "<html><body><i>All Features</i></body></html>", "AllowExtendedOptions", "Document", "DocumentArrowsBottom", "DocumentArrowsTop", "DocumentButtons", "DocumentContents", "DocumentHeading", "DocumentImages", "DocumentText", "DocumentTitles", "DocumentUseHTML", "RelatedDocuments", "Search", "SearchTypes" };
44 /** The list of known feature parts. */
45 static final public String DEFAULT_PARTS[] = { "", "DateList", "HList", "VList" };
46
47
48 /** Only "" and "Search" can have parts */
49 static public boolean canHavePart(String name) {
50 return (name.startsWith("<html>") || name.equalsIgnoreCase("") || name.equalsIgnoreCase("Search") || name.equalsIgnoreCase("Document"));
51 }
52
53 static public String generateName(Object feature, String part) {
54 if(feature instanceof Classifier) {
55 return ((Classifier)feature).getPositionString() + part;
56 }
57 else {
58 return feature.toString() + part;
59 }
60 }
61
62
63 /** Returns true if the parameter is a boolean type, false otherwise. */
64 static public boolean isParamType(String name)
65 {
66 if (name.equalsIgnoreCase("AllowExtendedOptions") || name.equalsIgnoreCase("DocumentArrowsBottom") || name.equalsIgnoreCase("DocumentArrowsTop") || name.equalsIgnoreCase("DocumentContents") || name.equalsIgnoreCase("DocumentImages") || name.equalsIgnoreCase("DocumentTitles") || name.equalsIgnoreCase("DocumentUseHTML")) {
67 return true;
68 }
69 else {
70 return false;
71 }
72 }
73
74
75 /** If this format is altering a Classifier then this is the classifier in question. */
76 private Classifier classifier = null;
77 /** The element this format is based on. */
78 private Element element = null;
79 /** We keep a copy of the part because its slightly more computationally tricky to calculate. */
80 private String part = null;
81 /** We keep a copy of the feature name */
82 private String feature_name = null;
83 /** Cached result of toString. */
84 private String text = null;
85
86 /** Constructor only to be used during DOMProxyListModel initialization. */
87 public Format() {
88
89 }
90
91 public Format(Element element) {
92
93 this.element = element;
94 // Immediately check if we are dealing with a classifier, by retrieving the feature name. If it is a classifier, then restore the live reference immediately before anything changes it.
95 Object object = getFeature();
96
97 // There may be format statements for no-longer-existing classifiers
98 if (object == null) {
99 DebugStream.println("No matching classifier for format statement!");
100 return;
101 }
102
103 if(object instanceof Classifier) {
104 classifier = (Classifier) object;
105 }
106 else {
107 String feature = (String) object;
108 if(feature.toUpperCase().startsWith(Classifier.CLASSIFIER_PREFIX)) {
109 // Extract the integer index
110 int index = Integer.parseInt(feature.substring(Classifier.CLASSIFIER_PREFIX.length()));
111 // Subtract one (as java is 0-2 where G2 is 1-3)
112 index = index - 1;
113 // Retrieve the appropriate classifier
114 classifier = CollectionDesignManager.classifier_manager.getClassifier(index);
115 }
116 }
117
118
119 }
120
121 /** Constructor for a flag format command.
122 * @param feature The <Strong>Object</strong> this format affects.
123 * @param part The specific part of the control to format as a <strong>String</strong>.
124 * @param state A <i>boolean</i> indicating this formats state.
125 */
126 public Format(Object feature, String part, boolean state) {
127
128 element = CollectionDesignManager.collect_config.document.createElement(CollectionConfiguration.FORMAT_ELEMENT);
129 setName(feature, part);
130 setState(state);
131
132 }
133
134 /** Constructor for a format-string format command.
135 * @param feature The <Strong>Object</strong> this format affects.
136 * @param part The specific part of the control to format as a <strong>String</strong>.
137 * @param value The format <strong>String</strong> which may be a label name, or a piece of HTML formatting.
138 */
139 public Format(Object feature, String part, String value) {
140
141 element = CollectionDesignManager.collect_config.document.createElement(CollectionConfiguration.FORMAT_ELEMENT);
142 setName(feature, part);
143 setValue(value);
144
145 }
146
147
148
149 public boolean canHavePart() {
150 return canHavePart(getName());
151 }
152
153 public int compareTo(Object object) {
154 if(object == null) { // It seems that occasionally compareTo is called and somehow null ends up in comparison.
155 return -1;
156 }
157 if(object instanceof Format && element != null) {
158 return element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE).compareTo(((Format)object).getName());
159 }
160 return toString().compareTo(object.toString());
161 }
162
163 public DOMProxyListEntry create(Element element) {
164 return new Format(element);
165 }
166
167 public boolean equals(Object object) {
168 return (compareTo(object) == 0);
169 }
170
171 public Element getElement() {
172 return element;
173 }
174
175 /** Method to retrieve the value of feature, which may either be a String or a Classifier.
176 * @return The value of feature as an <strong>Object</strong>.
177 */
178 public Object getFeature() {
179 if(classifier != null) {
180 return classifier;
181 }
182 else if(element != null) {
183 String feature = getFeatureName();
184 // If the feature now refers to a classifier, retrieve it.
185 if(feature.toUpperCase().startsWith(Classifier.CLASSIFIER_PREFIX)) {
186 // Extract the integer index
187 int index = Integer.parseInt(feature.substring(Classifier.CLASSIFIER_PREFIX.length()));
188 // Subtract one (as java is 0-2 where G2 is 1-3)
189 index = index - 1;
190 // Retrieve the appropriate classifier
191 classifier = CollectionDesignManager.classifier_manager.getClassifier(index);
192 return classifier;
193 }
194 else {
195 return feature;
196 }
197 }
198 else {
199 return "Error";
200 }
201 }
202
203 public String getFeatureName() {
204 if (feature_name == null) {
205 String name = element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
206 // Remove part
207 String part = getPart();
208 if(part != null) {
209 feature_name = name.substring(0, name.length() - part.length());
210 }
211 else {
212 feature_name = name;
213 }
214 part = null;
215 name = null;
216 }
217
218 if ( feature_name.equals("")){
219 feature_name = DEFAULT_FEATURES[0];
220 }
221
222 return feature_name;
223
224 }
225 public String getName() {
226 if(element != null) {
227 String name = element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
228 if (name.equals("")){
229 return DEFAULT_FEATURES[0];
230 }
231 return element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
232 }
233 return "Error";
234 }
235
236 /** Method to retrieve the value of part. Of course there may not be one, in which case return ""
237 * @return The value of part as a <Strong>String</strong>.
238 */
239 public String getPart() {
240 if(part == null && element != null) {
241 // To determine a part, we retrieve the Format name, then look for one of our recognized parts
242 String name = element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
243 // DEFAULT_PARTS[0] is an empty string to correctly enable comboboxes.
244 name = name.toLowerCase();
245 for(int i = 1; part == null && i < DEFAULT_PARTS.length; i++) {
246 if(name.endsWith(DEFAULT_PARTS[i].toLowerCase())) {
247 part = DEFAULT_PARTS[i];
248 }
249 }
250 if(part == null) {
251 part = DEFAULT_PARTS[0];
252 }
253 }
254 return part;
255 }
256
257 /** Method to retrieve the value of state.
258 * @return value of state as a <i>boolean</i>.
259 */
260 public boolean getState() {
261 return (element != null && element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR));
262 }
263
264 /** Retrieve the value of value.
265 * @return A <strong>String</strong> which is the value of value.
266 */
267 public String getValue() {
268 String value = "Error";
269 if(element != null) {
270 value = XMLTools.getValue(element);
271 // Decode into text
272 value = Codec.transform(value, Codec.DOM_TO_TEXT);
273 }
274 return value;
275 }
276
277 public boolean isAssigned() {
278 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
279 }
280
281 public boolean isParamType() {
282 return (element != null && element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE).length() > 0);
283 }
284
285 public void setAssigned(boolean assigned) {
286 if(element != null) {
287 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
288 }
289 }
290
291 public void setElement(Element element) {
292 this.element = element;
293 part = null;
294 text = null;
295 // Establish the classifier reference, if necessary
296 getFeature();
297 }
298
299 public void setName(Object feature, String part) {
300 // Can't do anything unless an element exists.
301
302 if(element != null) {
303 this.part = part; // Easier for later on.
304 if(feature instanceof Classifier) {
305 classifier = (Classifier) feature;
306 feature_name = classifier.getPositionString();
307 element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, classifier.getPositionString() + part);
308 }
309 else {
310 if (feature.toString().startsWith("<html>")){
311 feature_name = "";
312 }
313 else{
314 feature_name = feature.toString();
315 }
316 element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, feature_name + part);
317 }
318 text = null;
319 }
320 }
321
322 /** Set the value of state.
323 * @param state The new value for state as a <i>boolean</i>.
324 */
325 public void setState(boolean state) {
326 if(element != null) {
327 element.setAttribute(CollectionConfiguration.VALUE_ATTRIBUTE, (state ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
328 text = null;
329 }
330 }
331
332 /** Set the value of value. Hmmm.
333 * @param value The new value from value as a <strong>String</strong>.
334 */
335 public void setValue(String value) {
336 if(element != null) {
337 // Strip off any enclosing speech marks
338 if(value.startsWith(CollectionConfiguration.SPEECH_CHARACTER) && value.startsWith(CollectionConfiguration.SPEECH_CHARACTER)) {
339 value = value.substring(1, value.length() - 1);
340 }
341 // Encode
342 value = Codec.transform(value, Codec.TEXT_TO_DOM);
343 // Store in DOM
344 XMLTools.setValue(element, value);
345 text = null;
346 }
347 }
348
349 /** Method to translate this classes information into a line of text as you would expect in the collection configuration file.
350 * @return A <strong>String</strong> containing the format command.
351 */
352 public String toString() {
353 if(text == null && element != null) {
354 StringBuffer temp = new StringBuffer(" ");
355 temp.append(element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE));
356 temp.append(" ");
357 String value = element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE);
358 if(value.length() > 0) {
359 temp.append(value);
360 }
361 else {
362 value = XMLTools.getValue(element);
363 if(value.equalsIgnoreCase(CollectionConfiguration.TRUE_STR) || value.equalsIgnoreCase(CollectionConfiguration.FALSE_STR)) {
364 temp.append(value);
365 }
366 else {
367 temp.append("\"");
368 // Retrieve the DOM encoded value, decode and then append
369 value = Codec.transform(value, Codec.DOM_TO_TEXT);
370 temp.append(value);
371 temp.append("\"");
372 }
373 }
374 text = temp.toString();
375 temp = null;
376 }
377 return text;
378 }
379
380 public void update() {
381 if(classifier != null) {
382 element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, classifier.getPositionString() + getPart());
383 feature_name = classifier.getPositionString();
384 text = null;
385 }
386 }
387}
Note: See TracBrowser for help on using the repository browser.