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

Last change on this file since 6198 was 6198, checked in by jmt12, 20 years ago

Squelched an exception where a BasicCollectionConfiguration was abortively built around a CVS folder. It wasn't fatal - and I don't want to prevent anyone actually building a collection called cvs (case insensitive under windows)

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