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

Last change on this file since 4293 was 4293, checked in by jmt12, 21 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 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.Utility;
8import org.w3c.dom.*;
9/** This class provides access to a collection configuration file. This version accepts either a valid xml document or a historical collect.cfg file. */
10public class CollectionConfiguration {
11
12 private Element creator_element;
13 private Element description_element;
14 private Element maintainer_element;
15 private Element name_element;
16 private String creator;
17 private String description;
18 private String maintainer;
19 private String name;
20
21 public CollectionConfiguration(File file) {
22 try {
23 String filename = file.getName().toLowerCase();
24 if(filename.endsWith(".xml")) {
25
26 }
27 else if(filename.endsWith(".cfg")) {
28 FileReader fr = new FileReader(file);
29 BufferedReader br = new BufferedReader(fr);
30 String command = null;
31 while((command = br.readLine()) != null) {
32 if(command.length() > 0) {
33 // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
34 while(command.trim().endsWith("\\")) {
35 command = command.substring(0, command.lastIndexOf("\\"));
36 String next_line = br.readLine();
37 if(next_line != null) {
38 command = command + next_line;
39 }
40 next_line = null;
41 }
42 CommandTokenizer tokenizer = new CommandTokenizer(command);
43 if(tokenizer.countTokens() >= 2) {
44 String temp = tokenizer.nextToken();
45 if(temp.equalsIgnoreCase("creator")) {
46 creator = tokenizer.nextToken();
47 }
48 else if(temp.equalsIgnoreCase("maintainer")) {
49 maintainer = tokenizer.nextToken();
50 }
51 else if(temp.equalsIgnoreCase("collectionmeta")) {
52 temp = tokenizer.nextToken();
53 if(temp.equalsIgnoreCase("collectionname")) {
54 temp = tokenizer.nextToken();
55 if(temp.startsWith("[") && temp.endsWith("]")) {
56 String language = temp.substring(temp.indexOf("=") + 1, temp.length() - 1);
57 if(name == null || language.equalsIgnoreCase(Gatherer.dictionary.getLanguage())) {
58 name = Utility.decodeGreenstone(tokenizer.nextToken());
59 }
60 }
61 else if(name == null) {
62 name = Utility.decodeGreenstone(temp);
63 }
64 if(name != null && name.startsWith("\"") && name.endsWith("\"")) {
65 name = name.substring(1, name.length() - 1);
66 }
67 }
68 else if(temp.equalsIgnoreCase("collectionextra")) {
69 temp = tokenizer.nextToken();
70 ///ystem.err.println("Read: " + temp);
71 if(temp.startsWith("[") && temp.endsWith("]")) {
72 String language = temp.substring(temp.indexOf("=") + 1, temp.length() - 1);
73 ///ystem.err.println("Try to match " + language + " to " + Gatherer.dictionary.getLanguage());
74 if(description == null || language.equalsIgnoreCase(Gatherer.dictionary.getLanguage())) {
75 description = Utility.decodeGreenstone(tokenizer.nextToken());
76 ///ystem.err.println("Found language match, or first match: " + description);
77 }
78 }
79 else if(description == null) {
80 description = Utility.decodeGreenstone(temp);
81 ///ystem.err.println("Found first match: " + description);
82 }
83 if(description != null && description.startsWith("\"") && description.endsWith("\"")) {
84 description = description.substring(1, description.length() - 1);
85 }
86 }
87 }
88 temp = null;
89 }
90 tokenizer = null;
91 }
92 }
93 command = null;
94 br.close();
95 br = null;
96 fr.close();
97 fr = null;
98 }
99 ///ystem.err.println("Parsed collect.cfg");
100 ///ystem.err.println("name = " + name);
101 ///ystem.err.println("creator = " + creator);
102 ///ystem.err.println("maintainer = " + maintainer);
103 ///ystem.err.println("description = " + description);
104 }
105 catch(Exception error) {
106 Gatherer.println("Error in CollectionConfiguration.<init>(): " + error);
107 Gatherer.printStackTrace(error);
108 }
109 }
110
111 public String getCreator() {
112 String result = "";
113 if(creator_element != null) {
114 result = MSMUtils.getValue(creator_element);
115 }
116 else if(creator != null) {
117 result = creator;
118 }
119 return result;
120 }
121
122 public String getDescription() {
123 String result = "";
124 if(description_element != null) {
125 result = MSMUtils.getValue(description_element);
126 }
127 else if(description != null) {
128 result = description;
129 }
130 return result;
131 }
132
133 public String getMaintainer() {
134 String result = "";
135 if(maintainer_element != null) {
136 result = MSMUtils.getValue(maintainer_element);
137 }
138 else if(maintainer != null) {
139 result = maintainer;
140 }
141 return result;
142 }
143
144 public String getName() {
145 String result = "Error";
146 if(name_element != null) {
147 result = MSMUtils.getValue(name_element);
148 }
149 else if(name != null) {
150 result = name;
151 }
152 return result;
153 }
154
155 public void setName(String name) {
156 /** @todo */
157 }
158}
Note: See TracBrowser for help on using the repository browser.