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

Last change on this file since 36970 was 36970, checked in by kjdon, 17 months ago

added a helper method to create new displayitems

File size: 5.2 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 public static Element createDisplayItem(Document doc, String name, String lang, String value) {
39
40 Element di = doc.createElement(GSXML.DISPLAY_TEXT_ELEM);
41 di.setAttribute(GSXML.NAME_ATT, name);
42 di.setAttribute(GSXML.LANG_ATT, lang);
43 GSXML.setNodeText(di, value);
44 return di;
45 }
46 /** looks for displayItems from config element and stores them in displayItemList in the form
47
48 <displayItem name="X">
49 <displayItem name="X" lang="">value</displayItem>
50 <displayItem name="X" lang="">value</displayItem>
51 </displayItem>
52
53 */
54 public static boolean storeDisplayItems(Element display_item_list, Element config) {
55 if (config == null) {
56 logger.error("the config element is null, no displayItems to be found");
57 return false;
58 }
59 Document doc = display_item_list.getOwnerDocument();
60 NodeList displaynodes = config.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
61 if (displaynodes.getLength() > 0) {
62
63 for (int k = 0; k < displaynodes.getLength(); k++) {
64 Element d = (Element) displaynodes.item(k);
65 String name = d.getAttribute(GSXML.NAME_ATT);
66 Element this_item = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, name);
67 if (this_item == null) {
68 this_item = doc.createElement(GSXML.DISPLAY_TEXT_ELEM);
69 this_item.setAttribute(GSXML.NAME_ATT, name);
70 display_item_list.appendChild(this_item);
71 }
72
73 this_item.appendChild(doc.importNode(d, true));
74 }
75 }
76
77 return true;
78
79 }
80
81 /** Choose the best displayItem from a list - that matches lang - and return a copy of it created by doc. */
82 public static Element chooseBestMatchDisplayItem(Document doc, Element display_item_list, String lang, String default_lang, ClassLoader class_loader) {
83
84
85 // is there one with the specified language?
86 Element best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
87 if (best_di != null) {
88 return (Element)doc.importNode(best_di, true);
89 }
90
91 // if not, have we got one with a key?
92 best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.KEY_ATT, null);
93 if (best_di != null) {
94 // look up the dictionary
95 String value = Dictionary.createDictionaryAndGetString(best_di.getAttribute(GSXML.DICTIONARY_ATT), class_loader, best_di.getAttribute(GSXML.KEY_ATT), lang, null);
96 if (value != null) {
97 // copy the node now. Don't want to be modifying the underlying list as can lead to concurrent access problems.
98 best_di = (Element)doc.importNode(best_di, true);
99 GSXML.setNodeText(best_di, value);
100 return best_di;
101 }
102 }
103 // ok, key one didn't work, can we use default lang?
104 if (lang != default_lang) {
105 best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, default_lang);
106 if (best_di != null) {
107 return (Element)doc.importNode(best_di, true);
108 }
109 }
110 // STILL haven't found one, lets use the first one with a lang att (so we don't just get the key one back
111 best_di = (Element) GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, null);
112 if (best_di != null) {
113 return (Element)doc.importNode(best_di, true);
114 }
115 return null;
116
117 }
118
119 /** Finds the best language specific match for each displayItem in display_item_list and adds it to description */
120 public static boolean addLanguageSpecificDisplayItems(Element description, Element display_item_list, String lang, String default_lang, ClassLoader class_loader) {
121
122 Document doc = description.getOwnerDocument();
123 NodeList items = display_item_list.getChildNodes();
124 for (int i = 0; i < items.getLength(); i++)
125 { // for each key
126 Element di_list = (Element) items.item(i);
127 Element new_m = chooseBestMatchDisplayItem(doc, di_list, lang, default_lang, class_loader);
128
129 if (new_m != null) {
130 description.appendChild(new_m);
131 }
132 } // for each key
133 return true;
134
135 }
136
137
138}
Note: See TracBrowser for help on using the repository browser.