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.ArrayList;
|
---|
22 | import java.util.Date;
|
---|
23 | import java.util.HashMap;
|
---|
24 | import java.util.Locale;
|
---|
25 | import java.util.MissingResourceException;
|
---|
26 | import java.io.File;
|
---|
27 | import java.text.SimpleDateFormat;
|
---|
28 |
|
---|
29 | import net.tanesha.recaptcha.ReCaptcha;
|
---|
30 | import net.tanesha.recaptcha.ReCaptchaFactory;
|
---|
31 |
|
---|
32 | import org.apache.log4j.*;
|
---|
33 | import org.w3c.dom.Node;
|
---|
34 |
|
---|
35 | import org.apache.commons.lang3.StringUtils;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * a class to contain various static methods that are used by the xslt
|
---|
39 | * stylesheets
|
---|
40 | */
|
---|
41 | public class XSLTUtil
|
---|
42 | {
|
---|
43 | protected static HashMap<String, ArrayList<String>> _foundTableValues = new HashMap<String, ArrayList<String>>();
|
---|
44 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XSLTUtil.class.getName());
|
---|
45 |
|
---|
46 | /* some tests */
|
---|
47 | public static boolean equals(String s1, String s2)
|
---|
48 | {
|
---|
49 | return s1.equals(s2);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static boolean notEquals(String s1, String s2)
|
---|
53 | {
|
---|
54 | return !s1.equals(s2);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static boolean exists(String s1, String s2)
|
---|
58 | {
|
---|
59 | return !s1.equals("");
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static boolean contains(String s1, String s2)
|
---|
63 | {
|
---|
64 | return (s1.indexOf(s2) != -1);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static boolean startsWith(String s1, String s2)
|
---|
68 | {
|
---|
69 | return s1.startsWith(s2);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static boolean endsWith(String s1, String s2)
|
---|
73 | {
|
---|
74 | return s1.endsWith(s2);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public static boolean lessThan(String s1, String s2)
|
---|
78 | {
|
---|
79 | return (s1.compareTo(s2) < 0);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static boolean lessThanOrEquals(String s1, String s2)
|
---|
83 | {
|
---|
84 | return (s1.compareTo(s2) <= 0);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public static boolean greaterThan(String s1, String s2)
|
---|
88 | {
|
---|
89 | return (s1.compareTo(s2) > 0);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public static boolean greaterThanOrEquals(String s1, String s2)
|
---|
93 | {
|
---|
94 | return (s1.compareTo(s2) >= 0);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* some preprocessing functions */
|
---|
98 | public static String toLower(String orig)
|
---|
99 | {
|
---|
100 | return orig.toLowerCase();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public static String toUpper(String orig)
|
---|
104 | {
|
---|
105 | return orig.toUpperCase();
|
---|
106 | }
|
---|
107 |
|
---|
108 | public static byte[] toUTF8(String orig)
|
---|
109 | {
|
---|
110 | try
|
---|
111 | {
|
---|
112 | byte[] utf8 = orig.getBytes("UTF-8");
|
---|
113 | return utf8;
|
---|
114 | }
|
---|
115 | catch (Exception e)
|
---|
116 | {
|
---|
117 | logger.error("unsupported encoding");
|
---|
118 | return orig.getBytes();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public static String replace(String orig, String match, String replacement)
|
---|
123 | {
|
---|
124 | return orig.replace(match, replacement);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static String getNumberedItem(String list, int number)
|
---|
128 | {
|
---|
129 | String[] items = StringUtils.split(list, ",", -1);
|
---|
130 | if (items.length > number)
|
---|
131 | {
|
---|
132 | return items[number];
|
---|
133 | }
|
---|
134 | return ""; // index out of bounds
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Generates links to equivalent documents for a document with a default
|
---|
139 | * document icon/type. Links are generated from the parameters: a list of
|
---|
140 | * document icons which are each in turn embedded in the matching starting
|
---|
141 | * link tag in the list of docStartLinks (these starting links link to the
|
---|
142 | * equivalent documents in another format). Each link's start tag is closed
|
---|
143 | * with the corresponding closing tag in the docEndLinks list. Parameter
|
---|
144 | * token is the list separator. Parameter divider is the string that should
|
---|
145 | * separate each final link generated from the next. Returns a string that
|
---|
146 | * represents a sequence of links to equivalent documents, where the anchor
|
---|
147 | * is a document icon.
|
---|
148 | */
|
---|
149 | public static String getEquivDocLinks(String token, String docIconsString, String docStartLinksString, String docEndLinksString, String divider)
|
---|
150 | {
|
---|
151 | String[] docIcons = StringUtils.split(docIconsString, token, -1);
|
---|
152 | String[] startLinks = StringUtils.split(docStartLinksString, token, -1);
|
---|
153 | String[] endLinks = StringUtils.split(docEndLinksString, token, -1);
|
---|
154 |
|
---|
155 | StringBuffer buffer = new StringBuffer();
|
---|
156 | for (int i = 0; i < docIcons.length; i++)
|
---|
157 | {
|
---|
158 | if (i > 0)
|
---|
159 | {
|
---|
160 | buffer.append(divider);
|
---|
161 | }
|
---|
162 | buffer.append(startLinks[i] + docIcons[i] + endLinks[i]);
|
---|
163 | }
|
---|
164 |
|
---|
165 | return buffer.toString();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public static String tidyWhitespace(String original)
|
---|
169 | {
|
---|
170 |
|
---|
171 | if (original == null || original.equals(""))
|
---|
172 | {
|
---|
173 | return original;
|
---|
174 | }
|
---|
175 | String new_s = original.replaceAll("\\s+", " ");
|
---|
176 | return new_s;
|
---|
177 | }
|
---|
178 |
|
---|
179 | public static String getInterfaceText(String interface_name, String lang, String key)
|
---|
180 | {
|
---|
181 | return getInterfaceText(interface_name, lang, key, null);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public static String getInterfaceText(String interface_name, String lang, String key, String args_str)
|
---|
185 | {
|
---|
186 | key = key.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
187 |
|
---|
188 | String[] args = null;
|
---|
189 | if (args_str != null && !args_str.equals(""))
|
---|
190 | {
|
---|
191 | args = StringUtils.split(args_str, ";");
|
---|
192 | }
|
---|
193 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
194 | String result = dict.get(key, args);
|
---|
195 | if (result == null)
|
---|
196 | { // not found
|
---|
197 | //if not found, search a separate subdirectory named by the interface name
|
---|
198 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
199 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
200 | result = dict.get(key, args);
|
---|
201 | if (result != null)
|
---|
202 | {
|
---|
203 | result = result.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
204 | return result;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (result == null && !interface_name.equals("default"))
|
---|
209 | { // not found, try the default interface
|
---|
210 | dict = new Dictionary("interface_default", lang);
|
---|
211 | result = dict.get(key, args);
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (result == null)
|
---|
215 | { // not found
|
---|
216 | return "_" + key + "_";
|
---|
217 | }
|
---|
218 | result = result.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
219 | return result;
|
---|
220 | }
|
---|
221 |
|
---|
222 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg_node)
|
---|
223 | {
|
---|
224 | String[] args = new String[1];
|
---|
225 |
|
---|
226 | String node_str = XMLConverter.getString(arg_node);
|
---|
227 | args[0] = node_str;
|
---|
228 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
229 | String result = dict.get(key, args);
|
---|
230 | if (result == null)
|
---|
231 | { // not found
|
---|
232 | //if not found, search a separate subdirectory named by the interface name
|
---|
233 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
234 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
235 | result = dict.get(key, args);
|
---|
236 | if (result != null)
|
---|
237 | {
|
---|
238 | return result;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | if (result == null && !interface_name.equals("default"))
|
---|
243 | { // not found, try the default interface
|
---|
244 | dict = new Dictionary("interface_default", lang);
|
---|
245 | result = dict.get(key, args);
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (result == null)
|
---|
249 | { // not found
|
---|
250 | return "_" + key + "_";
|
---|
251 | }
|
---|
252 |
|
---|
253 | return result;
|
---|
254 | }
|
---|
255 |
|
---|
256 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg1_node, Node arg2_node)
|
---|
257 | {
|
---|
258 | String[] args = new String[2];
|
---|
259 |
|
---|
260 | String node_str = XMLConverter.getString(arg1_node);
|
---|
261 | args[0] = node_str;
|
---|
262 | node_str = XMLConverter.getString(arg2_node);
|
---|
263 | args[1] = node_str;
|
---|
264 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
265 | String result = dict.get(key, args);
|
---|
266 | if (result == null)
|
---|
267 | { // not found
|
---|
268 | //if not found, search a separate subdirectory named by the interface name
|
---|
269 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
270 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
271 | result = dict.get(key, args);
|
---|
272 | if (result != null)
|
---|
273 | {
|
---|
274 | return result;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | if (result == null && !interface_name.equals("default"))
|
---|
279 | { // not found, try the default interface
|
---|
280 | dict = new Dictionary("interface_default", lang);
|
---|
281 | result = dict.get(key, args);
|
---|
282 | }
|
---|
283 |
|
---|
284 | if (result == null)
|
---|
285 | { // not found
|
---|
286 | return "_" + key + "_";
|
---|
287 | }
|
---|
288 |
|
---|
289 | return result;
|
---|
290 | }
|
---|
291 |
|
---|
292 | public static boolean isImage(String mimetype)
|
---|
293 | {
|
---|
294 | if (mimetype.startsWith("image/"))
|
---|
295 | {
|
---|
296 | return true;
|
---|
297 | }
|
---|
298 | return false;
|
---|
299 | }
|
---|
300 |
|
---|
301 | public static String formatDate(String date, String lang)
|
---|
302 | {
|
---|
303 |
|
---|
304 | String in_pattern = "yyyyMMdd";
|
---|
305 | String out_pattern = "dd MMMM yyyy";
|
---|
306 | if (date.length() == 6)
|
---|
307 | {
|
---|
308 | in_pattern = "yyyyMM";
|
---|
309 | }
|
---|
310 |
|
---|
311 | SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
|
---|
312 | try
|
---|
313 | {
|
---|
314 | Date d = formatter.parse(date);
|
---|
315 | formatter.applyPattern(out_pattern);
|
---|
316 | String new_date = formatter.format(d);
|
---|
317 | return new_date;
|
---|
318 | }
|
---|
319 | catch (Exception e)
|
---|
320 | {
|
---|
321 | return date;
|
---|
322 | }
|
---|
323 |
|
---|
324 | }
|
---|
325 |
|
---|
326 | public static String formatLanguage(String display_lang, String lang)
|
---|
327 | {
|
---|
328 |
|
---|
329 | return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
|
---|
330 | }
|
---|
331 |
|
---|
332 | public static String cgiSafe(String original, String lang)
|
---|
333 | {
|
---|
334 |
|
---|
335 | original = original.replace('&', ' ');
|
---|
336 | original = original.replaceAll(" ", "%20");
|
---|
337 | return original;
|
---|
338 | }
|
---|
339 |
|
---|
340 | public static String formatBigNumber(String num)
|
---|
341 | {
|
---|
342 |
|
---|
343 | String num_str = num;
|
---|
344 | char[] num_chars = num_str.toCharArray();
|
---|
345 | String zero_str = "";
|
---|
346 | String formatted_str = "";
|
---|
347 |
|
---|
348 | for (int i = num_chars.length - 4; i >= 0; i--)
|
---|
349 | {
|
---|
350 | zero_str += '0';
|
---|
351 | }
|
---|
352 |
|
---|
353 | String sig_str = "";
|
---|
354 | for (int i = 0; i < 3 && i < num_chars.length; i++)
|
---|
355 | {
|
---|
356 | sig_str = sig_str + num_chars[i];
|
---|
357 | if (i == 1 && i + 1 < num_chars.length)
|
---|
358 | {
|
---|
359 | sig_str = sig_str + ".";
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | int sig_int = Math.round(Float.parseFloat(sig_str));
|
---|
364 | String new_sig_str = sig_int + "";
|
---|
365 | if (sig_str.length() > 2)
|
---|
366 | {
|
---|
367 | new_sig_str = sig_int + "0";
|
---|
368 | }
|
---|
369 |
|
---|
370 | char[] final_chars = (new_sig_str + zero_str).toCharArray();
|
---|
371 | int count = 1;
|
---|
372 | for (int i = final_chars.length - 1; i >= 0; i--)
|
---|
373 | {
|
---|
374 | formatted_str = final_chars[i] + formatted_str;
|
---|
375 | if (count == 3 && i != 0)
|
---|
376 | {
|
---|
377 | formatted_str = "," + formatted_str;
|
---|
378 | count = 1;
|
---|
379 | }
|
---|
380 | else
|
---|
381 | {
|
---|
382 | count++;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | return formatted_str;
|
---|
386 | }
|
---|
387 |
|
---|
388 | public static String hashToSectionId(String hashString)
|
---|
389 | {
|
---|
390 | if (hashString == null || hashString.length() == 0)
|
---|
391 | {
|
---|
392 | return "";
|
---|
393 | }
|
---|
394 |
|
---|
395 | int firstDotIndex = hashString.indexOf(".");
|
---|
396 | if (firstDotIndex == -1)
|
---|
397 | {
|
---|
398 | return "";
|
---|
399 | }
|
---|
400 |
|
---|
401 | String sectionString = hashString.substring(firstDotIndex + 1);
|
---|
402 |
|
---|
403 | return sectionString;
|
---|
404 | }
|
---|
405 |
|
---|
406 | public static String hashToDepthClass(String hashString)
|
---|
407 | {
|
---|
408 | if (hashString == null || hashString.length() == 0)
|
---|
409 | {
|
---|
410 | return "";
|
---|
411 | }
|
---|
412 |
|
---|
413 | String sectionString = hashToSectionId(hashString);
|
---|
414 |
|
---|
415 | int count = sectionString.split("\\.").length;
|
---|
416 |
|
---|
417 | if (sectionString.equals(""))
|
---|
418 | {
|
---|
419 | return "sectionHeaderDepthTitle";
|
---|
420 | }
|
---|
421 | else
|
---|
422 | {
|
---|
423 | return "sectionHeaderDepth" + count;
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | public static String escapeNewLines(String str)
|
---|
428 | {
|
---|
429 | if (str == null || str.length() < 1)
|
---|
430 | {
|
---|
431 | return null;
|
---|
432 | }
|
---|
433 | return str.replace("\n", "\\\n");
|
---|
434 | }
|
---|
435 |
|
---|
436 | public static String escapeQuotes(String str)
|
---|
437 | {
|
---|
438 | if (str == null || str.length() < 1)
|
---|
439 | {
|
---|
440 | return null;
|
---|
441 | }
|
---|
442 | return str.replace("\"", "\\\"");
|
---|
443 | }
|
---|
444 |
|
---|
445 | public static String escapeNewLinesAndQuotes(String str)
|
---|
446 | {
|
---|
447 | if (str == null || str.length() < 1)
|
---|
448 | {
|
---|
449 | return null;
|
---|
450 | }
|
---|
451 | return escapeNewLines(escapeQuotes(str));
|
---|
452 | }
|
---|
453 |
|
---|
454 | public static void clearMetadataStorage()
|
---|
455 | {
|
---|
456 | _foundTableValues.clear();
|
---|
457 | }
|
---|
458 |
|
---|
459 | public static boolean checkMetadataNotDuplicate(String name, String value)
|
---|
460 | {
|
---|
461 | if(_foundTableValues.containsKey(name))
|
---|
462 | {
|
---|
463 | for(String mapValue : _foundTableValues.get(name))
|
---|
464 | {
|
---|
465 | if(mapValue.equals(value))
|
---|
466 | {
|
---|
467 | return false;
|
---|
468 | }
|
---|
469 | }
|
---|
470 | _foundTableValues.get(name).add(value);
|
---|
471 | return true;
|
---|
472 | }
|
---|
473 |
|
---|
474 | ArrayList<String> newList = new ArrayList<String>();
|
---|
475 | newList.add(value);
|
---|
476 |
|
---|
477 | _foundTableValues.put(name, newList);
|
---|
478 |
|
---|
479 | return true;
|
---|
480 | }
|
---|
481 |
|
---|
482 | public static String reCAPTCHAimage()
|
---|
483 | {
|
---|
484 | ReCaptcha c = ReCaptchaFactory.newReCaptcha("6LckI88SAAAAACUYjj97WMcnz5HPjVp3lI-x-ue8", "6LckI88SAAAAAGnGy1PwuXYZzIMXZYoPxN51bWWG", false);
|
---|
485 | return c.createRecaptchaHtml(null, null);
|
---|
486 | }
|
---|
487 | }
|
---|