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

Last change on this file since 4932 was 4932, checked in by jmt12, 21 years ago

Major CDM rewrite so it uses DOM.

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