source: gsdl/tags/gsdl-2_70w-distribution/gli/src/org/greenstone/gatherer/collection/ScriptOptions.java@ 14121

Last change on this file since 14121 was 10726, checked in by mdewsnip, 19 years ago

(MAJOR CHANGE) This is the remote Greenstone building functionality implemented for the West Yorkshire Fire and Rescue Service. It allows collections to be built without having a local version of Greenstone, using either a stand-alone version of the GLI or the applet.

The collections are stored on the server (allowing people to collaborate on collections -- but not at the same time), and only what is necessary to let the user edit the collection is downloaded. Any changes to the collection are uploaded to the server.

An access restriction mechanism is implemented which uses the standard Greenstone user database to control who has access to collections.

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