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

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

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