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 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import java.util.Date;
|
---|
22 | import java.util.Locale;
|
---|
23 | import java.util.MissingResourceException;
|
---|
24 | import java.io.File;
|
---|
25 | import java.text.SimpleDateFormat;
|
---|
26 |
|
---|
27 | import org.apache.log4j.*;
|
---|
28 | import org.w3c.dom.Node;
|
---|
29 |
|
---|
30 | /** a class to contain various static methods that are used by the xslt
|
---|
31 | * stylesheets
|
---|
32 | */
|
---|
33 | public class XSLTUtil {
|
---|
34 |
|
---|
35 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XSLTUtil.class.getName());
|
---|
36 |
|
---|
37 | /* some tests */
|
---|
38 | public static boolean equals(String s1, String s2) {
|
---|
39 | return s1.equals(s2);
|
---|
40 | }
|
---|
41 | public static boolean notEquals(String s1, String s2) {
|
---|
42 | return !s1.equals(s2);
|
---|
43 | }
|
---|
44 | public static boolean exists(String s1, String s2) {
|
---|
45 | return !s1.equals("");
|
---|
46 | }
|
---|
47 | public static boolean contains(String s1, String s2) {
|
---|
48 | return (s1.indexOf(s2) != -1);
|
---|
49 | }
|
---|
50 | public static boolean startsWith(String s1, String s2) {
|
---|
51 | return s1.startsWith(s2);
|
---|
52 | }
|
---|
53 | public static boolean endsWith(String s1, String s2) {
|
---|
54 | return s1.endsWith(s2);
|
---|
55 | }
|
---|
56 | public static boolean lessThan(String s1, String s2) {
|
---|
57 | return (s1.compareTo(s2) < 0);
|
---|
58 | }
|
---|
59 | public static boolean lessThanOrEquals(String s1, String s2) {
|
---|
60 | return (s1.compareTo(s2) <= 0);
|
---|
61 | }
|
---|
62 | public static boolean greaterThan(String s1, String s2) {
|
---|
63 | return (s1.compareTo(s2) > 0);
|
---|
64 | }
|
---|
65 | public static boolean greaterThanOrEquals(String s1, String s2) {
|
---|
66 | return (s1.compareTo(s2) >= 0);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* some preprocessing functions */
|
---|
70 | public static String toLower(String orig) {
|
---|
71 | return orig.toLowerCase();
|
---|
72 | }
|
---|
73 | public static String toUpper(String orig) {
|
---|
74 | return orig.toUpperCase();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public static byte[] toUTF8(String orig) {
|
---|
78 |
|
---|
79 | try {
|
---|
80 | byte[] utf8 = orig.getBytes("UTF-8");
|
---|
81 | return utf8;
|
---|
82 | }
|
---|
83 | catch (Exception e){
|
---|
84 | logger.error("unsupported encoding");
|
---|
85 | return orig.getBytes();
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public static String getNumberedItem(String list, int number) {
|
---|
90 | String [] items = list.split(",", -1);
|
---|
91 | if (items.length > number) {
|
---|
92 | return items[number];
|
---|
93 | }
|
---|
94 | return ""; // index out of bounds
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | public static String tidyWhitespace(String original) {
|
---|
99 |
|
---|
100 | if (original==null || original.equals("")) {
|
---|
101 | return original;
|
---|
102 | }
|
---|
103 | String new_s = original.replaceAll("\\s+", " ");
|
---|
104 | return new_s;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | public static String getInterfaceText(String interface_name, String lang, String key) {
|
---|
109 | return getInterfaceText(interface_name, lang, key, null);
|
---|
110 | }
|
---|
111 | public static String getInterfaceText(String interface_name, String lang, String key, String args_str) {
|
---|
112 | String [] args = null;
|
---|
113 | if (args_str!=null && !args_str.equals("")) {
|
---|
114 | args = args_str.split(";");
|
---|
115 | }
|
---|
116 | Dictionary dict = new Dictionary("interface_"+interface_name, lang);
|
---|
117 | String result = dict.get(key, args);
|
---|
118 | if (result == null) { // not found
|
---|
119 | //if not found, search a separate subdirectory named by the interface name
|
---|
120 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
121 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
122 | result = dict.get(key, args);
|
---|
123 | if(result != null) {
|
---|
124 | return result;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (result == null && !interface_name.equals("default")) { // not found, try the default interface
|
---|
129 | dict = new Dictionary("interface_default", lang);
|
---|
130 | result = dict.get(key, args);
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (result == null) { // not found
|
---|
134 | return "_"+key+"_";
|
---|
135 | }
|
---|
136 | return result;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg_node) {
|
---|
140 | String [] args = new String [1];
|
---|
141 |
|
---|
142 | String node_str = XMLConverter.getString(arg_node);
|
---|
143 | args[0] = node_str;
|
---|
144 | Dictionary dict = new Dictionary("interface_"+interface_name, lang);
|
---|
145 | String result = dict.get(key, args);
|
---|
146 | if (result == null) { // not found
|
---|
147 | //if not found, search a separate subdirectory named by the interface name
|
---|
148 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
149 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
150 | result = dict.get(key, args);
|
---|
151 | if(result != null) {
|
---|
152 | return result;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (result == null && !interface_name.equals("default")) { // not found, try the default interface
|
---|
157 | dict = new Dictionary("interface_default", lang);
|
---|
158 | result = dict.get(key, args);
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (result == null) { // not found
|
---|
162 | return "_"+key+"_";
|
---|
163 | }
|
---|
164 |
|
---|
165 | return result;
|
---|
166 | }
|
---|
167 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg1_node, Node arg2_node) {
|
---|
168 | String [] args = new String [2];
|
---|
169 |
|
---|
170 | String node_str = XMLConverter.getString(arg1_node);
|
---|
171 | args[0] = node_str;
|
---|
172 | node_str = XMLConverter.getString(arg2_node);
|
---|
173 | args[1] = node_str;
|
---|
174 | Dictionary dict = new Dictionary("interface_"+interface_name, lang);
|
---|
175 | String result = dict.get(key, args);
|
---|
176 | if (result == null) { // not found
|
---|
177 | //if not found, search a separate subdirectory named by the interface name
|
---|
178 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
179 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
180 | result = dict.get(key, args);
|
---|
181 | if(result != null) {
|
---|
182 | return result;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (result == null && !interface_name.equals("default")) { // not found, try the default interface
|
---|
187 | dict = new Dictionary("interface_default", lang);
|
---|
188 | result = dict.get(key, args);
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (result == null) { // not found
|
---|
192 | return "_"+key+"_";
|
---|
193 | }
|
---|
194 |
|
---|
195 | return result;
|
---|
196 | }
|
---|
197 |
|
---|
198 | public static boolean isImage(String mimetype) {
|
---|
199 | if (mimetype.startsWith("image/")) {
|
---|
200 | return true;
|
---|
201 | }
|
---|
202 | return false;
|
---|
203 | }
|
---|
204 |
|
---|
205 | public static String formatDate(String date, String lang) {
|
---|
206 |
|
---|
207 | String in_pattern = "yyyyMMdd";
|
---|
208 | String out_pattern = "dd MMMM yyyy";
|
---|
209 | if (date.length()==6) {
|
---|
210 | in_pattern = "yyyyMM";
|
---|
211 | }
|
---|
212 |
|
---|
213 | SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
|
---|
214 | try {
|
---|
215 | Date d = formatter.parse(date);
|
---|
216 | formatter.applyPattern(out_pattern);
|
---|
217 | String new_date = formatter.format(d);
|
---|
218 | return new_date;
|
---|
219 | } catch (Exception e) {
|
---|
220 | return date;
|
---|
221 | }
|
---|
222 |
|
---|
223 | }
|
---|
224 |
|
---|
225 | public static String formatLanguage(String display_lang, String lang) {
|
---|
226 |
|
---|
227 | return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
|
---|
228 | }
|
---|
229 |
|
---|
230 | public static String cgiSafe(String original, String lang) {
|
---|
231 |
|
---|
232 | original = original.replace('&', ' ');
|
---|
233 | original = original.replaceAll(" ", "%20");
|
---|
234 | return original;
|
---|
235 | }
|
---|
236 |
|
---|
237 | public static String formatBigNumber(String num){
|
---|
238 |
|
---|
239 | String num_str = num;
|
---|
240 | char[] num_chars = num_str.toCharArray();
|
---|
241 | String zero_str = "";
|
---|
242 | String formatted_str = "";
|
---|
243 |
|
---|
244 | for(int i = num_chars.length-4; i >=0; i--){
|
---|
245 | zero_str += '0';
|
---|
246 | }
|
---|
247 |
|
---|
248 | String sig_str = "";
|
---|
249 | for(int i = 0; i<3 && i < num_chars.length; i++){
|
---|
250 | sig_str = sig_str + num_chars[i];
|
---|
251 | if(i == 1 && i+1 < num_chars.length){
|
---|
252 | sig_str = sig_str + ".";
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | int sig_int = Math.round(Float.parseFloat(sig_str));
|
---|
257 | String new_sig_str = sig_int +"";
|
---|
258 | if(sig_str.length() > 2){
|
---|
259 | new_sig_str = sig_int + "0";
|
---|
260 | }
|
---|
261 |
|
---|
262 | char[] final_chars = (new_sig_str+zero_str).toCharArray();
|
---|
263 | int count = 1;
|
---|
264 | for(int i=final_chars.length -1 ; i>=0; i-- ){
|
---|
265 | formatted_str = final_chars[i] + formatted_str ;
|
---|
266 | if(count == 3 && i !=0){
|
---|
267 | formatted_str = "," +formatted_str;
|
---|
268 | count = 1;
|
---|
269 | }
|
---|
270 | else{
|
---|
271 | count++;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | return formatted_str;
|
---|
275 | }
|
---|
276 |
|
---|
277 | }
|
---|
278 |
|
---|