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

Last change on this file since 31866 was 31866, checked in by kjdon, 7 years ago

handle properly 6 digit dates - only output month and year otherwise you get erroneous 01 day. Also handle the case where user has put 00 for the day - remove and pretend it was 6 digits

  • Property svn:keywords set to Author Date Id Revision
File size: 23.3 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.ByteArrayInputStream;
22import java.io.File;
23import java.io.UnsupportedEncodingException;
24import java.net.URLEncoder;
25import java.text.DateFormat;
26import java.text.SimpleDateFormat;
27import java.util.ArrayList;
28import java.util.Date;
29import java.util.Enumeration;
30import java.util.HashMap;
31import java.util.Locale;
32
33import javax.xml.parsers.DocumentBuilder;
34import javax.xml.parsers.DocumentBuilderFactory;
35
36import net.tanesha.recaptcha.ReCaptcha;
37import net.tanesha.recaptcha.ReCaptchaFactory;
38
39import org.apache.commons.lang3.StringUtils;
40import org.apache.log4j.Logger;
41import org.greenstone.util.GlobalProperties;
42import org.w3c.dom.Node;
43
44/**
45 * a class to contain various static methods that are used by the xslt
46 * stylesheets
47 */
48public class XSLTUtil
49{
50 protected static HashMap<String, ArrayList<String>> _foundTableValues = new HashMap<String, ArrayList<String>>();
51 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XSLTUtil.class.getName());
52 protected static HashMap<String, String> _stringVariables = new HashMap<String, String>();
53
54 public static void storeString(String name, String value)
55 {
56 _stringVariables.put(name, value);
57 }
58
59 public static String getString(String name)
60 {
61 return _stringVariables.get(name);
62 }
63
64 /* some tests */
65 public static boolean equals(String s1, String s2)
66 {
67 return s1.equals(s2);
68 }
69
70 public static boolean notEquals(String s1, String s2)
71 {
72 return !s1.equals(s2);
73 }
74
75 public static boolean exists(String s1, String s2)
76 {
77 return !s1.equals("");
78 }
79
80 public static boolean contains(String s1, String s2)
81 {
82 return (s1.indexOf(s2) != -1);
83 }
84
85 public static boolean startsWith(String s1, String s2)
86 {
87 return s1.startsWith(s2);
88 }
89
90 public static boolean endsWith(String s1, String s2)
91 {
92 return s1.endsWith(s2);
93 }
94
95 public static boolean lessThan(String s1, String s2)
96 {
97 return (s1.compareTo(s2) < 0);
98 }
99
100 public static boolean lessThanOrEquals(String s1, String s2)
101 {
102 return (s1.compareTo(s2) <= 0);
103 }
104
105 public static boolean greaterThan(String s1, String s2)
106 {
107 return (s1.compareTo(s2) > 0);
108 }
109
110 public static boolean greaterThanOrEquals(String s1, String s2)
111 {
112 return (s1.compareTo(s2) >= 0);
113 }
114
115 public static boolean oidIsMatchOrParent(String first, String second)
116 {
117 if (first.equals(second))
118 {
119 return true;
120 }
121
122 String[] firstParts = first.split(".");
123 String[] secondParts = second.split(".");
124
125 if (firstParts.length >= secondParts.length)
126 {
127 return false;
128 }
129
130 for (int i = 0; i < firstParts.length; i++)
131 {
132 if (!firstParts[i].equals(secondParts[i]))
133 {
134 return false;
135 }
136 }
137
138 return true;
139 }
140
141 public static String oidDocumentRoot(String oid)
142 {
143 String[] oidParts = oid.split("\\.");
144
145 return oidParts[0];
146 }
147
148 public static String replace(String orig, String match, String replacement)
149 {
150 return orig.replace(match, replacement);
151 }
152
153 public static String getNumberedItem(String list, int number)
154 {
155 String[] items = list.split(",", -1); //String[] items = StringUtils.split(list, ",", -1);
156 // Using StringUtils.split() causes an off-by-one error for the boolean operators (fqk)
157 // where boolean operators combining rows in multiforms are shifted up by 1 row.
158
159 if (items.length > number)
160 {
161 return items[number];
162 }
163 return ""; // index out of bounds
164 }
165
166 /**
167 * Generates links to equivalent documents for a document with a default
168 * document icon/type. Links are generated from the parameters: a list of
169 * document icons which are each in turn embedded in the matching starting
170 * link tag in the list of docStartLinks (these starting links link to the
171 * equivalent documents in another format). Each link's start tag is closed
172 * with the corresponding closing tag in the docEndLinks list. Parameter
173 * token is the list separator. Parameter divider is the string that should
174 * separate each final link generated from the next. Returns a string that
175 * represents a sequence of links to equivalent documents, where the anchor
176 * is a document icon.
177 */
178 public static String getEquivDocLinks(String token, String docIconsString, String docStartLinksString, String docEndLinksString, String divider)
179 {
180 String[] docIcons = StringUtils.split(docIconsString, token, -1);
181 String[] startLinks = StringUtils.split(docStartLinksString, token, -1);
182 String[] endLinks = StringUtils.split(docEndLinksString, token, -1);
183
184 StringBuffer buffer = new StringBuffer();
185 for (int i = 0; i < docIcons.length; i++)
186 {
187 if (i > 0)
188 {
189 buffer.append(divider);
190 }
191 buffer.append(startLinks[i] + docIcons[i] + endLinks[i]);
192 }
193
194 return buffer.toString();
195 }
196
197 public static String getInterfaceText(String interface_name, String lang, String key)
198 {
199 return getInterfaceText(interface_name, lang, key, null);
200 }
201
202 public static String getInterfaceText(String interface_name, String lang, String key, String args_str)
203 {
204 key = key.replaceAll("__INTERFACE_NAME__", interface_name);
205
206 String[] args = null;
207 if (args_str != null && !args_str.equals(""))
208 {
209 args = StringUtils.split(args_str, ";");
210 }
211 Dictionary dict = new Dictionary("interface_" + interface_name, lang);
212 String result = dict.get(key, args);
213 if (result == null)
214 { // not found
215 //if not found, search a separate subdirectory named by the interface name
216 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
217 dict = new Dictionary(sep_interface_dir, lang);
218 result = dict.get(key, args);
219 if (result != null)
220 {
221 result = result.replaceAll("__INTERFACE_NAME__", interface_name);
222 return result;
223 }
224 }
225
226 if (result == null && !interface_name.equals("default"))
227 { // not found, try the default interface
228 dict = new Dictionary("interface_default", lang);
229 result = dict.get(key, args);
230 }
231
232 if (result == null)
233 { // not found
234 return "_" + key + "_";
235 }
236 result = result.replaceAll("__INTERFACE_NAME__", interface_name);
237 return result;
238 }
239
240 public static String getInterfaceText(String interfaceName, String dictionaryName, String lang, String key, String args_str)
241 {
242 key = key.replaceAll("__INTERFACE_NAME__", interfaceName);
243
244 String[] args = null;
245 if (args_str != null && !args_str.equals(""))
246 {
247 args = StringUtils.split(args_str, ";");
248 }
249 Dictionary dict = new Dictionary(dictionaryName, lang);
250 String result = dict.get(key, args);
251 if (result == null)
252 { // not found
253 //if not found, search a separate subdirectory named by the interface name
254 String sep_interface_dir = interfaceName + File.separatorChar + lang + File.separatorChar + "interface";
255 dict = new Dictionary(sep_interface_dir, lang);
256 result = dict.get(key, args);
257 if (result != null)
258 {
259 result = result.replaceAll("__INTERFACE_NAME__", interfaceName);
260 return result;
261 }
262 }
263
264 if (result == null && !interfaceName.equals("default"))
265 { // not found, try the default interface
266 dict = new Dictionary("interface_default", lang);
267 result = dict.get(key, args);
268 }
269
270 if (result == null)
271 { // not found
272 return "_" + key + "_";
273 }
274 result = result.replaceAll("__INTERFACE_NAME__", interfaceName);
275 return result;
276 }
277
278 public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg_node)
279 {
280 String[] args = new String[1];
281
282 String node_str = XMLConverter.getString(arg_node);
283 args[0] = node_str;
284 Dictionary dict = new Dictionary("interface_" + interface_name, lang);
285 String result = dict.get(key, args);
286 if (result == null)
287 { // not found
288 //if not found, search a separate subdirectory named by the interface name
289 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
290 dict = new Dictionary(sep_interface_dir, lang);
291 result = dict.get(key, args);
292 if (result != null)
293 {
294 return result;
295 }
296 }
297
298 if (result == null && !interface_name.equals("default"))
299 { // not found, try the default interface
300 dict = new Dictionary("interface_default", lang);
301 result = dict.get(key, args);
302 }
303
304 if (result == null)
305 { // not found
306 return "_" + key + "_";
307 }
308
309 return result;
310 }
311
312 public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg1_node, Node arg2_node)
313 {
314 String[] args = new String[2];
315
316 String node_str = XMLConverter.getString(arg1_node);
317 args[0] = node_str;
318 node_str = XMLConverter.getString(arg2_node);
319 args[1] = node_str;
320 Dictionary dict = new Dictionary("interface_" + interface_name, lang);
321 String result = dict.get(key, args);
322 if (result == null)
323 { // not found
324 //if not found, search a separate subdirectory named by the interface name
325 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
326 dict = new Dictionary(sep_interface_dir, lang);
327 result = dict.get(key, args);
328 if (result != null)
329 {
330 return result;
331 }
332 }
333
334 if (result == null && !interface_name.equals("default"))
335 { // not found, try the default interface
336 dict = new Dictionary("interface_default", lang);
337 result = dict.get(key, args);
338 }
339
340 if (result == null)
341 { // not found
342 return "_" + key + "_";
343 }
344
345 return result;
346 }
347
348
349 public static String getInterfaceTextSubstituteArgs(String value, String args_str)
350 {
351 String[] args = null;
352 if (args_str != null && !args_str.equals("")) {
353 args = StringUtils.split(args_str, ";");
354 }
355
356 return Dictionary.processArgs(value,args);
357 }
358
359 public static Node getCollectionText(String collection, String site_name, String lang, String key)
360 {
361 return getCollectionTextWithArgs(collection, site_name, lang, key, null);
362 }
363
364 public static Node getCollectionText(String collection, String site_name, String lang, String key, String args_str)
365 {
366
367 String[] args = null;
368 if (args_str != null && !args_str.equals(""))
369 {
370 args = StringUtils.split(args_str, ";");
371 }
372
373 return getCollectionTextWithArgs(collection, site_name, lang, key, args);
374 }
375
376 // xslt didn't like calling the function with Node varargs, so have this hack for now
377 public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1)
378 {
379 return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1);
380 }
381
382 public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2)
383 {
384 return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2);
385 }
386
387 public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3)
388 {
389 return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2, n3);
390 }
391
392 public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3, Node n4)
393 {
394 return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2, n3, n4);
395 }
396
397 public static Node getCollectionTextWithDOMMulti(String collection, String site_name, String lang, String key, Node... nodes)
398 {
399 int num_nodes = nodes.length;
400 String[] args = null;
401 if (num_nodes != 0)
402 {
403 args = new String[num_nodes];
404
405 for (int i = 0; i < num_nodes; i++)
406 {
407 String node_str = XMLConverter.getString(nodes[i]);
408 args[i] = node_str;
409 }
410 }
411 return getCollectionTextWithArgs(collection, site_name, lang, key, args);
412 }
413
414 public static Node getCollectionTextWithArgs(String collection, String site_name, String lang, String key, String[] args)
415 {
416 try
417 {
418 DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
419
420 CustomClassLoader class_loader = new CustomClassLoader(XSLTUtil.class.getClassLoader(), GSFile.collectionResourceDir(GSFile.siteHome(GlobalProperties.getGSDL3Home(), site_name), collection));
421 Dictionary dict = new Dictionary(collection, lang, class_loader);
422 String result = dict.get(key, args);
423 if (result != null)
424 {
425 return docBuilder.parse(new ByteArrayInputStream(("<fragment>" + result + "</fragment>").getBytes("UTF-8"))).getDocumentElement();
426 }
427 return docBuilder.parse(new ByteArrayInputStream(("<fragment>" + "text:" + collection + ":" + key + "</fragment>").getBytes())).getDocumentElement();
428 }
429 catch (Exception ex)
430 {
431 return null;
432 }
433 }
434
435 public static boolean isImage(String mimetype)
436 {
437 if (mimetype.startsWith("image/"))
438 {
439 return true;
440 }
441 return false;
442 }
443
444 // formatting /preprocessing functions
445 // some require a language, so we'll have a language param for all
446 public static String toLower(String orig, String lang)
447 {
448 return orig.toLowerCase();
449 }
450
451 public static String toUpper(String orig, String lang)
452 {
453 return orig.toUpperCase();
454 }
455
456 public static String tidyWhitespace(String original, String lang)
457 {
458
459 if (original == null || original.equals(""))
460 {
461 return original;
462 }
463 String new_s = original.replaceAll("\\s+", " ");
464 return new_s;
465 }
466
467 public static String stripWhitespace(String original, String lang)
468 {
469
470 if (original == null || original.equals(""))
471 {
472 return original;
473 }
474 String new_s = original.replaceAll("\\s+", "");
475 return new_s;
476 }
477
478 public static byte[] toUTF8(String orig, String lang)
479 {
480 try
481 {
482 byte[] utf8 = orig.getBytes("UTF-8");
483 return utf8;
484 }
485 catch (Exception e)
486 {
487 logger.error("unsupported encoding");
488 return orig.getBytes();
489 }
490 }
491
492 public static String formatDate(String date, String lang)
493 {
494 String in_pattern = "yyyyMMdd";
495 String out_pattern = "dd MMMM yyyy";
496 if (date.length() == 6)
497 {
498 in_pattern = "yyyyMM";
499 out_pattern = "MMMM yyyy";
500 }
501 // remove the 00
502 else if (date.length() == 8 && date.endsWith("00")) {
503 date = date.substring(0,6);
504 in_pattern = "yyyyMM";
505 out_pattern = "MMMM yyyy";
506 }
507
508 SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
509 try
510 {
511 Date d = formatter.parse(date);
512 formatter.applyPattern(out_pattern);
513 String new_date = formatter.format(d);
514 return new_date;
515 }
516 catch (Exception e)
517 {
518 return date;
519 }
520
521 }
522
523 public static final int TS_SECS = 0;
524 public static final int TS_MILLISECS = 1;
525 public static final int F_DATE = 0;
526 public static final int F_TIME = 1;
527 public static final int F_DATETIME = 2;
528 public static final int F_DAYSAGO = 3;
529
530 public static String formatTimeStamp(String timestamp, int ts_type, int format_type, String lang) {
531 try {
532 long ts = Long.parseLong(timestamp);
533 if (ts_type == TS_SECS) {
534 ts = ts * 1000;
535 }
536 if (format_type == F_DAYSAGO) {
537 long current_time = new Date().getTime();
538 long days = (current_time - ts)/86400000;
539 return String.valueOf(days);
540 }
541 Date d = new Date(ts);
542 DateFormat df;
543 switch (format_type) {
544 case F_DATE:
545 df = DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale(lang));
546 break;
547 case F_TIME:
548 df = DateFormat.getTimeInstance(DateFormat.DEFAULT, new Locale(lang));
549 break;
550 case F_DATETIME:
551 df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, new Locale(lang));
552 break;
553 default:
554 df = DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale(lang));
555 break;
556 }
557
558 return df.format(d);
559 } catch (Exception e) {
560
561 return timestamp + e.getMessage();
562 }
563
564 }
565 public static String getDetailFromDate(String date, String detail, String lang)
566 {
567 String in_pattern = "yyyyMMdd";
568 if (date.length() == 6)
569 {
570 in_pattern = "yyyyMM";
571 }
572
573 SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
574 try
575 {
576 Date d = formatter.parse(date);
577 if (detail.toLowerCase().equals("day"))
578 {
579 formatter.applyPattern("dd");
580 }
581 else if (detail.toLowerCase().equals("month"))
582 {
583 formatter.applyPattern("MMMM");
584 }
585 else if (detail.toLowerCase().equals("year"))
586 {
587 formatter.applyPattern("yyyy");
588 }
589 else
590 {
591 return "";
592 }
593 return formatter.format(d);
594 }
595 catch (Exception ex)
596 {
597 return "";
598 }
599 }
600
601 public static String formatLanguage(String display_lang, String lang)
602 {
603
604 return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
605 }
606
607 public static boolean checkFileExistence(String site_name, String filePath){
608
609 String gsdl3_home = GlobalProperties.getGSDL3Home();
610 //Remove leading file separator
611 filePath = filePath.replaceAll("^/+", "");
612 //Remove duplicates and replace by separator for current platform
613 filePath = filePath.replaceAll("/+", File.separator);
614 //Create full path to check
615 String fullPath = GSFile.siteHome(gsdl3_home, site_name) + File.separator + filePath;
616 File file = new File(fullPath);
617 if (file.exists() && file.isFile()) {
618 return true;
619 }
620 return false;
621 }
622
623 public static String uriEncode(String input)
624 {
625 String result = "";
626 try {
627 result = URLEncoder.encode(input, "UTF-8");
628 } catch (UnsupportedEncodingException e) {
629 e.printStackTrace();
630 }
631
632 return result;
633
634 }
635
636 public static String cgiSafe(String original, String lang)
637 {
638
639 original = original.replace('&', ' ');
640 original = original.replaceAll(" ", "%20");
641 return original;
642 }
643
644 public static String formatBigNumber(String num, String lang)
645 {
646
647 String num_str = num;
648 char[] num_chars = num_str.toCharArray();
649 String zero_str = "";
650 String formatted_str = "";
651
652 for (int i = num_chars.length - 4; i >= 0; i--)
653 {
654 zero_str += '0';
655 }
656
657 String sig_str = "";
658 for (int i = 0; i < 3 && i < num_chars.length; i++)
659 {
660 sig_str = sig_str + num_chars[i];
661 if (i == 1 && i + 1 < num_chars.length)
662 {
663 sig_str = sig_str + ".";
664 }
665 }
666
667 int sig_int = Math.round(Float.parseFloat(sig_str));
668 String new_sig_str = sig_int + "";
669 if (sig_str.length() > 2)
670 {
671 new_sig_str = sig_int + "0";
672 }
673
674 char[] final_chars = (new_sig_str + zero_str).toCharArray();
675 int count = 1;
676 for (int i = final_chars.length - 1; i >= 0; i--)
677 {
678 formatted_str = final_chars[i] + formatted_str;
679 if (count == 3 && i != 0)
680 {
681 formatted_str = "," + formatted_str;
682 count = 1;
683 }
684 else
685 {
686 count++;
687 }
688 }
689 return formatted_str;
690 }
691
692 public static String hashToSectionId(String hashString)
693 {
694 if (hashString == null || hashString.length() == 0)
695 {
696 return "";
697 }
698
699 int firstDotIndex = hashString.indexOf(".");
700 if (firstDotIndex == -1)
701 {
702 return "";
703 }
704
705 String sectionString = hashString.substring(firstDotIndex + 1);
706
707 return sectionString;
708 }
709
710 public static String hashToDepthClass(String hashString)
711 {
712 if (hashString == null || hashString.length() == 0)
713 {
714 return "";
715 }
716
717 String sectionString = hashToSectionId(hashString);
718
719 int count = sectionString.split("\\.").length;
720
721 if (sectionString.equals(""))
722 {
723 return "sectionHeaderDepthTitle";
724 }
725 else
726 {
727 return "sectionHeaderDepth" + count;
728 }
729 }
730
731 public static String escapeNewLines(String str)
732 {
733 if (str == null || str.length() < 1)
734 {
735 return null;
736 }
737 return str.replace("\n", "\\\n");
738 }
739
740 public static String escapeQuotes(String str)
741 {
742 if (str == null || str.length() < 1)
743 {
744 return null;
745 }
746 return str.replace("\"", "\\\"");
747 }
748
749 public static String escapeNewLinesAndQuotes(String str)
750 {
751 if (str == null || str.length() < 1)
752 {
753 return null;
754 }
755 return escapeNewLines(escapeQuotes(str));
756 }
757
758 public static String getGlobalProperty(String name)
759 {
760 return GlobalProperties.getProperty(name);
761 }
762
763 public static void clearMetadataStorage()
764 {
765 _foundTableValues.clear();
766 }
767
768 public static boolean checkMetadataNotDuplicate(String name, String value)
769 {
770 if (_foundTableValues.containsKey(name))
771 {
772 for (String mapValue : _foundTableValues.get(name))
773 {
774 if (mapValue.equals(value))
775 {
776 return false;
777 }
778 }
779 _foundTableValues.get(name).add(value);
780 return true;
781 }
782
783 ArrayList<String> newList = new ArrayList<String>();
784 newList.add(value);
785
786 _foundTableValues.put(name, newList);
787
788 return true;
789 }
790
791 public static String reCAPTCHAimage(String publicKey, String privateKey)
792 {
793 ReCaptcha c = ReCaptchaFactory.newReCaptcha(publicKey, privateKey, false);
794 return c.createRecaptchaHtml(null, null);
795 }
796
797 public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix)
798 {
799 String prependToPrefix = "gs.text";
800 return XSLTUtil.getInterfaceStringsAsJavascript(interface_name, lang, prefix, prependToPrefix);
801 }
802
803 // generates javascript: 2 arrays are declared and populated with strings that declare variables and assign their values
804 // to be strings loaded from the interface_name.properties file for the language.
805 public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix, String prependToPrefix)
806 {
807 String prefixwithdot = prefix+".";
808 // 1. Generating Javascript of the form:
809 // if(!gs.text) { gs.text = new Array(); }
810 // if(!gs.text.dse) { gs.text.dse = new Array(); }
811 StringBuffer outputStr = new StringBuffer();
812 outputStr.append("if(!gs.text) { ");
813 outputStr.append(prependToPrefix + " = new Array(); ");
814 outputStr.append("}\n");
815 outputStr.append("if(!gs.text." + prefix + ") { ");
816 outputStr.append(prependToPrefix + "." + prefix + " = new Array(); ");
817 outputStr.append("}\n");
818
819 int foundCount = 0;
820
821 for (String dictName : new String[] { "interface_" + interface_name, "interface_default", "interface_default2" })
822 {
823 // get all the keys from the english dictionary as this is a complete set
824 Dictionary dict = new Dictionary(dictName, "en");
825 Enumeration keys = dict.getKeys();
826 if (keys == null)
827 {
828 continue;
829 }
830
831 // Get all properties in the language-specific dictionary with the given key prefix
832 // Create Javascript strings of the form:
833 // prependToPrefix.key= "value";\n
834 while (keys.hasMoreElements())
835 {
836 String key = (String) keys.nextElement();
837 if (key.startsWith(prefixwithdot))
838 {
839 // get the language dependent value for the key. This will return the english if no value found for the given lang
840 String value = getInterfaceText(interface_name, dictName, lang, key, null);
841
842 outputStr.append(prependToPrefix);
843 outputStr.append(".");
844 outputStr.append(key);
845 outputStr.append("=\"");
846 outputStr.append(value);
847 outputStr.append("\";\n");
848 }
849 }
850
851 if (foundCount > 0)
852 {
853 break;
854 }
855 }
856
857 return outputStr.toString();
858 }
859
860 public static String xmlNodeToString(Node node)
861 {
862 return GSXML.xmlNodeToString(node);
863 }
864
865 // Test from cmdline with:
866 // 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
867 public static void main(String args[])
868 {
869 System.out.println("\n@@@@@\n" + XSLTUtil.getInterfaceStringsAsJavascript("default", "en", "dse", "gs.text") + "@@@@@\n");
870 }
871}
Note: See TracBrowser for help on using the repository browser.