Changeset 36507 for main/trunk


Ignore:
Timestamp:
2022-08-26T12:27:28+12:00 (20 months ago)
Author:
kjdon
Message:

factored out the guts of addLanguageSpecificDisplayItems so that we can use that new method from XSLTUtil

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DisplayItemUtil.java

    r32319 r36507  
    7171  }
    7272 
     73    /** Choose the best displayItem from a list - that matches lang - and return a copy of it created by doc. */
     74    public static Element chooseBestMatchDisplayItem(Document doc, Element display_item_list, String lang, String default_lang, ClassLoader class_loader) {
     75
     76
     77    // is there one with the specified language?
     78    Element best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
     79    if (best_di != null) {
     80        return (Element)doc.importNode(best_di, true);
     81    }
     82
     83    // if not, have we got one with a key?
     84    best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.KEY_ATT, null);
     85    if (best_di != null) {
     86        // look up the dictionary
     87        String value = getTextString(best_di.getAttribute(GSXML.KEY_ATT), lang, best_di.getAttribute(GSXML.DICTIONARY_ATT), class_loader);
     88        if (value != null) {
     89        // copy the node now. Don't want to be modifying the underlying list as can lead to concurrent access problems.
     90        best_di = (Element)doc.importNode(best_di, true);
     91        GSXML.setNodeText(best_di, value);
     92        return best_di;
     93        }
     94    }
     95    // ok, key one didn't work, can we use default lang?
     96    if (lang != default_lang) {
     97      best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, default_lang);
     98      if (best_di != null) {
     99        return (Element)doc.importNode(best_di, true);
     100      }
     101    }
     102    // STILL haven't found one, lets use the first one with a lang att (so we don't just get the key one back
     103    best_di = (Element) GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, null);
     104    if (best_di != null) {
     105        return (Element)doc.importNode(best_di, true);
     106    }
     107    return null;
     108
     109    }
     110
    73111  /** Finds the best language specific match for each displayItem in display_item_list and adds it to description */
    74112  public static boolean addLanguageSpecificDisplayItems(Element description, Element display_item_list, String lang, String default_lang, ClassLoader class_loader) {
     
    78116    for (int i = 0; i < items.getLength(); i++)
    79117      { // for each key
    80     Element m = (Element) items.item(i);
    81     // is there one with the specified language?
    82     Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
    83     if (new_m == null) {
    84       // if not, have we got one with a key?
    85       new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.KEY_ATT, null);
    86       if (new_m != null) {
    87         // look up the dictionary
    88         String value = getTextString(new_m.getAttribute(GSXML.KEY_ATT), lang, new_m.getAttribute(GSXML.DICTIONARY_ATT), class_loader);
    89         if (value != null) {
    90           // copy the node now. Don't want to be modifying the underlying list as can lead to concurrent access problems.
    91           new_m = (Element)doc.importNode(new_m, true);
    92           GSXML.setNodeText(new_m, value);
    93         }
    94         else {
    95           // haven't found the key in the dictionary, ignore this display item
    96           new_m = null;
    97         }
    98       }
    99     }
    100     if (new_m == null && lang != default_lang) {
    101       // still haven't got a value. can we use the default lang?
    102       new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, default_lang);
    103     }
    104     if (new_m == null)
    105       {
    106         // STILL haven't found one, lets use the first one with a lang att (so we don't just get the key one back
    107         new_m = (Element) GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, null);
    108       }
     118    Element di_list = (Element) items.item(i);
     119    Element new_m = chooseBestMatchDisplayItem(doc, di_list, lang, default_lang, class_loader);
     120
    109121    if (new_m != null) {
    110       description.appendChild(doc.importNode(new_m, true));
     122      description.appendChild(new_m);
    111123    }
    112124      } // for each key
Note: See TracChangeset for help on using the changeset viewer.