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

Last change on this file since 15137 was 15137, checked in by kjdon, 16 years ago

added NavigationBar into the list of format options. I added pulldown as the default value - this is the only value that currently makes sense to add. Any other value you get the default nav bar

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