1 | package org.greenstone.gatherer.collection;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import java.util.*;
|
---|
5 | import org.greenstone.gatherer.Configuration;
|
---|
6 | import org.greenstone.gatherer.DebugStream;
|
---|
7 | import org.greenstone.gatherer.Gatherer;
|
---|
8 | import org.greenstone.gatherer.cdm.Argument;
|
---|
9 | import org.greenstone.gatherer.greenstone.LocalGreenstone;
|
---|
10 | import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
|
---|
11 | import org.greenstone.gatherer.util.ArrayTools;
|
---|
12 | import org.greenstone.gatherer.util.Codec;
|
---|
13 | import org.greenstone.gatherer.util.Utility;
|
---|
14 | import org.greenstone.gatherer.util.XMLTools;
|
---|
15 | import org.w3c.dom.*;
|
---|
16 |
|
---|
17 |
|
---|
18 | /** This class parses options from Perl scripts: import.pl, buildcol.pl and explode_metadata_database.pl in particular. */
|
---|
19 | public 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 = Gatherer.remoteGreenstoneServer.getScriptOptions(filename, "");
|
---|
228 | document = XMLTools.parseXML(new StringReader(script_output));
|
---|
229 | }
|
---|
230 | else {
|
---|
231 | String args[];
|
---|
232 | args = new String[6];
|
---|
233 | args[0] = Configuration.perl_path;
|
---|
234 | args[1] = "-S";
|
---|
235 | args[2] = LocalGreenstone.getBinScriptDirectoryPath() + filename;
|
---|
236 | args[3] = "-xml";
|
---|
237 | args[4] = "-language";
|
---|
238 | args[5] = lang;
|
---|
239 |
|
---|
240 |
|
---|
241 | // Create the process.
|
---|
242 | Runtime runtime = Runtime.getRuntime();
|
---|
243 | Process process = runtime.exec(args);
|
---|
244 |
|
---|
245 |
|
---|
246 | //for (int i=0; i<args.length; i++) {
|
---|
247 | // System.err.print(args[i] + " ");
|
---|
248 | //}
|
---|
249 | //System.err.println("");
|
---|
250 |
|
---|
251 |
|
---|
252 |
|
---|
253 | document = XMLTools.parseXML(process.getErrorStream());
|
---|
254 |
|
---|
255 | if (document == null) {
|
---|
256 | // command has not generated XML, script has probably failed in some way
|
---|
257 | for (int i=0; i<args.length; i++) {
|
---|
258 | DebugStream.print(args[i] + " ");
|
---|
259 | }
|
---|
260 | DebugStream.println("");
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | return document.getDocumentElement();
|
---|
265 | }
|
---|
266 | catch (Exception error) {
|
---|
267 | DebugStream.println("Error in ScriptOptions.loadArguments(): " + error);
|
---|
268 | DebugStream.printStackTrace(error);
|
---|
269 | }
|
---|
270 |
|
---|
271 | return null;
|
---|
272 | }
|
---|
273 | }
|
---|