source: gli/branches/rtl-gli/src/org/greenstone/gatherer/collection/BasicCollectionConfiguration.java@ 18358

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

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318

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