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

Last change on this file since 33278 was 33278, checked in by kjdon, 5 years ago

added a basic getGenericText where you can specify the dictionary name. Have only implemented the most basic version so far. will do the rest as/if needed

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