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 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import org.greenstone.gatherer.DebugStream;
|
---|
30 | import org.greenstone.gatherer.util.Codec;
|
---|
31 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
32 | import org.greenstone.gatherer.util.XMLTools;
|
---|
33 | import org.w3c.dom.*;
|
---|
34 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
35 | import 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 | */
|
---|
40 | public 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 && CollectionDesignManager.classifier_manager!=null) {
|
---|
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 | if (classifier != null){
|
---|
76 | classifier_options = classifier.toString();
|
---|
77 | }
|
---|
78 |
|
---|
79 | pure_format = toOneLineFormat(XMLTools.getNodeText(element));
|
---|
80 | feature_format = classifier_options + " " + pure_format;
|
---|
81 | } else {
|
---|
82 | feature_name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
83 | feature_format = toOneLineFormat(XMLTools.getNodeText(element));
|
---|
84 | pure_format = feature_format;
|
---|
85 | }
|
---|
86 |
|
---|
87 | }
|
---|
88 | //
|
---|
89 | public Format4gs3 (String feature_name, String feature_format) {
|
---|
90 | this.feature_name = feature_name;
|
---|
91 | this.feature_format = feature_format;
|
---|
92 | this.pure_format = feature_format;
|
---|
93 |
|
---|
94 | is_classifier = feature_name.startsWith (Classifier.CLASSIFIER_PREFIX);
|
---|
95 |
|
---|
96 | element = CollectionConfiguration.createElement (StaticStrings.FORMAT_STR);
|
---|
97 | if(is_classifier) {
|
---|
98 |
|
---|
99 | //Now get the options of the classifier
|
---|
100 | Classifier classifier = getClassifier(feature_name);
|
---|
101 | //set the classifier_options string
|
---|
102 | classifier_options = classifier.toString();
|
---|
103 |
|
---|
104 | feature_format = classifier_options + " " + feature_format;
|
---|
105 | } else {
|
---|
106 | element.setAttribute (StaticStrings.NAME_ATTRIBUTE, feature_name);
|
---|
107 | }
|
---|
108 |
|
---|
109 | XMLTools.setNodeText(element, toOneLineFormat(pure_format));
|
---|
110 |
|
---|
111 | }
|
---|
112 | public int getClassifierPosition(Element element) {
|
---|
113 | //we could also here use 'element.getParentNode().getParentNode()' to get this root node
|
---|
114 | Element root = CollectionDesignManager.collect_config.getDocumentElement ();
|
---|
115 | NodeList classify_list = root.getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT);
|
---|
116 | int num_classify_elements = classify_list.getLength();
|
---|
117 | for(int i=0; i<num_classify_elements; i++) {
|
---|
118 | Element classify = (Element)classify_list.item(i);
|
---|
119 | Element format = (Element)XMLTools.getChildByTagName(classify, StaticStrings.FORMAT_STR);
|
---|
120 | if(format == element) {
|
---|
121 | //Now get the options of the classifier
|
---|
122 | Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(i);
|
---|
123 | classifier_options = classifier.toString();
|
---|
124 | // i+1 because java is from 0 up, greenstone starts from 1.
|
---|
125 | return i + 1;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | return -1;
|
---|
129 | }
|
---|
130 | public static Classifier getClassifier(String feature_name) {
|
---|
131 | int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length()));
|
---|
132 | Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(index-1);
|
---|
133 | return classifier;
|
---|
134 | }
|
---|
135 | //This method is used when the add button is clicked and a new format element for a classifier is added to
|
---|
136 | // the DOM tree, we need to figure out which Classify element to append this format element to.
|
---|
137 | public Element getClassifyElement () {
|
---|
138 |
|
---|
139 | //String name = classifier.getPositionString();
|
---|
140 | int classifier_index = Integer.parseInt (feature_name.substring (Classifier.CLASSIFIER_PREFIX.length ()));
|
---|
141 | Element root = CollectionDesignManager.collect_config.getDocumentElement ();
|
---|
142 | NodeList classify_list = root.getElementsByTagName (StaticStrings.CLASSIFY_ELEMENT);
|
---|
143 |
|
---|
144 | // - 1 because java is from 0 up, greenstone starts from 1.
|
---|
145 | classifier_index = classifier_index - 1;
|
---|
146 | return (Element)classify_list.item (classifier_index);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Method to translate this class information into a line of text as you would expect on the format list panel
|
---|
150 | * @return A <strong>String</strong> containing the format command.
|
---|
151 | */
|
---|
152 | public String toString () {
|
---|
153 |
|
---|
154 | return feature_name + " " + toOneLineFormat(feature_format);
|
---|
155 | }
|
---|
156 | public void update () {
|
---|
157 |
|
---|
158 | if (is_classifier) {
|
---|
159 | feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element);
|
---|
160 | Classifier classifier = getClassifier(feature_name);
|
---|
161 | //update the classifier_options string
|
---|
162 | classifier_options = classifier.toString();
|
---|
163 | pure_format = toOneLineFormat(XMLTools.getNodeText(element));
|
---|
164 | feature_format = classifier_options + " " + pure_format;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | public String getFormatedFormat() {
|
---|
168 | return toFormatedFormat(feature_format);
|
---|
169 | }
|
---|
170 | public String getPureFormat() {
|
---|
171 | return toFormatedFormat(pure_format);
|
---|
172 | }
|
---|
173 |
|
---|
174 | public Classifier getClassifier() {
|
---|
175 | if(!isClassifier()) {
|
---|
176 |
|
---|
177 | return null;
|
---|
178 | }
|
---|
179 | int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length()));
|
---|
180 | //index-1 because java indexes from 0, but greenstone is from 1.
|
---|
181 | index = index - 1;
|
---|
182 | return CollectionDesignManager.classifier_manager.getClassifier(index);
|
---|
183 | }
|
---|
184 | public void setFeatureName (String name) {
|
---|
185 | feature_name = name;
|
---|
186 | }
|
---|
187 | /** Set the value of value. Hmmm.
|
---|
188 | * @param value The new value from value as a <strong>String</strong>.
|
---|
189 | */
|
---|
190 | public void setFeatureFormat (String value) {
|
---|
191 | if(is_classifier) {
|
---|
192 | feature_format = classifier_options + " " + value;
|
---|
193 | } else {
|
---|
194 | feature_format = value;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | public void setPureFormat (String value) {
|
---|
198 | pure_format = value;
|
---|
199 | setFeatureFormat (value);
|
---|
200 | XMLTools.setNodeText(element, pure_format);
|
---|
201 | }
|
---|
202 | public static String toOneLineFormat(String str) {
|
---|
203 | return str.replaceAll("\n", "").replaceAll(" ", "").replaceAll(" <", "<");
|
---|
204 | }
|
---|
205 | public static String toFormatedFormat(String str) {
|
---|
206 | return str.replaceAll(" ", "").replaceAll("<", "\n<").replaceFirst("\n", "");
|
---|
207 | }
|
---|
208 | public int compareTo (Object object) {
|
---|
209 | if(object == null) { // It seems that occasionally compareTo is called and somehow null ends up in comparison.
|
---|
210 | return -1;
|
---|
211 | }
|
---|
212 | return this.getFeatureName ().compareToIgnoreCase (((Format4gs3)object).getFeatureName());
|
---|
213 | }
|
---|
214 |
|
---|
215 | public DOMProxyListEntry create (Element element) {
|
---|
216 | return new Format4gs3 (element);
|
---|
217 | }
|
---|
218 |
|
---|
219 | public boolean equals (Object object) {
|
---|
220 | return (compareTo (object) == 0);
|
---|
221 | }
|
---|
222 |
|
---|
223 | public Element getElement () {
|
---|
224 | return element;
|
---|
225 | }
|
---|
226 | public boolean isClassifier() {
|
---|
227 | return feature_name.startsWith(Classifier.CLASSIFIER_PREFIX);
|
---|
228 | }
|
---|
229 | public String getFeatureName () {
|
---|
230 | return feature_name;
|
---|
231 | }
|
---|
232 | //The format appended as a row of string used to display in the format list panel
|
---|
233 | public String getFeatureFormat () {
|
---|
234 | return feature_format;
|
---|
235 | }
|
---|
236 | public void setElement (Element element) {
|
---|
237 | this.element = element;
|
---|
238 | }
|
---|
239 | public boolean isAssigned() {
|
---|
240 | return true;
|
---|
241 | }
|
---|
242 |
|
---|
243 | public void setAssigned(boolean assigned) {
|
---|
244 | }
|
---|
245 |
|
---|
246 | } |
---|