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

Last change on this file since 6147 was 6147, checked in by jmt12, 20 years ago

Changed the toString so it follows the format: title (filename)

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