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