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

Last change on this file since 8853 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

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