source: main/trunk/gli/src/org/greenstone/gatherer/collection/BasicCollectionConfiguration.java@ 36580

Last change on this file since 36580 was 36580, checked in by davidb, 20 months ago

Changed to using square brackets for displaying short collection name

  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.collection;
38
39import java.io.*;
40import java.util.ArrayList;
41import org.greenstone.gatherer.Configuration;
42import org.greenstone.gatherer.DebugStream;
43import org.greenstone.gatherer.Dictionary;
44import org.greenstone.gatherer.Gatherer;
45import org.greenstone.gatherer.cdm.CommandTokenizer;
46import org.greenstone.gatherer.util.Codec;
47import org.greenstone.gatherer.util.StaticStrings;
48import org.greenstone.gatherer.util.XMLTools;
49import org.w3c.dom.*;
50
51/** This class provides access to a collection configuration file. This version accepts either a valid xml document or a historical collect.cfg file. */
52public class BasicCollectionConfiguration
53 implements Comparable {
54
55 private File file;
56 private String creator = "";
57 private String description = "";
58 private String maintainer = "";
59 private String name = "";
60 private String is_public = "";
61 private String collectgroup = "false";
62
63 private String short_name = "";
64
65 private String site = null; // used for gs3 colls
66
67 public BasicCollectionConfiguration(File file) {
68 this.file = file;
69 try {
70 String filename = file.getName().toLowerCase();
71 if(!file.exists()) {
72 return;
73 }
74 if(filename.endsWith(".cfg")) {
75 FileInputStream fis = new FileInputStream(file);
76 InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
77 BufferedReader br = new BufferedReader(isr);
78 String command = null;
79 while((command = br.readLine()) != null) {
80 if (command.startsWith("#")) {
81 // a comment
82 continue;
83 }
84 if(command.length() > 0) {
85 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
86 while(command.trim().endsWith("\\")) {
87 command = command.substring(0, command.lastIndexOf("\\"));
88 String next_line = br.readLine();
89 if (next_line != null) {
90 command = command + "\n" + next_line;
91 }
92 next_line = null;
93 }
94 CommandTokenizer tokenizer = new CommandTokenizer(command, br);
95 String command_type_str = tokenizer.nextToken();
96 if (command_type_str == null) {
97 // Bad command. Do nothing
98 continue;
99 }
100
101 command_type_str = command_type_str.toLowerCase();
102 if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
103 creator = tokenizer.nextToken();
104 }
105 else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
106 maintainer = tokenizer.nextToken();
107 }
108 else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTGROUP_STR)) {
109 collectgroup = tokenizer.nextToken();
110 }
111 else if(command_type_str.equalsIgnoreCase(StaticStrings.COLLECTIONMETADATA_STR)) {
112 String meta_type_str = tokenizer.nextToken();
113 String value_str = tokenizer.nextToken();
114 // check for language
115 String language_str = StaticStrings.ENGLISH_LANGUAGE_STR; // assume the no-lang ones are english, but we shouldn't really do this.
116 if(meta_type_str != null && value_str != null) {
117 meta_type_str = meta_type_str.toLowerCase();
118 if(value_str.startsWith(StaticStrings.LBRACKET_CHARACTER) && value_str.endsWith(StaticStrings.RBRACKET_CHARACTER)) {
119 language_str = value_str.substring(value_str.indexOf(StaticStrings.EQUALS_CHARACTER) + 1, value_str.length() - 1);
120 language_str = language_str.toLowerCase();
121 value_str = tokenizer.nextToken();
122 }
123 // now we can work out which coll meta we are dealing with
124 if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR)) {
125 // If this is the first collection title found, then use it, otherwise search for one that more closely matches our choosen interface language
126 if (name == null || language_str.equals(Dictionary.getLanguage())) {
127 name = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
128 }
129 }
130 else if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR)) {
131 // Again we are either looking for the first description, then after that a language specific one
132 if (description == null || language_str.equals(Dictionary.getLanguage())) {
133 description = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
134 }
135 }
136 }
137 language_str = null;
138 value_str = null;
139 meta_type_str = null;
140 } else {
141 // we want to process all the tokens to make sure we get rid of multi line commands before trying to find the next one
142 while (tokenizer.hasMoreTokens()) {
143 tokenizer.nextToken();
144 }
145
146 } // end of coll meta bit
147 command_type_str = null;
148 tokenizer = null;
149 } // if command.length > 0
150 } // while
151 command = null;
152 br.close();
153 isr.close();
154 br = null;
155 isr = null;
156 } // cfg file
157 ///ystem.err.println("Parsed collect.cfg");
158 ///ystem.err.println("name = " + name);
159 ///ystem.err.println("creator = " + creator);
160 ///ystem.err.println("maintainer = " + maintainer);
161 ///ystem.err.println("description = " + description);
162
163 if(filename.endsWith(".xml")) {
164
165 Document xml_file_doc = XMLTools.parseXMLFile(file);
166 Element root = xml_file_doc.getDocumentElement();
167
168 Node metadataListNode = XMLTools.getChildByTagName(root, StaticStrings.METADATALIST_STR);
169 Node displayItemListNode = XMLTools.getChildByTagName(root, StaticStrings.DISPLAYITEMLIST_STR);
170
171 // Get the metadataList and displayItemList metadata for the current gli language
172 String default_language = StaticStrings.ENGLISH_LANGUAGE_STR; // is this always the case?
173 String gli_language_code = Configuration.getLanguage(); // definitely returns some language code
174
175 if (metadataListNode != null) {
176 ArrayList metadata_list = XMLTools.getChildElementsByTagName((Element)metadataListNode, StaticStrings.METADATA_STR);
177
178 String fallback_lang_creator = "";
179 String fallback_lang_maintainer = "";
180 String fallback_lang_is_public = "";
181
182 for (int i=0; i<metadata_list.size(); i++) {
183 Element meta = (Element) metadata_list.get(i);
184 String meta_name = meta.getAttribute(StaticStrings.NAME_ATTRIBUTE);
185
186 // want to get the metadata matching GLI's language, or else fall back on the default language
187 // version, or else fall back onto the first value for any language provided for the metadata
188 String meta_lang = meta.getAttribute(StaticStrings.LANG_ATTRIBUTE); // returns "" if no such attribute
189 if(meta_lang.equals("")) {
190 meta_lang = default_language;
191 }
192
193 if (meta_name.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
194 if (meta_lang.equals(gli_language_code)) { // metadata's language matched the requested language
195 creator = XMLTools.getNodeText(meta);
196 } else if (meta_lang.equals(default_language)) { // meta_lang matched fall back language
197 fallback_lang_creator = XMLTools.getNodeText(meta);
198 } else if (creator.equals("") && fallback_lang_creator.equals("")) { // resort to first value for meta we can find
199 fallback_lang_creator = XMLTools.getNodeText(meta);
200 }
201 } else if (meta_name.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
202 if (meta_lang.equals(gli_language_code)) {
203 maintainer = XMLTools.getNodeText(meta);
204 } else if (meta_lang.equals(default_language)) {
205 fallback_lang_maintainer = XMLTools.getNodeText(meta);
206 } else if (maintainer.equals("") && fallback_lang_maintainer.equals("")) { // resort to first value for meta we can find
207 fallback_lang_maintainer = XMLTools.getNodeText(meta);
208 }
209 } else if (meta_name.equals(StaticStrings.COLLECTIONMETADATA_PUBLIC_STR)) {
210 if (meta_lang.equals(gli_language_code)) {
211 is_public = XMLTools.getNodeText(meta);
212 } else if (meta_lang.equals(default_language)) {
213 fallback_lang_is_public = XMLTools.getNodeText(meta);
214 } else if (is_public.equals("") && fallback_lang_is_public.equals("")) { // resort to first value for meta we can find
215 fallback_lang_is_public = XMLTools.getNodeText(meta);
216 }
217 }
218 }
219 // if no metadata found for the requested language, use the one set in the default language
220 if(creator.equals("")) {
221 creator = fallback_lang_creator;
222 }
223 if(maintainer.equals("")) {
224 maintainer = fallback_lang_maintainer;
225 }
226 if(is_public.equals("")) {
227 is_public = fallback_lang_is_public;
228 }
229 }
230
231
232 if (displayItemListNode != null) {
233 ArrayList display_list = XMLTools.getChildElementsByTagName((Element)displayItemListNode, StaticStrings.DISPLAYITEM_STR);
234
235 String fallback_lang_name = "";
236 String fallback_lang_description = "";
237
238 for (int i=0; i<display_list.size(); i++) {
239 Element meta = (Element) display_list.get(i);
240 String meta_name = meta.getAttribute(StaticStrings.NAME_ATTRIBUTE);
241 String meta_lang = meta.getAttribute(StaticStrings.LANG_ATTRIBUTE);
242 if(meta_lang.equals("")) {
243 meta_lang = default_language;
244 }
245
246 if (meta_name.equals(StaticStrings.NAME_STR)) {
247 if (meta_lang.equals(gli_language_code)) {
248 name = XMLTools.getNodeText(meta);
249 } else if (meta_lang.equals(default_language)) {
250 fallback_lang_name = XMLTools.getNodeText(meta);
251 } else if (name.equals("") && fallback_lang_name.equals("")) {
252 fallback_lang_name = XMLTools.getNodeText(meta);
253 }
254 } else if (meta_name.equals(StaticStrings.DESCRIPTION_STR)) {
255 if (meta_lang.equals(gli_language_code)) {
256 description = XMLTools.getNodeText(meta);
257 } else if (meta_lang.equals(default_language)) {
258 fallback_lang_description = XMLTools.getNodeText(meta);
259 } else if (description.equals("") && fallback_lang_description.equals("")) {
260 fallback_lang_description = XMLTools.getNodeText(meta);
261 }
262 }
263
264 }
265
266 if(name.equals("")) {
267 name = fallback_lang_name;
268 }
269 if(description.equals("")) {
270 description = fallback_lang_description;
271 }
272 }
273 }
274
275 }
276 catch(Exception error) {
277 DebugStream.println("Error in CollectionConfiguration.<init>(): " + error);
278 DebugStream.printStackTrace(error);
279 }
280
281 }
282
283 /** Compare this configuration to another for ordering, which essentially compares two strings for ordering.
284 * @param other the other Object which is presumably another basic collection configuration
285 * @return an integer which is either <0, 0 or >0 if this configuration is naturally less than, equal to or greater than the target object
286 */
287 public int compareTo(Object other) {
288 if(other == null) {
289 return -1;
290 }
291 return toString().compareTo(other.toString());
292 }
293
294 public boolean equals(Object other) {
295 return (compareTo(other) == 0);
296 }
297
298 /** Retrieve the creators email for this collection.
299 * @return a String
300 */
301 public String getCreator() {
302 return creator;
303 }
304
305 public String getDescription() {
306 return description;
307 }
308
309 public File getFile() {
310 return file;
311 }
312
313 public String getMaintainer() {
314 return maintainer;
315 }
316
317 public String getName() {
318 return name;
319 }
320
321 public String getIsPublic() {
322 return is_public;
323 }
324
325 public String getCollectGroup() {
326 return collectgroup;
327 }
328
329 /** Retrieve the short name for this collection which, given this current file is in <col_name>/etc/collect.cfg, is the name of this file's parent file's parent.
330 * @return the short name of this collection as a String
331 */
332 public String getShortName() {
333 if (short_name.equals("")) {
334 String collectDir = Gatherer.getCollectDirectoryPath();
335 String currentCollDir = file.getParentFile().getParentFile().getAbsolutePath();
336 if (currentCollDir.startsWith(collectDir)) {
337 short_name = currentCollDir.substring(collectDir.length());
338 short_name = short_name.replace('\\', '/');
339 } else {
340 short_name = file.getParentFile().getParentFile().getName();
341 }
342 }
343 return short_name;
344
345 }
346
347 /** set the site for this coll */
348 public void setSite(String site) {
349 this.site = site;
350 }
351
352
353 /** Display the title for this collection. */
354 public String toString() {
355 if (this.site == null) {
356 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.LBRACKET_CHARACTER + getShortName() + StaticStrings.RBRACKET_CHARACTER;
357 } else {
358 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.LBRACKET_CHARACTER + getShortName()+", "+this.site + StaticStrings.RBRACKET_CHARACTER;
359 }
360 }
361}
Note: See TracBrowser for help on using the repository browser.