Changeset 24418

Show
Ignore:
Timestamp:
17.08.2011 16:58:02 (8 years ago)
Author:
sjm84
Message:

Fixed an error that was causing double spaces to become nothing (rather than one space), by using a regular expression. Also reformatted the file

Files:
1 modified

Legend:

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

    r14706 r24418  
    3535import org.greenstone.gatherer.metadata.MetadataTools; 
    3636 
    37  
    38 /** This class is the greenstone 3 version of the Format.java in gs2 for handling 'gsf' format statements. 
     37/** 
     38 * This class is the greenstone 3 version of the Format.java in gs2 for handling 
     39 * 'gsf' format statements. 
    3940 */ 
    40 public class Format4gs3 implements DOMProxyListEntry { 
    41      
    42     /** The element this format is based on. */ 
    43     private Element element = null; 
    44     /** We keep a copy of the feature name */ 
    45     private String feature_name = null; 
    46     /** The format statement for this feature (also contains classifier options if its a classifier), used by the format list on the top in GLI. */ 
    47     private String feature_format = null; 
    48      
    49     /** The format statement for this feature (does not contain options in the front), displayed in the format editor at the bottom in GLI.  
    50      *  If this format is not a classifier, feature_format==pure_format*/ 
    51     private String pure_format = null; 
    52      
    53     /** Wether this feature is about a classifier. The other way to check this is to see if feature_name starts with "CL" */ 
    54     private static boolean is_classifier = false; 
    55     /** If this format is about a classifier, this contains all its options */ 
    56     private String classifier_options = ""; 
    57      
    58     /** Constructor only to be used during DOMProxyListModel initialization. */ 
    59     public Format4gs3 () { 
    60          
    61     } 
    62     /** Constructor for a format-string format command. 
    63      * @param element The <Strong>element</strong> this format object contains. 
    64      */ 
    65     public Format4gs3 (Element element) { 
    66         this.element = element; 
    67          
    68         is_classifier = element.getParentNode().getNodeName().equals(StaticStrings.CLASSIFY_ELEMENT); 
    69  
    70     if(is_classifier && CollectionDesignManager.classifier_manager!=null) { 
    71             //The getClassifierPosition() method also sets the classifier_options variable. 
    72             feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element);    
    73             Classifier classifier = getClassifier(feature_name); 
    74             //set the classifier_options string 
    75         if (classifier != null){ 
    76         classifier_options = classifier.toString(); 
    77         } 
    78                      
    79             pure_format = toOneLineFormat(XMLTools.getNodeText(element)); 
    80             feature_format = classifier_options + " " + pure_format; 
    81         } else { 
    82             feature_name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 
    83             feature_format = toOneLineFormat(XMLTools.getNodeText(element)); 
    84             pure_format = feature_format; 
    85         } 
    86  
    87      } 
    88     // 
    89     public Format4gs3 (String feature_name, String feature_format) { 
    90         this.feature_name = feature_name; 
    91         this.feature_format = feature_format; 
    92         this.pure_format = feature_format; 
    93          
    94         is_classifier = feature_name.startsWith (Classifier.CLASSIFIER_PREFIX); 
    95          
    96         element = CollectionConfiguration.createElement (StaticStrings.FORMAT_STR); 
    97         if(is_classifier) { 
    98              
    99             //Now get the options of the classifier 
    100             Classifier classifier = getClassifier(feature_name); 
    101             //set the classifier_options string 
    102             classifier_options = classifier.toString(); 
    103              
    104             feature_format = classifier_options + " " + feature_format; 
    105         } else { 
    106             element.setAttribute (StaticStrings.NAME_ATTRIBUTE, feature_name); 
    107         } 
    108  
    109         XMLTools.setNodeText(element, toOneLineFormat(pure_format)); 
    110  
    111     } 
    112     public int getClassifierPosition(Element element) { 
    113         //we could also here use 'element.getParentNode().getParentNode()' to get this root node 
    114         Element root = CollectionDesignManager.collect_config.getDocumentElement (); 
    115         NodeList classify_list = root.getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT);    
    116         int num_classify_elements = classify_list.getLength(); 
    117         for(int i=0; i<num_classify_elements; i++) { 
    118             Element classify = (Element)classify_list.item(i); 
    119             Element format = (Element)XMLTools.getChildByTagName(classify, StaticStrings.FORMAT_STR); 
    120             if(format == element) { 
    121                 //Now get the options of the classifier 
    122                 Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(i); 
    123                 classifier_options = classifier.toString(); 
    124                 // i+1 because java is from 0 up, greenstone starts from 1. 
    125                 return i + 1; 
    126             } 
    127         } 
    128         return -1; 
    129     } 
    130     public static Classifier getClassifier(String feature_name) { 
    131             int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length())); 
    132             Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(index-1); 
    133             return classifier; 
    134     } 
    135     //This method is used when the add button is clicked and a new format element for a classifier is added to 
    136     // the DOM tree, we need to figure out which Classify element to append this format element to. 
    137     public Element getClassifyElement () { 
    138          
    139         //String name = classifier.getPositionString(); 
    140         int classifier_index = Integer.parseInt (feature_name.substring (Classifier.CLASSIFIER_PREFIX.length ())); 
    141         Element root = CollectionDesignManager.collect_config.getDocumentElement (); 
    142         NodeList classify_list = root.getElementsByTagName (StaticStrings.CLASSIFY_ELEMENT); 
    143          
    144         // - 1 because java is from 0 up, greenstone starts from 1. 
    145         classifier_index = classifier_index - 1; 
    146         return (Element)classify_list.item (classifier_index); 
    147     } 
    148              
    149     /** Method to translate this class information into a line of text as you would expect on the format list panel 
    150      * @return A <strong>String</strong> containing the format command. 
    151      */ 
    152     public String toString () { 
    153  
    154         return feature_name + " " + toOneLineFormat(feature_format); 
    155     } 
    156     public void update () { 
    157       
    158         if (is_classifier) { 
    159            feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element);  
    160            Classifier classifier = getClassifier(feature_name); 
    161             //update the classifier_options string 
    162             classifier_options = classifier.toString(); 
    163             pure_format = toOneLineFormat(XMLTools.getNodeText(element)); 
    164             feature_format = classifier_options + " " + pure_format; 
    165         } 
    166     }     
    167     public String getFormatedFormat() { 
    168         return toFormatedFormat(feature_format); 
    169     } 
    170     public String getPureFormat() { 
    171         return toFormatedFormat(pure_format); 
    172     } 
    173      
    174     public Classifier getClassifier() { 
    175         if(!isClassifier()) { 
    176              
    177             return null; 
    178         } 
    179         int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length())); 
    180         //index-1 because java indexes from 0, but greenstone is from 1. 
    181         index = index - 1; 
    182         return CollectionDesignManager.classifier_manager.getClassifier(index); 
    183     } 
    184     public void setFeatureName (String name) { 
    185         feature_name = name; 
    186     } 
    187     /** Set the value of value. Hmmm. 
    188      * @param value The new value from value as a <strong>String</strong>. 
    189      */ 
    190     public void setFeatureFormat (String value) { 
    191         if(is_classifier) { 
    192           feature_format = classifier_options + " " + value; 
    193         } else { 
    194           feature_format = value; 
    195         } 
    196     } 
    197     public void setPureFormat (String value) { 
    198         pure_format = value; 
    199         setFeatureFormat (value); 
    200         XMLTools.setNodeText(element, pure_format); 
    201     } 
    202     public static String toOneLineFormat(String str) { 
    203         return str.replaceAll("\n", "").replaceAll("  ", "").replaceAll(" <", "<"); 
    204     } 
    205     public static String toFormatedFormat(String str) { 
    206         return str.replaceAll("  ", "").replaceAll("<", "\n<").replaceFirst("\n", ""); 
    207     } 
    208     public int compareTo (Object object) { 
    209         if(object == null) { // It seems that occasionally compareTo is called and somehow null ends up in comparison. 
    210             return -1; 
    211         } 
    212         return this.getFeatureName ().compareToIgnoreCase (((Format4gs3)object).getFeatureName()); 
    213     } 
    214      
    215     public DOMProxyListEntry create (Element element) { 
    216         return new Format4gs3 (element); 
    217     } 
    218      
    219     public boolean equals (Object object) { 
    220         return (compareTo (object) == 0); 
    221     } 
    222      
    223     public Element getElement () { 
    224         return element; 
    225     } 
    226     public boolean isClassifier() { 
    227         return feature_name.startsWith(Classifier.CLASSIFIER_PREFIX); 
    228     } 
    229     public String getFeatureName () { 
    230         return feature_name; 
    231     } 
    232     //The format appended as a row of string used to display in the format list panel 
    233     public String getFeatureFormat () { 
    234         return feature_format; 
    235     }     
    236     public void setElement (Element element) { 
    237         this.element = element; 
    238     } 
    239     public boolean isAssigned() { 
    240         return true; 
    241     } 
    242  
    243     public void setAssigned(boolean assigned) { 
    244     } 
     41public class Format4gs3 implements DOMProxyListEntry 
     42{ 
     43 
     44    /** The element this format is based on. */ 
     45    private Element element = null; 
     46    /** We keep a copy of the feature name */ 
     47    private String feature_name = null; 
     48    /** 
     49     * The format statement for this feature (also contains classifier options 
     50     * if its a classifier), used by the format list on the top in GLI. 
     51     */ 
     52    private String feature_format = null; 
     53 
     54    /** 
     55     * The format statement for this feature (does not contain options in the 
     56     * front), displayed in the format editor at the bottom in GLI. If this 
     57     * format is not a classifier, feature_format==pure_format 
     58     */ 
     59    private String pure_format = null; 
     60 
     61    /** 
     62     * Wether this feature is about a classifier. The other way to check this is 
     63     * to see if feature_name starts with "CL" 
     64     */ 
     65    private static boolean is_classifier = false; 
     66    /** If this format is about a classifier, this contains all its options */ 
     67    private String classifier_options = ""; 
     68 
     69    /** Constructor only to be used during DOMProxyListModel initialization. */ 
     70    public Format4gs3() 
     71    { 
     72 
     73    } 
     74 
     75    /** 
     76     * Constructor for a format-string format command. 
     77     *  
     78     * @param element 
     79     *            The <Strong>element</strong> this format object contains. 
     80     */ 
     81    public Format4gs3(Element element) 
     82    { 
     83        this.element = element; 
     84 
     85        is_classifier = element.getParentNode().getNodeName().equals(StaticStrings.CLASSIFY_ELEMENT); 
     86 
     87        if (is_classifier && CollectionDesignManager.classifier_manager != null) 
     88        { 
     89            //The getClassifierPosition() method also sets the classifier_options variable. 
     90            feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element); 
     91            Classifier classifier = getClassifier(feature_name); 
     92            //set the classifier_options string 
     93            if (classifier != null) 
     94            { 
     95                classifier_options = classifier.toString(); 
     96            } 
     97 
     98            pure_format = toOneLineFormat(XMLTools.getNodeText(element)); 
     99            feature_format = classifier_options + " " + pure_format; 
     100        } 
     101        else 
     102        { 
     103            feature_name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 
     104            feature_format = toOneLineFormat(XMLTools.getNodeText(element)); 
     105            pure_format = feature_format; 
     106        } 
     107 
     108    } 
     109 
     110    // 
     111    public Format4gs3(String feature_name, String feature_format) 
     112    { 
     113        this.feature_name = feature_name; 
     114        this.feature_format = feature_format; 
     115        this.pure_format = feature_format; 
     116 
     117        is_classifier = feature_name.startsWith(Classifier.CLASSIFIER_PREFIX); 
     118 
     119        element = CollectionConfiguration.createElement(StaticStrings.FORMAT_STR); 
     120        if (is_classifier) 
     121        { 
     122 
     123            //Now get the options of the classifier 
     124            Classifier classifier = getClassifier(feature_name); 
     125            //set the classifier_options string 
     126            classifier_options = classifier.toString(); 
     127 
     128            feature_format = classifier_options + " " + feature_format; 
     129        } 
     130        else 
     131        { 
     132            element.setAttribute(StaticStrings.NAME_ATTRIBUTE, feature_name); 
     133        } 
     134 
     135        XMLTools.setNodeText(element, toOneLineFormat(pure_format)); 
     136 
     137    } 
     138 
     139    public int getClassifierPosition(Element element) 
     140    { 
     141        //we could also here use 'element.getParentNode().getParentNode()' to get this root node 
     142        Element root = CollectionDesignManager.collect_config.getDocumentElement(); 
     143        NodeList classify_list = root.getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT); 
     144        int num_classify_elements = classify_list.getLength(); 
     145        for (int i = 0; i < num_classify_elements; i++) 
     146        { 
     147            Element classify = (Element) classify_list.item(i); 
     148            Element format = (Element) XMLTools.getChildByTagName(classify, StaticStrings.FORMAT_STR); 
     149            if (format == element) 
     150            { 
     151                //Now get the options of the classifier 
     152                Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(i); 
     153                classifier_options = classifier.toString(); 
     154                // i+1 because java is from 0 up, greenstone starts from 1. 
     155                return i + 1; 
     156            } 
     157        } 
     158        return -1; 
     159    } 
     160 
     161    public static Classifier getClassifier(String feature_name) 
     162    { 
     163        int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length())); 
     164        Classifier classifier = CollectionDesignManager.classifier_manager.getClassifier(index - 1); 
     165        return classifier; 
     166    } 
     167 
     168    //This method is used when the add button is clicked and a new format element for a classifier is added to 
     169    // the DOM tree, we need to figure out which Classify element to append this format element to. 
     170    public Element getClassifyElement() 
     171    { 
     172 
     173        //String name = classifier.getPositionString(); 
     174        int classifier_index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length())); 
     175        Element root = CollectionDesignManager.collect_config.getDocumentElement(); 
     176        NodeList classify_list = root.getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT); 
     177 
     178        // - 1 because java is from 0 up, greenstone starts from 1. 
     179        classifier_index = classifier_index - 1; 
     180        return (Element) classify_list.item(classifier_index); 
     181    } 
     182 
     183    /** 
     184     * Method to translate this class information into a line of text as you 
     185     * would expect on the format list panel 
     186     *  
     187     * @return A <strong>String</strong> containing the format command. 
     188     */ 
     189    public String toString() 
     190    { 
     191 
     192        return feature_name + " " + toOneLineFormat(feature_format); 
     193    } 
     194 
     195    public void update() 
     196    { 
     197 
     198        if (is_classifier) 
     199        { 
     200            feature_name = Classifier.CLASSIFIER_PREFIX + getClassifierPosition(element); 
     201            Classifier classifier = getClassifier(feature_name); 
     202            //update the classifier_options string 
     203            classifier_options = classifier.toString(); 
     204            pure_format = toOneLineFormat(XMLTools.getNodeText(element)); 
     205            feature_format = classifier_options + " " + pure_format; 
     206        } 
     207    } 
     208 
     209    public String getFormatedFormat() 
     210    { 
     211        return toFormatedFormat(feature_format); 
     212    } 
     213 
     214    public String getPureFormat() 
     215    { 
     216        return toFormatedFormat(pure_format); 
     217    } 
     218 
     219    public Classifier getClassifier() 
     220    { 
     221        if (!isClassifier()) 
     222        { 
     223 
     224            return null; 
     225        } 
     226        int index = Integer.parseInt(feature_name.substring(Classifier.CLASSIFIER_PREFIX.length())); 
     227        //index-1 because java indexes from 0, but greenstone is from 1. 
     228        index = index - 1; 
     229        return CollectionDesignManager.classifier_manager.getClassifier(index); 
     230    } 
     231 
     232    public void setFeatureName(String name) 
     233    { 
     234        feature_name = name; 
     235    } 
     236 
     237    /** 
     238     * Set the value of value. Hmmm. 
     239     *  
     240     * @param value 
     241     *            The new value from value as a <strong>String</strong>. 
     242     */ 
     243    public void setFeatureFormat(String value) 
     244    { 
     245        if (is_classifier) 
     246        { 
     247            feature_format = classifier_options + " " + value; 
     248        } 
     249        else 
     250        { 
     251            feature_format = value; 
     252        } 
     253    } 
     254 
     255    public void setPureFormat(String value) 
     256    { 
     257        pure_format = value; 
     258        setFeatureFormat(value); 
     259        XMLTools.setNodeText(element, pure_format); 
     260    } 
     261 
     262    public static String toOneLineFormat(String str) 
     263    { 
     264         
     265        return str.replaceAll("\n", "").replaceAll("\\s+", " ").replaceAll(" <", "<"); 
     266    } 
     267 
     268    public static String toFormatedFormat(String str) 
     269    { 
     270        return str.replaceAll("\\s+", " ").replaceAll("<", "\n<").replaceFirst("\n", ""); 
     271    } 
     272 
     273    public int compareTo(Object object) 
     274    { 
     275        if (object == null) 
     276        { // It seems that occasionally compareTo is called and somehow null ends up in comparison. 
     277            return -1; 
     278        } 
     279        return this.getFeatureName().compareToIgnoreCase(((Format4gs3) object).getFeatureName()); 
     280    } 
     281 
     282    public DOMProxyListEntry create(Element element) 
     283    { 
     284        return new Format4gs3(element); 
     285    } 
     286 
     287    public boolean equals(Object object) 
     288    { 
     289        return (compareTo(object) == 0); 
     290    } 
     291 
     292    public Element getElement() 
     293    { 
     294        return element; 
     295    } 
     296 
     297    public boolean isClassifier() 
     298    { 
     299        return feature_name.startsWith(Classifier.CLASSIFIER_PREFIX); 
     300    } 
     301 
     302    public String getFeatureName() 
     303    { 
     304        return feature_name; 
     305    } 
     306 
     307    //The format appended as a row of string used to display in the format list panel 
     308    public String getFeatureFormat() 
     309    { 
     310        return feature_format; 
     311    } 
     312 
     313    public void setElement(Element element) 
     314    { 
     315        this.element = element; 
     316    } 
     317 
     318    public boolean isAssigned() 
     319    { 
     320        return true; 
     321    } 
     322 
     323    public void setAssigned(boolean assigned) 
     324    { 
     325    } 
    245326 
    246327}