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