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

Last change on this file since 7697 was 7603, checked in by kjdon, 20 years ago

removed the xml stuff - we don't use it at present and it is confusing. also while reading in the collect.cfg file, if we find a command we are not looking for, we read in all teh tokens for that command (using CommandTokeniser) so that multi line tokens are eaten up

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