source: trunk/gli/src/org/greenstone/gatherer/collection/ScriptOptions.java@ 9897

Last change on this file since 9897 was 9135, checked in by kjdon, 19 years ago

BuildOptions renamed to ScriptOptions. now theres no build/import methods, you only have one kind of argument per option class. so instead of one BuildOptions class, you have two ScriptOptions class, one for import, one for build

  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1package org.greenstone.gatherer.collection;
2
3import java.io.*;
4import java.lang.ref.*;
5import java.net.*;
6import java.util.*;
7import org.apache.xerces.parsers.DOMParser;
8import org.greenstone.gatherer.Configuration;
9import org.greenstone.gatherer.DebugStream;
10import org.greenstone.gatherer.Gatherer;
11import org.greenstone.gatherer.cdm.Argument;
12import org.greenstone.gatherer.util.ArrayTools;
13import org.greenstone.gatherer.util.Codec;
14import org.greenstone.gatherer.util.StaticStrings;
15import org.greenstone.gatherer.util.Utility;
16import org.greenstone.gatherer.util.XMLTools;
17import org.w3c.dom.*;
18import org.xml.sax.InputSource;
19
20/** 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. */
21public class ScriptOptions {
22 /** The root element of the argument tree. */
23 private Element arguments_element;
24 /** The root element of the values tree. */
25 private Element values_element;
26 /** A cache of previously created arguments. */
27 private Hashtable arguments_cache = new Hashtable();
28 /** whether the values should be saved or not */
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 ScriptOptions object we first try to retrieve the valid arguments from config.xml - if saved is true. If that fails we then try to parse the arguments noticed when calling script_name -xml. If that also fails, we try to load the default arguments from the xml template library. */
42 public ScriptOptions(Element values_element, String script_name, boolean saved) {
43 // first check the saved arguments language
44 String interface_lang = Configuration.getLanguage();
45 String args_lang = Configuration.getArgumentsLanguage();
46 if (saved && interface_lang.equals(args_lang)) {
47 // Try to retrieve the arguments for import and build.
48 arguments_element = Configuration.getArguments(script_name);
49 }
50 // If that fails try to reconstruct the arguments
51 if(arguments_element == null) {
52 boolean used_defaults = false;
53 arguments_element = loadArguments(script_name, interface_lang);
54
55 // And if that too fails, load the default argument templates
56 if(arguments_element == null) {
57 // replace .pl with .xml to get the template name
58 String template_name = script_name.replaceAll("\\.pl$", "\\.xml");
59 Document document = Utility.parse("xml/"+template_name, true);
60 if (document != null) {
61 arguments_element = document.getDocumentElement();
62 document = null;
63 used_defaults = true;
64
65 } else {
66 return;
67 }
68 ///atherer.println("Loaded default BO arguments from templates.");
69 }
70 else {
71 ///atherer.println("Loaded BO arguments from scripts.");
72 }
73 // 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.
74 if (saved) {
75 if (used_defaults) {
76 Configuration.setArgumentsLanguage("en");
77 } else {
78 Configuration.setArgumentsLanguage(interface_lang);
79 }
80 Configuration.setArguments(arguments_element);
81 }
82 }
83 else {
84 DebugStream.println("Loaded BO arguments from config.xml");
85 }
86 // Now take a note of the values too.
87 this.values_element = values_element;
88 }
89
90 /** Retrieve the indexth argument */
91 public Argument getArgument(int index) {
92 Argument argument = null;
93 // Try to find the argument in the cache.
94 SoftReference reference = (SoftReference) arguments_cache.get(new Integer(index));
95 if(reference != null) {
96 argument = (Argument) reference.get();
97 }
98 // Otherwise generate a new argument.
99 if(argument == null) {
100 NodeList option_list = arguments_element.getElementsByTagName(OPTION);
101 if(0 <= index && index < option_list.getLength()) {
102 argument = new Argument();
103 argument.parseXML((Element) option_list.item(index));
104 }
105 }
106 return argument;
107 }
108
109
110 /** Retrieve the number of arguments.
111 */
112 public int getArgumentCount() {
113 // Determining the total count is easy.
114 NodeList argument_elements = arguments_element.getElementsByTagName(OPTION);
115 return argument_elements.getLength();
116 }
117
118 /** Retrieve all the argument names and values */
119 public String[] getArguments() {
120 String arguments[] = null;
121 NodeList argument_list = null;
122 argument_list = values_element.getElementsByTagName(ARGUMENT);
123 for(int i = 0; i < argument_list.getLength(); i++) {
124 String[] temp = new String[2];
125 Element argument = (Element) argument_list.item(i);
126 ArrayTools.add(arguments, argument.getAttribute(NAME));
127 String value = XMLTools.getValue(argument);
128 if(value != null && value.length() > 0) {
129 ArrayTools.add(arguments, value);
130 }
131 }
132 return arguments;
133 }
134
135 /** Retrieve the value of a certain argument. */
136 public String getValue(String name) {
137 return getValue(name, false);
138 }
139
140 /** Determine if the named argument value is enabled or disabled. */
141 public boolean getValueEnabled(String name) {
142 boolean result = false;
143 String value = getValue(name, true);
144 if(value != null && value.length() > 0) {
145 result = (value.equalsIgnoreCase(TRUE));
146 }
147 return result;
148 }
149
150 /** Retrieve all of the values as a String array ready to added to the script call. */
151 public String[] getValues() {
152 ArrayList values = new ArrayList();
153 try {
154 NodeList arguments = values_element.getElementsByTagName(ARGUMENT);
155 for(int i = 0; i < arguments.getLength(); i++) {
156 Element argument_element = (Element) arguments.item(i);
157 // Determine if this argument is enabled.
158 if(argument_element.getAttribute(ENABLED).equalsIgnoreCase(TRUE)) {
159 // First store the name of the argument prefixed with a '-'
160 values.add("-" + argument_element.getAttribute(NAME));
161 // Now retrieve the value.
162 String argument_value = Codec.transform(XMLTools.getValue(argument_element), Codec.DOM_TO_TEXT);
163 // If there is a value, tokenize it by commas only.
164 if(argument_value != null && argument_value.length() > 0) {
165 values.add(argument_value);
166 }
167 argument_value = null;
168 }
169 argument_element = null;
170 }
171 arguments = null;
172 }
173 catch (Exception error) {
174 DebugStream.printStackTrace(error);
175 }
176 return ArrayTools.arrayListToStringArray(values);
177 }
178
179 /** Remove the named value from the arguments */
180 public void removeValue(String name) {
181 try {
182 NodeList arguments = values_element.getElementsByTagName(ARGUMENT);
183 boolean found = false;
184 for(int i = 0; !found && i < arguments.getLength(); i++) {
185 Element argument_element = (Element) arguments.item(i);
186 // Is this the argument we want.
187 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
188 values_element.removeChild(argument_element);
189 found = true;
190 }
191 argument_element = null;
192 }
193 arguments = null;
194 }
195 catch (Exception error) {
196 DebugStream.printStackTrace(error);
197 }
198 }
199
200 /** Set the state of some argument. Note that value may be either a single String, an ArrayList of Strings or null. If enable is false then any existing argument for the named argument is disabled. */
201 public void setValue(String name, boolean enable, String value) {
202 ///ystem.err.println("Set value: " + (arguments_element == build_values_element ? "Build" : "Import") + ", " + name + ", " + enable + ", " + value);
203 try {
204 Document document = values_element.getOwnerDocument();
205 NodeList arguments = values_element.getElementsByTagName(ARGUMENT);
206 boolean found = false;
207 for(int i = 0; i < arguments.getLength(); i++) {
208 Element argument_element = (Element) arguments.item(i);
209 // If this the argument named.
210 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
211 found = true;
212 // Set whether this argument is enabled
213 argument_element.setAttribute(ENABLED, (enable ? TRUE : FALSE));
214 // Now we set the value, depending or what it is.
215 if(value == null) {
216 // Nothing to do.
217 }
218 else {
219 // Remove existing text nodes.
220 while(argument_element.hasChildNodes()) {
221 argument_element.removeChild(argument_element.getFirstChild());
222 }
223 argument_element.appendChild(document.createTextNode((String)value));
224 }
225 }
226 argument_element = null;
227 }
228 // If we haven't found an instance of this argument, but should have, then add it.
229 if(!found && (enable || value != null)) {
230 Element argument_element = document.createElement(ARGUMENT);
231 argument_element.setAttribute(NAME, name);
232 argument_element.setAttribute(ENABLED, (enable ? TRUE : FALSE));
233 // Now we set the value, depending or what it is.
234 if(value == null) {
235 // Nothing to do.
236 }
237 else {
238 argument_element.appendChild(document.createTextNode((String)value));
239 }
240 values_element.appendChild(argument_element);
241 }
242 arguments = null;
243 document = null;
244 // Make sure the collection knows to save.
245 Gatherer.c_man.getCollection().setSaved(false);
246 }
247 catch (Exception error) {
248 DebugStream.printStackTrace(error);
249 }
250 }
251
252
253 private String getValue(String name, boolean is_enabled) {
254 String result = null;
255 try {
256 NodeList arguments = values_element.getElementsByTagName(ARGUMENT);
257 for(int i = 0; result == null && i < arguments.getLength(); i++) {
258 Element argument_element = (Element) arguments.item(i);
259 // Is this the argument we want.
260 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
261 // Are we simply determining if this argument is enabled
262 if(is_enabled) {
263 result = argument_element.getAttribute(ENABLED);
264 }
265 else {
266 String argument_value = XMLTools.getValue(argument_element);
267 if(argument_value != null) {
268 result = argument_value;
269 }
270 argument_value = null;
271 }
272 }
273 argument_element = null;
274 }
275 arguments = null;
276 }
277 catch (Exception error) {
278 DebugStream.printStackTrace(error);
279 }
280 return result;
281 }
282
283
284 private Element loadArguments(String filename, String lang) {
285 Element arguments_element = null;
286 InputStream input_stream = null;
287
288 // Run the required program.
289 try {
290 String args[];
291 if (Gatherer.isGsdlRemote) {
292 String launch = Gatherer.cgiBase + "launch";
293 launch += "?cmd=" + filename;
294 launch += "&xml=&language="+lang;
295
296 System.err.println("*** launch = " + launch);
297
298 URL launch_url = new URL(launch);
299 URLConnection launch_connection = launch_url.openConnection();
300 input_stream = launch_connection.getInputStream();
301 }
302 else {
303 if(Utility.isWindows()) {
304 args = new String[6];
305 args[0] = Configuration.perl_path;
306 args[1] = "-S";
307 args[2] = Configuration.getScriptPath() + filename;
308 args[3] = "-xml";
309 args[4] = "-language";
310 args[5] = lang;
311 }
312 else {
313 args = new String[4];
314 args[0] = Configuration.getScriptPath() + filename;
315 args[1] = "-xml";
316 args[2] = "-language";
317 args[3] = lang;
318 }
319
320 // Create the process.
321 Runtime runtime = Runtime.getRuntime();
322 Process process = runtime.exec(args);
323
324 input_stream = process.getErrorStream();
325 }
326
327 Document document = XMLTools.parseXML(input_stream);
328 arguments_element = document.getDocumentElement();
329 }
330 catch (Exception error) {
331 DebugStream.println("Error in ScriptOptions.loadArguments(): " + error);
332 DebugStream.printStackTrace(error);
333 }
334 return arguments_element;
335 }
336
337}
Note: See TracBrowser for help on using the repository browser.