source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XSLTUtil.java@ 26279

Last change on this file since 26279 was 26279, checked in by sjm84, 12 years ago

Adding months to date list

  • Property svn:keywords set to Author Date Id Revision
File size: 18.0 KB
Line 
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 */
19package org.greenstone.gsdl3.util;
20
21import java.io.File;
22import java.text.SimpleDateFormat;
23import java.util.ArrayList;
24import java.util.Date;
25import java.util.Enumeration;
26import java.util.HashMap;
27import java.util.Locale;
28
29import net.tanesha.recaptcha.ReCaptcha;
30import net.tanesha.recaptcha.ReCaptchaFactory;
31
32import org.apache.commons.lang3.StringUtils;
33import org.apache.log4j.Logger;
34import org.greenstone.util.GlobalProperties;
35import org.w3c.dom.Node;
36import org.w3c.dom.NodeList;
37
38/**
39 * a class to contain various static methods that are used by the xslt
40 * stylesheets
41 */
42public 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 protected static HashMap<String, String> _stringVariables = new HashMap<String, String>();
47
48 public static void storeString(String name, String value)
49 {
50 _stringVariables.put(name, value);
51 }
52
53 public static String getString(String name)
54 {
55 return _stringVariables.get(name);
56 }
57
58 /* some tests */
59 public static boolean equals(String s1, String s2)
60 {
61 return s1.equals(s2);
62 }
63
64 public static boolean notEquals(String s1, String s2)
65 {
66 return !s1.equals(s2);
67 }
68
69 public static boolean exists(String s1, String s2)
70 {
71 return !s1.equals("");
72 }
73
74 public static boolean contains(String s1, String s2)
75 {
76 return (s1.indexOf(s2) != -1);
77 }
78
79 public static boolean startsWith(String s1, String s2)
80 {
81 return s1.startsWith(s2);
82 }
83
84 public static boolean endsWith(String s1, String s2)
85 {
86 return s1.endsWith(s2);
87 }
88
89 public static boolean lessThan(String s1, String s2)
90 {
91 return (s1.compareTo(s2) < 0);
92 }
93
94 public static boolean lessThanOrEquals(String s1, String s2)
95 {
96 return (s1.compareTo(s2) <= 0);
97 }
98
99 public static boolean greaterThan(String s1, String s2)
100 {
101 return (s1.compareTo(s2) > 0);
102 }
103
104 public static boolean greaterThanOrEquals(String s1, String s2)
105 {
106 return (s1.compareTo(s2) >= 0);
107 }
108
109 public static boolean oidIsMatchOrParent(String first, String second)
110 {
111 if (first.equals(second))
112 {
113 return true;
114 }
115
116 String[] firstParts = first.split(".");
117 String[] secondParts = second.split(".");
118
119 if (firstParts.length >= secondParts.length)
120 {
121 return false;
122 }
123
124 for (int i = 0; i < firstParts.length; i++)
125 {
126 if (!firstParts[i].equals(secondParts[i]))
127 {
128 return false;
129 }
130 }
131
132 return true;
133 }
134
135 public static String replace(String orig, String match, String replacement)
136 {
137 return orig.replace(match, replacement);
138 }
139
140 public static String getNumberedItem(String list, int number)
141 {
142 String[] items = StringUtils.split(list, ",", -1);
143 if (items.length > number)
144 {
145 return items[number];
146 }
147 return ""; // index out of bounds
148 }
149
150 /**
151 * Generates links to equivalent documents for a document with a default
152 * document icon/type. Links are generated from the parameters: a list of
153 * document icons which are each in turn embedded in the matching starting
154 * link tag in the list of docStartLinks (these starting links link to the
155 * equivalent documents in another format). Each link's start tag is closed
156 * with the corresponding closing tag in the docEndLinks list. Parameter
157 * token is the list separator. Parameter divider is the string that should
158 * separate each final link generated from the next. Returns a string that
159 * represents a sequence of links to equivalent documents, where the anchor
160 * is a document icon.
161 */
162 public static String getEquivDocLinks(String token, String docIconsString, String docStartLinksString, String docEndLinksString, String divider)
163 {
164 String[] docIcons = StringUtils.split(docIconsString, token, -1);
165 String[] startLinks = StringUtils.split(docStartLinksString, token, -1);
166 String[] endLinks = StringUtils.split(docEndLinksString, token, -1);
167
168 StringBuffer buffer = new StringBuffer();
169 for (int i = 0; i < docIcons.length; i++)
170 {
171 if (i > 0)
172 {
173 buffer.append(divider);
174 }
175 buffer.append(startLinks[i] + docIcons[i] + endLinks[i]);
176 }
177
178 return buffer.toString();
179 }
180
181 public static String getInterfaceText(String interface_name, String lang, String key)
182 {
183 return getInterfaceText(interface_name, lang, key, null);
184 }
185
186 public static String getInterfaceText(String interface_name, String lang, String key, String args_str)
187 {
188 key = key.replaceAll("__INTERFACE_NAME__", interface_name);
189
190 String[] args = null;
191 if (args_str != null && !args_str.equals(""))
192 {
193 args = StringUtils.split(args_str, ";");
194 }
195 Dictionary dict = new Dictionary("interface_" + interface_name, lang);
196 String result = dict.get(key, args);
197 if (result == null)
198 { // not found
199 //if not found, search a separate subdirectory named by the interface name
200 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
201 dict = new Dictionary(sep_interface_dir, lang);
202 result = dict.get(key, args);
203 if (result != null)
204 {
205 result = result.replaceAll("__INTERFACE_NAME__", interface_name);
206 return result;
207 }
208 }
209
210 if (result == null && !interface_name.equals("default"))
211 { // not found, try the default interface
212 dict = new Dictionary("interface_default", lang);
213 result = dict.get(key, args);
214 }
215
216 if (result == null)
217 { // not found
218 return "_" + key + "_";
219 }
220 result = result.replaceAll("__INTERFACE_NAME__", interface_name);
221 return result;
222 }
223
224 public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg_node)
225 {
226 String[] args = new String[1];
227
228 String node_str = XMLConverter.getString(arg_node);
229 args[0] = node_str;
230 Dictionary dict = new Dictionary("interface_" + interface_name, lang);
231 String result = dict.get(key, args);
232 if (result == null)
233 { // not found
234 //if not found, search a separate subdirectory named by the interface name
235 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
236 dict = new Dictionary(sep_interface_dir, lang);
237 result = dict.get(key, args);
238 if (result != null)
239 {
240 return result;
241 }
242 }
243
244 if (result == null && !interface_name.equals("default"))
245 { // not found, try the default interface
246 dict = new Dictionary("interface_default", lang);
247 result = dict.get(key, args);
248 }
249
250 if (result == null)
251 { // not found
252 return "_" + key + "_";
253 }
254
255 return result;
256 }
257
258 public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg1_node, Node arg2_node)
259 {
260 String[] args = new String[2];
261
262 String node_str = XMLConverter.getString(arg1_node);
263 args[0] = node_str;
264 node_str = XMLConverter.getString(arg2_node);
265 args[1] = node_str;
266 Dictionary dict = new Dictionary("interface_" + interface_name, lang);
267 String result = dict.get(key, args);
268 if (result == null)
269 { // not found
270 //if not found, search a separate subdirectory named by the interface name
271 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
272 dict = new Dictionary(sep_interface_dir, lang);
273 result = dict.get(key, args);
274 if (result != null)
275 {
276 return result;
277 }
278 }
279
280 if (result == null && !interface_name.equals("default"))
281 { // not found, try the default interface
282 dict = new Dictionary("interface_default", lang);
283 result = dict.get(key, args);
284 }
285
286 if (result == null)
287 { // not found
288 return "_" + key + "_";
289 }
290
291 return result;
292 }
293
294 public static String getCollectionText(String collection, String site_name, String lang, String key)
295 {
296 return getCollectionTextWithDOMMulti(collection, site_name, lang, key);
297 }
298
299 // xslt didn't like calling the function with Node varargs, so have this hack for now
300 public static String getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1)
301 {
302 return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1);
303 }
304
305 public static String getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2)
306 {
307 return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2);
308 }
309
310 public static String getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3)
311 {
312 return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2, n3);
313 }
314
315 public static String getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3, Node n4)
316 {
317 return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2, n3, n4);
318 }
319
320 public static String getCollectionTextWithDOMMulti(String collection, String site_name, String lang, String key, Node... nodes)
321 {
322 int num_nodes = nodes.length;
323 String[] args = null;
324 if (num_nodes != 0)
325 {
326 args = new String[num_nodes];
327
328 for (int i = 0; i < num_nodes; i++)
329 {
330
331 String node_str = XMLConverter.getString(nodes[i]);
332 args[i] = node_str;
333 }
334 }
335
336 CollectionClassLoader class_loader = new CollectionClassLoader(XSLTUtil.class.getClassLoader(), GSFile.siteHome(GlobalProperties.getGSDL3Home(), site_name), collection);
337 Dictionary dict = new Dictionary(collection, lang, class_loader);
338 String result = dict.get(key, args);
339 if (result != null)
340 {
341 return result;
342 }
343 return "text:" + collection + ":" + key;
344
345 }
346
347 public static boolean isImage(String mimetype)
348 {
349 if (mimetype.startsWith("image/"))
350 {
351 return true;
352 }
353 return false;
354 }
355
356 // formatting /preprocessing functions
357 // some require a language, so we'll have a language param for all
358 public static String toLower(String orig, String lang)
359 {
360 return orig.toLowerCase();
361 }
362
363 public static String toUpper(String orig, String lang)
364 {
365 return orig.toUpperCase();
366 }
367
368 public static String tidyWhitespace(String original, String lang)
369 {
370
371 if (original == null || original.equals(""))
372 {
373 return original;
374 }
375 String new_s = original.replaceAll("\\s+", " ");
376 return new_s;
377 }
378
379 public static String stripWhitespace(String original, String lang)
380 {
381
382 if (original == null || original.equals(""))
383 {
384 return original;
385 }
386 String new_s = original.replaceAll("\\s+", "");
387 return new_s;
388 }
389
390 public static byte[] toUTF8(String orig, String lang)
391 {
392 try
393 {
394 byte[] utf8 = orig.getBytes("UTF-8");
395 return utf8;
396 }
397 catch (Exception e)
398 {
399 logger.error("unsupported encoding");
400 return orig.getBytes();
401 }
402 }
403
404 public static String formatDate(String date, String lang)
405 {
406 String in_pattern = "yyyyMMdd";
407 String out_pattern = "dd MMMM yyyy";
408 if (date.length() == 6)
409 {
410 in_pattern = "yyyyMM";
411 }
412
413 SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
414 try
415 {
416 Date d = formatter.parse(date);
417 formatter.applyPattern(out_pattern);
418 String new_date = formatter.format(d);
419 return new_date;
420 }
421 catch (Exception e)
422 {
423 return date;
424 }
425
426 }
427
428 public static String getDetailFromDate(String date, String detail, String lang)
429 {
430 String in_pattern = "yyyyMMdd";
431 if (date.length() == 6)
432 {
433 in_pattern = "yyyyMM";
434 }
435
436 SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
437 try
438 {
439 Date d = formatter.parse(date);
440 if (detail.toLowerCase().equals("day"))
441 {
442 formatter.applyPattern("dd");
443 }
444 else if (detail.toLowerCase().equals("month"))
445 {
446 formatter.applyPattern("MMMM");
447 }
448 else if (detail.toLowerCase().equals("year"))
449 {
450 formatter.applyPattern("yyyy");
451 }
452 else
453 {
454 return "";
455 }
456 return formatter.format(d);
457 }
458 catch (Exception ex)
459 {
460 return "";
461 }
462 }
463
464 public static String formatLanguage(String display_lang, String lang)
465 {
466
467 return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
468 }
469
470 public static String cgiSafe(String original, String lang)
471 {
472
473 original = original.replace('&', ' ');
474 original = original.replaceAll(" ", "%20");
475 return original;
476 }
477
478 public static String formatBigNumber(String num, String lang)
479 {
480
481 String num_str = num;
482 char[] num_chars = num_str.toCharArray();
483 String zero_str = "";
484 String formatted_str = "";
485
486 for (int i = num_chars.length - 4; i >= 0; i--)
487 {
488 zero_str += '0';
489 }
490
491 String sig_str = "";
492 for (int i = 0; i < 3 && i < num_chars.length; i++)
493 {
494 sig_str = sig_str + num_chars[i];
495 if (i == 1 && i + 1 < num_chars.length)
496 {
497 sig_str = sig_str + ".";
498 }
499 }
500
501 int sig_int = Math.round(Float.parseFloat(sig_str));
502 String new_sig_str = sig_int + "";
503 if (sig_str.length() > 2)
504 {
505 new_sig_str = sig_int + "0";
506 }
507
508 char[] final_chars = (new_sig_str + zero_str).toCharArray();
509 int count = 1;
510 for (int i = final_chars.length - 1; i >= 0; i--)
511 {
512 formatted_str = final_chars[i] + formatted_str;
513 if (count == 3 && i != 0)
514 {
515 formatted_str = "," + formatted_str;
516 count = 1;
517 }
518 else
519 {
520 count++;
521 }
522 }
523 return formatted_str;
524 }
525
526 public static String hashToSectionId(String hashString)
527 {
528 if (hashString == null || hashString.length() == 0)
529 {
530 return "";
531 }
532
533 int firstDotIndex = hashString.indexOf(".");
534 if (firstDotIndex == -1)
535 {
536 return "";
537 }
538
539 String sectionString = hashString.substring(firstDotIndex + 1);
540
541 return sectionString;
542 }
543
544 public static String hashToDepthClass(String hashString)
545 {
546 if (hashString == null || hashString.length() == 0)
547 {
548 return "";
549 }
550
551 String sectionString = hashToSectionId(hashString);
552
553 int count = sectionString.split("\\.").length;
554
555 if (sectionString.equals(""))
556 {
557 return "sectionHeaderDepthTitle";
558 }
559 else
560 {
561 return "sectionHeaderDepth" + count;
562 }
563 }
564
565 public static String escapeNewLines(String str)
566 {
567 if (str == null || str.length() < 1)
568 {
569 return null;
570 }
571 return str.replace("\n", "\\\n");
572 }
573
574 public static String escapeQuotes(String str)
575 {
576 if (str == null || str.length() < 1)
577 {
578 return null;
579 }
580 return str.replace("\"", "\\\"");
581 }
582
583 public static String escapeNewLinesAndQuotes(String str)
584 {
585 if (str == null || str.length() < 1)
586 {
587 return null;
588 }
589 return escapeNewLines(escapeQuotes(str));
590 }
591
592 public static String getGlobalProperty(String name)
593 {
594 return GlobalProperties.getProperty(name);
595 }
596
597 public static void clearMetadataStorage()
598 {
599 _foundTableValues.clear();
600 }
601
602 public static boolean checkMetadataNotDuplicate(String name, String value)
603 {
604 if (_foundTableValues.containsKey(name))
605 {
606 for (String mapValue : _foundTableValues.get(name))
607 {
608 if (mapValue.equals(value))
609 {
610 return false;
611 }
612 }
613 _foundTableValues.get(name).add(value);
614 return true;
615 }
616
617 ArrayList<String> newList = new ArrayList<String>();
618 newList.add(value);
619
620 _foundTableValues.put(name, newList);
621
622 return true;
623 }
624
625 public static String reCAPTCHAimage(String publicKey, String privateKey)
626 {
627 ReCaptcha c = ReCaptchaFactory.newReCaptcha(publicKey, privateKey, false);
628 return c.createRecaptchaHtml(null, null);
629 }
630
631 public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix)
632 {
633 String prependToPrefix = "gs.text";
634 return XSLTUtil.getInterfaceStringsAsJavascript(interface_name, lang, prefix, prependToPrefix);
635 }
636
637 // generates javascript: 2 arrays are declared and populated with strings that declare variables and assign their values
638 // to be strings loaded from the interface_name.properties file for the language.
639 public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix, String prependToPrefix)
640 {
641 // 1. Generating Javascript of the form:
642 // if(!gs.text) { gs.text = new Array(); }
643 // if(!gs.text.dse) { gs.text.dse = new Array(); }
644 StringBuffer outputStr = new StringBuffer();
645 outputStr.append("if(!gs.text) { ");
646 outputStr.append(prependToPrefix + " = new Array(); ");
647 outputStr.append("}\n");
648 outputStr.append("if(!gs.text." + prefix + ") { ");
649 outputStr.append(prependToPrefix + "." + prefix + " = new Array(); ");
650 outputStr.append("}\n");
651
652 Dictionary dict = new Dictionary("interface_" + interface_name, lang);
653 Enumeration keys = dict.getKeys();
654 if (keys == null)
655 { // try default interface
656 //logger.debug("****** Interface name: " + interface_name + " does not have any keys. Trying interface_default.");
657 dict = new Dictionary("interface_default", lang);
658 keys = dict.getKeys();
659 }
660
661 // Get all properties in the language-specific dictionary with the given key prefix
662 // Create Javascript strings of the form:
663 // prependToPrefix.key= "value";\n
664 while (keys.hasMoreElements())
665 {
666 String key = (String) keys.nextElement();
667 if (key.startsWith(prefix))
668 {
669 String value = getInterfaceText(interface_name, lang, key);
670
671 outputStr.append(prependToPrefix);
672 outputStr.append(".");
673 outputStr.append(key);
674 outputStr.append("=\"");
675 outputStr.append(value);
676 outputStr.append("\";\n");
677 }
678 }
679
680 return outputStr.toString();
681 }
682
683 public static String xmlNodeToString(Node node)
684 {
685 return GSXML.xmlNodeToString(node);
686 }
687
688 // Test from cmdline with:
689 // java -classpath /research/ak19/gs3-svn/web/WEB-INF/lib/gsdl3.jar:/research/ak19/gs3-svn/web/WEB-INF/lib/log4j-1.2.8.jar:/research/ak19/gs3-svn/web/WEB-INF/classes/ org.greenstone.gsdl3.util.XSLTUtil
690 public static void main(String args[])
691 {
692 System.out.println("\n@@@@@\n" + XSLTUtil.getInterfaceStringsAsJavascript("default", "en", "dse", "gs.text") + "@@@@@\n");
693 }
694}
Note: See TracBrowser for help on using the repository browser.