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

Last change on this file since 14049 was 14049, checked in by xiao, 17 years ago

Add implementation of reading collectionConfig.xml for openning a collection when the mode is gs3.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.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.DebugStream;
41import org.greenstone.gatherer.Dictionary;
42import org.greenstone.gatherer.cdm.CommandTokenizer;
43import org.greenstone.gatherer.util.Codec;
44import org.greenstone.gatherer.util.StaticStrings;
45import org.greenstone.gatherer.util.XMLTools;
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 File file;
53 private String creator = "";
54 private String description = "";
55 private String maintainer = "";
56 private String name = "";
57 private String is_public = "";
58
59 private String site = null; // used for gs3 colls
60
61 public BasicCollectionConfiguration(File file) {
62 this.file = file;
63 try {
64 String filename = file.getName().toLowerCase();
65 if(!file.exists()) {
66 return;
67 }
68 if(filename.endsWith(".cfg")) {
69 FileInputStream fis = new FileInputStream(file);
70 InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
71 BufferedReader br = new BufferedReader(isr);
72 String command = null;
73 while((command = br.readLine()) != null) {
74 if(command.length() > 0) {
75 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
76 while(command.trim().endsWith("\\")) {
77 command = command.substring(0, command.lastIndexOf("\\"));
78 String next_line = br.readLine();
79 if (next_line != null) {
80 command = command + "\n" + next_line;
81 }
82 next_line = null;
83 }
84 CommandTokenizer tokenizer = new CommandTokenizer(command, br);
85 String command_type_str = tokenizer.nextToken();
86 if (command_type_str == null) {
87 // Bad command. Do nothing
88 continue;
89 }
90
91 command_type_str = command_type_str.toLowerCase();
92 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(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(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 } else {
128 // we want to process all the tokens to make sure we get rid of multi line commands before trying to find the next one
129 while (tokenizer.hasMoreTokens()) {
130 tokenizer.nextToken();
131 }
132
133 } // end of coll meta bit
134 command_type_str = null;
135 tokenizer = null;
136 } // if command.length > 0
137 } // while
138 command = null;
139 br.close();
140 isr.close();
141 br = null;
142 isr = null;
143 } // cfg file
144 ///ystem.err.println("Parsed collect.cfg");
145 ///ystem.err.println("name = " + name);
146 ///ystem.err.println("creator = " + creator);
147 ///ystem.err.println("maintainer = " + maintainer);
148 ///ystem.err.println("description = " + description);
149
150 if(filename.endsWith(".xml")) {
151
152 Document xml_file_doc = XMLTools.parseXMLFile(file);
153 Element root = xml_file_doc.getDocumentElement();
154
155 Node metadataListNode = XMLTools.getChildByTagNameIndexed(root, StaticStrings.METADATALIST_STR, 0);
156 Node displayItemListNode = XMLTools.getChildByTagNameIndexed(root, StaticStrings.DISPLAYITEMLIST_STR, 0);
157 String []strings = {creator,
158 maintainer,
159 is_public,
160 name,
161 description};
162 String []att_names = {StaticStrings.COLLECTIONMETADATA_CREATOR_STR,
163 StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR,
164 StaticStrings.COLLECTIONMETADATA_PUBLIC_STR,
165 StaticStrings.NAME_STR,
166 StaticStrings.DESCRIPTION_STR};
167 for (int i=0; i<strings.length; i++) {
168 Element e = XMLTools.getNamedElement((Element)metadataListNode,
169 StaticStrings.METADATA_STR, StaticStrings.NAME_ATTRIBUTE, att_names[i]);
170 if (i >=3) {
171 e = XMLTools.getNamedElement((Element)displayItemListNode,
172 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, att_names[i]);
173 }
174 if (e == null) {
175 continue;
176 }
177 strings[i] = XMLTools.getNodeText(e);
178 }
179
180 }
181 }
182 catch(Exception error) {
183 DebugStream.println("Error in CollectionConfiguration.<init>(): " + error);
184 DebugStream.printStackTrace(error);
185 }
186
187 }
188
189 /** Compare this configuration to another for ordering, which essentially compares two strings for ordering.
190 * @param other the other Object which is presumably another basic collection configuration
191 * @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
192 */
193 public int compareTo(Object other) {
194 if(other == null) {
195 return -1;
196 }
197 return toString().compareTo(other.toString());
198 }
199
200 public boolean equals(Object other) {
201 return (compareTo(other) == 0);
202 }
203
204 /** Retrieve the creators email for this collection.
205 * @return a String
206 */
207 public String getCreator() {
208 return creator;
209 }
210
211 public String getDescription() {
212 return description;
213 }
214
215 public File getFile() {
216 return file;
217 }
218
219 public String getMaintainer() {
220 return maintainer;
221 }
222
223 public String getName() {
224 return name;
225 }
226
227 public String getIsPublic() {
228 return is_public;
229 }
230
231 /** 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.
232 * @return the short name of this collection as a String
233 */
234 public String getShortName() {
235 return file.getParentFile().getParentFile().getName();
236 }
237
238 /** set the site for this coll */
239 public void setSite(String site) {
240 this.site = site;
241 }
242
243
244 /** Display the title for this collection. */
245 public String toString() {
246 if (this.site == null) {
247 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName() + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
248 } else {
249 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName()+", "+this.site + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
250 }
251 }
252}
Note: See TracBrowser for help on using the repository browser.