1 | package org.greenstone.gatherer.collection;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import org.greenstone.gatherer.Gatherer;
|
---|
5 | import org.greenstone.gatherer.cdm.CommandTokenizer;
|
---|
6 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
7 | import org.greenstone.gatherer.util.Codec;
|
---|
8 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
9 | import org.greenstone.gatherer.util.Utility;
|
---|
10 | import 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. */
|
---|
13 | public 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("\'")) && (!temp.endsWith(start_string) || temp.length()==1)) {
|
---|
82 |
|
---|
83 | StringBuffer value_raw = new StringBuffer(temp.substring(1));
|
---|
84 | // add the new line back in
|
---|
85 | value_raw.append(StaticStrings.NEW_LINE_CHAR);
|
---|
86 | int pos = value_raw.indexOf(start_string);
|
---|
87 | int old_pos = 0;
|
---|
88 | while (pos != -1 && value_raw.charAt(pos-1)=='\\') {
|
---|
89 | old_pos = pos+1;
|
---|
90 | pos = value_raw.indexOf(start_string, old_pos);
|
---|
91 | }
|
---|
92 | while(pos == -1) {
|
---|
93 | String next_line = br.readLine();
|
---|
94 | if(next_line != null) {
|
---|
95 | value_raw.append(next_line);
|
---|
96 | value_raw.append(StaticStrings.NEW_LINE_CHAR);
|
---|
97 | }
|
---|
98 | next_line = null;
|
---|
99 | pos = value_raw.indexOf(start_string, old_pos);
|
---|
100 | while (pos != -1 && value_raw.charAt(pos-1)=='\\') {
|
---|
101 | old_pos = pos+1;
|
---|
102 | pos = value_raw.indexOf(start_string, old_pos);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | temp = value_raw.substring(0, value_raw.lastIndexOf(start_string));
|
---|
107 | value_raw = null;
|
---|
108 |
|
---|
109 | }
|
---|
110 |
|
---|
111 | // now we can work out which coll meta we are dealing with
|
---|
112 | if (meta_type.equalsIgnoreCase("collectionname")) {
|
---|
113 | if (name==null || language.equalsIgnoreCase(Gatherer.dictionary.getLanguage())) {
|
---|
114 | name= Utility.decodeGreenstone(temp);
|
---|
115 | // should we use Codec here too?
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | } else if (meta_type.equalsIgnoreCase("collectionextra")) {
|
---|
120 | if (description == null|| language.equalsIgnoreCase(Gatherer.dictionary.getLanguage())) {
|
---|
121 | //description = Utility.decodeGreenstone(temp);
|
---|
122 | // or should we use codec??
|
---|
123 | description = Codec.transform(temp, Codec.GREENSTONE_TO_TEXT);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | language = null;
|
---|
127 | meta_type = null;
|
---|
128 | } // end of coll meta bit
|
---|
129 | temp = null;
|
---|
130 | } // if num tokens >= 2
|
---|
131 | tokenizer = null;
|
---|
132 | } // if command.length > 0
|
---|
133 | } // while
|
---|
134 | // just check a couple of things
|
---|
135 | if (search_types != null && build_type==null) {
|
---|
136 | build_type = "mgpp";
|
---|
137 | }
|
---|
138 | command = null;
|
---|
139 | br.close();
|
---|
140 | br = null;
|
---|
141 | fr.close();
|
---|
142 | fr = null;
|
---|
143 | } // cfg file
|
---|
144 | ///ystem.err.println("Parsed collect.cfg");
|
---|
145 | ///ystem.err.println("name = " + name);
|
---|
146 | ///ystem.err.println("creator = " + creator);
|
---|
147 | ///ystem.err.println("maintainer = " + maintainer);
|
---|
148 | ///ystem.err.println("description = " + description);
|
---|
149 | }
|
---|
150 | catch(Exception error) {
|
---|
151 | Gatherer.println("Error in CollectionConfiguration.<init>(): " + error);
|
---|
152 | Gatherer.printStackTrace(error);
|
---|
153 | }
|
---|
154 | }
|
---|
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 | public String getBuildType() {
|
---|
168 | String result = "";
|
---|
169 |
|
---|
170 | if (build_type != null) {
|
---|
171 | result = build_type;
|
---|
172 | }
|
---|
173 | return result;
|
---|
174 | }
|
---|
175 |
|
---|
176 | public String getCreator() {
|
---|
177 | String result = "";
|
---|
178 | if(creator_element != null) {
|
---|
179 | result = MSMUtils.getValue(creator_element);
|
---|
180 | }
|
---|
181 | else if(creator != null) {
|
---|
182 | result = creator;
|
---|
183 | }
|
---|
184 | return result;
|
---|
185 | }
|
---|
186 |
|
---|
187 | public String getDescription() {
|
---|
188 | String result = "";
|
---|
189 | if(description_element != null) {
|
---|
190 | result = MSMUtils.getValue(description_element);
|
---|
191 | }
|
---|
192 | else if(description != null) {
|
---|
193 | result = description;
|
---|
194 | }
|
---|
195 | return result;
|
---|
196 | }
|
---|
197 |
|
---|
198 | public File getFile() {
|
---|
199 | return file;
|
---|
200 | }
|
---|
201 |
|
---|
202 | public String getMaintainer() {
|
---|
203 | String result = "";
|
---|
204 | if(maintainer_element != null) {
|
---|
205 | result = MSMUtils.getValue(maintainer_element);
|
---|
206 | }
|
---|
207 | else if(maintainer != null) {
|
---|
208 | result = maintainer;
|
---|
209 | }
|
---|
210 | return result;
|
---|
211 | }
|
---|
212 |
|
---|
213 | public String getName() {
|
---|
214 | String result = StaticStrings.ERROR_STR;
|
---|
215 | if(name_element != null) {
|
---|
216 | result = MSMUtils.getValue(name_element);
|
---|
217 | }
|
---|
218 | else if(name != null) {
|
---|
219 | result = name;
|
---|
220 | }
|
---|
221 | return result;
|
---|
222 | }
|
---|
223 |
|
---|
224 | public String getSearchTypes() {
|
---|
225 | String result = "";
|
---|
226 |
|
---|
227 | if (search_types != null) {
|
---|
228 | result = search_types;
|
---|
229 | }
|
---|
230 | return result;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** 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.
|
---|
234 | * @return the short name of this collection as a String
|
---|
235 | */
|
---|
236 | public String getShortName() {
|
---|
237 | return file.getParentFile().getParentFile().getName();
|
---|
238 | }
|
---|
239 |
|
---|
240 | public void setName(String name) {
|
---|
241 | /** @todo */
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** Display the title for this collection. */
|
---|
245 | public String toString() {
|
---|
246 | return getShortName() + " - \"" + getName() + "\"";
|
---|
247 | }
|
---|
248 | }
|
---|