source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DisplayItemUtil.java@ 36507

Last change on this file since 36507 was 36507, checked in by kjdon, 20 months ago

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

File size: 5.4 KB
Line 
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 */
19package org.greenstone.gsdl3.util;
20
21import org.apache.log4j.Logger;
22
23import org.greenstone.gsdl3.util.Dictionary;
24import org.greenstone.gsdl3.util.GSXML;
25import org.w3c.dom.Document;
26import org.w3c.dom.Element;
27import org.w3c.dom.Node;
28import org.w3c.dom.NodeList;
29
30
31/** various methods for handling displayItems and choosing the right one for the specified language */
32
33public 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 /** 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
111 /** Finds the best language specific match for each displayItem in display_item_list and adds it to description */
112 public static boolean addLanguageSpecificDisplayItems(Element description, Element display_item_list, String lang, String default_lang, ClassLoader class_loader) {
113
114 Document doc = description.getOwnerDocument();
115 NodeList items = display_item_list.getChildNodes();
116 for (int i = 0; i < items.getLength(); i++)
117 { // for each key
118 Element di_list = (Element) items.item(i);
119 Element new_m = chooseBestMatchDisplayItem(doc, di_list, lang, default_lang, class_loader);
120
121 if (new_m != null) {
122 description.appendChild(new_m);
123 }
124 } // for each key
125 return true;
126
127 }
128
129 protected static String getTextString(String key, String lang, String dictionary, ClassLoader class_loader) {
130 return getTextString(key, lang, dictionary, null, class_loader);
131 }
132
133 protected static String getTextString(String key, String lang, String dictionary, String[] args, ClassLoader class_loader)
134 {
135 Dictionary dict;
136 if (class_loader != null) {
137 dict = new Dictionary(dictionary, lang, class_loader);
138 } else {
139 dict = new Dictionary(dictionary, lang);
140 }
141 String result = dict.get(key, args);
142 return result;
143 }
144
145
146}
Note: See TracBrowser for help on using the repository browser.