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

Last change on this file since 19796 was 19796, checked in by kjdon, 15 years ago

don't try and parse comments into tokens - quote marks can stuff up the whole config file if there is no space afterwards, eg '. or don't - we work out if our token has finished by whether it ends in the quote mark that we started with

  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 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.DebugStream;
41import org.greenstone.gatherer.Dictionary;
42import org.greenstone.gatherer.Gatherer;
43import org.greenstone.gatherer.cdm.CommandTokenizer;
44import org.greenstone.gatherer.util.Codec;
45import org.greenstone.gatherer.util.StaticStrings;
46import org.greenstone.gatherer.util.XMLTools;
47import org.w3c.dom.*;
48
49/** This class provides access to a collection configuration file. This version accepts either a valid xml document or a historical collect.cfg file. */
50public class BasicCollectionConfiguration
51 implements Comparable {
52
53 private File file;
54 private String creator = "";
55 private String description = "";
56 private String maintainer = "";
57 private String name = "";
58 private String is_public = "";
59 private String collectgroup = "false";
60
61 private String short_name = "";
62
63 private String site = null; // used for gs3 colls
64
65 public BasicCollectionConfiguration(File file) {
66 this.file = file;
67 try {
68 String filename = file.getName().toLowerCase();
69 if(!file.exists()) {
70 return;
71 }
72 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.startsWith("#")) {
79 // a comment
80 continue;
81 }
82 if(command.length() > 0) {
83 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
84 while(command.trim().endsWith("\\")) {
85 command = command.substring(0, command.lastIndexOf("\\"));
86 String next_line = br.readLine();
87 if (next_line != null) {
88 command = command + "\n" + next_line;
89 }
90 next_line = null;
91 }
92 CommandTokenizer tokenizer = new CommandTokenizer(command, br);
93 String command_type_str = tokenizer.nextToken();
94 if (command_type_str == null) {
95 // Bad command. Do nothing
96 continue;
97 }
98
99 command_type_str = command_type_str.toLowerCase();
100 if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
101 creator = tokenizer.nextToken();
102 }
103 else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
104 maintainer = tokenizer.nextToken();
105 }
106 else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTGROUP_STR)) {
107 collectgroup = tokenizer.nextToken();
108 }
109 else if(command_type_str.equalsIgnoreCase(StaticStrings.COLLECTIONMETADATA_STR)) {
110 String meta_type_str = tokenizer.nextToken();
111 String value_str = tokenizer.nextToken();
112 // check for language
113 String language_str = StaticStrings.ENGLISH_LANGUAGE_STR; // assume the no-lang ones are english, but we shouldn't really do this.
114 if(meta_type_str != null && value_str != null) {
115 meta_type_str = meta_type_str.toLowerCase();
116 if(value_str.startsWith(StaticStrings.LBRACKET_CHARACTER) && value_str.endsWith(StaticStrings.RBRACKET_CHARACTER)) {
117 language_str = value_str.substring(value_str.indexOf(StaticStrings.EQUALS_CHARACTER) + 1, value_str.length() - 1);
118 language_str = language_str.toLowerCase();
119 value_str = tokenizer.nextToken();
120 }
121 // now we can work out which coll meta we are dealing with
122 if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR)) {
123 // If this is the first collection title found, then use it, otherwise search for one that more closely matches our choosen interface language
124 if (name == null || language_str.equals(Dictionary.getLanguage())) {
125 name = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
126 }
127 }
128 else if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR)) {
129 // Again we are either looking for the first description, then after that a language specific one
130 if (description == null || language_str.equals(Dictionary.getLanguage())) {
131 description = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
132 }
133 }
134 }
135 language_str = null;
136 value_str = null;
137 meta_type_str = null;
138 } else {
139 // we want to process all the tokens to make sure we get rid of multi line commands before trying to find the next one
140 while (tokenizer.hasMoreTokens()) {
141 tokenizer.nextToken();
142 }
143
144 } // end of coll meta bit
145 command_type_str = null;
146 tokenizer = null;
147 } // if command.length > 0
148 } // while
149 command = null;
150 br.close();
151 isr.close();
152 br = null;
153 isr = null;
154 } // cfg file
155 ///ystem.err.println("Parsed collect.cfg");
156 ///ystem.err.println("name = " + name);
157 ///ystem.err.println("creator = " + creator);
158 ///ystem.err.println("maintainer = " + maintainer);
159 ///ystem.err.println("description = " + description);
160
161 if(filename.endsWith(".xml")) {
162
163 Document xml_file_doc = XMLTools.parseXMLFile(file);
164 Element root = xml_file_doc.getDocumentElement();
165
166 Node metadataListNode = XMLTools.getChildByTagNameIndexed(root, StaticStrings.METADATALIST_STR, 0);
167 Node displayItemListNode = XMLTools.getChildByTagNameIndexed(root, StaticStrings.DISPLAYITEMLIST_STR, 0);
168 String []strings = {creator,
169 maintainer,
170 is_public,
171 name,
172 description};
173 String []att_names = {StaticStrings.COLLECTIONMETADATA_CREATOR_STR,
174 StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR,
175 StaticStrings.COLLECTIONMETADATA_PUBLIC_STR,
176 StaticStrings.NAME_STR,
177 StaticStrings.DESCRIPTION_STR};
178 for (int i=0; i<strings.length; i++) {
179 Element e = XMLTools.getNamedElement((Element)metadataListNode,
180 StaticStrings.METADATA_STR, StaticStrings.NAME_ATTRIBUTE, att_names[i]);
181 if (i >=3) {
182 e = XMLTools.getNamedElement((Element)displayItemListNode,
183 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, att_names[i]);
184 }
185 if (e == null) {
186 continue;
187 }
188 strings[i] = XMLTools.getNodeText(e);
189 }
190
191 }
192 }
193 catch(Exception error) {
194 DebugStream.println("Error in CollectionConfiguration.<init>(): " + error);
195 DebugStream.printStackTrace(error);
196 }
197
198 }
199
200 /** Compare this configuration to another for ordering, which essentially compares two strings for ordering.
201 * @param other the other Object which is presumably another basic collection configuration
202 * @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
203 */
204 public int compareTo(Object other) {
205 if(other == null) {
206 return -1;
207 }
208 return toString().compareTo(other.toString());
209 }
210
211 public boolean equals(Object other) {
212 return (compareTo(other) == 0);
213 }
214
215 /** Retrieve the creators email for this collection.
216 * @return a String
217 */
218 public String getCreator() {
219 return creator;
220 }
221
222 public String getDescription() {
223 return description;
224 }
225
226 public File getFile() {
227 return file;
228 }
229
230 public String getMaintainer() {
231 return maintainer;
232 }
233
234 public String getName() {
235 return name;
236 }
237
238 public String getIsPublic() {
239 return is_public;
240 }
241
242 public String getCollectGroup() {
243 return collectgroup;
244 }
245
246 /** 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.
247 * @return the short name of this collection as a String
248 */
249 public String getShortName() {
250 if (short_name.equals("")) {
251 String collectDir = Gatherer.getCollectDirectoryPath();
252 String currentCollDir = file.getParentFile().getParentFile().getAbsolutePath();
253 if (currentCollDir.startsWith(collectDir)) {
254 short_name = currentCollDir.substring(collectDir.length());
255 short_name.replace('\\', '/');
256 } else {
257 short_name = file.getParentFile().getParentFile().getName();
258 }
259 }
260 return short_name;
261
262 }
263
264 /** set the site for this coll */
265 public void setSite(String site) {
266 this.site = site;
267 }
268
269
270 /** Display the title for this collection. */
271 public String toString() {
272 if (this.site == null) {
273 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName() + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
274 } else {
275 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName()+", "+this.site + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
276 }
277 }
278}
Note: See TracBrowser for help on using the repository browser.