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

Last change on this file since 7275 was 6850, checked in by mdewsnip, 20 years ago

Now reads the collection configuration files as UTF-8, as it should.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 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 org.greenstone.gatherer.Gatherer;
41import org.greenstone.gatherer.cdm.CommandTokenizer;
42import org.greenstone.gatherer.msm.MSMUtils;
43import org.greenstone.gatherer.util.Codec;
44import org.greenstone.gatherer.util.StaticStrings;
45import org.greenstone.gatherer.util.Utility;
46import org.w3c.dom.*;
47
48/** This class provides access to a collection configuration file. This version accepts either a valid xml document or a historical collect.cfg file. */
49public class BasicCollectionConfiguration
50 implements Comparable {
51
52 private Element creator_element;
53 private Element description_element;
54 private Element maintainer_element;
55 private Element name_element;
56 private File file;
57 private String creator;
58 private String description;
59 private String maintainer;
60 private String name;
61
62 public BasicCollectionConfiguration(File file) {
63 this.file = file;
64 try {
65 String filename = file.getName().toLowerCase();
66 if(!file.exists()) {
67 return;
68 }
69 if(filename.endsWith(".xml")) {
70 // This is where G3 compliant code will one day live
71 }
72 else if(filename.endsWith(".cfg")) {
73 FileInputStream fis = new FileInputStream(file);
74 InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
75 BufferedReader br = new BufferedReader(isr);
76 String command = null;
77 while((command = br.readLine()) != null) {
78 if(command.length() > 0) {
79 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
80 while(command.trim().endsWith("\\")) {
81 command = command.substring(0, command.lastIndexOf("\\"));
82 String next_line = br.readLine();
83 if(next_line != null) {
84 command = command + next_line;
85 }
86 next_line = null;
87 }
88 CommandTokenizer tokenizer = new CommandTokenizer(command, br);
89 String command_type_str = tokenizer.nextToken().toLowerCase();
90 if(command_type_str == null) {
91 // Bad command. Do nothing
92 }
93 else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
94 creator = tokenizer.nextToken();
95 }
96 else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
97 maintainer = tokenizer.nextToken();
98 }
99 else if(command_type_str.equalsIgnoreCase(StaticStrings.COLLECTIONMETADATA_STR)) {
100 String meta_type_str = tokenizer.nextToken();
101 String value_str = tokenizer.nextToken();
102 // check for language
103 String language_str = StaticStrings.ENGLISH_LANGUAGE_STR; // assume the no-lang ones are english, but we shouldn't really do this.
104 if(meta_type_str != null && value_str != null) {
105 meta_type_str = meta_type_str.toLowerCase();
106 if(value_str.startsWith(StaticStrings.LBRACKET_CHARACTER) && value_str.endsWith(StaticStrings.RBRACKET_CHARACTER)) {
107 language_str = value_str.substring(value_str.indexOf(StaticStrings.EQUALS_CHARACTER) + 1, value_str.length() - 1);
108 language_str = language_str.toLowerCase();
109 value_str = tokenizer.nextToken();
110 }
111 // now we can work out which coll meta we are dealing with
112 if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR)) {
113 // If this is the first collection title found, then use it, otherwise search for one that more closely matches our choosen interface language
114 if (name == null || language_str.equals(Gatherer.dictionary.getLanguage())) {
115 name = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
116 }
117 }
118 else if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR)) {
119 // Again we are either looking for the first description, then after that a language specific one
120 if (description == null || language_str.equals(Gatherer.dictionary.getLanguage())) {
121 description = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
122 }
123 }
124 }
125 language_str = null;
126 value_str = null;
127 meta_type_str = null;
128 } // end of coll meta bit
129 command_type_str = null;
130 tokenizer = null;
131 } // if command.length > 0
132 } // while
133 command = null;
134 br.close();
135 isr.close();
136 br = null;
137 isr = null;
138 } // cfg file
139 ///ystem.err.println("Parsed collect.cfg");
140 ///ystem.err.println("name = " + name);
141 ///ystem.err.println("creator = " + creator);
142 ///ystem.err.println("maintainer = " + maintainer);
143 ///ystem.err.println("description = " + description);
144 }
145 catch(Exception error) {
146 Gatherer.println("Error in CollectionConfiguration.<init>(): " + error);
147 Gatherer.printStackTrace(error);
148 }
149 }
150
151 /** Compare this configuration to another for ordering, which essentially compares two strings for ordering.
152 * @param other the other Object which is presumably another basic collection configuration
153 * @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
154 */
155 public int compareTo(Object other) {
156 if(other == null) {
157 return -1;
158 }
159 return toString().compareTo(other.toString());
160 }
161
162 public boolean equals(Object other) {
163 return (compareTo(other) == 0);
164 }
165
166 /** Retrieve the creators email for this collection.
167 * @return a String
168 */
169 public String getCreator() {
170 String result = StaticStrings.EMPTY_STR;
171 if(creator_element != null) {
172 result = MSMUtils.getValue(creator_element);
173 }
174 else if(creator != null) {
175 result = creator;
176 }
177 return result;
178 }
179
180 public String getDescription() {
181 String result = StaticStrings.EMPTY_STR;
182 if(description_element != null) {
183 result = MSMUtils.getValue(description_element);
184 }
185 else if(description != null) {
186 result = description;
187 }
188 return result;
189 }
190
191 public File getFile() {
192 return file;
193 }
194
195 public String getMaintainer() {
196 String result = StaticStrings.EMPTY_STR;
197 if(maintainer_element != null) {
198 result = MSMUtils.getValue(maintainer_element);
199 }
200 else if(maintainer != null) {
201 result = maintainer;
202 }
203 return result;
204 }
205
206 public String getName() {
207 String result = StaticStrings.EMPTY_STR;
208 if(name_element != null) {
209 result = MSMUtils.getValue(name_element);
210 }
211 else if(name != null) {
212 result = name;
213 }
214 return result;
215 }
216
217 /** 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.
218 * @return the short name of this collection as a String
219 */
220 public String getShortName() {
221 return file.getParentFile().getParentFile().getName();
222 }
223
224 /** Display the title for this collection. */
225 public String toString() {
226 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName() + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
227 }
228}
Note: See TracBrowser for help on using the repository browser.