Ignore:
Timestamp:
2003-07-15T13:55:22+12:00 (21 years ago)
Author:
jmt12
Message:

Major CDM rewrite so it uses DOM.

File:
1 edited

Legend:

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

    r4675 r4932  
    66 * University of Waikato, New Zealand.
    77 *
    8  * <BR><BR>
    9  *
    108 * Author: John Thompson, Greenstone Digital Library, University of Waikato
    119 *
    12  * <BR><BR>
    13  *
    1410 * Copyright (C) 1999 New Zealand Digital Library Project
    15  *
    16  * <BR><BR>
    1711 *
    1812 * This program is free software; you can redistribute it and/or modify
     
    2115 * (at your option) any later version.
    2216 *
    23  * <BR><BR>
    24  *
    2517 * This program is distributed in the hope that it will be useful,
    2618 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2719 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2820 * GNU General Public License for more details.
    29  *
    30  * <BR><BR>
    3121 *
    3222 * You should have received a copy of the GNU General Public License
     
    3525 *########################################################################
    3626 */
    37 
    38  
    39 
    40 
    41 
    42 
    4327package org.greenstone.gatherer.cdm;
    44 /**
    45  * Title:        The Gatherer<br>
    46  * Description:  The Gatherer: a tool for gathering and enriching digital collections.<br>
    47  * Copyright:    Copyright (c) 2001<br>
    48  * Company:      The University of Waikato<br>
    49  * First Coded:  01/05/02
    50  * @author John Thompson, Greenstone Digital Libraries
    51  * @version 2.1
    52  */
    53 import java.io.Serializable;
    54 import java.util.ArrayList;
    55 import java.util.Collections;
     28
     29import java.io.*;
     30import java.util.*;
     31import org.greenstone.gatherer.Gatherer;
    5632import org.greenstone.gatherer.cdm.Argument;
    5733import org.greenstone.gatherer.cdm.ArgumentContainer;
     34import org.greenstone.gatherer.cdm.DOMProxyListEntry;
     35import org.greenstone.gatherer.util.StaticStrings;
     36import org.w3c.dom.*;
    5837/** 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. */
    59 // ####################################################################################
    60 // Optimization                          Saving
    61 // ####################################################################################
    62 // Vector -> ArrayList                   + Memory, + Processor (pos. - Processor)
    63 // ####################################################################################
    6438public class PlugIn
    65     extends ArrayList
    66     implements ArgumentContainer, Comparable, Serializable {
    67     /** A reference to the plugin that this one inherits from. */
    68     private PlugIn super_plugin = null;
    69     /** A string of custom arguments to pass to the plugin. */
    70     private String custom = null;
    71     /** A description of this plugin. */
    72     private String desc = null;
    73     /** The name of the plugin as it would appear in the collect.cfg file. */
    74     private String name = null;
    75     /** Default Constructor.
     39    extends ArrayList
     40    implements ArgumentContainer, Comparable, DOMProxyListEntry, Serializable {
     41    /** The DOM Element this assigned PlugIn is modelled on. */
     42    private Element element;
     43    /** The parent PlugIn this one inherits from, if any. */
     44    private PlugIn super_plugin;
     45    private String description;
     46    private String name;
     47
     48    /** Constructor used only in DOMProxyListModel initializations.
    7649     */
    7750    public PlugIn() {
     51    }
     52
     53    public PlugIn(Element element, PlugIn base_plugin) {
    7854    super();
    79     }
    80     /** Constructor.
    81       * @param name The name of this plugin as a <strong>String</strong>.
    82       * @param desc A description of this plugin as a <strong>String</strong>.
    83       * @param super_plugin The super class of this plugin, as a <strong>PlugIn</strong>.
    84       */
    85     public PlugIn(String name, String desc, PlugIn super_plugin) {
     55    this.element = element;
     56    this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
     57    ///atherer.println("Establishing Plugin: " + name);
     58    // Parse in any argument options for this plugin, keeping a list of the ones found
     59    HashMap known_arguments = new HashMap();
     60    NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
     61    int option_elements_length = option_elements.getLength();
     62    for(int i = 0; i < option_elements_length; i++) {
     63        Element option_element = (Element) option_elements.item(i);
     64        Argument argument = new Argument(option_element);
     65        ///atherer.println("Rebuilding existing argument: " + argument.getName());
     66        argument.setOwner(name);
     67        add(argument);
     68        known_arguments.put(argument.getName(), argument);
     69    }
     70    // If a base plugin was given
     71    if(base_plugin != null) {
     72        // Copy the details, and add a reference to whatever base_plugins super plugin is.
     73        description = base_plugin.getDescription();
     74        // 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.
     75        ArrayList all_arguments = base_plugin.getArguments(true, true);
     76        int argument_count = all_arguments.size();
     77        for(int j = 0; j < argument_count; j++) {
     78        Argument base_argument = (Argument) all_arguments.get(j);
     79        String base_argument_name = base_argument.getName();
     80        ///atherer.println("Library indicates this plugin should have an argument: " + base_argument_name);
     81        Argument existing_argument = (Argument) known_arguments.get(base_argument_name);
     82        // Found an existing argument. Complete its details
     83        if(existing_argument != null) {
     84            ///atherer.println("Found existing argument. Filling out details.");
     85            existing_argument.setCustomArgument(false);
     86            existing_argument.setDefaultValue(base_argument.getDefaultValue());
     87            existing_argument.setDescription(base_argument.getDescription());
     88            existing_argument.setOptions(base_argument.getOptions());
     89            existing_argument.setRequired(base_argument.isRequired());
     90            existing_argument.setType(base_argument.getType());
     91        }
     92        // 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.
     93        else {
     94            ///atherer.println("No such argument. Adding new, unassigned, argument.");
     95            // The trick thing is that we have to create a new element in the DOM as well.
     96            Argument new_argument = base_argument.copy();
     97            Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
     98            argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name);
     99            argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
     100            argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.FALSE_STR);
     101            new_argument.setElement(argument_element);
     102            // All done. Add it.
     103            element.appendChild(argument_element);
     104            add(new_argument);
     105        }
     106        }
     107    }
     108    }
     109
     110    /** This constructor is only used for library level Plugins.
     111     * @param name
     112     * @param description
     113     * @param super_plugin
     114     */
     115    public PlugIn(String name, String description, PlugIn super_plugin) {
    86116    super();
    87     this.desc = desc;
     117    this.description = description;
    88118    this.name = name;
    89119    this.super_plugin = super_plugin;
    90120    }
    91     /** Method to add an argument to this plugin. Only adds the argument if it isn't already present.
    92       * @param argument The <strong>Argument</strong> to add.
    93       */
     121
     122    /** 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).
     123     * @param argument the Argument to add
     124     */
    94125    public void addArgument(Argument argument) {
    95     if(!contains(argument)) {
     126    if(element == null && !contains(argument)) {
    96127        add(argument);
    97128        argument.setOwner(name);
    98129    }
    99130    }
     131
    100132    /** Method to compare two plugins for ordering.
    101       * @param object The plugin we are comparing to, as an <strong>Object</strong>.
    102       * @return An <i>int</i> specifying the plugin order, using values as set out in <strong>String</strong>.
    103       * @see java.lang.String#compareTo
    104       */
     133     * @param object The plugin we are comparing to, as an <strong>Object</strong>.
     134     * @return An <i>int</i> specifying the plugin order, using values as set out in <strong>String</strong>.
     135     * @see java.lang.String#compareTo
     136     */
    105137    public int compareTo(Object object) {
    106     if(object != null && object instanceof PlugIn) {
    107         PlugIn plugin = (PlugIn) object;
    108         return name.compareTo(plugin.getName());
     138    if(object instanceof PlugIn) {
     139        return name.compareTo(((PlugIn)object).getName());
    109140    }
    110141    return -1;
    111142    }
    112     /** This method produces a deep copy of this plugin. Note that this also creates a new copy of each of the super classes of plugins as well. This is the way it should be, as each assigned plugin may have different values for the higher plugins (such as BasPlug).
    113       * @return A newly created <strong>PlugIn</strong> with the same details and <strong>Argument</strong>s as this one.
    114       */
    115     public PlugIn copy() {
    116     PlugIn copy = null;
    117     if(super_plugin == null) {
    118         copy = new PlugIn(name, desc, null);
    119     }
    120     else {
    121         copy = new PlugIn(name, desc, super_plugin.copy());
    122     }
    123     for(int i = 0; i < size(); i++) {
    124         copy.addArgument(((Argument)get(i)).copy());
    125     }
    126     return copy;
    127     }
     143
     144    /** The assigned plugin constructor.
     145     * @param element the DOM Element this plugin is based upon
     146     * @param base_plugin the PlugIn from the stored library showing details about this plugin, may be null
     147     */
     148    public DOMProxyListEntry create(Element element) {
     149    String plugin_name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
     150    // Determine the base plugin from the plugin name
     151    PlugIn base_plugin = CollectionDesignManager.plugin_manager.getBasePlugIn(plugin_name);
     152    PlugIn plugin = new PlugIn(element, base_plugin);
     153    base_plugin = null;
     154    plugin_name = null;
     155    return plugin;
     156    }
     157
    128158    /** Method to determine if two plugins are equal.
    129       * @param object The plugin to test against, as an <strong>Object</strong>.
    130       * @return <i>true</i> if the plugin names match, <i>false</i> otherwise.
    131       */
     159     * @param object The plugin to test against, as an <strong>Object</strong>.
     160     * @return <i>true</i> if the plugin names match, <i>false</i> otherwise.
     161     */
    132162    public boolean equals(Object object) {
    133     if(object != null && compareTo(object) == 0) {
    134         return true;
    135     }
    136     return false;
    137     }
     163    return (compareTo(object) == 0);
     164    }
     165
    138166    /** Method to retrieve an argument by its name.
    139       * @param name The name of the argument as a <strong>String</strong>.
    140       * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
    141       */
     167     * @param name The name of the argument as a <strong>String</strong>.
     168     * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
     169     */
    142170    public Argument getArgument(String name) {
    143171    // The name given may still include the '-'
     
    145173        name = name.substring(1);
    146174    }
    147     ArrayList arguments = getArguments();
     175    ArrayList arguments = getArguments(true, true);
    148176    for(int i = 0; i < arguments.size(); i++) {
    149177        Argument argument = (Argument)arguments.get(i);
     
    154182    return null;
    155183    }
    156     /** Method to retrieve all of the arguments available to a plugin, including both specific and general ones.
    157       * @return A <strong>Hashtable</strong> of arguments, with <name> -> <argument> entries.
    158       */
    159     public ArrayList getArguments() {
    160     ArrayList all_arguments = new ArrayList(this);
    161     Collections.sort(all_arguments);
     184
     185    /** 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.
     186     * @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
     187     */
     188    public ArrayList getArguments(boolean include_normal, boolean include_custom) {
     189    ArrayList arguments = new ArrayList();
     190    if(include_normal && include_custom) {
     191        arguments.addAll(this);
     192    }
     193    else {
     194        int size = size();
     195        for(int i = 0; i < size; i++) {
     196        Argument argument = (Argument) get(i);
     197        if(argument.isCustomArgument()) {
     198            if(include_custom && !arguments.contains(argument)) {
     199            arguments.add(argument);
     200            }
     201        }
     202        else {
     203            if(include_normal && !arguments.contains(argument)) {
     204            arguments.add(argument);
     205            }
     206        }
     207        argument = null;
     208        }
     209    }
    162210    if(super_plugin != null) {
    163         ArrayList super_arguments = super_plugin.getArguments();
    164         for(int i = 0; i < super_arguments.size(); i++) {
    165         Object argument = super_arguments.get(i);
    166         if(!all_arguments.contains(argument)) {
    167             all_arguments.add(argument);
    168         }
    169         }
    170     }
    171     return all_arguments;
    172     }
    173     /** Method to retrieve a plugins custom argument information.
    174       * @return The custom arguments as a <strong>String</strong>.
    175       */
     211        ArrayList remainder = super_plugin.getArguments(include_normal, include_custom);
     212        remainder.removeAll(arguments);
     213        arguments.addAll(remainder);
     214    }
     215    return arguments;
     216    }
     217
     218    /** 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.
     219     * @return the custom arguments as a String
     220     */
    176221    public String getCustom() {
    177     return custom;
    178     }
     222    StringBuffer custom_text = new StringBuffer();
     223    // Retrieve all of the arguments, and append any that are custom into one long string
     224    ArrayList arguments = getArguments(false, true);
     225    int arguments_size = arguments.size();
     226    boolean first = true;
     227    for(int i = 0; i < arguments_size; i++) {
     228        Argument argument = (Argument) arguments.get(i);
     229        if(argument.isAssigned()) {
     230        if(!first) {
     231            custom_text.append(" ");
     232        }
     233        custom_text.append(argument.toString());
     234        first = false;
     235        }
     236    }
     237    return custom_text.toString();
     238    }
     239
     240    public String getDescription() {
     241    return description;
     242    }
     243
     244    public Element getElement() {
     245    return element;
     246    }
     247
    179248    /** Method to retrieve a plugins name.
    180       * @return A <strong>String</strong> containing the plugins name.
    181       */
     249     * @return A <strong>String</strong> containing the plugins name.
     250     */
    182251    public String getName() {
     252    if(name == null && element != null) {
     253        name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
     254    }
    183255    return name;
    184256    }
    185     public void setCustom(String custom) {
    186     this.custom = custom;
    187     }
     257
     258    public boolean isAssigned() {
     259    return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
     260    }
     261
     262    public boolean isSeparator() {
     263    return (element != null && element.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
     264    }
     265
     266    public void setAssigned(boolean assigned) {
     267    if(element != null) {
     268        element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
     269    }
     270    }
     271
     272    /** 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!).
     273     * @param custom_str the custom arguments all splodged together in one String
     274     */
     275    public void setCustom(String custom_str) {
     276    HashMap raw_arguments = CollectionConfiguration.parseArguments(new CommandTokenizer(custom_str));
     277    ArrayList custom_arguments = getArguments(false, true);
     278    int size = custom_arguments.size();
     279    for(int i = 0; i < size; i++) {
     280        Argument argument = (Argument) custom_arguments.get(i);
     281        String original_argument_name = StaticStrings.MINUS_CHARACTER + argument.getName();
     282        if(raw_arguments.containsKey(original_argument_name)) {
     283        // Set as assigned
     284        argument.setAssigned(true);
     285        String argument_value = (String)raw_arguments.remove(original_argument_name);
     286        if(argument_value != null) {
     287            argument.setValue(argument_value);
     288            argument_value = null;
     289        }
     290        }
     291        // We've removed it from our custom statement, so unassign
     292        else {
     293        argument.setAssigned(false);
     294        }
     295        argument = null;
     296    }
     297    // Any left over, add to the plugin
     298    Iterator argument_names = raw_arguments.keySet().iterator();
     299    while(argument_names.hasNext()) {
     300        String argument_name = (String) argument_names.next();
     301        String argument_value = (String) raw_arguments.get(argument_name);
     302        // The tricky thing is that we have to create a new element in the DOM as well.
     303        Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
     304        argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, argument_name.substring(1));
     305        argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
     306        argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.TRUE_STR);
     307        Argument argument = new Argument(argument_element);
     308        argument_name = null;
     309        if(argument_value != null) {
     310        argument.setValue(argument_value);
     311        argument_value = null;
     312        }
     313        // All done. Add it.
     314        element.appendChild(argument_element);
     315        add(argument);
     316        argument_element = null;
     317    }
     318    raw_arguments = null;
     319    }
     320
    188321    /** Method to set the value of desc.
    189       * @param desc The new value of desc as a <strong>String</strong>.
    190       */
    191     public void setDesc(String desc) {
    192     this.desc = desc;
    193     }
     322     * @param desc The new value of desc as a <strong>String</strong>.
     323     */
     324    public void setDescription(String desc) {
     325    this.description = description;
     326    }
     327
     328    public void setElement(Element element) {
     329    this.element = element;
     330    }
     331
    194332    /** Method to set the value of name.
    195       * @param name The new value of name as a <strong>String</strong>.
    196       */
     333     * @param name The new value of name as a <strong>String</strong>.
     334     */
    197335    public void setName(String name) {
    198336    this.name = name;
    199337    }
     338
    200339    /** Method to set the value of the super_plugin.
    201       * @param super_plugin The new value of super_plugin as a <strong>PlugIn</strong>, or <i>null</i> if this class has no inheritance.
    202       */
     340     * @param super_plugin The new value of super_plugin as a <strong>PlugIn</strong>, or <i>null</i> if this class has no inheritance.
     341     */
    203342    public void setSuper(PlugIn super_plugin) {
    204343    this.super_plugin = super_plugin;
    205344    }
     345
    206346    /** Method to print out this plugin as it would appear as a command within the collection configuration file.
    207       * @return A <strong>String</strong> containing a single plugin command.
    208       */
     347     * @return A <strong>String</strong> containing a single plugin command.
     348     */
    209349    public String toString() {
    210     String text = "plugin " + name + " ";
    211     ArrayList arguments = getArguments();
    212     for(int i = 0; i < arguments.size(); i++) {
    213         Argument argument = (Argument)arguments.get(i);
    214         if(argument.isAssigned()) {
    215         text = text + argument.toString();
    216         if(i < arguments.size() - 1) {
    217             text = text + " ";
    218         }
    219         }
    220     }
    221     // Now print custom arguments if any.
    222     if(custom != null) {
    223         text = text + " " + custom;
    224     }
    225     return text;
     350    if(element != null) {
     351        if(name == null) {
     352        name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
     353        }
     354        StringBuffer text = new StringBuffer(StaticStrings.PLUGIN_STR);
     355        text.append(" ");
     356        text.append(name);
     357        text.append(" ");
     358        ArrayList arguments = getArguments(true, true);
     359        int arguments_size = arguments.size();
     360        for(int i = 0; i < arguments_size; i++) {
     361        Argument argument = (Argument)arguments.get(i);
     362        if(argument.isAssigned()) {
     363            text.append(argument.toString());
     364            text.append(" ");
     365        }
     366        argument = null;
     367        }
     368        return text.substring(0, text.length() - 1);
     369    }
     370    // Basic Plugin
     371    else {
     372        return name;
     373    }
    226374    }         
    227375}
Note: See TracChangeset for help on using the changeset viewer.