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

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

changed how it was reading in gs3 config xml - the values weren't getting assigned to creator, name, desription etc variables

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