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