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/CollectionMeta.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 
    43 /* GPL_HEADER */
    4427package org.greenstone.gatherer.cdm;
    4528/**************************************************************************************
    46  * Title:        Gatherer
    47  * Description:  The Gatherer: a tool for gathering and enriching a digital collection.
    48  * Copyright:    Copyright (c) 2001
    49  * Company:      The University of Waikato
    50  * Written:        /05/02
    51  * Revised:      22/08/02 Revamped, Optimized and Commented.
     29 * Written:     
     30 * Revised:     28/06/03 - DOM support
    5231 **************************************************************************************/
     32import org.greenstone.gatherer.Configuration;
     33import org.greenstone.gatherer.Gatherer;
     34import org.greenstone.gatherer.cdm.CollectionConfiguration;
     35import org.greenstone.gatherer.cdm.CollectionDesignManager;
     36import org.greenstone.gatherer.cdm.DOMProxyListEntry;
    5337import org.greenstone.gatherer.cdm.Index;
    54 import org.greenstone.gatherer.cdm.Language;
     38import org.greenstone.gatherer.msm.MSMUtils;
     39import org.greenstone.gatherer.util.StaticStrings;
    5540import org.greenstone.gatherer.util.Utility;
     41import org.w3c.dom.*;
    5642/** This class encapsulates a single collection level metadata assignment, which constitutes a name, language and value.
    5743 * @author John Thompson, Greenstone Digital Library, University of Waikato
    58  * @version 2.3
     44 * @version 2.4
    5945 */
    6046public class CollectionMeta
    61     implements Comparable {
    62     /** A reference to the collection design manager for access to the language manager. */
    63     private CollectionDesignManager manager = null;
    64     /** The language of this metadata. Should be a two letter code. */
    65     private Language language = null;
    66     /** The name of the thing this metadata is assigned to, which may also refer to an Index or a Partition. */
    67     private Object name = null;
    68     /** The value of this metadata. */
    69     private String value = null;
     47    implements DOMProxyListEntry {
     48    private Element element;
     49    private String text;
     50
    7051    /** Constructor.
    71      * @param name The object the metadata is assigned to as an <strong>Object</strong>.
    72      * @param language The language of the metadata as a <strong>Language</strong>. Should be a two letter code.
    73      * @param value The value of this metadata, as a <strong>String</strong>.
    74       */
    75     public CollectionMeta(CollectionDesignManager manager, Object name, Language language, String value) {
    76     this.language = language;
    77     this.manager = manager;
    78     this.name = name;
    79     this.value = value;
     52     * @param element the Element from which we will determine metadata details
     53     */
     54    public CollectionMeta(Element element) {
     55    this.element = element;
    8056    }
     57
     58    /** Constructor to create a new piece of metadata given its name. */
     59    public CollectionMeta(String name) {
     60    element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
     61    element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
     62    element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, Gatherer.config.interface_language);
     63    }
     64
     65    /** Constructor to create a new piece of metadata given its name. */
     66    public CollectionMeta(String name, String language) {
     67    element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
     68    element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
     69    element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, language);
     70    }
     71
    8172    /** Method to compare two collection metadata objects to calculate their respective ordering.
    82       * @param object The other metadata to compare to, as an <strong>Object</strong>.
    83       * @return An <i>int</i> which is less than 0 if this object proceeds the given object, 0 if they are equal and greater than 0 otherwise.
    84       * @see org.greenstone.gatherer.cdm.Language
    85       */
     73     * @param object the other metadata to compare to, as an Object
     74     * @return an int which is less than 0 if this object proceeds the given object, 0 if they are equal and greater than 0 otherwise.
     75     * @see org.greenstone.gatherer.cdm.Language
     76     */
    8677    public int compareTo(Object object) {
    87     if(object instanceof CollectionMeta) {
    88         CollectionMeta metadata = (CollectionMeta) object;
    89         int result = name.toString().compareTo(metadata.getName().toString());
    90         if(result == 0) {
    91         Language other_language = metadata.getLanguage();
    92         if(language != null && other_language != null) {
    93             result = language.compareTo(metadata.getLanguage());
    94             if(result == 0) {
    95             return value.compareTo(metadata.getValue());
    96             }
    97         }
    98         else if(language != null) {
    99             return -1;
    100         }
    101         else if(other_language != null) {
    102             return 1;
    103         }
    104         }
    105         return result;
    106     }
    10778    return toString().compareTo(object.toString());
    10879    }
     80
     81    /** Factory constructor. */
     82    public DOMProxyListEntry create(Element element) {
     83    return new CollectionMeta(element);
     84    }
     85
    10986    /** Method to compare two collection metadata objects for equality.
    110       * @param object The other metadata to compare to, as an <strong>Object</strong>.
    111       * @return A <i>boolean</i> value of <i>true</i> if the object are equal, <i>false</i> otherwise.
    112       */
     87     * @param object The other metadata to compare to, as an <strong>Object</strong>.
     88     * @return A <i>boolean</i> value of <i>true</i> if the object are equal, <i>false</i> otherwise.
     89     */
    11390    public boolean equals(Object object) {
    114     if(compareTo(object) == 0) {
    115         return true;
    116     }
    117     return false;
     91    return (compareTo(object) == 0);
    11892    }
     93
     94    public Element getElement() {
     95    return element;
     96    }
     97
    11998    /** Method to retrieve the value of language.
    120      * @return The value of language as a <strong>Language</strong>.
     99     * @return The value of language as a <strong>String</strong>.
    121100     */
    122     public Language getLanguage() {
    123     return language;
     101    public String getLanguage() {
     102    // Retrieve the language string
     103    return element.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE);
    124104    }
     105
    125106    /** Method to retrieve the value of name.
    126      * @return The value of name as an <strong>Object</strong>.
     107     * @return
    127108     */
    128     public Object getName() {
    129     return name;
     109    public String getName() {
     110    return element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    130111    }
    131112    /** Method to retrieve the value of value (well great choice of name there).
    132       * @return The value of value as a <strong>String</strong>.
    133       */
     113     * @return The value of value as a <strong>String</strong>.
     114     */
    134115    public String getValue() {
    135     return value;
     116    // We have to replace the string made of '\' 'n' with the '\n' character! Also watch for other greenstone magic
     117    return Utility.decodeGreenstone(MSMUtils.getValue(element));
    136118    }
    137     /** Method to print out this class as it would appear within the collection configuration file.
    138       * @return A <strong>String</strong> containing the text value of this class.
    139       */
    140     public String toString() {
    141     String text = "collectionmeta ";
    142     if(name instanceof Index) {
    143         text = text + ".";
    144         Index index = (Index)name;
    145         text = text + index.toStringConfig() + " ";
     119
     120    public boolean isAssigned() {
     121    return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
     122    }
     123
     124    /** Determine if this metadata is one of the four special pieces of metadata.
     125     * @return true if this metadata is special, false otherwise.
     126     */
     127    public boolean isSpecial() {
     128    return (element != null && element.getAttribute(StaticStrings.SPECIAL_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
     129    }
     130
     131    public void setAssigned(boolean assigned) {
     132    if(element != null) {
     133        element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
    146134    }
    147     else if(name instanceof SubIndex) {
    148         text = text + ".";
    149         SubIndex index = (SubIndex)name;
    150         text = text + index.toString() + " ";
    151     }
    152     else {
    153         text = text + name.toString() + " ";
    154     }
    155     if(language != null && manager.languages.size() > 0 && !manager.languages.isDefaultLanguage(language)) {
    156         text = text + "[l=" + language.getCode() + "] ";
    157     }
    158     text = text + "\"" + Utility.encodeGreenstone(value) + "\"\n";
    159     return text;
    160135    }
    161     /** Used to update the contents of this collection level metadata to the given 'new' values.
    162       * @param name The new name of the metadata, as a <strong>Object</strong>.
    163       * @param language The new <strong>Language</strong> of the metadata.
    164       * @param value And the value the metadata is assigned, as a <strong>String</strong>.
    165       */
    166     public void update(Object name, Language language, String value) {
    167     this.name = name;
    168     this.language = language;
    169     this.value = value;
     136
     137    public void setElement(Element element) {
     138    this.element = element;
     139    text = null;
    170140    }
    171141
     
    174144     */
    175145    public void setValue(String value) {
    176     this.value = value;
     146    MSMUtils.setValue(element, Utility.encodeGreenstone(value));
     147    text = null; // Reset text
    177148    }
    178149
    179     /** Ensure this is a valid metadata entry by checking that the value is non-null and non-zero length (after having removed whitespace).
    180      * @return <i>true</i> if this metadata has a valid value and should be added to the config, <i>false</i> otherwise.
    181      */
    182     public boolean valid() {
    183     return(value != null && value.trim().length() > 0);
     150    /** Method to print out this class as it would appear within the collection configuration file.
     151      * @return A <strong>String</strong> containing the text value of this class.
     152      */
     153    public String toString() {
     154    if(text == null) {
     155        text = CollectionConfiguration.toString(element, true);
     156    }
     157    return text;
    184158    }
    185159}
Note: See TracChangeset for help on using the changeset viewer.