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 | import org.apache.commons.lang3.StringUtils;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * a class to contain various static methods that are used by the xslt
|
---|
34 | * stylesheets
|
---|
35 | */
|
---|
36 | public class XSLTUtil
|
---|
37 | {
|
---|
38 |
|
---|
39 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XSLTUtil.class.getName());
|
---|
40 |
|
---|
41 | /* some tests */
|
---|
42 | public static boolean equals(String s1, String s2)
|
---|
43 | {
|
---|
44 | return s1.equals(s2);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static boolean notEquals(String s1, String s2)
|
---|
48 | {
|
---|
49 | return !s1.equals(s2);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static boolean exists(String s1, String s2)
|
---|
53 | {
|
---|
54 | return !s1.equals("");
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static boolean contains(String s1, String s2)
|
---|
58 | {
|
---|
59 | return (s1.indexOf(s2) != -1);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static boolean startsWith(String s1, String s2)
|
---|
63 | {
|
---|
64 | return s1.startsWith(s2);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static boolean endsWith(String s1, String s2)
|
---|
68 | {
|
---|
69 | return s1.endsWith(s2);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static boolean lessThan(String s1, String s2)
|
---|
73 | {
|
---|
74 | return (s1.compareTo(s2) < 0);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public static boolean lessThanOrEquals(String s1, String s2)
|
---|
78 | {
|
---|
79 | return (s1.compareTo(s2) <= 0);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static boolean greaterThan(String s1, String s2)
|
---|
83 | {
|
---|
84 | return (s1.compareTo(s2) > 0);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public static boolean greaterThanOrEquals(String s1, String s2)
|
---|
88 | {
|
---|
89 | return (s1.compareTo(s2) >= 0);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* some preprocessing functions */
|
---|
93 | public static String toLower(String orig)
|
---|
94 | {
|
---|
95 | return orig.toLowerCase();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public static String toUpper(String orig)
|
---|
99 | {
|
---|
100 | return orig.toUpperCase();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public static byte[] toUTF8(String orig)
|
---|
104 | {
|
---|
105 |
|
---|
106 | try
|
---|
107 | {
|
---|
108 | byte[] utf8 = orig.getBytes("UTF-8");
|
---|
109 | return utf8;
|
---|
110 | }
|
---|
111 | catch (Exception e)
|
---|
112 | {
|
---|
113 | logger.error("unsupported encoding");
|
---|
114 | return orig.getBytes();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | public static String getNumberedItem(String list, int number)
|
---|
119 | {
|
---|
120 | String[] items = StringUtils.split(list, ",", -1);
|
---|
121 | if (items.length > number)
|
---|
122 | {
|
---|
123 | return items[number];
|
---|
124 | }
|
---|
125 | return ""; // index out of bounds
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Generates links to equivalent documents for a document with a default
|
---|
130 | * document icon/type. Links are generated from the parameters: a list of
|
---|
131 | * document icons which are each in turn embedded in the matching starting
|
---|
132 | * link tag in the list of docStartLinks (these starting links link to the
|
---|
133 | * equivalent documents in another format). Each link's start tag is closed
|
---|
134 | * with the corresponding closing tag in the docEndLinks list. Parameter
|
---|
135 | * token is the list separator. Parameter divider is the string that should
|
---|
136 | * separate each final link generated from the next. Returns a string that
|
---|
137 | * represents a sequence of links to equivalent documents, where the anchor
|
---|
138 | * is a document icon.
|
---|
139 | */
|
---|
140 | public static String getEquivDocLinks(String token, String docIconsString, String docStartLinksString, String docEndLinksString, String divider)
|
---|
141 | {
|
---|
142 | String[] docIcons = StringUtils.split(docIconsString, token, -1);
|
---|
143 | String[] startLinks = StringUtils.split(docStartLinksString, token, -1);
|
---|
144 | String[] endLinks = StringUtils.split(docEndLinksString, token, -1);
|
---|
145 |
|
---|
146 | StringBuffer buffer = new StringBuffer();
|
---|
147 | for (int i = 0; i < docIcons.length; i++)
|
---|
148 | {
|
---|
149 | if (i > 0)
|
---|
150 | {
|
---|
151 | buffer.append(divider);
|
---|
152 | }
|
---|
153 | buffer.append(startLinks[i] + docIcons[i] + endLinks[i]);
|
---|
154 | }
|
---|
155 |
|
---|
156 | return buffer.toString();
|
---|
157 | }
|
---|
158 |
|
---|
159 | public static String tidyWhitespace(String original)
|
---|
160 | {
|
---|
161 |
|
---|
162 | if (original == null || original.equals(""))
|
---|
163 | {
|
---|
164 | return original;
|
---|
165 | }
|
---|
166 | String new_s = original.replaceAll("\\s+", " ");
|
---|
167 | return new_s;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public static String getInterfaceText(String interface_name, String lang, String key)
|
---|
171 | {
|
---|
172 | return getInterfaceText(interface_name, lang, key, null);
|
---|
173 | }
|
---|
174 |
|
---|
175 | public static String getInterfaceText(String interface_name, String lang, String key, String args_str)
|
---|
176 | {
|
---|
177 | key = key.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
178 |
|
---|
179 | String[] args = null;
|
---|
180 | if (args_str != null && !args_str.equals(""))
|
---|
181 | {
|
---|
182 | args = StringUtils.split(args_str, ";");
|
---|
183 | }
|
---|
184 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
185 | String result = dict.get(key, args);
|
---|
186 | if (result == null)
|
---|
187 | { // not found
|
---|
188 | //if not found, search a separate subdirectory named by the interface name
|
---|
189 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
190 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
191 | result = dict.get(key, args);
|
---|
192 | if (result != null)
|
---|
193 | {
|
---|
194 | result = result.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
195 | return result;
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (result == null && !interface_name.equals("default"))
|
---|
200 | { // not found, try the default interface
|
---|
201 | dict = new Dictionary("interface_default", lang);
|
---|
202 | result = dict.get(key, args);
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (result == null)
|
---|
206 | { // not found
|
---|
207 | return "_" + key + "_";
|
---|
208 | }
|
---|
209 | result = result.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
210 | return result;
|
---|
211 | }
|
---|
212 |
|
---|
213 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg_node)
|
---|
214 | {
|
---|
215 | String[] args = new String[1];
|
---|
216 |
|
---|
217 | String node_str = XMLConverter.getString(arg_node);
|
---|
218 | args[0] = node_str;
|
---|
219 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
220 | String result = dict.get(key, args);
|
---|
221 | if (result == null)
|
---|
222 | { // not found
|
---|
223 | //if not found, search a separate subdirectory named by the interface name
|
---|
224 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
225 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
226 | result = dict.get(key, args);
|
---|
227 | if (result != null)
|
---|
228 | {
|
---|
229 | return result;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (result == null && !interface_name.equals("default"))
|
---|
234 | { // not found, try the default interface
|
---|
235 | dict = new Dictionary("interface_default", lang);
|
---|
236 | result = dict.get(key, args);
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (result == null)
|
---|
240 | { // not found
|
---|
241 | return "_" + key + "_";
|
---|
242 | }
|
---|
243 |
|
---|
244 | return result;
|
---|
245 | }
|
---|
246 |
|
---|
247 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg1_node, Node arg2_node)
|
---|
248 | {
|
---|
249 | String[] args = new String[2];
|
---|
250 |
|
---|
251 | String node_str = XMLConverter.getString(arg1_node);
|
---|
252 | args[0] = node_str;
|
---|
253 | node_str = XMLConverter.getString(arg2_node);
|
---|
254 | args[1] = node_str;
|
---|
255 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
256 | String result = dict.get(key, args);
|
---|
257 | if (result == null)
|
---|
258 | { // not found
|
---|
259 | //if not found, search a separate subdirectory named by the interface name
|
---|
260 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
261 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
262 | result = dict.get(key, args);
|
---|
263 | if (result != null)
|
---|
264 | {
|
---|
265 | return result;
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (result == null && !interface_name.equals("default"))
|
---|
270 | { // not found, try the default interface
|
---|
271 | dict = new Dictionary("interface_default", lang);
|
---|
272 | result = dict.get(key, args);
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (result == null)
|
---|
276 | { // not found
|
---|
277 | return "_" + key + "_";
|
---|
278 | }
|
---|
279 |
|
---|
280 | return result;
|
---|
281 | }
|
---|
282 |
|
---|
283 | public static boolean isImage(String mimetype)
|
---|
284 | {
|
---|
285 | if (mimetype.startsWith("image/"))
|
---|
286 | {
|
---|
287 | return true;
|
---|
288 | }
|
---|
289 | return false;
|
---|
290 | }
|
---|
291 |
|
---|
292 | public static String formatDate(String date, String lang)
|
---|
293 | {
|
---|
294 |
|
---|
295 | String in_pattern = "yyyyMMdd";
|
---|
296 | String out_pattern = "dd MMMM yyyy";
|
---|
297 | if (date.length() == 6)
|
---|
298 | {
|
---|
299 | in_pattern = "yyyyMM";
|
---|
300 | }
|
---|
301 |
|
---|
302 | SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
|
---|
303 | try
|
---|
304 | {
|
---|
305 | Date d = formatter.parse(date);
|
---|
306 | formatter.applyPattern(out_pattern);
|
---|
307 | String new_date = formatter.format(d);
|
---|
308 | return new_date;
|
---|
309 | }
|
---|
310 | catch (Exception e)
|
---|
311 | {
|
---|
312 | return date;
|
---|
313 | }
|
---|
314 |
|
---|
315 | }
|
---|
316 |
|
---|
317 | public static String formatLanguage(String display_lang, String lang)
|
---|
318 | {
|
---|
319 |
|
---|
320 | return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
|
---|
321 | }
|
---|
322 |
|
---|
323 | public static String cgiSafe(String original, String lang)
|
---|
324 | {
|
---|
325 |
|
---|
326 | original = original.replace('&', ' ');
|
---|
327 | original = original.replaceAll(" ", "%20");
|
---|
328 | return original;
|
---|
329 | }
|
---|
330 |
|
---|
331 | public static String formatBigNumber(String num)
|
---|
332 | {
|
---|
333 |
|
---|
334 | String num_str = num;
|
---|
335 | char[] num_chars = num_str.toCharArray();
|
---|
336 | String zero_str = "";
|
---|
337 | String formatted_str = "";
|
---|
338 |
|
---|
339 | for (int i = num_chars.length - 4; i >= 0; i--)
|
---|
340 | {
|
---|
341 | zero_str += '0';
|
---|
342 | }
|
---|
343 |
|
---|
344 | String sig_str = "";
|
---|
345 | for (int i = 0; i < 3 && i < num_chars.length; i++)
|
---|
346 | {
|
---|
347 | sig_str = sig_str + num_chars[i];
|
---|
348 | if (i == 1 && i + 1 < num_chars.length)
|
---|
349 | {
|
---|
350 | sig_str = sig_str + ".";
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | int sig_int = Math.round(Float.parseFloat(sig_str));
|
---|
355 | String new_sig_str = sig_int + "";
|
---|
356 | if (sig_str.length() > 2)
|
---|
357 | {
|
---|
358 | new_sig_str = sig_int + "0";
|
---|
359 | }
|
---|
360 |
|
---|
361 | char[] final_chars = (new_sig_str + zero_str).toCharArray();
|
---|
362 | int count = 1;
|
---|
363 | for (int i = final_chars.length - 1; i >= 0; i--)
|
---|
364 | {
|
---|
365 | formatted_str = final_chars[i] + formatted_str;
|
---|
366 | if (count == 3 && i != 0)
|
---|
367 | {
|
---|
368 | formatted_str = "," + formatted_str;
|
---|
369 | count = 1;
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | count++;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | return formatted_str;
|
---|
377 | }
|
---|
378 |
|
---|
379 | public static String hashToSectionId(String hashString)
|
---|
380 | {
|
---|
381 | if (hashString == null || hashString.length() == 0)
|
---|
382 | {
|
---|
383 | return "";
|
---|
384 | }
|
---|
385 |
|
---|
386 | int firstDotIndex = hashString.indexOf(".");
|
---|
387 | if (firstDotIndex == -1)
|
---|
388 | {
|
---|
389 | return "";
|
---|
390 | }
|
---|
391 |
|
---|
392 | String sectionString = hashString.substring(firstDotIndex + 1);
|
---|
393 |
|
---|
394 | return sectionString;
|
---|
395 | }
|
---|
396 |
|
---|
397 | public static String hashToDepthClass(String hashString)
|
---|
398 | {
|
---|
399 | if (hashString == null || hashString.length() == 0)
|
---|
400 | {
|
---|
401 | return "";
|
---|
402 | }
|
---|
403 |
|
---|
404 | String sectionString = hashToSectionId(hashString);
|
---|
405 |
|
---|
406 | int count = sectionString.split("\\.").length;
|
---|
407 |
|
---|
408 | if (sectionString.equals(""))
|
---|
409 | {
|
---|
410 | return "sectionHeaderDepthTitle";
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | return "sectionHeaderDepth" + count;
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | public static String escapeNewLines(String str)
|
---|
419 | {
|
---|
420 | if(str == null || str.length() < 1)
|
---|
421 | {
|
---|
422 | return null;
|
---|
423 | }
|
---|
424 | return str.replace("\n", "\\\n");
|
---|
425 | }
|
---|
426 | }
|
---|