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