Ignore:
Timestamp:
2020-07-03T12:18:35+12:00 (4 years 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.

File:
1 edited

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    {
Note: See TracChangeset for help on using the changeset viewer.