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

Last change on this file since 8055 was 8015, checked in by mdewsnip, 20 years ago

Moved clear() and setValue() functions from MSMUtils into XMLTools. Moved NS_SEP string from MSMUtils into StaticStrings.

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