source: trunk/gli/src/org/greenstone/gatherer/collection/CollectionConfiguration.java@ 5912

Last change on this file since 5912 was 5910, checked in by mdewsnip, 21 years ago

Removed quotes around collection meta items.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1package org.greenstone.gatherer.collection;
2
3import java.io.*;
4import org.greenstone.gatherer.Gatherer;
5import org.greenstone.gatherer.cdm.CommandTokenizer;
6import org.greenstone.gatherer.msm.MSMUtils;
7import org.greenstone.gatherer.util.Codec;
8import org.greenstone.gatherer.util.StaticStrings;
9import org.greenstone.gatherer.util.Utility;
10import org.w3c.dom.*;
11
12/** This class provides access to a collection configuration file. This version accepts either a valid xml document or a historical collect.cfg file. */
13public class CollectionConfiguration
14 implements Comparable {
15
16 private Element creator_element;
17 private Element description_element;
18 private Element maintainer_element;
19 private Element name_element;
20 private File file;
21 private String build_type;
22 private String creator;
23 private String description;
24 private String maintainer;
25 private String name;
26 private String search_types;
27
28 public CollectionConfiguration(File file) {
29 this.file = file;
30 try {
31 String filename = file.getName().toLowerCase();
32 if(filename.endsWith(".xml")) {
33
34 }
35 else if(filename.endsWith(".cfg")) {
36 FileReader fr = new FileReader(file);
37 BufferedReader br = new BufferedReader(fr);
38 String command = null;
39 while((command = br.readLine()) != null) {
40 if(command.length() > 0) {
41 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
42 while(command.trim().endsWith("\\")) {
43 command = command.substring(0, command.lastIndexOf("\\"));
44 String next_line = br.readLine();
45 if(next_line != null) {
46 command = command + next_line;
47 }
48 next_line = null;
49 }
50 CommandTokenizer tokenizer = new CommandTokenizer(command);
51 if(tokenizer.countTokens() >= 2) {
52 String temp = tokenizer.nextToken();
53 if(temp.equalsIgnoreCase("creator")) {
54 creator = tokenizer.nextToken();
55 }
56 else if(temp.equalsIgnoreCase("maintainer")) {
57 maintainer = tokenizer.nextToken();
58 }
59 else if (temp.equalsIgnoreCase("buildtype")) {
60 build_type = tokenizer.nextToken();
61 }
62 else if (temp.equalsIgnoreCase("searchtype")) {
63 search_types = tokenizer.nextToken();
64 temp = tokenizer.nextToken();
65 if (temp!=null && !temp.equals("")) {
66 search_types += ","+temp;
67 }
68 }
69 else if(temp.equalsIgnoreCase("collectionmeta")) {
70 String meta_type = tokenizer.nextToken();
71 temp = tokenizer.nextToken();
72 // check for language
73 String language = "en"; // assume the no-lang ones are english, but we shouldn't really do this.
74 if(temp.startsWith("[") && temp.endsWith("]")) {
75 language = temp.substring(temp.indexOf("=") + 1, temp.length() - 1);
76 temp = tokenizer.nextToken();
77 }
78
79 // now we need to read in the whole of the entry - may span multiple lines, may be surrounded by single or double quotes
80 String start_string = temp.substring(0,1);
81 if (start_string.equals("\"") || start_string.equals("\'")) {
82 if (temp.endsWith(start_string) && temp.length() != 1) {
83 temp = temp.substring(1, temp.length() - 1);
84 }
85 else {
86 StringBuffer value_raw = new StringBuffer(temp.substring(1));
87 // add the new line back in
88 value_raw.append(StaticStrings.NEW_LINE_CHAR);
89 int pos = value_raw.indexOf(start_string);
90 int old_pos = 0;
91 while (pos != -1 && value_raw.charAt(pos-1)=='\\') {
92 old_pos = pos+1;
93 pos = value_raw.indexOf(start_string, old_pos);
94 }
95 while(pos == -1) {
96 String next_line = br.readLine();
97 if(next_line != null) {
98 value_raw.append(next_line);
99 value_raw.append(StaticStrings.NEW_LINE_CHAR);
100 }
101 next_line = null;
102 pos = value_raw.indexOf(start_string, old_pos);
103 while (pos != -1 && value_raw.charAt(pos-1)=='\\') {
104 old_pos = pos+1;
105 pos = value_raw.indexOf(start_string, old_pos);
106 }
107 }
108
109 temp = value_raw.substring(0, value_raw.lastIndexOf(start_string));
110 value_raw = null;
111 }
112 }
113
114 // now we can work out which coll meta we are dealing with
115 if (meta_type.equalsIgnoreCase("collectionname")) {
116 if (name==null || language.equalsIgnoreCase(Gatherer.dictionary.getLanguage())) {
117 name= Utility.decodeGreenstone(temp);
118 // should we use Codec here too?
119
120 }
121
122 } else if (meta_type.equalsIgnoreCase("collectionextra")) {
123 if (description == null|| language.equalsIgnoreCase(Gatherer.dictionary.getLanguage())) {
124 //description = Utility.decodeGreenstone(temp);
125 // or should we use codec??
126 description = Codec.transform(temp, Codec.GREENSTONE_TO_TEXT);
127 }
128 }
129 language = null;
130 meta_type = null;
131 } // end of coll meta bit
132 temp = null;
133 } // if num tokens >= 2
134 tokenizer = null;
135 } // if command.length > 0
136 } // while
137 // just check a couple of things
138 if (search_types != null && build_type==null) {
139 build_type = "mgpp";
140 }
141 command = null;
142 br.close();
143 br = null;
144 fr.close();
145 fr = null;
146 } // cfg file
147 ///ystem.err.println("Parsed collect.cfg");
148 ///ystem.err.println("name = " + name);
149 ///ystem.err.println("creator = " + creator);
150 ///ystem.err.println("maintainer = " + maintainer);
151 ///ystem.err.println("description = " + description);
152 }
153 catch(Exception error) {
154 Gatherer.println("Error in CollectionConfiguration.<init>(): " + error);
155 Gatherer.printStackTrace(error);
156 }
157 }
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 public String getBuildType() {
171 String result = "";
172
173 if (build_type != null) {
174 result = build_type;
175 }
176 return result;
177 }
178
179 public String getCreator() {
180 String result = "";
181 if(creator_element != null) {
182 result = MSMUtils.getValue(creator_element);
183 }
184 else if(creator != null) {
185 result = creator;
186 }
187 return result;
188 }
189
190 public String getDescription() {
191 String result = "";
192 if(description_element != null) {
193 result = MSMUtils.getValue(description_element);
194 }
195 else if(description != null) {
196 result = description;
197 }
198 return result;
199 }
200
201 public File getFile() {
202 return file;
203 }
204
205 public String getMaintainer() {
206 String result = "";
207 if(maintainer_element != null) {
208 result = MSMUtils.getValue(maintainer_element);
209 }
210 else if(maintainer != null) {
211 result = maintainer;
212 }
213 return result;
214 }
215
216 public String getName() {
217 String result = StaticStrings.ERROR_STR;
218 if(name_element != null) {
219 result = MSMUtils.getValue(name_element);
220 }
221 else if(name != null) {
222 result = name;
223 }
224 return result;
225 }
226
227 public String getSearchTypes() {
228 String result = "";
229
230 if (search_types != null) {
231 result = search_types;
232 }
233 return result;
234 }
235
236 /** 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.
237 * @return the short name of this collection as a String
238 */
239 public String getShortName() {
240 return file.getParentFile().getParentFile().getName();
241 }
242
243 public void setName(String name) {
244 /** @todo */
245 }
246
247 /** Display the title for this collection. */
248 public String toString() {
249 return getShortName() + " - \"" + getName() + "\"";
250 }
251}
Note: See TracBrowser for help on using the repository browser.