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

Last change on this file since 24219 was 24219, checked in by ak19, 13 years ago

Thanks to Sam, Veronica and Dr Bainbridge, can finally commit the changes necessary for ticket 449.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 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 /** Generates links to equivalent documents for a document with a default document icon/type.
100 * Links are generated from the parameters: a list of document icons which are each in turn embedded
101 * in the matching starting link tag in the list of docStartLinks (these starting links link to the
102 * equivalent documents in another format). Each link's start tag is closed with the corresponding
103 * closing tag in the docEndLinks list. Parameter token is the list separator. Parameter divider is
104 * the string that should separate each final link generated from the next.
105 * Returns a string that represents a sequence of links to equivalent documents, where the anchor is
106 * a document icon. */
107 public static String getEquivDocLinks(String token, String docIconsString, String docStartLinksString,
108 String docEndLinksString, String divider)
109 {
110 String [] docIcons = StringUtils.split(docIconsString, token, -1);
111 String [] startLinks = StringUtils.split(docStartLinksString, token, -1);
112 String [] endLinks = StringUtils.split(docEndLinksString, token, -1);
113
114 StringBuffer buffer = new StringBuffer();
115 for(int i = 0; i < docIcons.length; i++) {
116 if(i > 0) {
117 buffer.append(divider);
118 }
119 buffer.append(startLinks[i]+docIcons[i]+endLinks[i]);
120 }
121
122 return buffer.toString();
123 }
124
125
126 public static String tidyWhitespace(String original) {
127
128 if (original==null || original.equals("")) {
129 return original;
130 }
131 String new_s = original.replaceAll("\\s+", " ");
132 return new_s;
133 }
134
135
136 public static String getInterfaceText(String interface_name, String lang, String key) {
137 return getInterfaceText(interface_name, lang, key, null);
138 }
139 public static String getInterfaceText(String interface_name, String lang, String key, String args_str) {
140 String [] args = null;
141 if (args_str!=null && !args_str.equals("")) {
142 args = StringUtils.split(args_str, ";");
143 }
144 Dictionary dict = new Dictionary("interface_"+interface_name, lang);
145 String result = dict.get(key, args);
146 if (result == null) { // not found
147 //if not found, search a separate subdirectory named by the interface name
148 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
149 dict = new Dictionary(sep_interface_dir, lang);
150 result = dict.get(key, args);
151 if(result != null) {
152 return result;
153 }
154 }
155
156 if (result == null && !interface_name.equals("default")) { // not found, try the default interface
157 dict = new Dictionary("interface_default", lang);
158 result = dict.get(key, args);
159 }
160
161 if (result == null) { // not found
162 return "_"+key+"_";
163 }
164 return result;
165 }
166
167 public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg_node) {
168 String [] args = new String [1];
169
170 String node_str = XMLConverter.getString(arg_node);
171 args[0] = node_str;
172 Dictionary dict = new Dictionary("interface_"+interface_name, lang);
173 String result = dict.get(key, args);
174 if (result == null) { // not found
175 //if not found, search a separate subdirectory named by the interface name
176 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
177 dict = new Dictionary(sep_interface_dir, lang);
178 result = dict.get(key, args);
179 if(result != null) {
180 return result;
181 }
182 }
183
184 if (result == null && !interface_name.equals("default")) { // not found, try the default interface
185 dict = new Dictionary("interface_default", lang);
186 result = dict.get(key, args);
187 }
188
189 if (result == null) { // not found
190 return "_"+key+"_";
191 }
192
193 return result;
194 }
195 public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg1_node, Node arg2_node) {
196 String [] args = new String [2];
197
198 String node_str = XMLConverter.getString(arg1_node);
199 args[0] = node_str;
200 node_str = XMLConverter.getString(arg2_node);
201 args[1] = node_str;
202 Dictionary dict = new Dictionary("interface_"+interface_name, lang);
203 String result = dict.get(key, args);
204 if (result == null) { // not found
205 //if not found, search a separate subdirectory named by the interface name
206 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
207 dict = new Dictionary(sep_interface_dir, lang);
208 result = dict.get(key, args);
209 if(result != null) {
210 return result;
211 }
212 }
213
214 if (result == null && !interface_name.equals("default")) { // not found, try the default interface
215 dict = new Dictionary("interface_default", lang);
216 result = dict.get(key, args);
217 }
218
219 if (result == null) { // not found
220 return "_"+key+"_";
221 }
222
223 return result;
224 }
225
226 public static boolean isImage(String mimetype) {
227 if (mimetype.startsWith("image/")) {
228 return true;
229 }
230 return false;
231 }
232
233 public static String formatDate(String date, String lang) {
234
235 String in_pattern = "yyyyMMdd";
236 String out_pattern = "dd MMMM yyyy";
237 if (date.length()==6) {
238 in_pattern = "yyyyMM";
239 }
240
241 SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
242 try {
243 Date d = formatter.parse(date);
244 formatter.applyPattern(out_pattern);
245 String new_date = formatter.format(d);
246 return new_date;
247 } catch (Exception e) {
248 return date;
249 }
250
251 }
252
253 public static String formatLanguage(String display_lang, String lang) {
254
255 return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
256 }
257
258 public static String cgiSafe(String original, String lang) {
259
260 original = original.replace('&', ' ');
261 original = original.replaceAll(" ", "%20");
262 return original;
263 }
264
265 public static String formatBigNumber(String num){
266
267 String num_str = num;
268 char[] num_chars = num_str.toCharArray();
269 String zero_str = "";
270 String formatted_str = "";
271
272 for(int i = num_chars.length-4; i >=0; i--){
273 zero_str += '0';
274 }
275
276 String sig_str = "";
277 for(int i = 0; i<3 && i < num_chars.length; i++){
278 sig_str = sig_str + num_chars[i];
279 if(i == 1 && i+1 < num_chars.length){
280 sig_str = sig_str + ".";
281 }
282 }
283
284 int sig_int = Math.round(Float.parseFloat(sig_str));
285 String new_sig_str = sig_int +"";
286 if(sig_str.length() > 2){
287 new_sig_str = sig_int + "0";
288 }
289
290 char[] final_chars = (new_sig_str+zero_str).toCharArray();
291 int count = 1;
292 for(int i=final_chars.length -1 ; i>=0; i-- ){
293 formatted_str = final_chars[i] + formatted_str ;
294 if(count == 3 && i !=0){
295 formatted_str = "," +formatted_str;
296 count = 1;
297 }
298 else{
299 count++;
300 }
301 }
302 return formatted_str;
303 }
304
305 public static String hashToSectionId(String hashString)
306 {
307 if(hashString == null || hashString.length() == 0) {return "";}
308
309 int firstDotIndex = hashString.indexOf(".");
310 if(firstDotIndex == -1)
311 {
312 return "";
313 }
314
315 String sectionString = hashString.substring(firstDotIndex + 1);
316
317 return sectionString;
318 }
319
320 public static String hashToDepthClass(String hashString)
321 {
322 if(hashString == null || hashString.length() == 0) {return "";}
323
324 String sectionString = hashToSectionId(hashString);
325
326 int count = sectionString.split("\\.").length;
327
328 if (sectionString.equals(""))
329 {
330 return "sectionHeaderDepthTitle";
331 }
332 else
333 {
334 return "sectionHeaderDepth" + count;
335 }
336 }
337}
338
Note: See TracBrowser for help on using the repository browser.