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