1 | /*
|
---|
2 | * XSLTUtil.java
|
---|
3 | * Copyright (C) 2008 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 java.util.Date;
|
---|
22 | import java.util.Locale;
|
---|
23 | import java.util.MissingResourceException;
|
---|
24 | import java.io.File;
|
---|
25 | import java.text.SimpleDateFormat;
|
---|
26 |
|
---|
27 | import org.apache.log4j.*;
|
---|
28 | import org.w3c.dom.Node;
|
---|
29 |
|
---|
30 | import org.apache.commons.lang3.StringUtils;
|
---|
31 |
|
---|
32 | /** a class to contain various static methods that are used by the xslt
|
---|
33 | * stylesheets
|
---|
34 | */
|
---|
35 | public class XSLTUtil {
|
---|
36 |
|
---|
37 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XSLTUtil.class.getName());
|
---|
38 |
|
---|
39 | /* some tests */
|
---|
40 | public static boolean equals(String s1, String s2) {
|
---|
41 | return s1.equals(s2);
|
---|
42 | }
|
---|
43 | public static boolean notEquals(String s1, String s2) {
|
---|
44 | return !s1.equals(s2);
|
---|
45 | }
|
---|
46 | public static boolean exists(String s1, String s2) {
|
---|
47 | return !s1.equals("");
|
---|
48 | }
|
---|
49 | public static boolean contains(String s1, String s2) {
|
---|
50 | return (s1.indexOf(s2) != -1);
|
---|
51 | }
|
---|
52 | public static boolean startsWith(String s1, String s2) {
|
---|
53 | return s1.startsWith(s2);
|
---|
54 | }
|
---|
55 | public static boolean endsWith(String s1, String s2) {
|
---|
56 | return s1.endsWith(s2);
|
---|
57 | }
|
---|
58 | public static boolean lessThan(String s1, String s2) {
|
---|
59 | return (s1.compareTo(s2) < 0);
|
---|
60 | }
|
---|
61 | public static boolean lessThanOrEquals(String s1, String s2) {
|
---|
62 | return (s1.compareTo(s2) <= 0);
|
---|
63 | }
|
---|
64 | public static boolean greaterThan(String s1, String s2) {
|
---|
65 | return (s1.compareTo(s2) > 0);
|
---|
66 | }
|
---|
67 | public static boolean greaterThanOrEquals(String s1, String s2) {
|
---|
68 | return (s1.compareTo(s2) >= 0);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /* some preprocessing functions */
|
---|
72 | public static String toLower(String orig) {
|
---|
73 | return orig.toLowerCase();
|
---|
74 | }
|
---|
75 | public static String toUpper(String orig) {
|
---|
76 | return orig.toUpperCase();
|
---|
77 | }
|
---|
78 |
|
---|
79 | public static byte[] toUTF8(String orig) {
|
---|
80 |
|
---|
81 | try {
|
---|
82 | byte[] utf8 = orig.getBytes("UTF-8");
|
---|
83 | return utf8;
|
---|
84 | }
|
---|
85 | catch (Exception e){
|
---|
86 | logger.error("unsupported encoding");
|
---|
87 | return orig.getBytes();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public static String getNumberedItem(String list, int number) {
|
---|
92 | String [] items = StringUtils.split(list, ",", -1);
|
---|
93 | if (items.length > number) {
|
---|
94 | return items[number];
|
---|
95 | }
|
---|
96 | return ""; // index out of bounds
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Generates links to equivalent documents for a document with a default document icon/type.
|
---|
100 | * Links are generated from the parameters: a list of document icons which are each in turn embedded
|
---|
101 | * in the matching starting link tag in the list of docStartLinks (these starting links link to the
|
---|
102 | * equivalent documents in another format). Each link's start tag is closed with the corresponding
|
---|
103 | * closing tag in the docEndLinks list. Parameter token is the list separator. Parameter divider is
|
---|
104 | * the string that should separate each final link generated from the next.
|
---|
105 | * Returns a string that represents a sequence of links to equivalent documents, where the anchor is
|
---|
106 | * a document icon. */
|
---|
107 | public static String getEquivDocLinks(String token, String docIconsString, String docStartLinksString,
|
---|
108 | String docEndLinksString, String divider)
|
---|
109 | {
|
---|
110 | String [] docIcons = StringUtils.split(docIconsString, token, -1);
|
---|
111 | String [] startLinks = StringUtils.split(docStartLinksString, token, -1);
|
---|
112 | String [] endLinks = StringUtils.split(docEndLinksString, token, -1);
|
---|
113 |
|
---|
114 | StringBuffer buffer = new StringBuffer();
|
---|
115 | for(int i = 0; i < docIcons.length; i++) {
|
---|
116 | if(i > 0) {
|
---|
117 | buffer.append(divider);
|
---|
118 | }
|
---|
119 | buffer.append(startLinks[i]+docIcons[i]+endLinks[i]);
|
---|
120 | }
|
---|
121 |
|
---|
122 | return buffer.toString();
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | public static String tidyWhitespace(String original) {
|
---|
127 |
|
---|
128 | if (original==null || original.equals("")) {
|
---|
129 | return original;
|
---|
130 | }
|
---|
131 | String new_s = original.replaceAll("\\s+", " ");
|
---|
132 | return new_s;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | public static String getInterfaceText(String interface_name, String lang, String key) {
|
---|
137 | return getInterfaceText(interface_name, lang, key, null);
|
---|
138 | }
|
---|
139 | public static String getInterfaceText(String interface_name, String lang, String key, String args_str) {
|
---|
140 |
|
---|
141 | key = key.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
142 |
|
---|
143 | String [] args = null;
|
---|
144 | if (args_str!=null && !args_str.equals("")) {
|
---|
145 | args = StringUtils.split(args_str, ";");
|
---|
146 | }
|
---|
147 | Dictionary dict = new Dictionary("interface_"+interface_name, lang);
|
---|
148 | String result = dict.get(key, args);
|
---|
149 | if (result == null) { // not found
|
---|
150 | //if not found, search a separate subdirectory named by the interface name
|
---|
151 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
152 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
153 | result = dict.get(key, args);
|
---|
154 | if(result != null) {
|
---|
155 | return result;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (result == null && !interface_name.equals("default")) { // not found, try the default interface
|
---|
160 | dict = new Dictionary("interface_default", lang);
|
---|
161 | result = dict.get(key, args);
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (result == null) { // not found
|
---|
165 | return "_"+key+"_";
|
---|
166 | }
|
---|
167 | return result;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg_node) {
|
---|
171 | String [] args = new String [1];
|
---|
172 |
|
---|
173 | String node_str = XMLConverter.getString(arg_node);
|
---|
174 | args[0] = node_str;
|
---|
175 | Dictionary dict = new Dictionary("interface_"+interface_name, lang);
|
---|
176 | String result = dict.get(key, args);
|
---|
177 | if (result == null) { // not found
|
---|
178 | //if not found, search a separate subdirectory named by the interface name
|
---|
179 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
180 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
181 | result = dict.get(key, args);
|
---|
182 | if(result != null) {
|
---|
183 | return result;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (result == null && !interface_name.equals("default")) { // not found, try the default interface
|
---|
188 | dict = new Dictionary("interface_default", lang);
|
---|
189 | result = dict.get(key, args);
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (result == null) { // not found
|
---|
193 | return "_"+key+"_";
|
---|
194 | }
|
---|
195 |
|
---|
196 | return result;
|
---|
197 | }
|
---|
198 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg1_node, Node arg2_node) {
|
---|
199 | String [] args = new String [2];
|
---|
200 |
|
---|
201 | String node_str = XMLConverter.getString(arg1_node);
|
---|
202 | args[0] = node_str;
|
---|
203 | node_str = XMLConverter.getString(arg2_node);
|
---|
204 | args[1] = node_str;
|
---|
205 | Dictionary dict = new Dictionary("interface_"+interface_name, lang);
|
---|
206 | String result = dict.get(key, args);
|
---|
207 | if (result == null) { // not found
|
---|
208 | //if not found, search a separate subdirectory named by the interface name
|
---|
209 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
210 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
211 | result = dict.get(key, args);
|
---|
212 | if(result != null) {
|
---|
213 | return result;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (result == null && !interface_name.equals("default")) { // not found, try the default interface
|
---|
218 | dict = new Dictionary("interface_default", lang);
|
---|
219 | result = dict.get(key, args);
|
---|
220 | }
|
---|
221 |
|
---|
222 | if (result == null) { // not found
|
---|
223 | return "_"+key+"_";
|
---|
224 | }
|
---|
225 |
|
---|
226 | return result;
|
---|
227 | }
|
---|
228 |
|
---|
229 | public static boolean isImage(String mimetype) {
|
---|
230 | if (mimetype.startsWith("image/")) {
|
---|
231 | return true;
|
---|
232 | }
|
---|
233 | return false;
|
---|
234 | }
|
---|
235 |
|
---|
236 | public static String formatDate(String date, String lang) {
|
---|
237 |
|
---|
238 | String in_pattern = "yyyyMMdd";
|
---|
239 | String out_pattern = "dd MMMM yyyy";
|
---|
240 | if (date.length()==6) {
|
---|
241 | in_pattern = "yyyyMM";
|
---|
242 | }
|
---|
243 |
|
---|
244 | SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
|
---|
245 | try {
|
---|
246 | Date d = formatter.parse(date);
|
---|
247 | formatter.applyPattern(out_pattern);
|
---|
248 | String new_date = formatter.format(d);
|
---|
249 | return new_date;
|
---|
250 | } catch (Exception e) {
|
---|
251 | return date;
|
---|
252 | }
|
---|
253 |
|
---|
254 | }
|
---|
255 |
|
---|
256 | public static String formatLanguage(String display_lang, String lang) {
|
---|
257 |
|
---|
258 | return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
|
---|
259 | }
|
---|
260 |
|
---|
261 | public static String cgiSafe(String original, String lang) {
|
---|
262 |
|
---|
263 | original = original.replace('&', ' ');
|
---|
264 | original = original.replaceAll(" ", "%20");
|
---|
265 | return original;
|
---|
266 | }
|
---|
267 |
|
---|
268 | public static String formatBigNumber(String num){
|
---|
269 |
|
---|
270 | String num_str = num;
|
---|
271 | char[] num_chars = num_str.toCharArray();
|
---|
272 | String zero_str = "";
|
---|
273 | String formatted_str = "";
|
---|
274 |
|
---|
275 | for(int i = num_chars.length-4; i >=0; i--){
|
---|
276 | zero_str += '0';
|
---|
277 | }
|
---|
278 |
|
---|
279 | String sig_str = "";
|
---|
280 | for(int i = 0; i<3 && i < num_chars.length; i++){
|
---|
281 | sig_str = sig_str + num_chars[i];
|
---|
282 | if(i == 1 && i+1 < num_chars.length){
|
---|
283 | sig_str = sig_str + ".";
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | int sig_int = Math.round(Float.parseFloat(sig_str));
|
---|
288 | String new_sig_str = sig_int +"";
|
---|
289 | if(sig_str.length() > 2){
|
---|
290 | new_sig_str = sig_int + "0";
|
---|
291 | }
|
---|
292 |
|
---|
293 | char[] final_chars = (new_sig_str+zero_str).toCharArray();
|
---|
294 | int count = 1;
|
---|
295 | for(int i=final_chars.length -1 ; i>=0; i-- ){
|
---|
296 | formatted_str = final_chars[i] + formatted_str ;
|
---|
297 | if(count == 3 && i !=0){
|
---|
298 | formatted_str = "," +formatted_str;
|
---|
299 | count = 1;
|
---|
300 | }
|
---|
301 | else{
|
---|
302 | count++;
|
---|
303 | }
|
---|
304 | }
|
---|
305 | return formatted_str;
|
---|
306 | }
|
---|
307 |
|
---|
308 | public static String hashToSectionId(String hashString)
|
---|
309 | {
|
---|
310 | if(hashString == null || hashString.length() == 0) {return "";}
|
---|
311 |
|
---|
312 | int firstDotIndex = hashString.indexOf(".");
|
---|
313 | if(firstDotIndex == -1)
|
---|
314 | {
|
---|
315 | return "";
|
---|
316 | }
|
---|
317 |
|
---|
318 | String sectionString = hashString.substring(firstDotIndex + 1);
|
---|
319 |
|
---|
320 | return sectionString;
|
---|
321 | }
|
---|
322 |
|
---|
323 | public static String hashToDepthClass(String hashString)
|
---|
324 | {
|
---|
325 | if(hashString == null || hashString.length() == 0) {return "";}
|
---|
326 |
|
---|
327 | String sectionString = hashToSectionId(hashString);
|
---|
328 |
|
---|
329 | int count = sectionString.split("\\.").length;
|
---|
330 |
|
---|
331 | if (sectionString.equals(""))
|
---|
332 | {
|
---|
333 | return "sectionHeaderDepthTitle";
|
---|
334 | }
|
---|
335 | else
|
---|
336 | {
|
---|
337 | return "sectionHeaderDepth" + count;
|
---|
338 | }
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|