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