Changeset 34241

Show
Ignore:
Timestamp:
03.07.2020 12:18:35 (5 weeks ago)
Author:
ak19
Message:

The previous commit fixed the issue where HTML in collection descriptions was not being preserved when GLI was not involved. This commit fixes the remaining problems with preserving HTML in coll descriptions when GLI is involved.

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
5 modified

Legend:

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

    r34185 r34241  
    9494        { 
    9595            Element item = (Element) item_list.get(i); 
    96             String text = XMLTools.getNodeText(item);            
    97  
     96            boolean isCollDescr = name_value.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR); 
     97 
     98            // Sadly, XMLTools.getNodeText(item) returns empty for any HTML in displayItem 
     99            // so we have to do it the hard way 
     100            String text = isCollDescr ? preserveHTMLInDescriptionDisplayItem(item) 
     101                : XMLTools.getNodeText(item); // else as before for all other displayItems 
     102 
     103             
    98104            //If there is nothing to display, don't bother creating the element 
    99105            // Either lang or key should be set. If key set, no textnode. 
     
    28002806    } 
    28012807 
     2808    /** 
     2809     * For now it only makes sends to allow a collection's about page description to contain HTML. 
     2810     * Collection titles may go into a pre tag or text only context or into HTML headings, 
     2811     * where preserving any HTML int coll title would become meaningless or even a hindrance. 
     2812     * TODO: if users used HTML pre tags, they'll have wanted whitespace preserved. 
     2813     * TODO There's probably a better way to do this, but I'm unable to think of it. Messy but works. 
     2814     */ 
     2815    static private String preserveHTMLInDescriptionDisplayItem(Element collDescription) { 
     2816    // First get the <displayItem> as text 
     2817    String text = XMLTools.elementToString(collDescription, true); 
     2818 
     2819    // We only want the contents, not the xml processing instruction or the 
     2820    // <displayItem></displayItem> or self-closing(!) <displayItem/> when no text for colldescr 
     2821    // Remove xml processing instr too by removing <displayItem until first > after that 
     2822    // this will also handle self-closing tag case 
     2823    String lookFor = "<displayItem"; 
     2824    int start = text.indexOf(lookFor); 
     2825    if(start != -1) { 
     2826        start += lookFor.length(); 
     2827        start = text.indexOf(">", start); 
     2828        text = text.substring(start+1).trim(); // trim to ensure no empty lines to deceive us 
     2829        // especially after a self-closing tag 
     2830    } 
     2831 
     2832    // If we have a positive number of lines remaining, it means it wasn't a self-closing 
     2833    // tag. Remove the closing tag 
     2834    String[] lines = text.split("\\r?\\n"); 
     2835    text = "";               
     2836    if(lines.length > 1) { 
     2837        for (int j = 0; j < lines.length-1; j++) { // skip last line: closing tag 
     2838        text += lines[j].trim() + "\n"; // Easiest solution: 
     2839                                   // trim white space introduced by the one extra level of 
     2840                                   // indentation intoduced when enclosing <dispItem> tags removed 
     2841        } 
     2842    } 
     2843 
     2844    text = text.replaceAll("&amp;", "&");// feels stupid, when we're going to change it back again soon 
     2845               // but it's the last bit needed to get GLI to preserve html coll descriptions 
     2846               // while displaying the html as html (without char entities) in the config file editor 
     2847     
     2848    return text;//.trim(); // don't want trailing whitespace within displayItem 
     2849    } 
     2850     
    28022851    static public String generateStringVersion(Document doc) 
    28032852    { 
  • main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectionMeta.java

    r20448 r34241  
    162162     */ 
    163163    public void setValue(String raw_value) { 
     164    setValue(raw_value, false); 
     165    } 
     166 
     167    public void setValue(String raw_value, boolean preserveTags) { 
    164168    // we need to check if the value has changed 
    165169    String current_value = XMLTools.getValue(element); 
    166     String new_value = Codec.transform(raw_value, Codec.TEXT_TO_DOM); 
     170     
     171    String new_value = preserveTags ? 
     172        Codec.transform(raw_value, Codec.TEXT_TO_DOM_PRESERVE_TAGS) 
     173        : Codec.transform(raw_value, Codec.TEXT_TO_DOM); 
    167174    if (!current_value.equals(new_value)) { 
    168     // Only raw html text can be given to setValue so we need to encode it 
     175        // Only raw html text can be given to setValue so we need to encode it 
    169176        XMLTools.setValue(element, new_value); 
    170177        text = null; // Reset text 
  • main/trunk/gli/src/org/greenstone/gatherer/cdm/GeneralManager.java

    r22410 r34241  
    320320            if (!ready) return; 
    321321 
     322        boolean preserveTags = true; // for coll description, allow HTML tags to be saved as HTML tags 
    322323        public_collectionmeta.setValue((public_checkbox.isSelected() ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); 
    323324        String creator_email_str = creator_emailfield.getText(); 
    324325        creator_collectionmeta.setValue(creator_email_str); 
    325         collection_extra_collectionmeta.setValue(description_textarea.getText()); 
     326        collection_extra_collectionmeta.setValue(description_textarea.getText(), preserveTags); 
    326327        icon_collection_collectionmeta.setValue(icon_textfield.getText()); 
    327328        maintainer_collectionmeta.setValue(maintainer_emailfield.getText()); 
    328329        icon_collection_small_collectionmeta.setValue(small_icon_textfield.getText()); 
    329330        collection_name_collectionmeta.setValue(name_textfield.getText()); 
    330       
    331      
     331         
    332332    } 
    333333 
  • main/trunk/gli/src/org/greenstone/gatherer/util/Codec.java

    r28995 r34241  
    4545    static final public String GREENSTONE_TO_TEXT = "GREENSTONE_TO_TEXT"; 
    4646    static final public String TEXT_TO_DOM = "TEXT_TO_DOM"; 
     47    static final public String TEXT_TO_DOM_PRESERVE_TAGS = "TEXT_TO_DOM_PRESERVE_TAGS"; 
    4748    static final public String TEXT_TO_GREENSTONE = "TEXT_TO_GREENSTONE"; 
    4849    static final public String TEXT_TO_REGEXP = "TEXT_TO_REGEXP"; 
     
    169170    TRANSFORMS.put(TEXT_TO_DOM, text_to_dom); 
    170171    text_to_dom = null; 
     172 
     173    // Same as above, but preserve html element tags 
     174    String[] text_to_dom_preserve_tags = { 
     175        "&", "&amp;", 
     176        "\"", "&quot;", 
     177        "\'", "&apos;" 
     178    }; 
     179    TRANSFORMS.put(TEXT_TO_DOM_PRESERVE_TAGS, text_to_dom_preserve_tags); 
     180    text_to_dom_preserve_tags = null; 
    171181 
    172182    // Unescape html (or xml) text 
  • main/trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java

    r31776 r34241  
    957957            // And close. 
    958958            os.close(); 
     959 
     960            // Useful for debugging unescaped chars that may otherwise break stuff 
     961            // w = new StringWriter(); 
     962            // // Generate a new serializer from the above. 
     963            // s = new XMLSerializer(w, f); 
     964            // s.asDOMSerializer(); 
     965            // // Finally serialize the document to file. 
     966            // s.serialize(document); 
     967 
     968            // System.err.println("@@@@ XMLTools: wrote out to doc:\n" + w.toString()); 
     969             
    959970        } 
    960971        catch (Exception exception)