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

Last change on this file since 12468 was 12301, checked in by kjdon, 18 years ago

changed the default features back to have "" as the first one - will change the display in FormatManager to display All Features

  • Property svn:keywords set to Author Date Id Revision
File size: 13.5 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[] = { "", "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(getFeatureName());
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 return feature_name;
219
220 }
221 public String getName() {
222 if(element != null) {
223 String name = element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
224 return element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
225 }
226 return "Error";
227 }
228
229 /** Method to retrieve the value of part. Of course there may not be one, in which case return ""
230 * @return The value of part as a <Strong>String</strong>.
231 */
232 public String getPart() {
233 if(part == null && element != null) {
234 // To determine a part, we retrieve the Format name, then look for one of our recognized parts
235 String name = element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
236 // DEFAULT_PARTS[0] is an empty string to correctly enable comboboxes.
237 name = name.toLowerCase();
238 for(int i = 1; part == null && i < DEFAULT_PARTS.length; i++) {
239 if(name.endsWith(DEFAULT_PARTS[i].toLowerCase())) {
240 part = DEFAULT_PARTS[i];
241 }
242 }
243 if(part == null) {
244 part = DEFAULT_PARTS[0];
245 }
246 }
247 return part;
248 }
249
250 /** Method to retrieve the value of state.
251 * @return value of state as a <i>boolean</i>.
252 */
253 public boolean getState() {
254 return (element != null && element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR));
255 }
256
257 /** Retrieve the value of value.
258 * @return A <strong>String</strong> which is the value of value.
259 */
260 public String getValue() {
261 String value = "Error";
262 if(element != null) {
263 value = XMLTools.getValue(element);
264 // Decode into text
265 value = Codec.transform(value, Codec.DOM_TO_TEXT);
266 }
267 return value;
268 }
269
270 public boolean isAssigned() {
271 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
272 }
273
274 public boolean isParamType() {
275 return (element != null && element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE).length() > 0);
276 }
277
278 public void setAssigned(boolean assigned) {
279 if(element != null) {
280 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
281 }
282 }
283
284 public void setElement(Element element) {
285 this.element = element;
286 part = null;
287 text = null;
288 // Establish the classifier reference, if necessary
289 getFeature();
290 }
291
292 public void setName(Object feature, String part) {
293 // Can't do anything unless an element exists.
294
295 if(element != null) {
296 this.part = part; // Easier for later on.
297 if(feature instanceof Classifier) {
298 classifier = (Classifier) feature;
299 feature_name = classifier.getPositionString();
300 element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, classifier.getPositionString() + part);
301 }
302 else {
303 if (feature.toString().startsWith("<html>")){
304 feature_name = "";
305 }
306 else{
307 feature_name = feature.toString();
308 }
309 element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, feature_name + part);
310 }
311 text = null;
312 }
313 }
314
315 /** Set the value of state.
316 * @param state The new value for state as a <i>boolean</i>.
317 */
318 public void setState(boolean state) {
319 if(element != null) {
320 element.setAttribute(CollectionConfiguration.VALUE_ATTRIBUTE, (state ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
321 text = null;
322 }
323 }
324
325 /** Set the value of value. Hmmm.
326 * @param value The new value from value as a <strong>String</strong>.
327 */
328 public void setValue(String value) {
329 if(element != null) {
330 // Strip off any enclosing speech marks
331 if(value.startsWith(CollectionConfiguration.SPEECH_CHARACTER) && value.startsWith(CollectionConfiguration.SPEECH_CHARACTER)) {
332 value = value.substring(1, value.length() - 1);
333 }
334 // Encode
335 value = Codec.transform(value, Codec.TEXT_TO_DOM);
336 // Store in DOM
337 XMLTools.setValue(element, value);
338 text = null;
339 }
340 }
341
342 /** Method to translate this classes information into a line of text as you would expect in the collection configuration file.
343 * @return A <strong>String</strong> containing the format command.
344 */
345 public String toString() {
346 if(text == null && element != null) {
347 StringBuffer temp = new StringBuffer(" ");
348 temp.append(element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE));
349 temp.append(" ");
350 String value = element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE);
351 if(value.length() > 0) {
352 temp.append(value);
353 }
354 else {
355 value = XMLTools.getValue(element);
356 if(value.equalsIgnoreCase(CollectionConfiguration.TRUE_STR) || value.equalsIgnoreCase(CollectionConfiguration.FALSE_STR)) {
357 temp.append(value);
358 }
359 else {
360 temp.append("\"");
361 // Retrieve the DOM encoded value, decode and then append
362 value = Codec.transform(value, Codec.DOM_TO_TEXT);
363 temp.append(value);
364 temp.append("\"");
365 }
366 }
367 text = temp.toString();
368 temp = null;
369 }
370 return text;
371 }
372
373 public void update() {
374 if(classifier != null) {
375 element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, classifier.getPositionString() + getPart());
376 feature_name = classifier.getPositionString();
377 text = null;
378 }
379 }
380}
Note: See TracBrowser for help on using the repository browser.