source: trunk/gli/src/org/greenstone/gatherer/collection/BuildOptions.java@ 4366

Last change on this file since 4366 was 4366, checked in by kjdon, 21 years ago

re-tabbed the code for java

  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1package org.greenstone.gatherer.collection;
2
3import java.io.*;
4import java.lang.ref.*;
5import java.util.*;
6import org.apache.xerces.parsers.DOMParser;
7import org.greenstone.gatherer.Configuration;
8import org.greenstone.gatherer.Gatherer;
9import org.greenstone.gatherer.cdm.Argument;
10import org.greenstone.gatherer.msm.MSMUtils;
11import org.greenstone.gatherer.util.ArrayTools;
12import org.greenstone.gatherer.util.Utility;
13import org.w3c.dom.*;
14import org.xml.sax.InputSource;
15/** Build options uses the argument list found in config.xml and the current settings from the loaded collection configuration to represent the options the user wants to use during import and build. If there are no arguments stored in config.xml, or if the user indicates the argument are out of date, this class tries to parse new ones. */
16public class BuildOptions {
17 /** The root element of the build argument tree. */
18 private Element build_arguments_element;
19 /** The root element of the build values tree. */
20 private Element build_values_element;
21 /** The root element of the import argument tree. */
22 private Element import_arguments_element;
23 /** The root element of the import values tree. */
24 private Element import_values_element;
25 /** A cache of previously created build arguments. */
26 private Hashtable build_arguments_cache = new Hashtable();
27 /** A cache of previously created import arguments. */
28 private Hashtable import_arguments_cache = new Hashtable();
29 /** The name of an argument element. */
30 static final private String ARGUMENT = "Argument";
31 /** The name of the enabled attribute. */
32 static final private String ENABLED = "enabled";
33 /** The name of the 'false' value. */
34 static final private String FALSE = "false";
35 /** The name of a name attribute. */
36 static final private String NAME = "name";
37 /** The name of an option element. */
38 static final private String OPTION = "Option";
39 /** The name of the 'true' value. */
40 static final private String TRUE = "true";
41 /** When constructing a BuildOptions object we first try to retrieve the valid arguments from config.xml. If that fails we then try to parse the arguments noticed when calling import.pl -xml and buildcol.pl -xml. If that also fails, we load the default arguments from the xml template library. */
42 public BuildOptions(Element build_values_element, Element import_values_element) {
43 // Try to retrieve the arguments for import and build.
44 build_arguments_element = Gatherer.config.getArguments("buildcol.pl");
45 import_arguments_element = Gatherer.config.getArguments("import.pl");
46 // If that fails try to reconstruct the arguments
47 if(build_arguments_element == null || import_arguments_element == null) {
48 build_arguments_element = loadArguments("buildcol.pl");
49 import_arguments_element = loadArguments("import.pl");
50 // And if that too fails, load the default argument templates
51 if(build_arguments_element == null || import_arguments_element == null) {
52 Document build_document = Utility.parse("xml" + File.separator + "buildcol.xml", true);
53 build_arguments_element = build_document.getDocumentElement();
54 build_document = null;
55 Document import_document = Utility.parse("xml" + File.separator + "import.xml", true);
56 import_arguments_element = import_document.getDocumentElement();
57 import_document = null;
58 ///atherer.println("Loaded default BO arguments from templates.");
59 }
60 else {
61 ///atherer.println("Loaded BO arguments from scripts.");
62 }
63 // By now we should definately have the arguments. However the reason we are here is because they are not stored in the config.xml file, to make sure they are stored now.
64 Gatherer.config.setArguments(build_arguments_element);
65 Gatherer.config.setArguments(import_arguments_element);
66 }
67 else {
68 Gatherer.println("Loaded BO arguments from config.xml");
69 }
70 // Now take a note of the values too.
71 this.build_values_element = build_values_element;
72 this.import_values_element = import_values_element;
73 }
74
75 /** Retrieve the arguments associated with a certain type of action, ie import or build. */
76 public String[] getArguments(boolean build) {
77 String arguments[] = null;
78 NodeList argument_list = null;
79 if(build) {
80 argument_list = build_values_element.getElementsByTagName(ARGUMENT);
81 }
82 else {
83 argument_list = import_values_element.getElementsByTagName(ARGUMENT);
84 }
85 for(int i = 0; i < argument_list.getLength(); i++) {
86 String[] temp = new String[2];
87 Element argument = (Element) argument_list.item(i);
88 ArrayTools.add(arguments, argument.getAttribute(NAME));
89 String value = MSMUtils.getValue(argument);
90 if(value != null && value.length() > 0) {
91 ArrayTools.add(arguments, value);
92 }
93 }
94 return arguments;
95 }
96
97 /** Retrieve the indexth argument belonging to build. */
98 public Argument getBuildArgument(int index) {
99 Argument argument = null;
100 // Try to find the argument in the cache.
101 SoftReference reference = (SoftReference) build_arguments_cache.get(new Integer(index));
102 if(reference != null) {
103 argument = (Argument) reference.get();
104 }
105 // Otherwise generate a new argument.
106 if(argument == null) {
107 argument = getArgument(build_arguments_element, index);
108 }
109 return argument;
110 }
111
112 /** Retrieve the number of arguments involved in building. */
113 public int getBuildArgumentCount() {
114 NodeList build_options = build_arguments_element.getElementsByTagName(OPTION);
115 return build_options.getLength();
116 }
117
118 /** Retrieve the value of a certain build argument. */
119 public String getBuildValue(String name) {
120 return getValue(build_values_element, name, false);
121 }
122
123 /** Determine if the named argument value is enabled or disabled. */
124 public boolean getBuildValueEnabled(String name) {
125 boolean result = false;
126 String value = getValue(build_values_element, name, true);
127 if(value != null && value.length() > 0) {
128 result = (value.equalsIgnoreCase(TRUE));
129 }
130 return result;
131 }
132
133 /** Retrieve all of the build values as a String array ready to added to the buildcol.pl call. */
134 public String[] getBuildValues() {
135 return getValues(build_values_element);
136 }
137
138 /** Retrieve the indexth argument belonging to import. */
139 public Argument getImportArgument(int index) {
140 Argument argument = null;
141 // Try to find the argument in the cache.
142 SoftReference reference = (SoftReference) import_arguments_cache.get(new Integer(index));
143 if(reference != null) {
144 argument = (Argument) reference.get();
145 }
146 // Otherwise generate a new argument.
147 if(argument == null) {
148 argument = getArgument(import_arguments_element, index);
149 }
150 return argument;
151 }
152
153 /** Retrieve the number of arguments involved in importing. */
154 public int getImportArgumentCount() {
155 NodeList import_options = import_arguments_element.getElementsByTagName(OPTION);
156 return import_options.getLength();
157 }
158
159 /** Retrieve the value of a certain build argument. */
160 public String getImportValue(String name) {
161 return getValue(import_values_element, name, false);
162 }
163
164 /** Determine if the named argument value is enabled or disabled. */
165 public boolean getImportValueEnabled(String name) {
166 boolean result = false;
167 String value = getValue(import_values_element, name, true);
168 if(value != null && value.length() > 0) {
169 result = (value.equalsIgnoreCase(TRUE));
170 }
171 return result;
172 }
173
174 /** Retrieve all of the import arguments in a form ready to be sent out to a shell process. */
175 public String[] getImportValues() {
176 return getValues(import_values_element);
177 }
178
179 /** Set the value of a build argument. */
180 public void setBuildValue(String name, boolean enable, String value) {
181 setValue(build_values_element, name, enable, value);
182 }
183
184 /** Set the value of a build argument. */
185 public void setImportValue(String name, boolean enable, String value) {
186 setValue(import_values_element, name, enable, value);
187 }
188
189 /** Remove the given build value. */
190 public void removeBuildValue(String name) {
191 removeValue(build_values_element, name);
192 }
193
194 /** Remove the given import value. */
195 public void removeImportValue(String name) {
196 removeValue(import_values_element, name);
197 }
198
199 /** Retrieve the indexth element from the given set of arguments. */
200 private Argument getArgument(Element arguments_element, int index) {
201 Argument argument = null;
202 try {
203 NodeList option_list = arguments_element.getElementsByTagName(OPTION);
204 if(0 <= index && index < option_list.getLength()) {
205 // Retrieve the appropriate argument
206 Element option = (Element) option_list.item(index);
207 // Iterate through this option elements children, making note of any details we find.
208 argument = new Argument();
209 for(Node node = option.getFirstChild(); node != null; node = node.getNextSibling()) {
210 String node_name = node.getNodeName();
211 if(node_name.equals("Name")) {
212 argument.setName(MSMUtils.getValue(node));
213 }
214 else if(node_name.equals("Desc")) {
215 argument.setDesc(MSMUtils.getValue(node));
216 }
217 else if(node_name.equals("Type")) {
218 argument.setType(MSMUtils.getValue(node));
219 }
220 else if(node_name.equals("Default")) {
221 argument.setDefault(MSMUtils.getValue(node));
222 }
223 else if(node_name.equals("List")) {
224 // Two final loops are required to parse lists.
225 for(Node value = node.getFirstChild(); value != null; value = value.getNextSibling()) {
226 if(value.getNodeName().equals("Value")) {
227 String key = null;
228 String desc = "";
229 for(Node subvalue = value.getFirstChild(); subvalue != null; subvalue = subvalue.getNextSibling()) {
230 node_name = subvalue.getNodeName();
231 if(node_name.equals("Name")) {
232 key = MSMUtils.getValue(subvalue);
233 }
234 else if(node_name.equals("Desc")) {
235 desc = MSMUtils.getValue(subvalue);
236 }
237 }
238 if(key != null) {
239 argument.addOption(key, desc);
240 }
241 }
242 }
243 }
244 else if(node_name.equals("Required")) {
245 String v = MSMUtils.getValue(node);
246 if(v != null && v.equals("yes")) {
247 argument.setRequired(true);
248 }
249 }
250 }
251 }
252 }
253 catch (Exception error) {
254 Gatherer.println("Error in BuildOptions.getArgument(): " + error);
255 Gatherer.printStackTrace(error);
256 }
257 return argument;
258 }
259
260 private String getValue(Element arguments_element, String name, boolean is_enabled) {
261 String result = null;
262 try {
263 NodeList arguments = arguments_element.getElementsByTagName(ARGUMENT);
264 for(int i = 0; result == null && i < arguments.getLength(); i++) {
265 Element argument_element = (Element) arguments.item(i);
266 // Is this the argument we want.
267 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
268 // Are we simply determining if this argument is enabled
269 if(is_enabled) {
270 result = argument_element.getAttribute(ENABLED);
271 }
272 else {
273 String argument_value = MSMUtils.getValue(argument_element);
274 if(argument_value != null) {
275 result = argument_value;
276 }
277 argument_value = null;
278 }
279 }
280 argument_element = null;
281 }
282 arguments = null;
283 }
284 catch (Exception error) {
285 Gatherer.printStackTrace(error);
286 }
287 return result;
288 }
289
290 private String[] getValues(Element arguments_element) {
291 ArrayList values = new ArrayList();
292 try {
293 NodeList arguments = arguments_element.getElementsByTagName(ARGUMENT);
294 for(int i = 0; i < arguments.getLength(); i++) {
295 Element argument_element = (Element) arguments.item(i);
296 // Determine if this argument is enabled.
297 if(argument_element.getAttribute(ENABLED).equalsIgnoreCase(TRUE)) {
298 // First store the name of the argument prefixed with a '-'
299 values.add("-" + argument_element.getAttribute(NAME));
300 // Now retrieve the value.
301 String argument_value = MSMUtils.getValue(argument_element);
302 // If there is a value, tokenize it by commas only.
303 if(argument_value != null && argument_value.length() > 0) {
304 values.add(argument_value);
305 }
306 argument_value = null;
307 }
308 argument_element = null;
309 }
310 arguments = null;
311 }
312 catch (Exception error) {
313 Gatherer.printStackTrace(error);
314 }
315 return ArrayTools.arrayListToStringArray(values);
316 }
317
318 private Element loadArguments(String filename) {
319 Element arguments_element = null;
320 // Run the required program.
321 /** @todo - I never finished implementing this. Have to test that the perl script handles being loaded in this way.
322 try {
323 String args[] = new String[3];
324 args[0] = Gatherer.config.perl_path;
325 args[1] = filename;
326 args[2] = "-xml";
327 // Create the process.
328 Runtime runtime = Runtime.getRuntime();
329 Process process = runtime.exec(args);
330 //InputStream input_stream = process.getErrorStream();
331 InputSource source = new InputSource(new InputStreamReader(process.getErrorStream()));
332 DOMParser parser = new DOMParser();
333 parser.parse(source);
334 Document document = parser.getDocument();
335 arguments_element = document.getDocumentElement();
336 }
337 catch (Exception error) {
338 Gatherer.println("Error in BuildOptions.loadArguments(): " + error);
339 Gatherer.printStackTrace(error);
340 }
341 */
342 return arguments_element;
343 }
344
345 /** Set the state of some build or import argument. Note that value may be either a single String, and ArrayList of Strings or null. If enable is false then any existing argument for the named argument is disabled. */
346 public void setValue(Element arguments_element, String name, boolean enable, String value) {
347 ///ystem.err.println("Set value: " + (arguments_element == build_values_element ? "Build" : "Import") + ", " + name + ", " + enable + ", " + value);
348 try {
349 Document document = arguments_element.getOwnerDocument();
350 NodeList arguments = arguments_element.getElementsByTagName(ARGUMENT);
351 boolean found = false;
352 for(int i = 0; i < arguments.getLength(); i++) {
353 Element argument_element = (Element) arguments.item(i);
354 // If this the argument named.
355 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
356 found = true;
357 // Set whether this argument is enabled
358 argument_element.setAttribute(ENABLED, (enable ? TRUE : FALSE));
359 // Now we set the value, depending or what it is.
360 if(value == null) {
361 // Nothing to do.
362 }
363 else {
364 // Remove existing text nodes.
365 while(argument_element.hasChildNodes()) {
366 argument_element.removeChild(argument_element.getFirstChild());
367 }
368 argument_element.appendChild(document.createTextNode((String)value));
369 }
370 }
371 argument_element = null;
372 }
373 // If we haven't found an instance of this argument, but should have, then add it.
374 if(!found && (enable || value != null)) {
375 Element argument_element = document.createElement(ARGUMENT);
376 argument_element.setAttribute(NAME, name);
377 argument_element.setAttribute(ENABLED, (enable ? TRUE : FALSE));
378 // Now we set the value, depending or what it is.
379 if(value == null) {
380 // Nothing to do.
381 }
382 else {
383 argument_element.appendChild(document.createTextNode((String)value));
384 }
385 arguments_element.appendChild(argument_element);
386 }
387 arguments = null;
388 document = null;
389 // Make sure the collection knows to save.
390 Gatherer.c_man.getCollection().setSaved(false);
391 }
392 catch (Exception error) {
393 Gatherer.printStackTrace(error);
394 }
395 }
396
397 /** Remove the named value from the given arguments element. */
398 private void removeValue(Element arguments_element, String name) {
399 try {
400 NodeList arguments = arguments_element.getElementsByTagName(ARGUMENT);
401 boolean found = false;
402 for(int i = 0; !found && i < arguments.getLength(); i++) {
403 Element argument_element = (Element) arguments.item(i);
404 // Is this the argument we want.
405 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
406 arguments_element.removeChild(argument_element);
407 found = true;
408 }
409 argument_element = null;
410 }
411 arguments = null;
412 }
413 catch (Exception error) {
414 Gatherer.printStackTrace(error);
415 }
416 }
417}
Note: See TracBrowser for help on using the repository browser.