Changeset 5254


Ignore:
Timestamp:
2003-08-22T10:06:59+12:00 (21 years ago)
Author:
jmt12
Message:

Added Codec calls to transform strings for collection metadata

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

Legend:

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

    r5230 r5254  
    3535import org.greenstone.gatherer.msm.MSMUtils;
    3636import org.greenstone.gatherer.util.DOMTree;
     37import org.greenstone.gatherer.util.Codec;
    3738import org.greenstone.gatherer.util.StaticStrings;
    3839import org.greenstone.gatherer.util.Utility;
    3940import org.w3c.dom.*;
    40 /** This class provides either access to a pseudo-G3 document, or parses a collect.cfg file in such a way as to provide an xml-type view of its content. This later version is useful as it allows the manipulation and free form editing of a legacy collect.cfg file while still allowing the various CDM data managers to base themselves directly on this model (whereas they used to be independant ListModels which clobbered the ordering of unparsed commands). 
     41/** This class provides either access to a pseudo-G3 document, or parses a collect.cfg file in such a way as to provide an xml-type view of its content. This later version is useful as it allows the manipulation and free form editing of a legacy collect.cfg file while still allowing the various CDM data managers to base themselves directly on this model (whereas they used to be independant ListModels which clobbered the ordering of unparsed commands).
    4142 * @author John Thompson, Greenstone Digital Library, University of Waikato
    4243 * @version 2.3d
    4344 */
    44 public class CollectionConfiguration 
     45public class CollectionConfiguration
    4546    extends StaticStrings {
    4647
     
    6061    }
    6162
    62     /** Find the best insertion position for the given DOM Element. This should try to match command tag, and if found should then try to group by name or type (eg CollectionMeta), or append to end is no such grouping exists (eg PlugIns). Failing a command match it will check against the command order for the best insertion location. 
     63    /** Find the best insertion position for the given DOM Element. This should try to match command tag, and if found should then try to group by name or type (eg CollectionMeta), or append to end is no such grouping exists (eg PlugIns). Failing a command match it will check against the command order for the best insertion location.
    6364     * @param element the command Element to be inserted
    6465     * @return the Element which the given command should be inserted before, or null to append to end of list
     
    130131            else {
    131132            Element last_element = (Element) matching_elements.item(matching_elements.getLength() - 1);
    132             return last_element.getNextSibling(); 
    133             }
    134         }
    135        
     133            return last_element.getNextSibling();
     134            }
     135        }
     136
    136137        }
    137138        else {
     
    222223    }
    223224    else if(command_element_name.equals(COLLECTIONMETADATA_ELEMENT)) {
    224         return self.metadataToString(command_element);
     225        return self.metadataToString(command_element, show_extracted_namespace);
    225226    }
    226227    else if(command_element_name.equals(COLLECTIONMETADATA_CREATOR_ELEMENT)) {
    227         return self.metadataToString(command_element);
     228        return self.metadataToString(command_element, show_extracted_namespace);
    228229    }
    229230    else if(command_element_name.equals(COLLECTIONMETADATA_MAINTAINER_ELEMENT)) {
    230         return self.metadataToString(command_element);
     231        return self.metadataToString(command_element, show_extracted_namespace);
    231232    }
    232233    else if(command_element_name.equals(COLLECTIONMETADATA_PUBLIC_ELEMENT)) {
    233         return self.metadataToString(command_element);
     234        return self.metadataToString(command_element, show_extracted_namespace);
    234235    }
    235236    else if(command_element_name.equals(COLLECTIONMETADATA_BETA_ELEMENT)) {
    236         return self.metadataToString(command_element);
     237        return self.metadataToString(command_element, show_extracted_namespace);
    237238    }
    238239    else if(command_element_name.equals(PLUGIN_ELEMENT)) {
     
    291292        arguments.put(name, null);
    292293        name = null;
    293         } 
     294        }
    294295    }
    295296    return arguments;
    296297    }
    297298
    298     static private ArrayList known_metadata;   
     299    static private ArrayList known_metadata;
    299300
    300301    static private CollectionConfiguration self;
     
    598599    document_element = null;
    599600    return element;
    600     }   
     601    }
    601602
    602603    private String indexesToString(Element command_element, boolean show_extracted_namespace) {
     
    693694        if(j < language_elements_length - 1) {
    694695        text.append(SPACE_CHARACTER);
    695         } 
     696        }
    696697    }
    697698    return text.toString();
     
    727728    }
    728729
    729     static public String metadataToString(Element command_element) {
     730    static public String metadataToString(Element command_element, boolean text_value) {
    730731    // If there is no value attribute, then we don't write anything
    731732    String value_str = MSMUtils.getValue(command_element);
     
    766767        }
    767768        name_str = null;
     769
     770        // The value string we retrieved will be encoded for xml, so we now decode it - to text if text_value set. This parameter was originally show_extracted_namespace, but sincethis is only true for 'toString()' commands from within the CDM, its good enough to determine if this toString() will be used to display on screen, or write to collect.cfg
     771        if(text_value == CollectionMeta.TEXT) {
     772        value_str = Codec.transform(value_str, Codec.DOM_TO_TEXT);
     773        }
     774        else {
     775            value_str = Codec.transform(value_str, Codec.DOM_TO_GREENSTONE);
     776        }
     777
    768778        // We don't wrap the email addresses in quotes, nor any string without spaces
    769779        if(value_str.indexOf(SPACE_CHARACTER) == -1) {
     
    906916            String name = (String) names.next();
    907917            String value = (String) arguments.get(name); // Can be null
     918            // The metadata argument gets added as the content attribute
     919            if(name.equals(METADATA_ARGUMENT) && value != null) {
     920            // The metadata argument must be the fully qualified name of a metadata element, so if it doesn't yet have a namespace, append the extracted metadata namespace.
     921            if(value.indexOf(MSMUtils.NS_SEP) == -1) {
     922                value = Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP + value;
     923            }
     924            //command_element.setAttribute(CONTENT_ATTRIBUTE, value);
     925            }
    908926            // Everything else is an Option Element
    909927            Element option_element = document.createElement(OPTION_ELEMENT);
     
    913931            if(value.startsWith(SPEECH_CHARACTER) && value.endsWith(SPEECH_CHARACTER)) {
    914932                value = value.substring(1, value.length() - 1);
    915             }
    916             // The metadata argument gets added as the content attribute
    917             if(name.equals(METADATA_ARGUMENT) && value != null) {
    918                 // The metadata argument must be the fully qualified name of a metadata element, so if it doesn't yet have a namespace, append the extracted metadata namespace.
    919                 if(value.indexOf(MSMUtils.NS_SEP) == -1) {
    920                 value = Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP + value;
    921                 }
    922                 //command_element.setAttribute(CONTENT_ATTRIBUTE, value);
    923933            }
    924934            MSMUtils.setValue(option_element, value);
     
    11391149        }
    11401150        if(value_str != null) {
     1151            // Ready the value str (which can contain all sorts of funky characters) for writing as a DOM value
     1152            value_str = Codec.transform(value_str, Codec.GREENSTONE_TO_DOM);
    11411153            command_element.setAttribute(NAME_ATTRIBUTE, name_str);
    11421154            command_element.setAttribute(LANGUAGE_ATTRIBUTE, language_str);
    1143             MSMUtils.setValue(command_element, Utility.encodeXML(value_str));
     1155            MSMUtils.setValue(command_element, value_str);
    11441156        }
    11451157        else {
     
    11761188            command_element = document.createElement(COLLECTIONMETADATA_PUBLIC_ELEMENT);
    11771189        }
    1178         if(command_element != null && value_str != null) {
     1190        if(command_element != null) {
    11791191            command_element.setAttribute(NAME_ATTRIBUTE, name_str);
    11801192            command_element.setAttribute(LANGUAGE_ATTRIBUTE, ENGLISH_LANGUAGE_STR);
     
    12751287            search_element = null;
    12761288        }
    1277         }   
     1289        }
    12781290    }
    12791291    catch(Exception exception) {
     
    13501362    catch(Exception exception) {
    13511363    }
    1352     return command_element; 
     1364    return command_element;
    13531365    }
    13541366
     
    14021414    catch(Exception exception) {
    14031415    }
    1404     return command_element; 
     1416    return command_element;
    14051417    }
    14061418
     
    15641576     * @param str the String to be written
    15651577     */
    1566     private void write(BufferedWriter writer, String str) 
     1578    private void write(BufferedWriter writer, String str)
    15671579    throws IOException {
    15681580    writer.write(str, 0, str.length());
  • trunk/gli/src/org/greenstone/gatherer/cdm/CollectionMeta.java

    r4932 r5254  
    3737import org.greenstone.gatherer.cdm.Index;
    3838import org.greenstone.gatherer.msm.MSMUtils;
     39import org.greenstone.gatherer.util.Codec;
    3940import org.greenstone.gatherer.util.StaticStrings;
    4041import org.greenstone.gatherer.util.Utility;
     
    4647public class CollectionMeta
    4748    implements DOMProxyListEntry {
     49
     50    static final public boolean TEXT = true;
     51    static final public boolean GREENSTONE = false;
     52
    4853    private Element element;
    4954    private String text;
     
    113118     * @return The value of value as a <strong>String</strong>.
    114119     */
    115     public String getValue() {
    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));
     120    public String getValue(boolean text_value) {
     121    String raw_value = MSMUtils.getValue(element);
     122    // Decode the raw value depending on whether the user asked for the TEXT or GREENSTONE version
     123    if(text_value == TEXT) {
     124        return Codec.transform(raw_value, Codec.DOM_TO_TEXT);
     125    }
     126    else {
     127        return Codec.transform(raw_value, Codec.DOM_TO_GREENSTONE);
     128    }
    118129    }
    119130
     
    143154     * @param value the new value as a String.
    144155     */
    145     public void setValue(String value) {
    146     MSMUtils.setValue(element, Utility.encodeGreenstone(value));
     156    public void setValue(String raw_value) {
     157    // Only raw html text can be given to setValue so we need to encode it
     158    MSMUtils.setValue(element, Codec.transform(raw_value, Codec.TEXT_TO_DOM));
    147159    text = null; // Reset text
    148160    }
Note: See TracChangeset for help on using the changeset viewer.