Ignore:
Timestamp:
2007-11-07T11:13:16+13:00 (16 years ago)
Author:
anna
Message:

If a plugin option has a display name, use the displayName instead of option name, such as process_expression instead of process_exp.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gli/branches/2.75/src/org/greenstone/gatherer/cdm/Argument.java

    r12824 r14784  
    5151 */
    5252public class Argument
    53     implements Comparable, Serializable {
    54     /** An element of the argument type enumeration specifying a combobox control. */
    55     static final public byte ENUM = 0;
    56     /** An element of the argument type enumeration specifying a checkbox control. */
    57     static final public byte FLAG = 1;
    58     /** An element of the argument type enumeration specifying a tree control. */
    59     static final public byte HIERARCHY = 2;
    60     /** An element of the argument type enumeration specifying a spinner control. */
    61     static final public byte INTEGER = 3;
    62     /** An element of the argument type enumeration specifying a language combobox control. */
    63     static final public byte LANGUAGE = 4;
    64     /** An element of the argument type enumeration specifying a list control. */
    65     static final public byte METADATA = 5;
    66     /** An element of the argument type enumeration specifying a metadata combobox control. */
    67     static final public byte METADATUM = 6;
    68     /** An element of the argument type enumeration specifying a text field. */
    69     static final public byte STRING = 7;
    70     /** An element of the argument type enumeration specifying a regular expression text field. */
    71     static final public byte REGEXP = 8;
    72     /** An element of the argument type enumeration specifying a metadata set combobox control. */
    73     static final public byte METADATA_SET_NAMESPACE = 9;
    74 
    75  ///////////kk added the number was 9, I changed it to 10//////////////
    76   /** An element of the argument type enumeration specifying a text field. */
    77     static final public byte URL = 10;
    78     /////////////////////////////////////////////////////////////////
    79 
    80     /** true if this argument should actually be hidden within the GLI. This is important for arguments such as import dir or other location critical arguments. */
    81     private boolean hidden_gli = false;
    82     /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */
    83     private boolean required = false;
    84     /** The type of this argument. Used to be an int, but bytes are cheaper. */
    85     private byte type = STRING;
    86     /** The maximum value an integer based control can have. */
    87     private int maximum = Integer.MAX_VALUE;
    88     /** The minimum value an integer based control can have. */
    89     private int minimum = Integer.MIN_VALUE;
    90     /** Every argument has a detail mode level at which it becomes available to the user to edit.
    91      * @see org.greenstone.gatherer.Configuration
    92      */
    93     private int mode_level = Configuration.LIBRARIAN_MODE;
    94     /** The DOM element this argument is built around, if any. */
    95     private Element element;
    96     /** If the argument is of type ENUM then this map holds all the various options. Each entry is an &lt;option value&gt; -&gt; &lt;description&gt; mapping. */
    97     private ArrayList option_list = null;
    98     /** A default value for parameter-type arguments. May be a Perl pattern. */
    99     private String default_value = null;
    100     /** The text description of this argument parsed from the pluginfo output. */
    101     private String description = null;
    102     /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */
    103     private String name = null;
    104     /** The plugin that owns this argument, for the purposes of visualising inheritance. */
    105     private String owner = null;
    106 
    107     private String display_name = null;
    108 
    109     /** Default Constructor. */
    110     public Argument() {
    111     }
    112 
    113     /** Another constructor but this one is a little more interesting as it takes a DOM element.
    114      * @param element the Element this argument is based around
    115      */
    116     public Argument(Element element) {
    117     this.element = element;
    118     }
    119 
    120     /** Method to add an element to the option_list.
    121      * @param name the name value of the option as a String
    122      * @param desc the description of this options as a String
    123      */
    124     public void addOption(String name, String desc) {
    125     if(type == ENUM && name != null) {
    126         if(desc == null) {
    127         desc = "";
    128         }
    129         if(option_list == null) {
    130         option_list = new ArrayList();
    131         }
    132         option_list.add(new ArgumentOption(name, desc));
    133     }
    134     }
    135 
    136     /** Method to compare two arguments for ordering.
    137      * @param object the argument we are comparing to, as an Object
    138      * @return an int specifying the argument order, using values as set out in String
    139      * @see org.greenstone.gatherer.cdm.Argument
    140      */
    141     public int compareTo(Object object) {
    142     if(object instanceof Argument) {
    143         return getName().compareTo(((Argument)object).getName());
    144     }
    145     else {
    146         return toString().compareTo(object.toString());
    147     }
    148     }
    149 
    150     /** Create a copy of this argument.
    151      * @return a newly created Argument with the same details as this one
    152      */
    153     public Argument copy() {
    154     Argument copy = new Argument();
    155     copy.setDefaultValue(default_value);
    156     copy.setDescription(description);
    157     copy.setOptions(option_list);
    158     copy.setOwner(owner);
    159     copy.setName(name);
    160     copy.setRequired(required);
    161     copy.setType(type);
    162     copy.setMinimum(minimum);
    163     copy.setMaximum(maximum);
    164     copy.setModeLevel(mode_level);
    165     copy.setHiddenGLI(hidden_gli);
    166     return copy;
    167     }
    168 
    169     /** Method to determine if two arguments are equal.
    170      * @param object the argument to test against, as an Object
    171      * @return true if the arguments names match, false otherwise
    172      */
    173     public boolean equals(Object object) {
    174     return (compareTo(object) == 0);
    175     }
    176 
    177     /** Method to retrieve the value of default_value.
    178      * @return a String containing the default value
    179      */
    180     public String getDefaultValue() {
    181     return default_value;
    182     }
    183 
    184     /** Method to retrieve this arguments description.
    185      * @return a String containing the description
    186      */
    187     public String getDescription() {
    188     return description;
    189     }
    190 
    191     public Element getElement() {
    192     return element;
    193     }
    194     /** Retrieve the upper bound of a range based argument.
    195      * @return the maximum as an int
    196      */
    197     public int getMaximum() {
    198     return maximum;
    199     }
    200 
    201     /** Retrieve the lower bound of a range based argument.
    202      * @return the minimum as an int
    203      */
    204     public int getMinimum() {
    205     return minimum;
    206     }
    207 
    208     /** Retrieves the mode level at which this argument should become available. Any higher levels should also see this argument.
    209      * @return the mode level as an int
    210      */
    211     public int getModeLevel() {
    212     return mode_level;
    213     }
    214 
    215     /** Method to retrieve the value of name.
    216      * @return a String containing the argument name
    217      * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
    218      */
    219     public String getName() {
    220     if(name == null && element != null) {
    221         name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    222     }
    223     return name;
    224     }
    225 
    226     public String getDisplayName() {
    227     return display_name;
    228     }
    229 
    230 
    231     /** Method to retrieve the option list for this argument.
    232      * @return a HashMap containing &lt;option value&gt; -&gt; &lt;description&gt; entries
    233      */
    234     public ArrayList getOptions() {
    235     return option_list;
    236     }
    237 
    238     /** Retrieve the name of the owner of this argument.
    239      * @return the owners name as a String
    240      */
    241     public String getOwner() {
    242     return owner;
    243     }
    244    
    245     /** Method to determine the type of this argument.
    246      * @return a byte specifying the type
    247      */
    248     public byte getType() {
    249     return type;
    250     }
    251 
    252     /** Method to retrieve the value of value.
    253      * @return the value of value as a String
    254      * @see org.greenstone.gatherer.Gatherer#c_man
    255      * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
    256      */
    257     public String getValue()
    258     {
    259     // Only assigned arguments have values.
    260     if (element == null) {
    261         return null;
    262     }
    263 
    264     return XMLTools.getValue(element);
    265     }
    266 
    267 
    268     /** Method to determine if this argument has been assigned.
    269      * @return true if it has, false otherwise
    270      * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
    271      * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
    272      */
    273     public boolean isAssigned() {
    274     return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
    275     }
    276 
    277     /** Determine if this is a custom argument ie one that has been parsed from the config file but doesn't have a matching entry in the argument library.
    278      * @return true if this argument is a custom, false otherwise
    279      * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
    280      * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
    281      */
    282     public boolean isCustomArgument() {
    283     return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
    284     }
    285 
    286     /** Determine if this argument is hidden in GLI
    287      * @return true if the argument is hidden, false otherwise
    288      */
    289     public boolean isHiddenGLI() {
    290     return hidden_gli;
    291     }
    292 
    293     /** Method to determine of this argument is required for the associated script to work.
    294      * @return true if this argument is required, false otherwise
    295      */
    296     public boolean isRequired() {
    297     return required;
    298     }
    299 
    300     /** Method to allow for the activation of arguments that might never have their setValue() method called.
    301      * @param assigned the desired state as a boolean
    302      * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
    303      * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
    304      * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
    305      */
    306     public void setAssigned(boolean assigned) {
    307     if(element != null) {
    308         element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
    309     }
    310     }
    311 
    312     /** Sets the value of default_value.
    313      * @param default_value The new value for default_value as a <strong>String</strong>.
    314      */
    315     public void setDefaultValue(String default_value) {
    316     this.default_value = default_value;
    317     }
    318 
    319     /** Set the value of desc.
    320      * @param description the new value of desc as a String
    321      */
    322     public void setDescription(String description) {
    323     this.description = description;
    324     }
    325 
    326     /** Set the element this argument should be based upon.
    327      * @param element the Element
    328      */
    329     public void setElement(Element element) {
    330     this.element = element;
    331     }
    332 
    333     /** Mark this argument as being hidden in GLI. */
    334     public void setHiddenGLI(boolean hidden) {
    335     this.hidden_gli = hidden;
    336     }
    337 
    338     /** Set the upper bound for a range type argument.
    339      * @param maximum the maximum as an int
    340      */
    341     public void setMaximum(int maximum) {
    342     this.maximum = maximum;
    343     }
    344 
    345     /** Set the lower bound for a range type argument.
    346      * @param minimum the minimum as an int
    347      */
    348     public void setMinimum(int minimum) {
    349     this.minimum = minimum;
    350     }
    351 
    352     /** Set the detail mode level where this argument will become available.
    353      * @param mode_level the mode level as an int
    354      */
    355     public void setModeLevel(int mode_level) {
    356     this.mode_level = mode_level;
    357     }
    358 
    359     /** Set the value of name.
    360      * @param name the new value of name as a String
    361      */
    362     public void setName(String name) {
    363     this.name = name;
    364     }
    365 
    366     /** Sets the value of the options list.
    367      * @param list the new options list as a HashMap
    368      */
    369     public void setOptions(ArrayList list) {
    370     this.option_list = list;
    371     }
    372 
    373     /** Set the owner of this argument.
    374      * @param owner the name of the owner of this argument as a String
    375      */
    376     public void setOwner(String owner) {
    377     this.owner = owner;
    378     }
    379 
    380     /** Set the value of required.
    381      * @param required the new value of required as a boolean
    382      */
    383     public void setRequired(boolean required) {
    384     this.required = required;
    385     }
    386 
    387     /** Set the value of type.
    388      * @param type the new value of type as an byte
    389      */
    390     public void setType(byte type) {
    391     this.type = type;
    392     }
    393 
    394     /** Set the value of type, by matching a type to the given string.
    395      * @param new_type a String which contains the name of a certain argument type
    396      * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR
    397      * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR
    398      * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR
    399      * @see org.greenstone.gatherer.util.StaticStrings#INT_STR
    400      * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR
    401      * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR
    402      * @see org.greenstone.gatherer.util.StaticStrings#METADATUM_TYPE_STR
    403      * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR
    404      */
    405     public void setType(String new_type) {
    406     if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) {
    407         this.type = ENUM;
    408         option_list = new ArrayList();
    409     }
    410     else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) {
    411         this.type = FLAG;
    412     }
    413     else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) {
    414         this.type = HIERARCHY;
    415     }
    416     else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) {
    417         this.type = INTEGER;
    418     }
    419     else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) {
    420         this.type = LANGUAGE;
    421     }
    422     else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) {
    423         this.type = METADATA;
    424     }
    425     else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) {
    426         this.type = METADATUM;
    427     }
    428     else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) {
    429         this.type = REGEXP;
    430     }
    431     else {
    432         this.type = STRING;
    433     }
    434     }
    435 
    436     /** Method to set the value of this argument.
    437      * @param value the new value for the argument
    438      * @see org.greenstone.gatherer.Gatherer#println
    439      */
    440     public void setValue(String value) {
    441     if(element != null) {
    442         XMLTools.setValue(element, value);
    443     }
    444     else {
    445         DebugStream.println("Argument.setValue(" + value + ") called on a base Argument.");
    446     }
    447     }
    448 
    449     /** Set the values vector to the given values. Currently I just assign the new values, whereas I may later want to implement a deep clone.
    450      * @param values an ArrayList of values
    451      * @see org.greenstone.gatherer.Gatherer#println
    452      */
    453     public void setValues(ArrayList values) {
    454     if(element != null) {
    455         StringBuffer value = new StringBuffer();
    456         int value_length = values.size();
    457         for(int i = 0; i < value_length; i++) {
    458         value.append(values.get(i));
    459         value.append(StaticStrings.COMMA_CHARACTER);
    460         }
    461         value.deleteCharAt(value.length() - 1); // Remove last ','
    462         XMLTools.setValue(element, value.toString());
    463     }
    464     else {
    465         DebugStream.println("Argument.setValues([" + values.size() + " items]) called on a base Argument.");
    466     }
    467     }
    468 
    469     /** Method for translating the data of this class into a string.
    470      * @return a String containing a fragment of the total arguments string
    471      * @see org.greenstone.gatherer.Gatherer#c_man
    472      * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
    473      * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER
    474      * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
    475      * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER
    476      * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER
    477      */
    478     public String toString()
    479     {
    480     StringBuffer text = new StringBuffer("-");
    481 
    482     if (element == null) {
    483         return text.toString();
    484     }
    485 
    486     if (name == null) {
    487         name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    488     }
    489     text.append(name);
    490 
    491     String value = XMLTools.getValue(element);
    492     if (value.length() == 0) {
    493         return text.toString();
    494     }
    495 
    496     text.append(StaticStrings.SPACE_CHARACTER);
    497 
    498 //  // Handle metadata elements specially
    499 //  if (type == METADATA || type == METADATUM) {
    500 //      // Tokenize the string
    501 //      StringTokenizer tokenizer = new StringTokenizer(value, ",");
    502 //      while (tokenizer.hasMoreTokens()) {
    503 //      String token = tokenizer.nextToken();
    504 
    505 //      MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName(token);
    506 //      if (metadata_element != null) {
    507 //          text.append(metadata_element.getFullName());
    508 //      }
    509 //      else {
    510 //          text.append(token);
    511 //      }
    512 
    513 //      if (tokenizer.hasMoreTokens()) {
    514 //          text.append(StaticStrings.COMMA_CHARACTER);
    515 //      }
    516 //      }
    517 //      return text.toString();
    518 //  }
    519 
    520     // If the value contains a space, add speech marks
    521     //   (Except for metadata elements, which won't have spaces when written out to collect.cfg)
    522     if (value.indexOf(StaticStrings.SPACE_CHARACTER) != -1 && !(type == METADATUM || type == METADATA)) {
    523         value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER;
    524     }
    525 
    526     text.append(value);
    527     return text.toString();
    528     }
    529 
    530     /** parse the <Option> XML from eg import.pl -xml or pluginfo.pl -xml */
    531     public void parseXML(Element option) {
    532 
    533     for(Node node = option.getFirstChild(); node != null; node = node.getNextSibling()) {
    534         String node_name = node.getNodeName();
    535         if(node_name.equals("Name")) {
    536         setName(XMLTools.getValue(node));
    537         }
    538         else if(node_name.equals("Desc")) {
    539         setDescription(XMLTools.getValue(node));
    540         }
    541         else if(node_name.equals("Type")) {
    542         setType(XMLTools.getValue(node));
    543         }
    544         else if(node_name.equals("Default")) {
    545         setDefaultValue(XMLTools.getValue(node));
    546         }
    547         else if(node_name.equals("Required")) {
    548         String v = XMLTools.getValue(node);
    549         if(v != null && v.equals("yes")) {
    550             setRequired(true);
    551         }
    552         }
    553         else if(node_name.equals("List")) {
    554         // Two final loops are required to parse lists.
    555         for(Node value = node.getFirstChild(); value != null; value = value.getNextSibling()) {
    556             if(value.getNodeName().equals("Value")) {
    557             String key = null;
    558             String desc = "";
    559             for(Node subvalue = value.getFirstChild(); subvalue != null; subvalue = subvalue.getNextSibling()) {
    560                 node_name = subvalue.getNodeName();
    561                 if(node_name.equals("Name")) {
    562                 key = XMLTools.getValue(subvalue);
    563                 }
    564                 else if(node_name.equals("Desc")) {
    565                 desc = XMLTools.getValue(subvalue);
    566                 }
    567             }
    568             if(key != null) {
    569                 addOption(key, desc);
    570             }
    571             }
    572         }
    573         }
    574         else if(node_name.equals("Range")) {
    575         String range_raw = XMLTools.getValue(node);
    576         int index = -1;
    577         if((index = range_raw.indexOf(StaticStrings.COMMA_CHARACTER)) != -1) {
    578             if(index > 0) {
    579             try {
    580                 String first_number = range_raw.substring(0, index);
    581                 setMinimum(Integer.parseInt(first_number));
    582                 first_number = null;
    583             }
    584             catch(Exception exception) {
    585             }
    586             }
    587            
    588             if(index + 1 < range_raw.length()) {
    589             try {
    590                 String second_number = range_raw.substring(index + 1);
    591                 setMaximum(Integer.parseInt(second_number));
    592                 second_number = null;   
    593             }
    594             catch(Exception exception) {
    595             }
    596             }
    597         }
    598         // Else it wasn't a valid range anyway, so ignore it
    599         }
    600         else if(node_name.equals("HiddenGLI")) {
    601         setHiddenGLI(true);
    602         }
    603         else if(node_name.equals("ModeGLI")) {
    604         String mode_level_str = XMLTools.getValue(node);
    605         try {
    606             int mode_level = Integer.parseInt(mode_level_str);
    607             setModeLevel(mode_level);
    608         }
    609         catch(Exception exception) {
    610             DebugStream.println("Exception in Argument.parseXML() - Unexpected but non-fatal");
    611             DebugStream.printStackTrace(exception);
    612         }
    613         }
    614        
    615     } // for each option
     53implements Comparable, Serializable {
     54    /** An element of the argument type enumeration specifying a combobox control. */
     55    static final public byte ENUM = 0;
     56    /** An element of the argument type enumeration specifying a checkbox control. */
     57    static final public byte FLAG = 1;
     58    /** An element of the argument type enumeration specifying a tree control. */
     59    static final public byte HIERARCHY = 2;
     60    /** An element of the argument type enumeration specifying a spinner control. */
     61    static final public byte INTEGER = 3;
     62    /** An element of the argument type enumeration specifying a language combobox control. */
     63    static final public byte LANGUAGE = 4;
     64    /** An element of the argument type enumeration specifying a list control. */
     65    static final public byte METADATA = 5;
     66    /** An element of the argument type enumeration specifying a metadata combobox control. */
     67    static final public byte METADATUM = 6;
     68    /** An element of the argument type enumeration specifying a text field. */
     69    static final public byte STRING = 7;
     70    /** An element of the argument type enumeration specifying a regular expression text field. */
     71    static final public byte REGEXP = 8;
     72    /** An element of the argument type enumeration specifying a metadata set combobox control. */
     73    static final public byte METADATA_SET_NAMESPACE = 9;
     74
     75    ///////////kk added the number was 9, I changed it to 10//////////////
     76    /** An element of the argument type enumeration specifying a text field. */
     77    static final public byte URL = 10;
     78    /////////////////////////////////////////////////////////////////
     79
     80    /** true if this argument should actually be hidden within the GLI. This is important for arguments such as import dir or other location critical arguments. */
     81    private boolean hidden_gli = false;
     82    /** <i>true</i> if this argument is required for the applicable script to work properly, <i>false</i> otherwise. */
     83    private boolean required = false;
     84    /** The type of this argument. Used to be an int, but bytes are cheaper. */
     85    private byte type = STRING;
     86    /** The maximum value an integer based control can have. */
     87    private int maximum = Integer.MAX_VALUE;
     88    /** The minimum value an integer based control can have. */
     89    private int minimum = Integer.MIN_VALUE;
     90    /** Every argument has a detail mode level at which it becomes available to the user to edit.
     91     * @see org.greenstone.gatherer.Configuration
     92     */
     93    private int mode_level = Configuration.LIBRARIAN_MODE;
     94    /** The DOM element this argument is built around, if any. */
     95    private Element element;
     96    /** If the argument is of type ENUM then this map holds all the various options. Each entry is an &lt;option value&gt; -&gt; &lt;description&gt; mapping. */
     97    private ArrayList option_list = null;
     98    /** A default value for parameter-type arguments. May be a Perl pattern. */
     99    private String default_value = null;
     100    /** The text description of this argument parsed from the pluginfo output. */
     101    private String description = null;
     102    /** The argument flag as it appears in the command. Also used as the unique identifier of an argument. */
     103    private String name = null;
     104    /** The plugin that owns this argument, for the purposes of visualising inheritance. */
     105    private String owner = null;
     106
     107    private String display_name = null;
     108
     109    /** Default Constructor. */
     110    public Argument() {
     111    }
     112
     113    /** Another constructor but this one is a little more interesting as it takes a DOM element.
     114     * @param element the Element this argument is based around
     115     */
     116    public Argument(Element element) {
     117        this.element = element;
     118    }
     119
     120    /** Method to add an element to the option_list.
     121     * @param name the name value of the option as a String
     122     * @param desc the description of this options as a String
     123     */
     124    public void addOption(String name, String desc) {
     125        if(type == ENUM && name != null) {
     126            if(desc == null) {
     127                desc = "";
     128            }
     129            if(option_list == null) {
     130                option_list = new ArrayList();
     131            }
     132            option_list.add(new ArgumentOption(name, desc));
     133        }
     134    }
     135
     136    /** Method to compare two arguments for ordering.
     137     * @param object the argument we are comparing to, as an Object
     138     * @return an int specifying the argument order, using values as set out in String
     139     * @see org.greenstone.gatherer.cdm.Argument
     140     */
     141    public int compareTo(Object object) {
     142        if(object instanceof Argument) {
     143            return getName().compareTo(((Argument)object).getName());
     144        }
     145        else {
     146            return toString().compareTo(object.toString());
     147        }
     148    }
     149
     150    /** Create a copy of this argument.
     151     * @return a newly created Argument with the same details as this one
     152     */
     153    public Argument copy() {
     154        Argument copy = new Argument();
     155        copy.setDefaultValue(default_value);
     156        copy.setDescription(description);
     157        copy.setOptions(option_list);
     158        copy.setOwner(owner);
     159        copy.setName(name);     
     160        copy.setDispalyName(display_name);
     161        copy.setRequired(required);
     162        copy.setType(type);
     163        copy.setMinimum(minimum);
     164        copy.setMaximum(maximum);
     165        copy.setModeLevel(mode_level);
     166        copy.setHiddenGLI(hidden_gli);
     167        return copy;
     168    }
     169
     170    /** Method to determine if two arguments are equal.
     171     * @param object the argument to test against, as an Object
     172     * @return true if the arguments names match, false otherwise
     173     */
     174    public boolean equals(Object object) {
     175        return (compareTo(object) == 0);
     176    }
     177
     178    /** Method to retrieve the value of default_value.
     179     * @return a String containing the default value
     180     */
     181    public String getDefaultValue() {
     182        return default_value;
     183    }
     184
     185    /** Method to retrieve this arguments description.
     186     * @return a String containing the description
     187     */
     188    public String getDescription() {
     189        return description;
     190    }
     191
     192    public Element getElement() {
     193        return element;
     194    }
     195    /** Retrieve the upper bound of a range based argument.
     196     * @return the maximum as an int
     197     */
     198    public int getMaximum() {
     199        return maximum;
     200    }
     201
     202    /** Retrieve the lower bound of a range based argument.
     203     * @return the minimum as an int
     204     */
     205    public int getMinimum() {
     206        return minimum;
     207    }
     208
     209    /** Retrieves the mode level at which this argument should become available. Any higher levels should also see this argument.
     210     * @return the mode level as an int
     211     */
     212    public int getModeLevel() {
     213        return mode_level;
     214    }
     215
     216    /** Method to retrieve the value of name.
     217     * @return a String containing the argument name
     218     * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
     219     */
     220    public String getName() {
     221        if(name == null && element != null) {
     222            name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     223        }
     224        return name;
     225    }
     226
     227    public String getDisplayName() {
     228        if(display_name==null)
     229            return "";
     230        return display_name;
     231    }
     232
     233    /** Method to retrieve the option list for this argument.
     234     * @return a HashMap containing &lt;option value&gt; -&gt; &lt;description&gt; entries
     235     */
     236    public ArrayList getOptions() {
     237        return option_list;
     238    }
     239
     240    /** Retrieve the name of the owner of this argument.
     241     * @return the owners name as a String
     242     */
     243    public String getOwner() {
     244        return owner;
     245    }
     246
     247    /** Method to determine the type of this argument.
     248     * @return a byte specifying the type
     249     */
     250    public byte getType() {
     251        return type;
     252    }
     253
     254    /** Method to retrieve the value of value.
     255     * @return the value of value as a String
     256     * @see org.greenstone.gatherer.Gatherer#c_man
     257     * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
     258     */
     259    public String getValue()
     260    {
     261        // Only assigned arguments have values.
     262        if (element == null) {
     263            return null;
     264        }
     265
     266        return XMLTools.getValue(element);
     267    }
     268
     269
     270    /** Method to determine if this argument has been assigned.
     271     * @return true if it has, false otherwise
     272     * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
     273     * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
     274     */
     275    public boolean isAssigned() {
     276        return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
     277    }
     278
     279    /** Determine if this is a custom argument ie one that has been parsed from the config file but doesn't have a matching entry in the argument library.
     280     * @return true if this argument is a custom, false otherwise
     281     * @see org.greenstone.gatherer.util.StaticStrings#CUSTOM_ATTRIBUTE
     282     * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
     283     */
     284    public boolean isCustomArgument() {
     285        return (element != null && element.getAttribute(StaticStrings.CUSTOM_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
     286    }
     287
     288    /** Determine if this argument is hidden in GLI
     289     * @return true if the argument is hidden, false otherwise
     290     */
     291    public boolean isHiddenGLI() {
     292        return hidden_gli;
     293    }
     294
     295    /** Method to determine of this argument is required for the associated script to work.
     296     * @return true if this argument is required, false otherwise
     297     */
     298    public boolean isRequired() {
     299        return required;
     300    }
     301
     302    /** Method to allow for the activation of arguments that might never have their setValue() method called.
     303     * @param assigned the desired state as a boolean
     304     * @see org.greenstone.gatherer.util.StaticStrings#ASSIGNED_ATTRIBUTE
     305     * @see org.greenstone.gatherer.util.StaticStrings#FALSE_STR
     306     * @see org.greenstone.gatherer.util.StaticStrings#TRUE_STR
     307     */
     308    public void setAssigned(boolean assigned) {
     309        if(element != null) {
     310            element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
     311        }
     312    }
     313
     314    /** Sets the value of default_value.
     315     * @param default_value The new value for default_value as a <strong>String</strong>.
     316     */
     317    public void setDefaultValue(String default_value) {
     318        this.default_value = default_value;
     319    }
     320
     321    /** Set the value of desc.
     322     * @param description the new value of desc as a String
     323     */
     324    public void setDescription(String description) {
     325        this.description = description;
     326    }
     327
     328    /** Set the element this argument should be based upon.
     329     * @param element the Element
     330     */
     331    public void setElement(Element element) {
     332        this.element = element;
     333    }
     334
     335    /** Mark this argument as being hidden in GLI. */
     336    public void setHiddenGLI(boolean hidden) {
     337        this.hidden_gli = hidden;
     338    }
     339
     340    /** Set the upper bound for a range type argument.
     341     * @param maximum the maximum as an int
     342     */
     343    public void setMaximum(int maximum) {
     344        this.maximum = maximum;
     345    }
     346
     347    /** Set the lower bound for a range type argument.
     348     * @param minimum the minimum as an int
     349     */
     350    public void setMinimum(int minimum) {
     351        this.minimum = minimum;
     352    }
     353
     354    /** Set the detail mode level where this argument will become available.
     355     * @param mode_level the mode level as an int
     356     */
     357    public void setModeLevel(int mode_level) {
     358        this.mode_level = mode_level;
     359    }
     360
     361    /** Set the value of name.
     362     * @param name the new value of name as a String
     363     */
     364    public void setName(String name) {
     365        this.name = name;
     366    }
    616367   
    617     } // parseXML
    618 
    619     public class ArgumentOption
     368    public void setDispalyName(String name) {       
     369        this.display_name = name;
     370    }   
     371
     372    /** Sets the value of the options list.
     373     * @param list the new options list as a HashMap
     374     */
     375    public void setOptions(ArrayList list) {
     376        this.option_list = list;
     377    }
     378
     379    /** Set the owner of this argument.
     380     * @param owner the name of the owner of this argument as a String
     381     */
     382    public void setOwner(String owner) {
     383        this.owner = owner;
     384    }
     385
     386    /** Set the value of required.
     387     * @param required the new value of required as a boolean
     388     */
     389    public void setRequired(boolean required) {
     390        this.required = required;
     391    }
     392
     393    /** Set the value of type.
     394     * @param type the new value of type as an byte
     395     */
     396    public void setType(byte type) {
     397        this.type = type;
     398    }
     399
     400    /** Set the value of type, by matching a type to the given string.
     401     * @param new_type a String which contains the name of a certain argument type
     402     * @see org.greenstone.gatherer.util.StaticStrings#ENUM_STR
     403     * @see org.greenstone.gatherer.util.StaticStrings#FLAG_STR
     404     * @see org.greenstone.gatherer.util.StaticStrings#HIERARCHY_STR
     405     * @see org.greenstone.gatherer.util.StaticStrings#INT_STR
     406     * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_STR
     407     * @see org.greenstone.gatherer.util.StaticStrings#METADATA_TYPE_STR
     408     * @see org.greenstone.gatherer.util.StaticStrings#METADATUM_TYPE_STR
     409     * @see org.greenstone.gatherer.util.StaticStrings#REGEXP_STR
     410     */
     411    public void setType(String new_type) {
     412        if(new_type.equalsIgnoreCase(StaticStrings.ENUM_STR)) {
     413            this.type = ENUM;
     414            option_list = new ArrayList();
     415        }
     416        else if(new_type.equalsIgnoreCase(StaticStrings.FLAG_STR)) {
     417            this.type = FLAG;
     418        }
     419        else if(new_type.equalsIgnoreCase(StaticStrings.HIERARCHY_STR)) {
     420            this.type = HIERARCHY;
     421        }
     422        else if(new_type.equalsIgnoreCase(StaticStrings.INT_STR)) {
     423            this.type = INTEGER;
     424        }
     425        else if(new_type.equalsIgnoreCase(StaticStrings.LANGUAGE_STR)) {
     426            this.type = LANGUAGE;
     427        }
     428        else if(new_type.equalsIgnoreCase(StaticStrings.METADATA_TYPE_STR)) {
     429            this.type = METADATA;
     430        }
     431        else if(new_type.equalsIgnoreCase(StaticStrings.METADATUM_TYPE_STR)) {
     432            this.type = METADATUM;
     433        }
     434        else if(new_type.equalsIgnoreCase(StaticStrings.REGEXP_STR)) {
     435            this.type = REGEXP;
     436        }
     437        else {
     438            this.type = STRING;
     439        }
     440    }
     441
     442    /** Method to set the value of this argument.
     443     * @param value the new value for the argument
     444     * @see org.greenstone.gatherer.Gatherer#println
     445     */
     446    public void setValue(String value) {
     447        if(element != null) {
     448            XMLTools.setValue(element, value);
     449        }
     450        else {
     451            DebugStream.println("Argument.setValue(" + value + ") called on a base Argument.");
     452        }
     453    }
     454
     455    /** Set the values vector to the given values. Currently I just assign the new values, whereas I may later want to implement a deep clone.
     456     * @param values an ArrayList of values
     457     * @see org.greenstone.gatherer.Gatherer#println
     458     */
     459    public void setValues(ArrayList values) {
     460        if(element != null) {
     461            StringBuffer value = new StringBuffer();
     462            int value_length = values.size();
     463            for(int i = 0; i < value_length; i++) {
     464                value.append(values.get(i));
     465                value.append(StaticStrings.COMMA_CHARACTER);
     466            }
     467            value.deleteCharAt(value.length() - 1); // Remove last ','
     468            XMLTools.setValue(element, value.toString());
     469        }
     470        else {
     471            DebugStream.println("Argument.setValues([" + values.size() + " items]) called on a base Argument.");
     472        }
     473    }
     474
     475    /** Method for translating the data of this class into a string.
     476     * @return a String containing a fragment of the total arguments string
     477     * @see org.greenstone.gatherer.Gatherer#c_man
     478     * @see org.greenstone.gatherer.collection.CollectionManager#getCollection
     479     * @see org.greenstone.gatherer.util.StaticStrings#COMMA_CHARACTER
     480     * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
     481     * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER
     482     * @see org.greenstone.gatherer.util.StaticStrings#SPEECH_CHARACTER
     483     */
     484    public String toString()
     485    {
     486        StringBuffer text = new StringBuffer("-");
     487
     488        if (element == null) {
     489            return text.toString();
     490        }
     491
     492        if (name == null) {
     493            name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     494        }
     495        text.append(name);
     496
     497        String value = XMLTools.getValue(element);
     498        if (value.length() == 0) {
     499            return text.toString();
     500        }
     501
     502        text.append(StaticStrings.SPACE_CHARACTER);
     503
     504//      // Handle metadata elements specially
     505//      if (type == METADATA || type == METADATUM) {
     506//      // Tokenize the string
     507//      StringTokenizer tokenizer = new StringTokenizer(value, ",");
     508//      while (tokenizer.hasMoreTokens()) {
     509//      String token = tokenizer.nextToken();
     510
     511//      MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName(token);
     512//      if (metadata_element != null) {
     513//      text.append(metadata_element.getFullName());
     514//      }
     515//      else {
     516//      text.append(token);
     517//      }
     518
     519//      if (tokenizer.hasMoreTokens()) {
     520//      text.append(StaticStrings.COMMA_CHARACTER);
     521//      }
     522//      }
     523//      return text.toString();
     524//      }
     525
     526        // If the value contains a space, add speech marks
     527        //   (Except for metadata elements, which won't have spaces when written out to collect.cfg)
     528        if (value.indexOf(StaticStrings.SPACE_CHARACTER) != -1 && !(type == METADATUM || type == METADATA)) {
     529            value = StaticStrings.SPEECH_CHARACTER + value + StaticStrings.SPEECH_CHARACTER;
     530        }
     531
     532        text.append(value);
     533        return text.toString();
     534    }
     535
     536    /** parse the <Option> XML from eg import.pl -xml or pluginfo.pl -xml */
     537    public void parseXML(Element option) {
     538
     539        for(Node node = option.getFirstChild(); node != null; node = node.getNextSibling()) {
     540            String node_name = node.getNodeName();
     541            if(node_name.equals("Name")) {
     542                setName(XMLTools.getValue(node));
     543            }
     544            else if(node_name.equals("DisplayName")) {
     545                setDispalyName(XMLTools.getValue(node));
     546            }
     547            else if(node_name.equals("Desc")) {
     548                setDescription(XMLTools.getValue(node));
     549            }
     550            else if(node_name.equals("Type")) {
     551                setType(XMLTools.getValue(node));
     552            }
     553            else if(node_name.equals("Default")) {
     554                setDefaultValue(XMLTools.getValue(node));
     555            }
     556            else if(node_name.equals("Required")) {
     557                String v = XMLTools.getValue(node);
     558                if(v != null && v.equals("yes")) {
     559                    setRequired(true);
     560                }
     561            }
     562            else if(node_name.equals("List")) {
     563                // Two final loops are required to parse lists.
     564                for(Node value = node.getFirstChild(); value != null; value = value.getNextSibling()) {
     565                    if(value.getNodeName().equals("Value")) {
     566                        String key = null;
     567                        String desc = "";
     568                        for(Node subvalue = value.getFirstChild(); subvalue != null; subvalue = subvalue.getNextSibling()) {
     569                            node_name = subvalue.getNodeName();
     570                            if(node_name.equals("Name")) {
     571                                key = XMLTools.getValue(subvalue);
     572                            }
     573                            else if(node_name.equals("Desc")) {
     574                                desc = XMLTools.getValue(subvalue);
     575                            }
     576                        }
     577                        if(key != null) {
     578                            addOption(key, desc);
     579                        }
     580                    }
     581                }
     582            }
     583            else if(node_name.equals("Range")) {
     584                String range_raw = XMLTools.getValue(node);
     585                int index = -1;
     586                if((index = range_raw.indexOf(StaticStrings.COMMA_CHARACTER)) != -1) {
     587                    if(index > 0) {
     588                        try {
     589                            String first_number = range_raw.substring(0, index);
     590                            setMinimum(Integer.parseInt(first_number));
     591                            first_number = null;
     592                        }
     593                        catch(Exception exception) {
     594                        }
     595                    }
     596
     597                    if(index + 1 < range_raw.length()) {
     598                        try {
     599                            String second_number = range_raw.substring(index + 1);
     600                            setMaximum(Integer.parseInt(second_number));
     601                            second_number = null;   
     602                        }
     603                        catch(Exception exception) {
     604                        }
     605                    }
     606                }
     607                // Else it wasn't a valid range anyway, so ignore it
     608            }
     609            else if(node_name.equals("HiddenGLI")) {
     610                setHiddenGLI(true);
     611            }
     612            else if(node_name.equals("ModeGLI")) {
     613                String mode_level_str = XMLTools.getValue(node);
     614                try {
     615                    int mode_level = Integer.parseInt(mode_level_str);
     616                    setModeLevel(mode_level);
     617                }
     618                catch(Exception exception) {
     619                    DebugStream.println("Exception in Argument.parseXML() - Unexpected but non-fatal");
     620                    DebugStream.printStackTrace(exception);
     621                }
     622            }
     623
     624        } // for each option
     625
     626    } // parseXML
     627
     628    public class ArgumentOption
    620629    implements Comparable {
    621     public String name;
    622     public String description;
    623     private String text; // cached version
    624 
    625     public ArgumentOption(String name, String desc) {
    626         this.name = name;
    627         this.description = desc;
    628     }
    629    
    630     public int compareTo(Object obj) {
    631         return toString().compareTo(obj.toString());
    632     }
    633 
    634     public boolean equals(Object obj) {     
    635         return (obj != null && compareTo(obj) == 0);
    636     }
    637    
    638     public String toString() {
    639         return name + " "+ StaticStrings.MINUS_CHARACTER + " "+ description;
    640     }
    641    
    642     public String getToolTip() {
    643         return Utility.formatHTMLWidth(name+": "+description, 80);
    644     }
    645     }
     630        public String name;
     631        public String description;
     632        private String text; // cached version
     633
     634        public ArgumentOption(String name, String desc) {
     635            this.name = name;
     636            this.description = desc;
     637        }
     638
     639        public int compareTo(Object obj) {
     640            return toString().compareTo(obj.toString());
     641        }
     642
     643        public boolean equals(Object obj) {     
     644            return (obj != null && compareTo(obj) == 0);
     645        }
     646
     647        public String toString() {
     648            return name + " "+ StaticStrings.MINUS_CHARACTER + " "+ description;
     649        }
     650
     651        public String getToolTip() {
     652            return Utility.formatHTMLWidth(name+": "+description, 80);
     653        }
     654    }
    646655}
Note: See TracChangeset for help on using the changeset viewer.