source: gli/trunk/src/org/greenstone/gatherer/cdm/Format4gs3.java@ 14359

Last change on this file since 14359 was 14359, checked in by xiao, 17 years ago

modify to make a Format4gs3 object contain two format features: feature_format - contains format statements plus classifier options if this object is a classifier, displayed on the format list in GLI; pure_format - contains only format statements, displayed in the format editor in GLI.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.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: Xiao F. Yu, 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.*;
34import org.greenstone.gatherer.metadata.MetadataElement;
35import org.greenstone.gatherer.metadata.MetadataTools;
36
37
38/** This class is the greenstone 3 version of the Format.java in gs2 for handling 'gsf' format statements.
39 */
40public class Format4gs3 implements DOMProxyListEntry {
41
42 /** The element this format is based on. */
43 private Element element = null;
44 /** We keep a copy of the feature name */
45 private String feature_name = null;
46 /** The format statement for this feature (also contains classifier options if its a classifier), used by the format list on the top in GLI. */
47 private String feature_format = null;
48
49 /** The format statement for this feature (does not contain options in the front), displayed in the format editor at the bottom in GLI.
50 * If this format is not a classifier, feature_format==pure_format*/
51 private String pure_format = null;
52
53 /** Wether this feature is about a classifier. The other way to check this is to see if feature_name starts with "CL" */
54 private static boolean is_classifier = false;
55 /** If this format is about a classifier, this contains all its options */
56 private String classifier_options = "";
57
58 /** Constructor only to be used during DOMProxyListModel initialization. */
59 public Format4gs3 () {
60
61 }
62 /** Constructor for a format-string format command.
63 * @param element The <Strong>element</strong> this format object contains.
64 */
65 public Format4gs3 (Element element) {
66 this.element = element;
67
68 is_classifier = element.getParentNode().getNodeName().equals(StaticStrings.CLASSIFY_ELEMENT);
69
70 if(is_classifier) {
71 //The getClassifierPosition() method also sets the classifier_options variable.
72 feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element);
73 Classifier classifier = getClassifier(feature_name);
74 //set the classifier_options string
75 classifier_options = classifier.toString();
76
77
78 pure_format = toOneLineFormat(XMLTools.getNodeText(element));
79 feature_format = classifier_options + " " + pure_format;
80 } else {
81 feature_name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
82 feature_format = toOneLineFormat(XMLTools.getNodeText(element));
83 pure_format = feature_format;
84 }
85
86 }
87 //
88 public Format4gs3 (String feature_name, String feature_format) {
89 this.feature_name = feature_name;
90 this.feature_format = feature_format;
91 this.pure_format = feature_format;
92
93 is_classifier = feature_name.startsWith (Classifier.CLASSIFIER_PREFIX);
94
95 element = CollectionConfiguration.createElement (StaticStrings.FORMAT_STR);
96 if(is_classifier) {
97
98 //Now get the options of the classifier
99 Classifier classifier = getClassifier(feature_name);
100 //set the classifier_options string
101 classifier_options = classifier.toString();
102
103 feature_format = classifier_options + " " + feature_format;
104 } else {
105 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, feature_name);
106 }
107
108 XMLTools.setNodeText(element, toOneLineFormat(pure_format));
109
110 }
111 public int getClassifierPosition(Element element) {
112 //we could also here use 'element.getParentNode().getParentNode()' to get this root node
113 Element root = CollectionDesignManager.collect_config.getDocumentElement ();
114 NodeList classify_list = root.getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT);
115 int num_classify_elements = classify_list.getLength();
116 for(int i=0; i<num_classify_elements; i++) {
117 Element classify = (Element)classify_list.item(i);
118 Element format = (Element)XMLTools.getChildByTagName(classify, StaticStrings.FORMAT_STR);
119 if(format == element) {
120 //Now get the options of the classifier
121 Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(i);
122 classifier_options = classifier.toString();
123 // i+1 because java is from 0 up, greenstone starts from 1.
124 return i + 1;
125 }
126 }
127 return -1;
128 }
129 public static Classifier getClassifier(String feature_name) {
130 int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length()));
131 Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(index-1);
132 return classifier;
133 }
134 //This method is used when the add button is clicked and a new format element for a classifier is added to
135 // the DOM tree, we need to figure out which Classify element to append this format element to.
136 public Element getClassifyElement () {
137
138 //String name = classifier.getPositionString();
139 int classifier_index = Integer.parseInt (feature_name.substring (Classifier.CLASSIFIER_PREFIX.length ()));
140 Element root = CollectionDesignManager.collect_config.getDocumentElement ();
141 NodeList classify_list = root.getElementsByTagName (StaticStrings.CLASSIFY_ELEMENT);
142
143 // - 1 because java is from 0 up, greenstone starts from 1.
144 classifier_index = classifier_index - 1;
145 return (Element)classify_list.item (classifier_index);
146 }
147
148 /** Method to translate this class information into a line of text as you would expect on the format list panel
149 * @return A <strong>String</strong> containing the format command.
150 */
151 public String toString () {
152
153 return feature_name + " " + toOneLineFormat(feature_format);
154 }
155 public void update () {
156
157 if (is_classifier) {
158 feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element);
159 Classifier classifier = getClassifier(feature_name);
160 //update the classifier_options string
161 classifier_options = classifier.toString();
162 pure_format = toOneLineFormat(XMLTools.getNodeText(element));
163 feature_format = classifier_options + " " + pure_format;
164 }
165 }
166 public String getFormatedFormat() {
167 return toFormatedFormat(feature_format);
168 }
169 public String getPureFormat() {
170 return toFormatedFormat(pure_format);
171 }
172
173 public Classifier getClassifier() {
174 if(!isClassifier()) {
175
176 return null;
177 }
178 int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length()));
179 //index-1 because java indexes from 0, but greenstone is from 1.
180 index = index - 1;
181 return CollectionDesignManager.classifier_manager.getClassifier(index);
182 }
183 public void setFeatureName (String name) {
184 feature_name = name;
185 }
186 /** Set the value of value. Hmmm.
187 * @param value The new value from value as a <strong>String</strong>.
188 */
189 public void setFeatureFormat (String value) {
190 if(is_classifier) {
191 feature_format = classifier_options + " " + value;
192 } else {
193 feature_format = value;
194 }
195 }
196 public void setPureFormat (String value) {
197 pure_format = value;
198 setFeatureFormat (value);
199 XMLTools.setNodeText(element, pure_format);
200 }
201 public static String toOneLineFormat(String str) {
202 return str.replaceAll("\n", "").replaceAll(" ", "").replaceAll(" <", "<");
203 }
204 public static String toFormatedFormat(String str) {
205 return str.replaceAll(" ", "").replaceAll("<", "\n<").replaceFirst("\n", "");
206 }
207 public int compareTo (Object object) {
208 if(object == null) { // It seems that occasionally compareTo is called and somehow null ends up in comparison.
209 return -1;
210 }
211 return this.getFeatureName ().compareToIgnoreCase (((Format4gs3)object).getFeatureName());
212 }
213
214 public DOMProxyListEntry create (Element element) {
215 return new Format4gs3 (element);
216 }
217
218 public boolean equals (Object object) {
219 return (compareTo (object) == 0);
220 }
221
222 public Element getElement () {
223 return element;
224 }
225 public boolean isClassifier() {
226 return feature_name.startsWith(Classifier.CLASSIFIER_PREFIX);
227 }
228 public String getFeatureName () {
229 return feature_name;
230 }
231 //The format appended as a row of string used to display in the format list panel
232 public String getFeatureFormat () {
233 return feature_format;
234 }
235 public void setElement (Element element) {
236 this.element = element;
237 }
238 public boolean isAssigned() {
239 return true;
240 }
241
242 public void setAssigned(boolean assigned) {
243 }
244
245}
Note: See TracBrowser for help on using the repository browser.