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

Last change on this file since 13599 was 10345, checked in by mdewsnip, 19 years ago

Removed some more crap out of the Utility class.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 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.w3c.dom.*;
46
47/** This class provides access to a collection configuration file. This version accepts either a valid xml document or a historical collect.cfg file. */
48public class BasicCollectionConfiguration
49 implements Comparable {
50
51 private File file;
52 private String creator = "";
53 private String description = "";
54 private String maintainer = "";
55 private String name = "";
56
57 private String site = null; // used for gs3 colls
58
59 public BasicCollectionConfiguration(File file) {
60 this.file = file;
61 try {
62 String filename = file.getName().toLowerCase();
63 if(!file.exists()) {
64 return;
65 }
66 if(filename.endsWith(".cfg")) {
67 FileInputStream fis = new FileInputStream(file);
68 InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
69 BufferedReader br = new BufferedReader(isr);
70 String command = null;
71 while((command = br.readLine()) != null) {
72 if(command.length() > 0) {
73 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
74 while(command.trim().endsWith("\\")) {
75 command = command.substring(0, command.lastIndexOf("\\"));
76 String next_line = br.readLine();
77 if (next_line != null) {
78 command = command + "\n" + next_line;
79 }
80 next_line = null;
81 }
82 CommandTokenizer tokenizer = new CommandTokenizer(command, br);
83 String command_type_str = tokenizer.nextToken();
84 if (command_type_str == null) {
85 // Bad command. Do nothing
86 continue;
87 }
88
89 command_type_str = command_type_str.toLowerCase();
90 if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
91 creator = tokenizer.nextToken();
92 }
93 else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
94 maintainer = tokenizer.nextToken();
95 }
96 else if(command_type_str.equalsIgnoreCase(StaticStrings.COLLECTIONMETADATA_STR)) {
97 String meta_type_str = tokenizer.nextToken();
98 String value_str = tokenizer.nextToken();
99 // check for language
100 String language_str = StaticStrings.ENGLISH_LANGUAGE_STR; // assume the no-lang ones are english, but we shouldn't really do this.
101 if(meta_type_str != null && value_str != null) {
102 meta_type_str = meta_type_str.toLowerCase();
103 if(value_str.startsWith(StaticStrings.LBRACKET_CHARACTER) && value_str.endsWith(StaticStrings.RBRACKET_CHARACTER)) {
104 language_str = value_str.substring(value_str.indexOf(StaticStrings.EQUALS_CHARACTER) + 1, value_str.length() - 1);
105 language_str = language_str.toLowerCase();
106 value_str = tokenizer.nextToken();
107 }
108 // now we can work out which coll meta we are dealing with
109 if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR)) {
110 // If this is the first collection title found, then use it, otherwise search for one that more closely matches our choosen interface language
111 if (name == null || language_str.equals(Dictionary.getLanguage())) {
112 name = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
113 }
114 }
115 else if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR)) {
116 // Again we are either looking for the first description, then after that a language specific one
117 if (description == null || language_str.equals(Dictionary.getLanguage())) {
118 description = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
119 }
120 }
121 }
122 language_str = null;
123 value_str = null;
124 meta_type_str = null;
125 } else {
126 // we want to process all the tokens to make sure we get rid of multi line commands before trying to find the next one
127 while (tokenizer.hasMoreTokens()) {
128 tokenizer.nextToken();
129 }
130
131 } // end of coll meta bit
132 command_type_str = null;
133 tokenizer = null;
134 } // if command.length > 0
135 } // while
136 command = null;
137 br.close();
138 isr.close();
139 br = null;
140 isr = null;
141 } // cfg file
142 ///ystem.err.println("Parsed collect.cfg");
143 ///ystem.err.println("name = " + name);
144 ///ystem.err.println("creator = " + creator);
145 ///ystem.err.println("maintainer = " + maintainer);
146 ///ystem.err.println("description = " + description);
147 }
148 catch(Exception error) {
149 DebugStream.println("Error in CollectionConfiguration.<init>(): " + error);
150 DebugStream.printStackTrace(error);
151 }
152 }
153
154 /** Compare this configuration to another for ordering, which essentially compares two strings for ordering.
155 * @param other the other Object which is presumably another basic collection configuration
156 * @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
157 */
158 public int compareTo(Object other) {
159 if(other == null) {
160 return -1;
161 }
162 return toString().compareTo(other.toString());
163 }
164
165 public boolean equals(Object other) {
166 return (compareTo(other) == 0);
167 }
168
169 /** Retrieve the creators email for this collection.
170 * @return a String
171 */
172 public String getCreator() {
173 return creator;
174 }
175
176 public String getDescription() {
177 return description;
178 }
179
180 public File getFile() {
181 return file;
182 }
183
184 public String getMaintainer() {
185 return maintainer;
186 }
187
188 public String getName() {
189 return name;
190 }
191
192 /** 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.
193 * @return the short name of this collection as a String
194 */
195 public String getShortName() {
196 return file.getParentFile().getParentFile().getName();
197 }
198
199 /** set the site for this coll */
200 public void setSite(String site) {
201 this.site = site;
202 }
203
204
205 /** Display the title for this collection. */
206 public String toString() {
207 if (this.site == null) {
208 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName() + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
209 } else {
210 return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName()+", "+this.site + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
211 }
212 }
213}
Note: See TracBrowser for help on using the repository browser.