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.util.Date;
|
---|
22 | import java.util.Locale;
|
---|
23 | import java.util.MissingResourceException;
|
---|
24 | import java.io.File;
|
---|
25 | import java.text.SimpleDateFormat;
|
---|
26 |
|
---|
27 | import org.apache.log4j.*;
|
---|
28 |
|
---|
29 | /** a class to contain various static methods that are used by the xslt
|
---|
30 | * stylesheets
|
---|
31 | */
|
---|
32 | public class XSLTUtil {
|
---|
33 |
|
---|
34 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XSLTUtil.class.getName());
|
---|
35 |
|
---|
36 | /* some tests */
|
---|
37 | public static boolean equals(String s1, String s2) {
|
---|
38 | return s1.equals(s2);
|
---|
39 | }
|
---|
40 | public static boolean notEquals(String s1, String s2) {
|
---|
41 | return !s1.equals(s2);
|
---|
42 | }
|
---|
43 | public static boolean exists(String s1, String s2) {
|
---|
44 | return !s1.equals("");
|
---|
45 | }
|
---|
46 | public static boolean contains(String s1, String s2) {
|
---|
47 | return (s1.indexOf(s2) != -1);
|
---|
48 | }
|
---|
49 | public static boolean startsWith(String s1, String s2) {
|
---|
50 | return s1.startsWith(s2);
|
---|
51 | }
|
---|
52 | public static boolean endsWith(String s1, String s2) {
|
---|
53 | return s1.endsWith(s2);
|
---|
54 | }
|
---|
55 | public static boolean lessThan(String s1, String s2) {
|
---|
56 | return (s1.compareTo(s2) < 0);
|
---|
57 | }
|
---|
58 | public static boolean lessThanOrEquals(String s1, String s2) {
|
---|
59 | return (s1.compareTo(s2) <= 0);
|
---|
60 | }
|
---|
61 | public static boolean greaterThan(String s1, String s2) {
|
---|
62 | return (s1.compareTo(s2) > 0);
|
---|
63 | }
|
---|
64 | public static boolean greaterThanOrEquals(String s1, String s2) {
|
---|
65 | return (s1.compareTo(s2) >= 0);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* some preprocessing functions */
|
---|
69 | public static String toLower(String orig) {
|
---|
70 | return orig.toLowerCase();
|
---|
71 | }
|
---|
72 | public static String toUpper(String orig) {
|
---|
73 | return orig.toUpperCase();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static byte[] toUTF8(String orig) {
|
---|
77 |
|
---|
78 | try {
|
---|
79 | byte[] utf8 = orig.getBytes("UTF-8");
|
---|
80 | return utf8;
|
---|
81 | }
|
---|
82 | catch (Exception e){
|
---|
83 | logger.error("unsupported encoding");
|
---|
84 | return orig.getBytes();
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public static String getNumberedItem(String list, int number) {
|
---|
89 | String [] items = list.split(",", -1);
|
---|
90 | if (items.length > number) {
|
---|
91 | return items[number];
|
---|
92 | }
|
---|
93 | return ""; // index out of bounds
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | public static String tidyWhitespace(String original) {
|
---|
98 |
|
---|
99 | if (original==null || original.equals("")) {
|
---|
100 | return original;
|
---|
101 | }
|
---|
102 | String new_s = original.replaceAll("\\s+", " ");
|
---|
103 | return new_s;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | public static String getInterfaceText(String interface_name, String lang, String key) {
|
---|
108 | return getInterfaceText(interface_name, lang, key, null);
|
---|
109 | }
|
---|
110 | public static String getInterfaceText(String interface_name, String lang, String key, String args_str) {
|
---|
111 | String [] args = null;
|
---|
112 | if (args_str!=null && !args_str.equals("")) {
|
---|
113 | args = args_str.split(";");
|
---|
114 | }
|
---|
115 | Dictionary dict = new Dictionary("interface_"+interface_name, lang);
|
---|
116 | String result = dict.get(key, args);
|
---|
117 | if (result == null) { // not found
|
---|
118 | //if not found, search a separate subdirectory named by the interface name
|
---|
119 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
120 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
121 | result = dict.get(key, args);
|
---|
122 | if(result != null) {
|
---|
123 | return result;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (result == null && !interface_name.equals("default")) { // not found, try the default interface
|
---|
128 | dict = new Dictionary("interface_default", lang);
|
---|
129 | result = dict.get(key, args);
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (result == null) { // not found
|
---|
133 | return "_"+key+"_";
|
---|
134 | }
|
---|
135 | return result;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public static boolean isImage(String mimetype) {
|
---|
139 | if (mimetype.startsWith("image/")) {
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public static String formatDate(String date, String lang) {
|
---|
146 |
|
---|
147 | String in_pattern = "yyyyMMdd";
|
---|
148 | String out_pattern = "dd MMMM yyyy";
|
---|
149 | if (date.length()==6) {
|
---|
150 | in_pattern = "yyyyMM";
|
---|
151 | }
|
---|
152 |
|
---|
153 | SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
|
---|
154 | try {
|
---|
155 | Date d = formatter.parse(date);
|
---|
156 | formatter.applyPattern(out_pattern);
|
---|
157 | String new_date = formatter.format(d);
|
---|
158 | return new_date;
|
---|
159 | } catch (Exception e) {
|
---|
160 | return date;
|
---|
161 | }
|
---|
162 |
|
---|
163 | }
|
---|
164 |
|
---|
165 | public static String formatLanguage(String display_lang, String lang) {
|
---|
166 |
|
---|
167 | return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
|
---|
168 | }
|
---|
169 |
|
---|
170 | public static String cgiSafe(String original, String lang) {
|
---|
171 |
|
---|
172 | original = original.replace('&', ' ');
|
---|
173 | original = original.replaceAll(" ", "%20");
|
---|
174 | return original;
|
---|
175 | }
|
---|
176 |
|
---|
177 | public static String formatBigNumber(String num){
|
---|
178 |
|
---|
179 | String num_str = num;
|
---|
180 | char[] num_chars = num_str.toCharArray();
|
---|
181 | String zero_str = "";
|
---|
182 | String formatted_str = "";
|
---|
183 |
|
---|
184 | for(int i = num_chars.length-4; i >=0; i--){
|
---|
185 | zero_str += '0';
|
---|
186 | }
|
---|
187 |
|
---|
188 | String sig_str = "";
|
---|
189 | for(int i = 0; i<3 && i < num_chars.length; i++){
|
---|
190 | sig_str = sig_str + num_chars[i];
|
---|
191 | if(i == 1 && i+1 < num_chars.length){
|
---|
192 | sig_str = sig_str + ".";
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | int sig_int = Math.round(Float.parseFloat(sig_str));
|
---|
197 | String new_sig_str = sig_int +"";
|
---|
198 | if(sig_str.length() > 2){
|
---|
199 | new_sig_str = sig_int + "0";
|
---|
200 | }
|
---|
201 |
|
---|
202 | char[] final_chars = (new_sig_str+zero_str).toCharArray();
|
---|
203 | int count = 1;
|
---|
204 | for(int i=final_chars.length -1 ; i>=0; i-- ){
|
---|
205 | formatted_str = final_chars[i] + formatted_str ;
|
---|
206 | if(count == 3 && i !=0){
|
---|
207 | formatted_str = "," +formatted_str;
|
---|
208 | count = 1;
|
---|
209 | }
|
---|
210 | else{
|
---|
211 | count++;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | return formatted_str;
|
---|
215 | }
|
---|
216 |
|
---|
217 | }
|
---|
218 |
|
---|