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

Last change on this file since 14974 was 14974, checked in by davidb, 16 years ago

Changes to GLI to support export into Fedora. New utility called flisvn diff gems/MetadataSetManager.java

  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1package org.greenstone.gatherer.collection;
2
3import java.io.*;
4import java.util.*;
5import org.greenstone.gatherer.Configuration;
6import org.greenstone.gatherer.DebugStream;
7import org.greenstone.gatherer.Gatherer;
8import org.greenstone.gatherer.cdm.Argument;
9import org.greenstone.gatherer.greenstone.LocalGreenstone;
10import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
11import org.greenstone.gatherer.util.ArrayTools;
12import org.greenstone.gatherer.util.Codec;
13import org.greenstone.gatherer.util.Utility;
14import org.greenstone.gatherer.util.XMLTools;
15import org.w3c.dom.*;
16
17
18/** This class parses options from Perl scripts: import.pl, buildcol.pl and explode_metadata_database.pl in particular. */
19public class ScriptOptions
20{
21 /** The root element of the argument tree. */
22 private Element arguments_element;
23 /** The root element of the values tree. */
24 private Element values_element;
25 /** The name of an argument element. */
26 static final private String ARGUMENT = "Argument";
27 /** The name of the enabled attribute. */
28 static final private String ENABLED = "enabled";
29 /** The name of the 'false' value. */
30 static final private String FALSE = "false";
31 /** The name of a name attribute. */
32 static final private String NAME = "name";
33 /** The name of an option element. */
34 static final private String OPTION = "Option";
35 /** The name of the 'true' value. */
36 static final private String TRUE = "true";
37
38
39 /** Parse the arguments from running "script_name -xml" */
40 public ScriptOptions(Element values_element, String script_name)
41 {
42 this.values_element = values_element;
43 this.arguments_element = loadArguments(script_name, Configuration.getLanguage());
44 }
45
46
47 /** Retrieve the indexth argument */
48 public Argument getArgument(int index)
49 {
50 Argument argument = null;
51
52 NodeList option_list = arguments_element.getElementsByTagName(OPTION);
53 if (index >= 0 && index < option_list.getLength()) {
54 argument = new Argument();
55 argument.parseXML((Element) option_list.item(index));
56 }
57
58 return argument;
59 }
60
61
62 /** Retrieve the number of arguments.
63 */
64 public int getArgumentCount() {
65 // Determining the total count is easy.
66 NodeList argument_elements = arguments_element.getElementsByTagName(OPTION);
67 return argument_elements.getLength();
68 }
69
70
71 /** Retrieve the value of a certain argument. */
72 public String getValue(String name) {
73 return getValue(name, false);
74 }
75
76 /** Determine if the named argument value is enabled or disabled. */
77 public boolean getValueEnabled(String name) {
78 boolean result = false;
79 String value = getValue(name, true);
80 if(value != null && value.length() > 0) {
81 result = (value.equalsIgnoreCase(TRUE));
82 }
83 return result;
84 }
85
86 /** Retrieve all of the values as a String array ready to added to the script call. */
87 public String[] getValues() {
88 ArrayList values = new ArrayList();
89 try {
90 NodeList arguments = values_element.getElementsByTagName(ARGUMENT);
91 for(int i = 0; i < arguments.getLength(); i++) {
92 Element argument_element = (Element) arguments.item(i);
93 // Determine if this argument is enabled.
94 if(argument_element.getAttribute(ENABLED).equalsIgnoreCase(TRUE)) {
95 // First store the name of the argument prefixed with a '-'
96 values.add("-" + argument_element.getAttribute(NAME));
97 // Now retrieve the value.
98 String argument_value = Codec.transform(XMLTools.getValue(argument_element), Codec.DOM_TO_TEXT);
99 // If there is a value, tokenize it by commas only.
100 if(argument_value != null && argument_value.length() > 0) {
101 values.add(argument_value);
102 }
103 argument_value = null;
104 }
105 argument_element = null;
106 }
107 arguments = null;
108 }
109 catch (Exception error) {
110 DebugStream.printStackTrace(error);
111 }
112 return ArrayTools.arrayListToStringArray(values);
113 }
114
115 /** Remove the named value from the arguments */
116 public void removeValue(String name) {
117 try {
118 NodeList arguments = values_element.getElementsByTagName(ARGUMENT);
119 boolean found = false;
120 for(int i = 0; !found && i < arguments.getLength(); i++) {
121 Element argument_element = (Element) arguments.item(i);
122 // Is this the argument we want.
123 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
124 values_element.removeChild(argument_element);
125 found = true;
126 }
127 argument_element = null;
128 }
129 arguments = null;
130 }
131 catch (Exception error) {
132 DebugStream.printStackTrace(error);
133 }
134 }
135
136 /** 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. */
137 public void setValue(String name, boolean enable, String value) {
138 ///ystem.err.println("Set value: " + (arguments_element == build_values_element ? "Build" : "Import") + ", " + name + ", " + enable + ", " + value);
139 try {
140 Document document = values_element.getOwnerDocument();
141 NodeList arguments = values_element.getElementsByTagName(ARGUMENT);
142 boolean found = false;
143 for(int i = 0; i < arguments.getLength(); i++) {
144 Element argument_element = (Element) arguments.item(i);
145 // If this the argument named.
146 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
147 found = true;
148 // Set whether this argument is enabled
149 argument_element.setAttribute(ENABLED, (enable ? TRUE : FALSE));
150 // Now we set the value, depending or what it is.
151 if(value == null) {
152 // Nothing to do.
153 }
154 else {
155 // Remove existing text nodes.
156 while(argument_element.hasChildNodes()) {
157 argument_element.removeChild(argument_element.getFirstChild());
158 }
159 argument_element.appendChild(document.createTextNode((String)value));
160 }
161 }
162 argument_element = null;
163 }
164 // If we haven't found an instance of this argument, but should have, then add it.
165 if(!found && (enable || value != null)) {
166 Element argument_element = document.createElement(ARGUMENT);
167 argument_element.setAttribute(NAME, name);
168 argument_element.setAttribute(ENABLED, (enable ? TRUE : FALSE));
169 // Now we set the value, depending or what it is.
170 if(value == null) {
171 // Nothing to do.
172 }
173 else {
174 argument_element.appendChild(document.createTextNode((String)value));
175 }
176 values_element.appendChild(argument_element);
177 }
178 arguments = null;
179 document = null;
180 // Make sure the collection knows to save.
181 Gatherer.c_man.getCollection().setSaved(false);
182 }
183 catch (Exception error) {
184 DebugStream.printStackTrace(error);
185 }
186 }
187
188
189 private String getValue(String name, boolean is_enabled) {
190 String result = null;
191 try {
192 NodeList arguments = values_element.getElementsByTagName(ARGUMENT);
193 for(int i = 0; result == null && i < arguments.getLength(); i++) {
194 Element argument_element = (Element) arguments.item(i);
195 // Is this the argument we want.
196 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {
197 // Are we simply determining if this argument is enabled
198 if(is_enabled) {
199 result = argument_element.getAttribute(ENABLED);
200 }
201 else {
202 String argument_value = XMLTools.getValue(argument_element);
203 if(argument_value != null) {
204 result = argument_value;
205 }
206 argument_value = null;
207 }
208 }
209 argument_element = null;
210 }
211 arguments = null;
212 }
213 catch (Exception error) {
214 DebugStream.printStackTrace(error);
215 }
216 return result;
217 }
218
219
220 private Element loadArguments(String filename, String lang)
221 {
222 // Run the required program.
223 try {
224 Document document;
225
226 if (Gatherer.isGsdlRemote) {
227 String script_output = RemoteGreenstoneServer.getScriptOptions(filename, "");
228 document = XMLTools.parseXML(new StringReader(script_output));
229 }
230 else {
231 String args[];
232 if(Utility.isWindows()) {
233 args = new String[6];
234 args[0] = Configuration.perl_path;
235 args[1] = "-S";
236 args[2] = LocalGreenstone.getBinScriptDirectoryPath() + filename;
237 args[3] = "-xml";
238 args[4] = "-language";
239 args[5] = lang;
240 }
241 else {
242 args = new String[4];
243 args[0] = LocalGreenstone.getBinScriptDirectoryPath() + filename;
244 args[1] = "-xml";
245 args[2] = "-language";
246 args[3] = lang;
247 }
248
249
250 // Create the process.
251 Runtime runtime = Runtime.getRuntime();
252 Process process = runtime.exec(args);
253
254 /*
255 for (int i=0; i<args.length; i++) {
256 System.err.print(args[i] + " ");
257 }
258 System.err.println("");
259 */
260
261
262 document = XMLTools.parseXML(process.getErrorStream());
263
264 if (document == null) {
265 // command has not generated XML, script has probably failed in some way
266 for (int i=0; i<args.length; i++) {
267 DebugStream.print(args[i] + " ");
268 }
269 DebugStream.println("");
270 }
271 }
272
273 return document.getDocumentElement();
274 }
275 catch (Exception error) {
276 DebugStream.println("Error in ScriptOptions.loadArguments(): " + error);
277 DebugStream.printStackTrace(error);
278 }
279
280 return null;
281 }
282}
Note: See TracBrowser for help on using the repository browser.