1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.util;
25
26 import org.dbforms.util.external.PrintfFormat;
27
28 import java.io.UnsupportedEncodingException;
29
30 import java.net.URLDecoder;
31 import java.net.URLEncoder;
32
33 import java.text.Format;
34
35 import javax.servlet.http.HttpServletRequest;
36
37
38
39 /***
40 * Simple general utility class
41 */
42 public class Util {
43 /***
44 * Test if the input string is null or empty (does not contain any
45 * character)
46 *
47 * @param s
48 * the string value to test
49 *
50 * @return true if the input string is null or empty, false otherwise
51 */
52 public static final boolean isNull(String s) {
53 return ((s == null) || (s.trim()
54 .length() == 0));
55 }
56
57
58 /***
59 * Decodes a string
60 *
61 * @param s
62 * the string to encode
63 *
64 * @return the encoded string
65 */
66 public static final String decode(String s,
67 String enc)
68 throws UnsupportedEncodingException {
69 if (!Util.isNull(s)) {
70 try {
71 s = decCheck(s, enc);
72 } catch (NoSuchMethodError nsme) {
73 s = URLDecoder.decode(s);
74 }
75 }
76
77 return s;
78 }
79
80
81 /***
82 * Encode a string with desired character encoding.
83 *
84 * @param s
85 * the string to encode
86 * @param enc
87 * the desired encoding
88 *
89 * @return the encoded string
90 * @throws UnsupportedEncodingException
91 * DOCUMENT ME!
92 */
93 public static final String encode(String s,
94 String enc)
95 throws UnsupportedEncodingException {
96 if (!Util.isNull(s)) {
97 try {
98 s = encCheck(s, enc);
99 } catch (NoSuchMethodError nsme) {
100 s = URLEncoder.encode(s);
101 }
102 }
103
104 return s;
105 }
106
107
108 /***
109 * DOCUMENT ME!
110 *
111 * @param value DOCUMENT ME!
112 *
113 * @return DOCUMENT ME!
114 */
115 public static boolean getFalse(String value) {
116 return !Util.getTrue(value);
117 }
118
119
120 /***
121 * DOCUMENT ME!
122 *
123 * @param f DOCUMENT ME!
124 *
125 * @return DOCUMENT ME!
126 */
127 public static String getPattern(Format f) {
128 if (f instanceof java.text.DecimalFormat) {
129 return ((java.text.DecimalFormat) f).toPattern();
130 } else if (f instanceof java.text.SimpleDateFormat) {
131 return ((java.text.SimpleDateFormat) f).toPattern();
132 } else {
133 return null;
134 }
135 }
136
137
138 /***
139 * DOCUMENT ME!
140 *
141 * @param value DOCUMENT ME!
142 *
143 * @return DOCUMENT ME!
144 */
145 public static boolean getTrue(String value) {
146 return "true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value);
147 }
148
149
150 /***
151 * returns a formated string
152 *
153 * @param format
154 * format string
155 * @param o
156 * objects to use to format
157 *
158 * @return String
159 */
160 public static String sprintf(String format,
161 Object[] o) {
162 PrintfFormat printfFormat = new PrintfFormat(format);
163
164
165
166
167 return printfFormat.sprintf(o);
168 }
169
170
171 /***
172 * needed by Util.encode(s,enc) to check if URLEncoder.encode(String s,
173 * String enc) is available (from jdk 1.4)
174 *
175 * @param s
176 * the string to encode
177 * @param enc
178 * the desired encoding
179 *
180 * @return the encoded string
181 *
182 * @throws UnsupportedEncodingException
183 * DOCUMENT ME!
184 * @throws NoSuchMethodException
185 * to signal that jdk 1.3 is being used
186 */
187 private static final String decCheck(String s,
188 String enc)
189 throws UnsupportedEncodingException,
190 NoSuchMethodError {
191 if (isNull(enc)) {
192 enc = "UTF-8";
193 }
194
195 return URLDecoder.decode(s, enc);
196 }
197
198
199 /***
200 * needed by Util.encode(s,enc) to check if URLEncoder.encode(String s,
201 * String enc) is available (from jdk 1.4)
202 *
203 * @param s
204 * the string to encode
205 * @param enc
206 * the desired encoding
207 *
208 * @return the encoded string
209 *
210 * @throws UnsupportedEncodingException
211 * DOCUMENT ME!
212 * @throws NoSuchMethodException
213 * to signal that jdk 1.3 is being used
214 */
215 private static final String encCheck(String s,
216 String enc)
217 throws UnsupportedEncodingException,
218 NoSuchMethodError {
219 if (isNull(enc)) {
220 enc = "UTF-8";
221 }
222
223 s = URLEncoder.encode(s, enc);
224
225 return s;
226 }
227
228 public static String getBaseURL(HttpServletRequest request) {
229 StringBuffer buf = new StringBuffer();
230 buf.append(request.getScheme());
231 buf.append("://");
232 buf.append(request.getServerName());
233
234 int port = request.getServerPort();
235 if((port!=80) && (port !=443)) {
236 buf.append(":");
237 buf.append(String.valueOf(port));
238 }
239 buf.append(request.getContextPath());
240 if (buf.charAt(buf.length() - 1) != '/') {
241 buf.append("/");
242 }
243 return buf.toString();
244 }
245
246 public static String getRequestURL(HttpServletRequest request) {
247 StringBuffer buf = new StringBuffer();
248 buf.append(request.getScheme());
249 buf.append("://");
250 buf.append(request.getServerName());
251 int port = request.getServerPort();
252 if ((port != 80) && (port != 443)) {
253 buf.append(":");
254 buf.append(port);
255 }
256 buf.append(request.getRequestURI());
257 String query = request.getQueryString();
258 if (!isNull(query)) {
259 buf.append("?");
260 buf.append(query);
261 }
262 return buf.toString();
263 }
264
265 }