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