source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XSLTUtil.java@ 25107

Last change on this file since 25107 was 25107, checked in by sjm84, 12 years ago

Added functions that check for duplicate metadata values when displaying metadata

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