source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/XSLTUtil.java@ 8827

Last change on this file since 8827 was 8824, checked in by kjdon, 19 years ago

added a cgiSafe method

  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1package org.greenstone.gsdl3.util;
2
3import java.util.Date;
4import java.util.Locale;
5import java.text.SimpleDateFormat;
6
7/** a class to contain various static methods that are used by the xslt
8 * stylesheets
9 */
10public class XSLTUtil {
11
12 public static boolean equals(String s1, String s2) {
13 return s1.equals(s2);
14 }
15
16 public static boolean exists(String s1, String s2) {
17 return !s1.equals("");
18 }
19 public static boolean contains(String s1, String s2) {
20 return (s1.indexOf(s2) != -1);
21 }
22 public static boolean startsWith(String s1, String s2) {
23 return s1.startsWith(s2);
24 }
25 public static boolean endsWith(String s1, String s2) {
26 return s1.endsWith(s2);
27 }
28
29 public static String toLower(String orig) {
30 return orig.toLowerCase();
31 }
32 public static String toUpper(String orig) {
33 return orig.toUpperCase();
34 }
35
36 public static byte[] toUTF8(String orig) {
37
38 try {
39 byte[] utf8 = orig.getBytes("UTF-8");
40 return utf8;
41 }
42 catch (Exception e){
43 System.err.println("XSLTUtil.toUTF8: unsupported encoding");
44 return orig.getBytes();
45 }
46 }
47
48 public static String getNumberedItem(String list, int number) {
49 String [] items = list.split(",", -1);
50 if (items.length > number) {
51 return items[number];
52 }
53 return ""; // index out of bounds
54 }
55
56
57 public static String tidyWhitespace(String original) {
58
59 if (original==null || original.equals("")) {
60 return original;
61 }
62 String new_s = original.replaceAll("\\s+", " ");
63 return new_s;
64 }
65
66
67 public static String getInterfaceText(String interface_name, String lang, String key) {
68 return getInterfaceText(interface_name, lang, key, null);
69 }
70 public static String getInterfaceText(String interface_name, String lang, String key, String args_str) {
71 String [] args = null;
72 if (args_str!=null && !args_str.equals("")) {
73 args = args_str.split(";");
74 }
75 Dictionary dict = new Dictionary("interface_"+interface_name, lang);
76 String result = dict.get(key, args);
77
78 if (result == null && !interface_name.equals("default")) { // not found, try the default interface
79 dict = new Dictionary("interface_default", lang);
80 result = dict.get(key, args);
81 }
82
83 if (result == null) { // not found
84 return "_"+key+"_";
85 }
86 return result;
87 }
88
89 public static boolean isImage(String mimetype) {
90 if (mimetype.startsWith("image/")) {
91 return true;
92 }
93 return false;
94 }
95
96 public static String formatDate(String date, String lang) {
97
98 String in_pattern = "yyyyMMdd";
99 String out_pattern = "dd MMMM yyyy";
100 if (date.length()==6) {
101 in_pattern = "yyyyMM";
102 }
103
104 SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
105 try {
106 Date d = formatter.parse(date);
107 formatter.applyPattern(out_pattern);
108 String new_date = formatter.format(d);
109 return new_date;
110 } catch (Exception e) {
111 return date;
112 }
113
114 }
115
116 public static String formatLanguage(String display_lang, String lang) {
117
118 return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
119 }
120
121 public static String cgiSafe(String original, String lang) {
122
123 original = original.replace('&', ' ');
124 original = original.replaceAll(" ", "%20");
125 return original;
126 }
127}
128
Note: See TracBrowser for help on using the repository browser.