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

Last change on this file since 24117 was 24117, checked in by sjm84, 13 years ago

Fixed a problem with using OIDType incremental on the dev/oran skin

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