[30832] | 1 | /*
|
---|
| 2 | * DisplayItemUtil.java
|
---|
| 3 | * Copyright (C) 2016 New Zealand Digital Library, http://www.nzdl.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program; if not, write to the Free Software
|
---|
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 18 | */
|
---|
| 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | import org.apache.log4j.Logger;
|
---|
| 22 |
|
---|
| 23 | import org.greenstone.gsdl3.util.Dictionary;
|
---|
| 24 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 25 | import org.w3c.dom.Document;
|
---|
| 26 | import org.w3c.dom.Element;
|
---|
| 27 | import org.w3c.dom.Node;
|
---|
| 28 | import org.w3c.dom.NodeList;
|
---|
| 29 |
|
---|
| 30 |
|
---|
[32082] | 31 | /** various methods for handling displayItems and choosing the right one for the specified language */
|
---|
[30832] | 32 |
|
---|
| 33 | public class DisplayItemUtil
|
---|
| 34 | {
|
---|
| 35 |
|
---|
| 36 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.DisplayItemUtil.class.getName());
|
---|
| 37 |
|
---|
| 38 | /** looks for displayItems from config element and stores them in displayItemList in the form
|
---|
| 39 |
|
---|
| 40 | <displayItem name="X">
|
---|
| 41 | <displayItem name="X" lang="">value</displayItem>
|
---|
| 42 | <displayItem name="X" lang="">value</displayItem>
|
---|
| 43 | </displayItem>
|
---|
| 44 |
|
---|
| 45 | */
|
---|
| 46 | public static boolean storeDisplayItems(Element display_item_list, Element config) {
|
---|
| 47 | if (config == null) {
|
---|
| 48 | logger.error("the config element is null, no displayItems to be found");
|
---|
| 49 | return false;
|
---|
| 50 | }
|
---|
| 51 | Document doc = display_item_list.getOwnerDocument();
|
---|
| 52 | NodeList displaynodes = config.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
|
---|
| 53 | if (displaynodes.getLength() > 0) {
|
---|
| 54 |
|
---|
| 55 | for (int k = 0; k < displaynodes.getLength(); k++) {
|
---|
| 56 | Element d = (Element) displaynodes.item(k);
|
---|
| 57 | String name = d.getAttribute(GSXML.NAME_ATT);
|
---|
| 58 | Element this_item = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, name);
|
---|
| 59 | if (this_item == null) {
|
---|
| 60 | this_item = doc.createElement(GSXML.DISPLAY_TEXT_ELEM);
|
---|
| 61 | this_item.setAttribute(GSXML.NAME_ATT, name);
|
---|
| 62 | display_item_list.appendChild(this_item);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | this_item.appendChild(doc.importNode(d, true));
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | return true;
|
---|
| 70 |
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /** Finds the best language specific match for each displayItem in display_item_list and adds it to description */
|
---|
| 74 | public static boolean addLanguageSpecificDisplayItems(Element description, Element display_item_list, String lang, String default_lang, ClassLoader class_loader) {
|
---|
| 75 |
|
---|
| 76 | Document doc = description.getOwnerDocument();
|
---|
| 77 | NodeList items = display_item_list.getChildNodes();
|
---|
| 78 | for (int i = 0; i < items.getLength(); i++)
|
---|
| 79 | { // 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) {
|
---|
[32319] | 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);
|
---|
[30832] | 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 | }
|
---|
| 109 | if (new_m != null) {
|
---|
| 110 | description.appendChild(doc.importNode(new_m, true));
|
---|
| 111 | }
|
---|
| 112 | } // for each key
|
---|
| 113 | return true;
|
---|
| 114 |
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | protected static String getTextString(String key, String lang, String dictionary, ClassLoader class_loader) {
|
---|
| 118 | return getTextString(key, lang, dictionary, null, class_loader);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | protected static String getTextString(String key, String lang, String dictionary, String[] args, ClassLoader class_loader)
|
---|
| 122 | {
|
---|
| 123 | Dictionary dict;
|
---|
| 124 | if (class_loader != null) {
|
---|
| 125 | dict = new Dictionary(dictionary, lang, class_loader);
|
---|
| 126 | } else {
|
---|
| 127 | dict = new Dictionary(dictionary, lang);
|
---|
| 128 | }
|
---|
| 129 | String result = dict.get(key, args);
|
---|
| 130 | return result;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
[32082] | 134 | }
|
---|