Changeset 12247


Ignore:
Timestamp:
2006-07-19T14:51:13+12:00 (18 years ago)
Author:
kjdon
Message:

made ArgumentContainer a base class instead of an interface and moved all the shared code from Plugin and Classifier into it (ie most of these two classes). also removed custom stuff from classifier and plugin - no more custom ones allowed

Location:
trunk/gli/src/org/greenstone/gatherer/cdm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/cdm/ArgumentContainer.java

    r5590 r12247  
    2727package org.greenstone.gatherer.cdm;
    2828
     29import java.io.Serializable;
     30import java.util.ArrayList;
     31import java.util.HashMap;
     32import java.util.Iterator;
     33import java.util.Map;
     34import java.util.Set;
     35import org.greenstone.gatherer.DebugStream;
     36import org.greenstone.gatherer.util.StaticStrings;
     37import org.w3c.dom.*;
     38
    2939/**************************************************************************************
    3040 * Written:      20/05/02
     
    3747 * @version 2.3
    3848 */
    39 public interface ArgumentContainer {
     49abstract public class ArgumentContainer
     50    extends ArrayList
     51    implements Comparable, DOMProxyListEntry, Serializable  {
     52
     53    // The DOM Element this container is modelled on
     54    protected Element element;
     55    protected boolean is_abstract = false;
     56    protected ArgumentContainer super_container;
     57    // should be set by the constructor
     58    protected String display_name = "Unknown";
     59    protected String description;
     60    protected String name;
     61
     62    /** Constructor used in DOMProxyListModel initializations
     63     */
     64    public ArgumentContainer() {
     65    }
     66
     67    public ArgumentContainer(Element element, ArgumentContainer base_container) {
     68
     69    super();
     70    this.element = element;
     71    this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
     72
     73    // Parse in any argument options for this container, keeping a list of the ones found
     74    HashMap known_arguments = new HashMap();
     75    NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
     76    int option_elements_length = option_elements.getLength();
     77    for(int i = 0; i < option_elements_length; i++) {
     78        Element option_element = (Element) option_elements.item(i);
     79        Argument argument = new Argument(option_element);
     80        ///ystem.err.println("Rebuilding existing argument: " + argument.getName());
     81        known_arguments.put(argument.getName(), argument);
     82    }
     83    // If a base classifier was given
     84    if(base_container != null) {
     85        // Copy the details, and add a reference to whatever the base container's super container is
     86        description = base_container.getDescription();
     87        // Now search through the 'dummy' arguments belonging to the base container. For each found, if it is already assigned, fill out further details such as type. If any are found that are not already assigned for this container, copy them and add them, but without a value.
     88        ArrayList all_arguments = base_container.getArguments();
     89        int argument_count = all_arguments.size();
     90        for(int j = 0; j < argument_count; j++) {
     91        Argument base_argument = (Argument) all_arguments.get(j);
     92        String base_argument_name = base_argument.getName();
     93        ///ystem.err.println("Library indicates this container should have an argument: " + base_argument_name);
     94        Argument existing_argument = (Argument) known_arguments.get(base_argument_name);
     95        // Found an existing argument. Complete its details
     96        if(existing_argument != null) {
     97            ///ystem.err.println("Found existing argument. Filling out details.");
     98            known_arguments.remove(base_argument_name);
     99            existing_argument.setDefaultValue(base_argument.getDefaultValue());
     100            existing_argument.setDescription(base_argument.getDescription());
     101            existing_argument.setOptions(base_argument.getOptions());
     102            existing_argument.setRequired(base_argument.isRequired());
     103            existing_argument.setType(base_argument.getType());
     104            existing_argument.setMinimum(base_argument.getMinimum());
     105            existing_argument.setMaximum(base_argument.getMaximum());
     106            existing_argument.setOwner(base_argument.getOwner());
     107            existing_argument.setHiddenGLI(base_argument.isHiddenGLI());
     108            add(existing_argument);
     109        }
     110        // No existing argument. Copy base_argument and add it, but set its assigned flag to false
     111        else {
     112            ///atherer.println("No such argument. Adding new, unassigned, argument.");
     113            // The trick thing is that we have to create a new element in the DOM as well.
     114            Argument new_argument = base_argument.copy();
     115            Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
     116            argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name);
     117            argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
     118            new_argument.setElement(argument_element);
     119            // All done. Add it.
     120            element.appendChild(argument_element);
     121            add(new_argument);
     122        }
     123        }
     124    }
     125   
     126    // The first time we do this, the args come from the config file and
     127    // may contain invalid args. Here we remove them from the DOM.
     128    if (!known_arguments.isEmpty()) {
     129        Set entry_set = known_arguments.entrySet();
     130        Iterator entry_iterator = entry_set.iterator();
     131        while (entry_iterator.hasNext()) {
     132        Argument arg = (Argument)((Map.Entry)entry_iterator.next()).getValue();
     133        Element e = arg.getElement();
     134        element.removeChild(e);
     135        }
     136    }
     137    }
     138
     139
     140   
     141
     142    /** Constructor used for the plugins that are in the DOMProxyList */
     143    /** Method to add an argument to this container. Only adds the argument if it isn't already present.
     144     * @param argument The <strong>Argument</strong> to add.
     145     */
     146    public void addArgument(Argument argument) {
     147    if(element == null && !contains(argument)) {
     148        add(argument);
     149        argument.setOwner(name);
     150    }
     151    }
     152
     153    /** Method to compare two containers for ordering.
     154     * @param object The container we are comparing to, as an <strong>Object</strong>.
     155     * @return An <i>int</i> specifying the container order, using values as set out in String.
     156     * @see java.lang.String#compareTo
     157     */
     158    public int compareTo(Object object) {
     159    if(object == null ) {
     160        return -1;
     161    }
     162    return toString().compareTo(object.toString());
     163    }
     164
     165    /** Method to determine if two containers are equal.
     166     * @param object The container to test against, as an <strong>Object</strong>.
     167     * @return <i>true</i> if they match (based on the equals method), <i>false</i> otherwise.
     168     */
     169
     170    public boolean equals(Object object) {
     171    return (compareTo(object) == 0);
     172    }
     173
     174    abstract public DOMProxyListEntry create(Element element);
     175    /** Method to retrieve an argument by its name.
     176     * @param name The name of the argument as a <strong>String</strong>.
     177     * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
     178     */
     179    public Argument getArgument(String name) {
     180    // The name given may still include the '-'
     181    if(name.startsWith("-")) {
     182        name = name.substring(1);
     183    }
     184    //ArrayList arguments = getArguments(true, true);
     185    ArrayList arguments = getArguments();
     186    for(int i = 0; i < arguments.size(); i++) {
     187        Argument argument = (Argument)arguments.get(i);
     188        if(argument.getName().equals(name)) {
     189        return argument;
     190        }
     191    }
     192    return null;
     193    }
     194
    40195    /** Method to retrieve the list of arguments from this container. Note that this method returns both the containers arguments plus its 'supers' arguments if any, and alphabetically orders them.
    41      * @param include_normal true to filter so normal arguments are returned
    42      * @param include_custom true to filter so custom arguments are returned
    43196     * @return the arguments within a ArrayList
    44197     */
    45     public ArrayList getArguments(boolean include_normal, boolean include_custom);
    46     /** Method to retrieve this containers custom argument string.
    47      * @return the custom arguments as a String
    48      */
    49     public String getCustom();
     198    public ArrayList getArguments() {
     199    ArrayList arguments = new ArrayList();
     200    arguments.addAll(this);
     201    if(super_container != null) {
     202        ArrayList remainder = super_container.getArguments();
     203        remainder.removeAll(arguments);
     204        arguments.addAll(remainder);
     205    }
     206    return arguments;
     207
     208    }
     209
     210    public String getDescription() {
     211    return description;
     212    }
     213
     214    public Element getElement() {
     215    return element;
     216    }
    50217    /** Method to retrieve the name associated with this argument container.
    51218     * @return the name as a String
    52219     */
    53     public String getName();
    54     /** Method to set the custom arguments string.
    55      * @param custom the new custom argument String
    56      */
    57     public void setCustom(String custom);
     220    public String getName() {
     221    if(name == null && element != null) {
     222        name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
     223    }
     224    return name;
     225    }
     226
     227    public boolean isAbstract() {
     228    return is_abstract;
     229    }
     230
     231    public boolean isAssigned() {
     232    return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
     233    }
     234
     235    public void setAssigned(boolean assigned) {
     236    if(element != null) {
     237        element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
     238    }
     239    }
     240
     241    public void setDescription(String description) {
     242    this.description = description;
     243    }
     244
     245    public void setElement(Element element) {
     246    this.element = element;
     247    }
     248
     249    public void setIsAbstract(boolean is_abstract) {
     250    this.is_abstract = is_abstract;
     251    }
     252
     253    public void setName(String name) {
     254    this.name = name;
     255    }
     256
     257    /** Method to set the value of the super_container.
     258     * @param super_container The new value of super_container as a <strong>ArgumentContainer</strong>, or <i>null</i> if this class has no inheritance.
     259     */
     260    public void setSuper(ArgumentContainer super_container) {
     261    this.super_container = super_container;
     262    }
     263
     264    public String toString()
     265    {
     266    if (element == null) {
     267        return name;
     268    }
     269
     270    if (name == null) {
     271        name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
     272    }
     273
     274    StringBuffer text;
     275    if (isAssigned()) {
     276        text = new StringBuffer(display_name);
     277    } else {
     278        text = new StringBuffer("#");
     279        text.append(display_name);
     280    }
     281    text.append(" ");
     282    text.append(name);
     283
     284    ArrayList arguments = getArguments();
     285    for (int i = 0; i < arguments.size(); i++) {
     286        Argument argument = (Argument) arguments.get(i);
     287        if (argument.isAssigned()) {
     288        text.append(" ");
     289        text.append(argument.toString());
     290        }
     291    }
     292
     293    return text.toString();
     294    }
     295
    58296}
  • trunk/gli/src/org/greenstone/gatherer/cdm/Classifier.java

    r11542 r12247  
    4343 */
    4444public class Classifier
    45     extends ArrayList
    46     implements ArgumentContainer, Comparable, DOMProxyListEntry, Serializable {
     45    extends ArgumentContainer {
     46   
    4747
    4848    static final public String CLASSIFIER_PREFIX = "CL";
    49 
    50     private boolean is_abstract = false;
    51 
    52     /** A reference to the classifier that this one inherits from. */
    53     private Classifier super_classifier = null;
    54     /** The element this classifier is based upon. */
    55     private Element element;
    56     /** A description of this classifier. */
    57     private String description = null;
    58     /** The name of the classifier as it would appear in the collect.cfg file. */
    59     private String name = null;
    60     /** This string is filled out the first time this classifier is created, and remains unchanged there-after. It is used to match up with Format commands that may not yet have been instantiated (and thus only have offline references along the lines of 'CL1' to figure out what Classifier they want.) */
    61     private String old_position_string = null;
    6249
    6350    /** Constructor used only in DOMProxyListModel initializations.
     
    6552    public Classifier() {
    6653    }
    67 
     54   
    6855    public Classifier(Element element, Classifier base_classifier) {
    69     super();
    70     this.element = element;
    71     this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
    72     ///atherer.println("Establishing Classifier: " + name);
    73     // Parse in any argument options for this classifier, keeping a list of the ones found
    74     HashMap known_arguments = new HashMap();
    75     NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
    76     int option_elements_length = option_elements.getLength();
    77     for(int i = 0; i < option_elements_length; i++) {
    78         Element option_element = (Element) option_elements.item(i);
    79         Argument argument = new Argument(option_element);
    80         ///atherer.println("Rebuilding existing argument: " + argument.getName());
    81         argument.setOwner(name);
    82         add(argument);
    83         known_arguments.put(argument.getName(), argument);
    84     }
    85     // If a base classifier was given
    86     if(base_classifier != null) {
    87         // Copy the details, and add a reference to whatever base_classifiers super classifier is.
    88         description = base_classifier.getDescription();
    89         // Now search through the 'dummy' arguments belonging to the base classifier. For each found, if it is already assigned, fill out further details such as type. If any are found that are not already assigned for this classifier, copy them and add them, but without a value.
    90         ArrayList all_arguments = base_classifier.getArguments(true, true);
    91         int argument_count = all_arguments.size();
    92         for(int j = 0; j < argument_count; j++) {
    93         Argument base_argument = (Argument) all_arguments.get(j);
    94         String base_argument_name = base_argument.getName();
    95         ///atherer.println("Library indicates this classifier should have an argument: " + base_argument_name);
    96         Argument existing_argument = (Argument) known_arguments.get(base_argument_name);
    97         // Found an existing argument. Complete its details
    98         if(existing_argument != null) {
    99             ///atherer.println("Found existing argument. Filling out details.");
    100             existing_argument.setCustomArgument(false);
    101             existing_argument.setDefaultValue(base_argument.getDefaultValue());
    102             existing_argument.setDescription(base_argument.getDescription());
    103             existing_argument.setOptions(base_argument.getOptions());
    104             existing_argument.setRequired(base_argument.isRequired());
    105             existing_argument.setType(base_argument.getType());
    106             existing_argument.setOwner(base_argument.getOwner());
    107             existing_argument.setHiddenGLI(base_argument.isHiddenGLI());
    108         }
    109         // No existing argument. Copy base_argument and add it, but do not set its assigned flag. That should be set the first time its changed by the user.
    110         else {
    111             ///atherer.println("No such argument. Adding new, unassigned, argument.");
    112             // The trick thing is that we have to create a new element in the DOM as well.
    113             Argument new_argument = base_argument.copy();
    114             //new_argument.setOwner(name);
    115             Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
    116             argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name);
    117             argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
    118             argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.FALSE_STR);
    119             new_argument.setElement(argument_element);
    120             // All done. Add it.
    121             element.appendChild(argument_element);
    122             add(new_argument);
    123         }
    124         }
    125     }
    126     old_position_string = getPositionString();
    127     }
    128 
    129     /** Constructor.
    130      * @param name The name of this classifier as a <strong>String</strong>.
    131      * @param description A description of this classifier as a <strong>String</strong>.
    132      * @param super_classifier The super class of this classifier, as a <strong>Classifier</strong>.
    133      */
    134     public Classifier(String name, String description, Classifier super_classifier) {
    135     super();
    136     this.description = description;
    137     this.name = name;
    138     this.super_classifier = super_classifier;
    139     }
    140 
    141     /** Method to add an argument to this classifier. Only adds the argument if it isn't already present.
    142      * @param argument The <strong>Argument</strong> to add.
    143      */
    144     public void addArgument(Argument argument) {
    145     if(element == null && !contains(argument)) {
    146         add(argument);
    147         argument.setOwner(name);
    148     }
    149     }
    150 
    151     /** Method to compare two classifiers for ordering.
    152      * @param object The classifier we are comparing to, as an <strong>Object</strong>.
    153      * @return An <i>int</i> specifying the classifier order, using values as set out in String.
    154      * @see java.lang.String#compareTo
    155      */
    156     public int compareTo(Object object) {
    157     if(object == null) {
    158         return -1;
    159     }
    160     return toString().compareTo(object.toString());
     56    super(element, base_classifier);
    16157    }
    16258
     
    16965    Classifier base_classifier = CollectionDesignManager.classifier_manager.getBaseClassifier(classifier_name);
    17066    Classifier classifier = new Classifier(element, base_classifier);
     67    if (base_classifier == null) {
     68        // this is kind of a place holder classifier for one which is invalid. It won't get written out to the config file when its next saved.
     69        classifier.setAssigned(false);
     70    }
    17171    base_classifier = null;
    17272    classifier_name = null;
    17373    return classifier;
    17474    }
    175 
    176     /** Method to determine if two classifiers are equal.
    177      * @param object The classifier to test against, as an <strong>Object</strong>.
    178      * @return <i>true</i> if the classifier names match, <i>false</i> otherwise.
    179      */
    180     public boolean equals(Object object) {
    181     return (compareTo(object) == 0);
    182     }
    183 
    184     /** Method to retrieve an argument by its name.
    185      * @param name The name of the argument as a <strong>String</strong>.
    186      * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
    187      */
    188     public Argument getArgument(String name) {
    189     // The name given may still include the '-'
    190     if(name.startsWith("-")) {
    191         name = name.substring(1);
    192     }
    193     ArrayList arguments = getArguments(true, true);
    194     for(int i = 0; i < arguments.size(); i++) {
    195         Argument argument = (Argument)arguments.get(i);
    196         if(argument.getName().equals(name)) {
    197         return argument;
    198         }
    199     }
    200     return null;
    201     }
    202 
    203      /** Retrieve all of the arguments available to this base classifier, including its super classifiers arguments. Some complexity is added by allowing the caller to choose whether they want normal arguments, custom arguments, or both.
    204       * @return an ArrayList of all of the arguments, starting with those for this classifier and ending with the arguments for basplug or similiar root classifier
    205       */
    206     public ArrayList getArguments(boolean include_normal, boolean include_custom) {
    207     ArrayList arguments = new ArrayList();
    208     if(include_normal && include_custom) {
    209         arguments.addAll(this);
    210     }
    211     else {
    212         int size = size();
    213         for(int i = 0; i < size; i++) {
    214         Argument argument = (Argument) get(i);
    215         if(argument.isCustomArgument()) {
    216             if(include_custom && !arguments.contains(argument)) {
    217             arguments.add(argument);
    218             }
    219         }
    220         else {
    221             if(include_normal && !arguments.contains(argument)) {
    222             arguments.add(argument);
    223             }
    224         }
    225         argument = null;
    226         }
    227     }
    228     if(super_classifier != null) {
    229         ArrayList remainder = super_classifier.getArguments(include_normal, include_custom);
    230         remainder.removeAll(arguments);
    231         arguments.addAll(remainder);
    232     }
    233     return arguments;
    234     }
    235 
    236     /** Method to retrieve a classifiers custom argument information. Custom arguments are defined to be those that have not got matching arguments in the base reference classifier from the library. Of course if there is no base classifier then all arguments are considered to be custom.
    237      * @return the custom arguments as a String
    238      */
    239     public String getCustom() {
    240     StringBuffer custom_text = new StringBuffer();
    241     // Retrieve all of the arguments, and append any that are custom into one long string
    242     ArrayList arguments = getArguments(false, true);
    243     int arguments_size = arguments.size();
    244     boolean first = true;
    245     for(int i = 0; i < arguments_size; i++) {
    246         Argument argument = (Argument) arguments.get(i);
    247         if(argument.isAssigned()) {
    248         if(!first) {
    249             custom_text.append(" ");
    250         }
    251         custom_text.append(argument.toString());
    252         first = false;
    253         }
    254     }
    255     return custom_text.toString();
    256     }
    257 
    258     public String getDescription() {
    259     return description;
    260     }
    261 
    262     public Element getElement() {
    263     return element;
    264     }
    265 
    266     /** Method to retrieve a classifiers name.
    267      * @return A <strong>String</strong> containing the classifiers name.
    268      */
    269     public String getName() {
    270     if(name == null && element != null) {
    271         name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
    272     }
    273     return name;
    274     }
    275 
    276     /* private String getOldPositionString() {
    277     return old_position_string;
    278     } */
    27975
    28076    /** Generate the string showing this classifiers position. */
     
    29187    }
    29288
    293     public boolean isAbstract() {
    294     return is_abstract;
    295     }
    296 
    297     public boolean isAssigned() {
    298     return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
    299     }
    300 
    301     public void setAssigned(boolean assigned) {
    302     if(element != null) {
    303         element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
    304     }
    305     }
    306 
    307     /** Set the custom arguments. This turns out to be quite tricky. We must parse in the string, searching for arguments (for that we use a handy method in CollectionConfiguration). Next, for each argument, we check if we already know about it. If so we update its value, otherwise we create a new argument and assign it (must assign!).
    308      * @param custom_str the custom arguments all splodged together in one String
    309      */
    310     public void setCustom(String custom_str) {
    311     HashMap raw_arguments = CollectionConfiguration.parseArguments(new CommandTokenizer(custom_str));
    312     ArrayList custom_arguments = getArguments(false, true);
    313     int size = custom_arguments.size();
    314     for(int i = 0; i < size; i++) {
    315         Argument argument = (Argument) custom_arguments.get(i);
    316         String original_argument_name = StaticStrings.MINUS_CHARACTER + argument.getName();
    317         if(raw_arguments.containsKey(original_argument_name)) {
    318         // Set as assigned
    319         argument.setAssigned(true);
    320         String argument_value = (String)raw_arguments.remove(original_argument_name);
    321         if(argument_value != null) {
    322             argument.setValue(argument_value);
    323             argument_value = null;
    324         }
    325         }
    326         // We've removed it from our custom statement, so unassign
    327         else {
    328         argument.setAssigned(false);
    329         }
    330         argument = null;
    331     }
    332     // Any left over, add to the classifier
    333     Iterator argument_names = raw_arguments.keySet().iterator();
    334     while(argument_names.hasNext()) {
    335         String argument_name = (String) argument_names.next();
    336         String argument_value = (String) raw_arguments.get(argument_name);
    337         // The tricky thing is that we have to create a new element in the DOM as well.
    338         Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
    339         argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, argument_name.substring(1));
    340         argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
    341         argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.TRUE_STR);
    342         Argument argument = new Argument(argument_element);
    343         argument_name = null;
    344         if(argument_value != null) {
    345         argument.setValue(argument_value);
    346         argument_value = null;
    347         }
    348         // All done. Add it.
    349         element.appendChild(argument_element);
    350         add(argument);
    351         argument_element = null;
    352     }
    353     raw_arguments = null;
    354     }
    355 
    356     /** Method to set the value of desc.
    357      * @param description The new value of desc as a <strong>String</strong>.
    358      */
    359     public void setDescription(String description) {
    360     this.description = description;
    361     }
    362 
    363     public void setElement(Element element) {
    364     this.element = element;
    365     }
    366 
    367     public void setIsAbstract(boolean is_abstract) {
    368     this.is_abstract = is_abstract;
    369     }
    370 
    371     /** Method to set the value of name.
    372      * @param name The new value of name as a <strong>String</strong>.
    373      */
    374     public void setName(String name) {
    375     this.name = name;
    376     }
    377 
    378     /** Method to set the value of the super_classifier.
    379      * @param super_classifier The new value of super_classifier as a <strong>Classifier</strong>, or <i>null</i> if this class has no inheritance.
    380      */
    381     public void setSuper(Classifier super_classifier) {
    382     this.super_classifier = super_classifier;
    383     }
    384 
    385 
    386     /** Method to print out this classifier as it would appear to the user in the interface
    387      * @return A <strong>String</strong> containing a single classifier command.
    388      */
    389     public String toString()
    390     {
    391     if (element == null) {
    392         return name;
    393     }
    394 
    395     if (name == null) {
    396         name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
    397     }
    398 
    399     StringBuffer text = new StringBuffer(StaticStrings.CLASSIFY_STR);
    400     text.append(" ");
    401     text.append(name);
    402 
    403     ArrayList arguments = getArguments(true, true);
    404     for (int i = 0; i < arguments.size(); i++) {
    405         Argument argument = (Argument) arguments.get(i);
    406         if (argument.isAssigned()) {
    407         text.append(" ");
    408         text.append(argument.toString());
    409         }
    410     }
    411 
    412     return text.toString();
    413     }
    41489}
  • trunk/gli/src/org/greenstone/gatherer/cdm/Plugin.java

    r11378 r12247  
    3535/** This class is responsible for storing information from a parsed pluginfo call in such a way that it allows easy access to parsed details for the purposes of user design and specification of plugins. */
    3636public class Plugin
    37     extends ArrayList
    38     implements ArgumentContainer, Comparable, DOMProxyListEntry, Serializable {
    39     /** The DOM Element this assigned Plugin is modelled on. */
    40     private Element element;
    41     private boolean does_explode_metadata_databases = false; // Only for Base
    42     private boolean is_abstract = false; // Only for Base
    43     /** The parent Plugin this one inherits from, if any. */
    44     private Plugin super_plugin; // Only for Base
    45     private String description; // Only for Base
    46     private String name; // Only for Base
    47 
     37    extends ArgumentContainer {
     38    private boolean does_explode_metadata_databases = false;
     39   
    4840    /** Constructor used in DOMProxyListModel initializations, and Library Level. Used for Base plugins (those in the list of available plugins, not ones that are in the DOMProxyList)
    4941     */
     
    5143    }
    5244   
    53     /** Constructor used for the plugins that are in the DOMProxyList */
    54     // Every time the list of plugins in the assigned plugins box changes, (eg plugin added or removed, not when plugin configured), the plugins seem to be regenerated, using the element from the old plugin. All known args get added to the element the first time this happens - we need to add them to the arguments list in the order they are found in the base plugins though, not this order.
    5545    public Plugin(Element element, Plugin base_plugin) {
    56     super();
    57     this.element = element;
    58     this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
    59     //DebugStream.println("Establishing Plugin: " + name);
    60     // Parse in any argument options for this plugin, keeping a list of the ones found
    61     HashMap known_arguments = new HashMap();
    62     NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
    63     int option_elements_length = option_elements.getLength();
    64     for(int i = 0; i < option_elements_length; i++) {
    65         Element option_element = (Element) option_elements.item(i);
    66         Argument argument = new Argument(option_element);
    67         //DebugStream.println("Rebuilding existing argument: " + argument.getName());
    68         known_arguments.put(argument.getName(), argument);
    69         if (base_plugin == null) {
    70         // otherwise they will all be added in the next step
    71         argument.setOwner(name);
    72         add(argument);
    73         }
    74     }
    75     // If a base plugin was given
    76     if(base_plugin != null) {
    77         //DebugStream.println("Based on previous plugin.");
    78         // Copy the details, and add a reference to whatever base_plugins super plugin is. ??
    79         description = base_plugin.getDescription();
    80         // Now search through the 'dummy' arguments belonging to the base plugin. For each found, if it is already assigned, fill out further details such as type. If any are found that are not already assigned for this plugin, copy them and add them, but without a value.
    81         ArrayList all_arguments = base_plugin.getArguments(true, true);
    82         int argument_count = all_arguments.size();
    83         for(int j = 0; j < argument_count; j++) {
    84         Argument base_argument = (Argument) all_arguments.get(j);
    85         String base_argument_name = base_argument.getName();
    86         //DebugStream.println("Library indicates this plugin should have an argument: " + base_argument_name);
    87         Argument existing_argument = (Argument) known_arguments.get(base_argument_name);
    88         // Found an existing argument. Complete its details
    89         if(existing_argument != null) {
    90             //DebugStream.println("Found existing argument. Filling out details.");
    91             existing_argument.setCustomArgument(false);
    92             existing_argument.setDefaultValue(base_argument.getDefaultValue());
    93             existing_argument.setDescription(base_argument.getDescription());
    94             existing_argument.setOptions(base_argument.getOptions());
    95             existing_argument.setRequired(base_argument.isRequired());
    96             existing_argument.setType(base_argument.getType());
    97             existing_argument.setMinimum(base_argument.getMinimum());
    98             existing_argument.setMaximum(base_argument.getMaximum());
    99             existing_argument.setOwner(base_argument.getOwner());
    100             existing_argument.setHiddenGLI(base_argument.isHiddenGLI());
    101             add(existing_argument);
    102            
    103         }
    104         // No existing argument. Copy base_argument and add it, but do not set its assigned flag. That should be set the first time its changed by the user.
    105         else {
    106             //DebugStream.println("No such argument. Adding new, unassigned, argument.");
    107             // The trick thing is that we have to create a new element in the DOM as well.
    108             Argument new_argument = base_argument.copy();
    109             Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
    110             argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name);
    111             argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
    112             argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.FALSE_STR);
    113             // All done. Add it.
    114             // if we are not in the first collection opened, then this
    115             /// will belong to a different document, so need to import
    116             // it.
    117             Node new_element = element.getOwnerDocument().importNode(argument_element, true);
    118             new_argument.setElement((Element) new_element);
    119             element.appendChild(new_element);
    120             add(new_argument);
    121         }
    122         }
    123     }
    124     }
    125 
    126     /** Method to add an argument to this base plugin. Only adds the argument if it isn't already present, and only if this is a base plugin (ie not based on DOM).
    127      * @param argument the Argument to add
    128      */
    129     public void addArgument(Argument argument) {
    130     if(element == null && !contains(argument)) {
    131         add(argument);
    132         argument.setOwner(name);
    133     }
     46    super(element, base_plugin);
    13447    }
    13548
    13649    /** Method to compare two plugins for ordering.
    137      * @param object The plugin we are comparing to, as an <strong>Object</strong>.
    138      * @return An <i>int</i> specifying the plugin order, using values as set out in <strong>String</strong>.
    139      * @see java.lang.String#compareTo
     50     * We override the base method cos we compare by plugin name rather than toString()
    14051     */
    14152    public int compareTo(Object object) {
     
    14556    return -1;
    14657    }
    147 
     58   
    14859    /** The assigned plugin constructor.
    14960     * @param element the DOM Element this plugin is based upon
     
    15465    Plugin base_plugin = CollectionDesignManager.plugin_manager.getBasePlugin(plugin_name);
    15566    Plugin plugin = new Plugin(element, base_plugin);
     67    if (base_plugin == null) {
     68        plugin.setAssigned(false);
     69    }
    15670    base_plugin = null;
    15771    plugin_name = null;
     
    17589    {
    17690    // Check the filename against the plugin's block_exp value
    177     ArrayList arguments = getArguments(true, true);
     91    //ArrayList arguments = getArguments(true, true);
     92    ArrayList arguments = getArguments();
    17893    for (int i = 0; i < arguments.size(); i++) {
    17994        Argument argument = (Argument) arguments.get(i);
     
    214129    {
    215130    // Check the filename against the plugin's process_exp value
    216     ArrayList arguments = getArguments(true, true);
     131    //ArrayList arguments = getArguments(true, true);
     132    ArrayList arguments = getArguments();
    217133    for (int i = 0; i < arguments.size(); i++) {
    218134        Argument argument = (Argument) arguments.get(i);
     
    248164    }
    249165
    250 
    251     /** Method to determine if two plugins are equal.
    252      * @param object The plugin to test against, as an <strong>Object</strong>.
    253      * @return <i>true</i> if the plugin names match, <i>false</i> otherwise.
    254      */
    255     public boolean equals(Object object) {
    256     return (compareTo(object) == 0);
    257     }
    258 
    259     /** Method to retrieve an argument by its name.
    260      * @param name The name of the argument as a <strong>String</strong>.
    261      * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
    262      */
    263     public Argument getArgument(String name) {
    264     // The name given may still include the '-'
    265     if(name.startsWith("-")) {
    266         name = name.substring(1);
    267     }
    268     ArrayList arguments = getArguments(true, true);
    269     for(int i = 0; i < arguments.size(); i++) {
    270         Argument argument = (Argument)arguments.get(i);
    271         if(argument.getName().equals(name)) {
    272         return argument;
    273         }
    274     }
    275     return null;
    276     }
    277 
    278     /** Retrieve all of the arguments available to this base plugin, including its super plugins arguments. Some complexity is added by allowing the caller to choose whether they want normal arguments, custom arguments, or both.
    279      * @return an ArrayList of all of the arguments, starting with those for this plugin and ending with the arguments for basplug or similiar root plugin
    280      */
    281     public ArrayList getArguments(boolean include_normal, boolean include_custom) {
    282     ArrayList arguments = new ArrayList();
    283     if(include_normal && include_custom) {
    284         arguments.addAll(this);
    285     }
    286     else {
    287         int size = size();
    288         for(int i = 0; i < size; i++) {
    289         Argument argument = (Argument) get(i);
    290         if(argument.isCustomArgument()) {
    291             if(include_custom && !arguments.contains(argument)) {
    292             arguments.add(argument);
    293             }
    294         }
    295         else {
    296             if(include_normal && !arguments.contains(argument)) {
    297             arguments.add(argument);
    298             }
    299         }
    300         argument = null;
    301         }
    302     }
    303     if(super_plugin != null) {
    304         ArrayList remainder = super_plugin.getArguments(include_normal, include_custom);
    305         remainder.removeAll(arguments);
    306         arguments.addAll(remainder);
    307     }
    308     return arguments;
    309     }
    310 
    311     /** Method to retrieve a plugins custom argument information. Custom arguments are defined to be those that have not got matching arguments in the base reference plugin from the library. Of course if there is no base plugin then all arguments are considered to be custom.
    312      * @return the custom arguments as a String
    313      */
    314     public String getCustom() {
    315     StringBuffer custom_text = new StringBuffer();
    316     // Retrieve all of the arguments, and append any that are custom into one long string
    317     ArrayList arguments = getArguments(false, true);
    318     int arguments_size = arguments.size();
    319     boolean first = true;
    320     for(int i = 0; i < arguments_size; i++) {
    321         Argument argument = (Argument) arguments.get(i);
    322         if(argument.isAssigned()) {
    323         if(!first) {
    324             custom_text.append(" ");
    325         }
    326         custom_text.append(argument.toString());
    327         first = false;
    328         }
    329     }
    330     return custom_text.toString();
    331     }
    332 
    333     public String getDescription() {
    334     return description;
    335     }
    336 
    337     public Element getElement() {
    338     return element;
    339     }
    340 
    341     /** Method to retrieve a plugins name.
    342      * @return A <strong>String</strong> containing the plugins name.
    343      */
    344     public String getName() {
    345     if(name == null && element != null) {
    346         name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
    347     }
    348     return name;
    349     }
    350 
    351     public boolean isAbstract() {
    352     return is_abstract;
    353     }
    354 
    355     public boolean isAssigned() {
    356     return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
    357     }
    358 
    359166    public boolean isSeparator() {
    360167    return (element != null && element.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
    361     }
    362 
    363     public void setAssigned(boolean assigned) {
    364     if(element != null) {
    365         element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
    366     }
    367     }
    368 
    369     /** Set the custom arguments. This turns out to be quite tricky. We must parse in the string, searching for arguments (for that we use a handy method in CollectionConfiguration). Next, for each argument, we check if we already know about it. If so we update its value, otherwise we create a new argument and assign it (must assign!).
    370      * @param custom_str the custom arguments all splodged together in one String
    371      */
    372     public void setCustom(String custom_str) {
    373     HashMap raw_arguments = CollectionConfiguration.parseArguments(new CommandTokenizer(custom_str));
    374     ArrayList custom_arguments = getArguments(false, true);
    375     int size = custom_arguments.size();
    376     for(int i = 0; i < size; i++) {
    377         Argument argument = (Argument) custom_arguments.get(i);
    378         String original_argument_name = StaticStrings.MINUS_CHARACTER + argument.getName();
    379         if(raw_arguments.containsKey(original_argument_name)) {
    380         // Set as assigned
    381         argument.setAssigned(true);
    382         String argument_value = (String)raw_arguments.remove(original_argument_name);
    383         if(argument_value != null) {
    384             argument.setValue(argument_value);
    385             argument_value = null;
    386         }
    387         }
    388         // We've removed it from our custom statement, so unassign
    389         else {
    390         argument.setAssigned(false);
    391         }
    392         argument = null;
    393     }
    394     // Any left over, add to the plugin
    395     Iterator argument_names = raw_arguments.keySet().iterator();
    396     while(argument_names.hasNext()) {
    397         String argument_name = (String) argument_names.next();
    398         String argument_value = (String) raw_arguments.get(argument_name);
    399         // The tricky thing is that we have to create a new element in the DOM as well.
    400         Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
    401         argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, argument_name.substring(1));
    402         argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
    403         argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.TRUE_STR);
    404         Argument argument = new Argument(argument_element);
    405         argument_name = null;
    406         if(argument_value != null) {
    407         argument.setValue(argument_value);
    408         argument_value = null;
    409         }
    410         // All done. Add it.
    411         element.appendChild(argument_element);
    412         add(argument);
    413         argument_element = null;
    414     }
    415     raw_arguments = null;
    416     }
    417 
    418     /** Method to set the value of desc.
    419      * @param description The new value of desc as a <strong>String</strong>.
    420      */
    421     public void setDescription(String description) {
    422     this.description = description;
    423168    }
    424169
     
    427172    }
    428173
    429     public void setElement(Element element) {
    430     this.element = element;
    431     }
    432 
    433     public void setIsAbstract(boolean is_abstract) {
    434     this.is_abstract = is_abstract;
    435     }
    436 
    437     /** Method to set the value of name.
    438      * @param name The new value of name as a <strong>String</strong>.
    439      */
    440     public void setName(String name) {
    441     this.name = name;
    442     }
    443 
    444     /** Method to set the value of the super_plugin.
    445      * @param super_plugin The new value of super_plugin as a <strong>Plugin</strong>, or <i>null</i> if this class has no inheritance.
    446      */
    447     public void setSuper(Plugin super_plugin) {
    448     this.super_plugin = super_plugin;
    449     }
    450 
    451     /** Method to print out this plugin as it would appear as a command within the collection configuration file.
    452      * @return A <strong>String</strong> containing a single plugin command.
    453      */
    454     public String toString() {
    455     if(element != null) {
    456         if(name == null) {
    457         name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
    458         }
    459         StringBuffer text = new StringBuffer(StaticStrings.PLUGIN_STR);
    460         text.append(" ");
    461         text.append(name);
    462         text.append(" ");
    463         ArrayList arguments = getArguments(true, true);
    464         int arguments_size = arguments.size();
    465         for(int i = 0; i < arguments_size; i++) {
    466         Argument argument = (Argument)arguments.get(i);
    467         if(argument.isAssigned()) {
    468             text.append(argument.toString());
    469             text.append(" ");
    470         }
    471         argument = null;
    472         }
    473         return text.substring(0, text.length() - 1);
    474     }
    475     // Basic Plugin
    476     else {
    477         return name;
    478     }
    479     }
    480174}
Note: See TracChangeset for help on using the changeset viewer.