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/Subcollection.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;
    4428/**************************************************************************************
    45  * Title:        Gatherer
    46  * Description:  The Gatherer: a tool for gathering and enriching a digital collection.
    47  * Copyright:    Copyright (c) 2001
    48  * Company:      The University of Waikato
    4929 * Written:      03/05/02
    5030 * Revised:      17/11/02 - Commenting
     31 *               04/07/03 - Completely rewritten to support DOM
    5132 **************************************************************************************/
     33import org.greenstone.gatherer.Gatherer;
     34import org.greenstone.gatherer.cdm.CollectionConfiguration;
     35import org.greenstone.gatherer.cdm.CollectionDesignManager;
     36import org.greenstone.gatherer.cdm.DOMProxyListEntry;
     37import org.greenstone.gatherer.msm.ElementWrapper;
    5238import org.greenstone.gatherer.msm.MSMUtils;
    53 import org.w3c.dom.Element;
     39import org.greenstone.gatherer.util.StaticStrings;
     40import org.greenstone.gatherer.util.Troolean;
     41import org.w3c.dom.*;
    5442/** This class encapsulates one subcollection entry in the collection configuration file.
    5543 * @author John Thompson, Greenstone Digital Library, University of Waikato
    56  * @version 2.3
     44 * @version 2.4
    5745 */
    5846public class Subcollection
    59     implements Comparable {
     47    implements Comparable, DOMProxyListEntry {
    6048    /** A <i>boolean</i> which is <i>true</i> if the condition is an include one, <i>false</i> otherwise. */
    61     private boolean include = true;
    62     /** Either the fully qualified name of the metadata whose value should be matched against the given expression, or <i>null</i> if you wish to match against the file name. */
    63     private String element = null;
    64     /** A String containing a Perl expression which is used as the filter for this subcollection. */
    65     private String exp = null;
     49    private Troolean include = new Troolean();
     50    /** The DOM Element this Subcollection is based upon. */
     51    private Element element = null;
     52    /** A String containing a Perl expression which is used as the pattern by which to filter this subcollection. */
     53    private String pattern = null;
    6654    /** A series of flags to be used when matching the expression. */
    6755    private String flags = null;
    6856    /** A String which is a unique identifier of a subcollection. */
    6957    private String name = null;
    70     /** Constructor.
    71      * @param name A <strong>String</strong> which is a unique identifier of a subcollection.
    72      * @param pattern A <strong>String</strong> containing the inclusion, the element to filter, a Perl expression which is used as the filter for this subcollection, and a series of argument flags.
    73      */
    74     public Subcollection(String name, String pattern) {
     58    /** Either the fully qualified name of the metadata whose value should be matched against the given expression, or <i>null</i> if you wish to match against the file name. */
     59    private String source = null;
     60    private String text = null;
     61
     62    /** Default constructor which should only be used during DOMProxyListModel initialization. */
     63    public Subcollection() {
     64    }
     65
     66    /** Constructor for representing an existing assigned Subcollection.
     67     * @param element the Element this subcollection is based upon
     68     */
     69    public Subcollection(Element element) {
     70    this.element = element;
     71    }
     72
     73    /** Constructor for assigning a brand new Subcollection.
     74     * @param name a unique identifier for this collection as a String
     75     * @param include true if this the pattern should be an inclusion filter, false for exclusion
     76     * @param source either the fully qualified name of an element as a String, or null to filter filenames
     77     * @param pattern the matching pattern as a String
     78     * @param flags any flags to use while matching, as a String
     79     */
     80    public Subcollection(String name, boolean include, String source, String pattern, String flags) {
     81    // Cache the details
     82    this.flags = flags;
     83    this.include.set(include);
    7584    this.name = name;
    76     // Have to do some work on pattern.
    77     // Remove quote marks.
    78     if(pattern.startsWith("\"") || pattern.startsWith("'")) {
    79         pattern = pattern.substring(1, pattern.length() - 1);
    80     }
    81     // Test for exclusion
    82     if(pattern.startsWith("!")) {
    83         this.include = false;
    84         pattern = pattern.substring(1, pattern.length());
     85    this.pattern = pattern;
     86    if(source != null) {
     87        this.source = source;
    8588    }
    8689    else {
    87         this.include = true;
    88     }
    89     if(pattern.indexOf("/") != -1) {
    90                 // Now element (which may be Filename)
    91         this.element = pattern.substring(0, pattern.indexOf("/"));
    92         if(this.element.toLowerCase().equals("filename")) {
    93         this.element = null;
     90        this.source = StaticStrings.FILENAME_STR;
     91    }
     92    // Create a new DOM Element with the appropriate attributes and text value
     93    element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.SUBCOLLECTION_ELEMENT);
     94    element.setAttribute(StaticStrings.CONTENT_ATTRIBUTE, source);
     95    element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
     96    element.setAttribute(StaticStrings.OPTIONS_ATTRIBUTE, flags);
     97    element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, (include ? StaticStrings.INCLUDE_STR : StaticStrings.EXCLUDE_STR));
     98    MSMUtils.setValue(element, pattern);
     99    }
     100
     101    /** Method to compare two subcollections.
     102     * @param object he other subcollection to compare to, as an Object
     103     * @return an int which is &gt;0, 0, or &lt;0 if this subcollection is before, equal to, or after the target object respectively.
     104     */
     105    public int compareTo(Object object) {
     106    return getName().compareTo(((Subcollection)object).getName());
     107    }
     108
     109    public DOMProxyListEntry create(Element element) {
     110    return new Subcollection(element);
     111    }
     112
     113    /** Method to check two subcollections for equality.
     114     * @param object the other subcollection to compare to, as an Object
     115     * @return true if the subcollections are equal, false otherwise.
     116     */
     117    public boolean equals(Object object) {
     118    return (compareTo(object) == 0);
     119    }
     120
     121    /** Retrieve the DOM Element this Subcollection is based upon.
     122     * @return an Element
     123     */
     124    public Element getElement() {
     125    return element;
     126    }
     127
     128    /** Method to get the value of flags.
     129     * @return the value of flags as a String
     130     */
     131    public String getFlags() {
     132    if(flags == null && element != null) {
     133        flags = element.getAttribute(StaticStrings.OPTIONS_ATTRIBUTE);
     134    }
     135    return flags;
     136    }
     137
     138    /** Method to get the value of name.
     139     * @param String The value of name as a <strong>String</string>.
     140     */
     141    public String getName() {
     142    if(name == null && element != null) {
     143        name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     144    }
     145    return name;
     146    }
     147
     148    public String getPattern() {
     149    if(pattern == null && element != null) {
     150        pattern = MSMUtils.getValue(element);
     151    }
     152    return pattern;
     153    }
     154
     155    /** Method to get the name of the source of the strings used in pattern matching.
     156     * @return a String which is either the fully qualified name of a metadata element, or filename
     157     */
     158    public String getSource() {
     159    if(source == null &&element != null) {
     160        source = element.getAttribute(StaticStrings.CONTENT_ATTRIBUTE);
     161        // If this is a MSM Element then retrieve the appropriate ElementWrapper and use the language specific name
     162        if(!source.equals(StaticStrings.FILENAME_STR)) {
     163        ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(source);
     164        if(element_wrapper != null) {
     165            source = element_wrapper.toString();
     166        }
    94167        }
    95                 // Extract Perl expression
    96         if(pattern.indexOf("/") != pattern.lastIndexOf("/")) {
    97         this.exp = pattern.substring(pattern.indexOf("/") + 1,
    98                          pattern.lastIndexOf("/"));
    99         this.flags = pattern.substring(pattern.lastIndexOf("/") + 1);
    100         }
    101         else {
    102         this.exp = pattern.substring(pattern.indexOf("/") + 1);
    103         }
    104     }
    105     else {
    106                 // Hmmm. Well I don't know what this is but it isn't a proper subcollection definition. Can't really 'return null' or something. So I'll just not set include, element, exp and flag expressions.
    107     }
    108     }
    109     /** Constructor.
    110       * @param name A <strong>String</strong> which is a unique identifier of a subcollection.
    111       * @param include A <i>boolean</i> which is <i>true</i> if the condition is an include one, <i>false</i> otherwise.
    112       * @param exp A <strong>String</strong> containing a Perl expression which is used as the filter for this subcollection.
    113       */
    114     public Subcollection(String name, boolean include, String exp, String flags) {
    115     this.element = null;
    116     this.exp = exp;
    117     this.flags = flags;
    118     this.include = include;
    119     this.name = name;
    120     }
    121     /** Constructor.
    122       * @param name A <strong>String</strong> which is a unique identifier of a subcollection.
    123       * @param include A <i>boolean</i> which is <i>true</i> if the condition is an include one, <i>false</i> otherwise.
    124       * @param element Either the fully qualified name of the metadata whose value should be matched against the given expression, or <i>null</i> if you wish to match against the file name.
    125       * @param exp A <strong>String</strong> containing a Perl expression which is used as the filter for this subcollection.
    126       * @param flags A <strong>String</strong> of flags to be taken into account when matching the expression.
    127       */
    128     public Subcollection(String name, boolean include, String element, String exp, String flags) {
     168    }
     169    return source;
     170    }
     171
     172    public boolean isAssigned() {
     173    return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
     174    }
     175
     176    /** Method to get the value of include.
     177     * @param boolean true if this is an inclusion filter, false otherwise
     178     */
     179    public boolean isInclusive() {
     180    if(!include.isDecided() && element != null) {
     181        include.set(element.getAttribute(StaticStrings.TYPE_ATTRIBUTE).equals(StaticStrings.INCLUDE_STR));
     182    }
     183    return include.isTrue();
     184    }
     185
     186    public void setAssigned(boolean assigned) {
     187    if(element != null) {
     188        element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
     189    }
     190    }
     191
     192    public void setElement(Element element) {
    129193    this.element = element;
    130     this.exp = exp;
    131     this.flags = flags;
    132     this.include = include;
    133     this.name = name;
    134     }
    135     /** Method to compare two subcollections.
    136       * @param object The other subcollection to compare to, as an <strong>Object</strong>.
    137       * @return An <i>int</i> which is greater than, equal to or less than zero if the this object is before, equal to, or after the target object respectively.
    138       */
    139     public int compareTo(Object object) {
    140     Subcollection sub = (Subcollection) object;
    141     return getName().compareTo(sub.getName());
    142     }
    143     /** Method to check two subcollections for equality.
    144       * @param object The other subcollection to compare to, as an <strong>Object</strong>.
    145       * @return A <i>boolean</i> which is <i>true</i> if the subcollections are equal, <i>false</i> otherwise.
    146       */
    147     public boolean equals(Object object) {
    148     if(compareTo(object) == 0) {
    149         return true;
    150     }
    151     return false;
    152     }
    153     /** Method to get the value of element.
    154       * @return The value of element as an <strong>String</strong>.
    155       */
    156     public String getElement() {
    157     return element;
    158     }
    159     /** Method to get the value of expression.
    160       * @return The value of exp as a <strong>String</strong>.
    161       */
    162     public String getExpression() {
    163     return exp;
    164     }
    165     /** Method to get the value of flags.
    166       * @return The value of flags as a <strong>String</strong>.
    167       */
    168     public String getFlags() {
    169     return flags;
    170     }
    171     /** Method to get the value of include.
    172       * @param boolean The value of include as a <i>boolean</i>.
    173       */
    174     public boolean getInclude() {
    175     return include;
    176     }
    177     /** Method to get the value of name.
    178       * @param String The value of name as a <strong>String</string>.
    179       */
    180     public String getName() {
    181     return name;
    182     }
    183     /** Method to get the name of the source of the strings used in pattern matching.
    184       * @return A <strong>String</strong> which is either the fully qualified name of a metadata element, or "Filename".
    185       */
    186     public String getSource() {
    187     if(element != null) {
    188         return element;
    189     }
    190     return "Filename";
    191     }
     194    include.reset();
     195    flags = null;
     196    name = null;
     197    pattern = null;
     198    source = null;
     199    text = null;
     200    }
     201
     202    public void setFlags(String new_flags) {
     203    if(element != null) {
     204        // Change element
     205        element.setAttribute(StaticStrings.OPTIONS_ATTRIBUTE, new_flags);
     206        flags = new_flags;
     207        text = null;
     208    }
     209    }
     210
     211    public void setInclusive(boolean new_include) {
     212    if(element != null) {
     213        // Change element
     214        element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, (new_include ? StaticStrings.INCLUDE_STR : StaticStrings.EXCLUDE_STR));
     215        include.set(new_include);
     216        text = null;
     217    }
     218    }
     219
     220    public void setName(String new_name) {
     221    if(element != null) {
     222        // Change element
     223        element.setAttribute(StaticStrings.NAME_ATTRIBUTE, new_name);
     224        name = new_name;
     225        text = null;
     226    }
     227    }
     228
     229    public void setPattern(String new_pattern) {
     230    if(element != null) {
     231        // Change element
     232        MSMUtils.setValue(element, new_pattern);
     233        pattern = new_pattern;
     234        text = null;
     235    }
     236    }
     237
     238    public void setSource(String new_source) {
     239    if(element != null) {
     240        // Change element
     241        element.setAttribute(StaticStrings.CONTENT_ATTRIBUTE, new_source);
     242        source = new_source;
     243        text = null;
     244    }
     245    }
     246
    192247    /** Method to display the contents of this class as it would appear in the collection configuration file.
    193       * @return A <strong>String</strong> which could be used as a subcollection entry in collect.cfg.
    194       */
     248     * @return a String representing this subcollection
     249     */
    195250    public String toString() {
    196     String text = "subcollection        ";
    197     text = text + name + " ";
    198     text = text + "\"";
    199     if(!include) {
    200         text = text + "!";
    201     }
    202     if(element != null) {
    203         text = text + element;
    204     }
    205     else {
    206         text = text + "Filename";
    207     }
    208     text = text + "/";
    209     text = text + exp;
    210     text = text + "/";
    211     if(flags != null) {
    212         text = text + flags;
    213     }
    214     text = text + "\"\n";
     251    if(text == null && element != null) {
     252        text = CollectionConfiguration.toString(element, true);
     253    }
    215254    return text;
    216255    }
    217 
    218     /** Update certain fields of this subcollection.
    219       * @param name A <strong>String</strong> which is a unique identifier of a subcollection.
    220       * @param source Either the fully qualified name of the metadata whose value should be matched against the given expression, or <i>null</i> if you wish to match against the file name.
    221       * @param exp A <strong>String</strong> containing a Perl expression which is used as the filter for this subcollection.
    222       * @param include A <i>boolean</i> which is <i>true</i> if the condition is an include one, <i>false</i> otherwise.
    223       * @param flags A <strong>String</strong> of flags to be taken into account when matching the expression.
    224         */
    225     public void update(String name, String source, String exp, boolean include, String flags) {
    226     this.name = name;
    227     this.element = source;
    228     this.exp = exp;
    229     this.include = include;
    230     this.flags = flags;
    231     }
    232 
    233     /** Method to update the name of a metadata element, if it changes.
    234       * @param old_name The current name of the element as a <strong>String</strong>.
    235       * @param new_name The new name of the element as a <strong>String</strong>.
    236       */
    237     public void updateElement(String old_name, String new_name) {
    238     if(element.equals(old_name)) {
    239         element = new_name;
    240     }
    241     }
    242256}
    243257
Note: See TracChangeset for help on using the changeset viewer.