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 |
|
---|
31 | /** various methods for handling displayItems and choosing the right one for the speicifed language */
|
---|
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) {
|
---|
90 | GSXML.setNodeText(new_m, value);
|
---|
91 | }
|
---|
92 | else {
|
---|
93 | // haven't found the key in the dictionary, ignore this display item
|
---|
94 | new_m = null;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | if (new_m == null && lang != default_lang) {
|
---|
99 | // still haven't got a value. can we use the default lang?
|
---|
100 | new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, default_lang);
|
---|
101 | }
|
---|
102 | if (new_m == null)
|
---|
103 | {
|
---|
104 | // STILL haven't found one, lets use the first one with a lang att (so we don't just get the key one back
|
---|
105 | new_m = (Element) GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, null);
|
---|
106 | }
|
---|
107 | if (new_m != null) {
|
---|
108 | description.appendChild(doc.importNode(new_m, true));
|
---|
109 | }
|
---|
110 | } // for each key
|
---|
111 | return true;
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected static String getTextString(String key, String lang, String dictionary, ClassLoader class_loader) {
|
---|
116 | return getTextString(key, lang, dictionary, null, class_loader);
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected static String getTextString(String key, String lang, String dictionary, String[] args, ClassLoader class_loader)
|
---|
120 | {
|
---|
121 | Dictionary dict;
|
---|
122 | if (class_loader != null) {
|
---|
123 | dict = new Dictionary(dictionary, lang, class_loader);
|
---|
124 | } else {
|
---|
125 | dict = new Dictionary(dictionary, lang);
|
---|
126 | }
|
---|
127 | String result = dict.get(key, args);
|
---|
128 | return result;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | } |
---|