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

Last change on this file since 7224 was 6539, checked in by jmt12, 20 years ago

Heres a bunch of other changed files. If it wasn't a Friday afternoon I might be bothered finding out what I actually changed in them. Such changes include: a new option or three on preferences, a bug fix for the GDM classes, several changes to CDM to allow for G2.39 configuration files, a fix to Codec to allow for quotes in format strings and more work on CommandTokenizer to allow for stupid, stupid, stupid collectionextra's starting with speech marks then a new line. Plus other stuff. And things. Peace Out.

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