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/Format.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  * Company:      The University of Waikato
    4929 * Written:      06/05/02
    5030 * Revised:      04/10/02 - Commented
     31 *               14/07/03 - DOM Support
    5132 **************************************************************************************/
     33import org.greenstone.gatherer.cdm.CollectionConfiguration;
     34import org.greenstone.gatherer.cdm.CollectionDesignManager;
    5235import org.greenstone.gatherer.cdm.Classifier;
    53 /** This class encapsulates all the information about a format command from the collection configuration. This includes both the 'general' formatting commands, and the commands targetted at specific classifiers or classification component types.
     36import org.greenstone.gatherer.cdm.DOMProxyListEntry;
     37import org.greenstone.gatherer.msm.MSMUtils;
     38import org.w3c.dom.*;
     39/** This class encapsulates all the information about a format command from the collection configuration. This includes both the 'general' formatting commands, and the commands targetted at specific classifiers or classification component types. This class is, unfortunately, the most complex in terms of DOM model support, since we have to maintain a live reference to any classifier this format applies to. We temporarily use the feature name as the format name attribute but since the order of classifiers can change, this of course gets horribly out of date. Before the CollectionDesignManager saves the CollectionConfiguration it calls a method in the FormatManager to force all of the Formats to update their name attributes.
    5440 * @author John Thompson, Greenstone Digital Library, University of Waikato
    5541 * @version 2.3
    5642 */
    57 public class Format {
    58     /** If this format is of a true/false type, the current state. */
    59     private boolean state = false;
    60     /** The type of this format command, either FLAG, FORMAT or PARAM. */
    61     private int type = -1;
    62     /** The feature this format command affects. */
    63     private Object feature = null;
    64     /** If this format refers to a specific part of a feature, this indicates the part. */
    65     private String part = null;
    66     /** A command string, which may include a specific HTML chunk used to format a control. */
    67     private String value = null;
    68     /** An element of the format type enumeration. A true/false option. */
    69     static final public int FLAG = 0;
    70     /** An element of the format type enumeration. A string parameter. */
    71     static final public int VALUE = 1;
     43public class Format
     44    implements DOMProxyListEntry {
     45
    7246    /** The default features as specified by the Greenstone Developers manual. */
    7347    static final public String DEFAULT_FEATURES[] = {"", "DocumentArrowsBottom","DocumentButtons","DocumentContents","DocumentHeading","DocumentImages","DocumentText","DocumentUseHTML","Search"};
    7448    /** The list of known feature parts. */
    7549    static final public String DEFAULT_PARTS[] = {"","DateList","HList","Invisible","VList"};
     50
     51    /** Not all features have an associated part, such as those in the default features list (apart from Search which does have parts!) */
     52    static public boolean canHavePart(String name) {
     53    return !(name.equalsIgnoreCase(DEFAULT_FEATURES[1]) || name.equalsIgnoreCase(DEFAULT_FEATURES[2]) || name.equalsIgnoreCase(DEFAULT_FEATURES[3]) || name.equalsIgnoreCase(DEFAULT_FEATURES[4]) || name.equalsIgnoreCase(DEFAULT_FEATURES[5]) || name.equalsIgnoreCase(DEFAULT_FEATURES[6]) || name.equalsIgnoreCase(DEFAULT_FEATURES[7]));
     54    }
     55
     56    static public boolean isParamType(String name) {
     57    return (name.equalsIgnoreCase(DEFAULT_FEATURES[1]) || name.equalsIgnoreCase(DEFAULT_FEATURES[3]) || name.equalsIgnoreCase(DEFAULT_FEATURES[5]) || name.equalsIgnoreCase(DEFAULT_FEATURES[7]));
     58    }
     59
     60    /** If this format is altering a Classifier then this is the classifier in question. */
     61    private Classifier classifier = null;
     62    /** The element this format is based on. */
     63    private Element element = null;
     64    /** We keep a copy of the part because its slightly more computationally tricky to calculate. */
     65    private String part = null;
     66    /** Cached result of toString. */
     67    private String text = null;
     68
     69    /** Constructor only to be used during DOMProxyListModel initialization. */
     70    public Format() {
     71    }
     72
     73    public Format(Element element) {
     74    this.element = element;
     75    // Immediately check if we are dealing with a classifier, by retrieving the feature name. If it is a classifier, then restore the live reference immediately before anything changes it.
     76    Object object = getFeature();
     77    if(object instanceof Classifier) {
     78        classifier = (Classifier) object;
     79    }
     80    else {
     81        String feature = (String) object;
     82        if(feature.toUpperCase().startsWith(Classifier.CLASSIFIER_PREFIX)) {
     83        // Extract the integer index
     84        int index = Integer.parseInt(feature.substring(Classifier.CLASSIFIER_PREFIX.length()));
     85        // Subtract one (as java is 0-2 where G2 is 1-3)
     86        index = index - 1;
     87        // Retrieve the appropriate classifier
     88        classifier = CollectionDesignManager.classifier_manager.getClassifier(index);
     89        }
     90    }
     91    }
     92
    7693    /** Constructor for a flag format command.
    7794     * @param feature The <Strong>Object</strong> this format affects.
     
    8097     */
    8198    public Format(Object feature, String part, boolean state) {
    82     this.feature = feature;
    83     this.part = part;
    84     this.state = state;
    85     this.type = FLAG;
    86     }
     99    element = CollectionDesignManager.collect_config.document.createElement(CollectionConfiguration.FORMAT_ELEMENT);
     100    setName(feature, part);
     101    setState(state);
     102    }
     103
    87104    /** Constructor for a format-string format command.
    88105     * @param feature The <Strong>Object</strong> this format affects.
     
    91108     */
    92109    public Format(Object feature, String part, String value) {
    93     this.feature = feature;
    94     this.part = part;
    95     this.type = VALUE;
    96     this.value = value;
    97     }
     110    element = CollectionDesignManager.collect_config.document.createElement(CollectionConfiguration.FORMAT_ELEMENT);
     111    setName(feature, part);
     112    setValue(value);
     113    }
     114
     115    public boolean canHavePart() {
     116    return canHavePart(getName());
     117    }
     118
     119    public int compareTo(Object object) {
     120    if(object == null) { // It seems that occasionally compareTo is called and somehow null ends up in comparison.
     121        return -1;
     122    }
     123    if(object instanceof Format && element != null) {
     124        return element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE).compareTo(((Format)object).getName());
     125    }
     126    return toString().compareTo(object.toString());
     127    }
     128
     129    public DOMProxyListEntry create(Element element) {
     130    return new Format(element);
     131    }
     132
     133    public boolean equals(Object object) {
     134    return (compareTo(object) == 0);
     135    }
     136
     137    public Element getElement() {
     138    return element;
     139    }
     140
    98141    /** Method to retrieve the value of feature, which may either be a String or a Classifier.
    99142     * @return The value of feature as an <strong>Object</strong>.
    100143     */
    101144    public Object getFeature() {
    102     return feature;
    103     }
    104     /** Method to retrieve the value of part.
     145    if(classifier != null) {
     146        return classifier;
     147    }
     148    else if(element != null) {
     149        String name = element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
     150        // Remove part
     151        String part = getPart();
     152        String feature;
     153        if(part != null) {
     154        feature = name.substring(0, name.length() - part.length());
     155        }
     156        else {
     157        feature = name;
     158        }
     159        part = null;
     160        name = null;
     161        // If the feature now refers to a classifier, retrieve it.
     162        if(feature.toUpperCase().startsWith(Classifier.CLASSIFIER_PREFIX)) {
     163        // Extract the integer index
     164        int index = Integer.parseInt(feature.substring(Classifier.CLASSIFIER_PREFIX.length()));
     165        // Subtract one (as java is 0-2 where G2 is 1-3)
     166        index = index - 1;
     167        // Retrieve the appropriate classifier
     168        classifier = CollectionDesignManager.classifier_manager.getClassifier(index);
     169        return classifier;
     170        }
     171        else {
     172        return feature;
     173        }
     174    }
     175    else {
     176        return "Error";
     177    }
     178    }
     179
     180    public String getName() {
     181    if(element != null) {
     182        return element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
     183    }
     184    return "Error";
     185    }
     186
     187    /** Method to retrieve the value of part. Of course there may not be one, in which case return ""
    105188     * @return The value of part as a <Strong>String</strong>.
    106189     */
    107190    public String getPart() {
     191    if(part == null && element != null) {
     192        // To determine a part, we retrieve the Format name, then look for one of our recognized parts
     193        String name = element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
     194        // DEFAULT_PARTS[0] is an empty string to correctly enable comboboxes.
     195        name = name.toLowerCase();
     196        for(int i = 1; part == null && i < DEFAULT_PARTS.length; i++) {
     197        if(name.endsWith(DEFAULT_PARTS[i].toLowerCase())) {
     198            part = DEFAULT_PARTS[i];
     199        }
     200        }
     201        if(part == null) {
     202        part = DEFAULT_PARTS[0];
     203        }
     204    }
    108205    return part;
    109206    }
    110     /** Method to retrieve this formats special Greenstone position code.
    111      * @return A <strong>String</strong> which distinctly indicates this classifiers position.
    112      * @see org.greenstone.gatherer.cdm.Classifier
    113      */
    114     public String getPosition() {
    115     ///ystem.err.println("Retrieving the name of feature " + feature);
    116     String position = null;
    117     if(feature instanceof Classifier) {
    118         position = ((Classifier)feature).getPositionString();
    119     }
    120     else {
    121         position = feature.toString();
    122     }
    123     ///ystem.err.println("Result: " + position + part);
    124     return position + part;
    125     }
     207
    126208    /** Method to retrieve the value of state.
    127      * @param The value of state as a <i>boolean</i>.
     209     * @return value of state as a <i>boolean</i>.
    128210     */
    129211    public boolean getState() {
    130     return state;
    131     }
    132     /** Method to retrieve the value of type.
    133      * @param The value of type as an <i>int</i>.
    134      */
    135     public int getType() {
    136     return type;
    137     }
     212    return (element != null && element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR));
     213    }
     214
    138215    /** Retrieve the value of value.
    139216     * @return A <strong>String</strong> which is the value of value.
    140217     */
    141218    public String getValue() {
     219    String value = "Error";
     220    if(element != null) {
     221        value = MSMUtils.getValue(element);
     222    }
    142223    return value;
    143224    }
     225
     226    public boolean isAssigned() {
     227    return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
     228    }
     229
     230    public boolean isParamType() {
     231    return (element != null && element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE).length() > 0);
     232    }
     233
     234    public void setAssigned(boolean assigned) {
     235    if(element != null) {
     236        element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
     237    }
     238    }
     239
     240    public void setElement(Element element) {
     241    this.element = element;
     242    part = null;
     243    text = null;
     244    // Establish the classifier reference, if necessary
     245    getFeature();
     246    }
     247
     248    public void setName(Object feature, String part) {
     249    // Can't do anything unless an element exists.
     250    if(element != null) {
     251        this.part = part; // Easier for later on.
     252        if(feature instanceof Classifier) {
     253        classifier = (Classifier) feature;
     254        element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, classifier.getPositionString() + part);
     255        }
     256        else {
     257        element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, feature.toString() + part);
     258        }
     259        text = null;
     260    }
     261    }
     262
    144263    /** Set the value of state.
    145264     * @param state The new value for state as a <i>boolean</i>.
    146265     */
    147266    public void setState(boolean state) {
    148     this.state = state;
    149     }
    150     /** Set the value of type.
    151      * @param type The new value for type as an <i>int</i>.
    152      */
    153     public void setType(int type) {
    154     this.type = type;
    155     }
     267    if(element != null) {
     268        element.setAttribute(CollectionConfiguration.VALUE_ATTRIBUTE, (state ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
     269        text = null;
     270    }
     271    }
     272
    156273    /** Set the value of value. Hmmm.
    157274     * @param value The new value from value as a <strong>String</strong>.
    158275     */
    159276    public void setValue(String value) {
    160     this.value = value;
    161     }
     277    if(element != null) {
     278        MSMUtils.setValue(element, value);
     279        text = null;
     280    }
     281    }
     282
    162283    /** Method to translate this classes information into a line of text as you would expect in the collection configuration file.
    163284     * @return A <strong>String</strong> containing the format command.
    164285     */
    165286    public String toString() {
    166     String text = "format ";
    167     text = text + getPosition() + " ";
    168     switch(type) {
    169     case FLAG:
    170         if(state) {
    171         text = text + "true";
     287    if(text == null && element != null) {
     288        StringBuffer temp = new StringBuffer(CollectionConfiguration.FORMAT_STR);
     289        temp.append(" ");
     290        temp.append(element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE));
     291        temp.append(" ");
     292        String value = element.getAttribute(CollectionConfiguration.VALUE_ATTRIBUTE);
     293        if(value.length() > 0) {
     294        temp.append(value);
    172295        }
    173296        else {
    174         text = text + "false";
    175         }
    176         break;
    177     default:
    178         text = text + "\"" + value + "\"";
     297        temp.append("\"");
     298        temp.append(MSMUtils.getValue(element));
     299        temp.append("\"");
     300        }
     301        text = temp.toString();
     302        temp = null;
    179303    }
    180304    return text;
    181305    }
    182     /** Retrieve the type of a certain named feature.
    183      * @param name The name of the feature as a <strong>String</strong>.
    184      * @return An <i>int</i> indicating the type, and hence the controls needed to edit this type.
    185      */
    186     static public int getType(String name) {
    187     int index = -1;
    188     for(int i = 1; i < DEFAULT_FEATURES.length; i++) {
    189         if(name.equalsIgnoreCase(DEFAULT_FEATURES[i])) {
    190         index = i;
    191         }
    192     }
    193     if(index == 1 || index == 3 || index == 5 || index == 7) {
    194         return FLAG;
    195     }
    196     return VALUE;
     306
     307    public void update() {
     308    if(classifier != null) {
     309        element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, classifier.getPositionString() + getPart());
     310        text = null;
     311    }
    197312    }
    198313}
Note: See TracChangeset for help on using the changeset viewer.