KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > util > Util


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/Util.java,v 1.32 2004/12/16 10:49:37 hkollmann Exp $
3  * $Revision: 1.32 $
4  * $Date: 2004/12/16 10:49:37 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.util;
25
26 import org.dbforms.util.external.PrintfFormat;
27
28 import java.io.UnsupportedEncodingException JavaDoc;
29
30 import java.net.URLDecoder JavaDoc;
31 import java.net.URLEncoder JavaDoc;
32
33 import java.text.Format JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36
37
38
39 /**
40  * Simple general utility class
41  */

42 public class Util {
43    private static final String JavaDoc REALPATH = "$(SERVLETCONTEXT_REALPATH)";
44
45    /**
46     * Test if the input string is null or empty (does not contain any
47     * character)
48     *
49     * @param s
50     * the string value to test
51     *
52     * @return true if the input string is null or empty, false otherwise
53     */

54    public static final boolean isNull(String JavaDoc s) {
55       return ((s == null) || (s.trim()
56                                .length() == 0));
57    }
58
59
60    /**
61     * Decodes a string
62     *
63     * @param s
64     * the string to encode
65     *
66     * @return the encoded string
67     */

68    public static final String JavaDoc decode(String JavaDoc s,
69                                      String JavaDoc enc)
70                               throws UnsupportedEncodingException JavaDoc {
71       if (!Util.isNull(s)) {
72          try {
73             s = decCheck(s, enc);
74          } catch (NoSuchMethodError JavaDoc nsme) {
75             s = URLDecoder.decode(s);
76          }
77       }
78
79       return s;
80    }
81
82
83    /**
84     * Encode a string with desired character encoding.
85     *
86     * @param s
87     * the string to encode
88     * @param enc
89     * the desired encoding
90     *
91     * @return the encoded string
92     * @throws UnsupportedEncodingException
93     * DOCUMENT ME!
94     */

95    public static final String JavaDoc encode(String JavaDoc s,
96                                      String JavaDoc enc)
97                               throws UnsupportedEncodingException JavaDoc {
98       if (!Util.isNull(s)) {
99          try {
100             s = encCheck(s, enc);
101          } catch (NoSuchMethodError JavaDoc nsme) {
102             s = URLEncoder.encode(s);
103          }
104       }
105
106       return s;
107    }
108
109
110    /**
111     * Replaces the occurens from REALPATH in s with realpath.
112     *
113     * @param s
114     * the string containing the REALPATH token
115     * @param realpath
116     * the value used to replace the REALPATH token
117     *
118     * @return the input string, with the REALPATH token replaced with the
119     * realpath value
120     */

121    public static final String JavaDoc replaceRealPath(String JavaDoc s,
122                                               String JavaDoc realpath) {
123       if (!isNull(s) && !isNull(realpath)) {
124          // 20030604-HKK: Bugfixing for different engine, e.g. cactus. Path
125
// maybe without trailing '/'!!!
126
if (realpath.charAt(realpath.length() - 1) != '/') {
127             realpath = realpath + '/';
128          }
129
130          int i = s.indexOf(REALPATH);
131
132          while (i >= 0) {
133             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
134             buf.append(s.substring(0, i));
135             buf.append(realpath);
136             buf.append(s.substring(i + REALPATH.length() + 1));
137             s = buf.toString();
138             i = s.indexOf(REALPATH);
139          }
140       }
141
142       return s;
143    }
144
145
146    /**
147     * DOCUMENT ME!
148     *
149     * @param value DOCUMENT ME!
150     *
151     * @return DOCUMENT ME!
152     */

153    public static boolean getFalse(String JavaDoc value) {
154       return !Util.getTrue(value);
155    }
156
157
158    /**
159     * DOCUMENT ME!
160     *
161     * @param f DOCUMENT ME!
162     *
163     * @return DOCUMENT ME!
164     */

165    public static String JavaDoc getPattern(Format f) {
166       if (f instanceof java.text.DecimalFormat JavaDoc) {
167          return ((java.text.DecimalFormat JavaDoc) f).toPattern();
168       } else if (f instanceof java.text.SimpleDateFormat JavaDoc) {
169          return ((java.text.SimpleDateFormat JavaDoc) f).toPattern();
170       } else {
171          return null;
172       }
173    }
174
175
176    /**
177     * DOCUMENT ME!
178     *
179     * @param value DOCUMENT ME!
180     *
181     * @return DOCUMENT ME!
182     */

183    public static boolean getTrue(String JavaDoc value) {
184       return "true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value);
185    }
186
187
188    /**
189     * returns a formated string
190     *
191     * @param format
192     * format string
193     * @param o
194     * objects to use to format
195     *
196     * @return String
197     */

198    public static String JavaDoc sprintf(String JavaDoc format,
199                                 Object JavaDoc[] o) {
200       PrintfFormat printfFormat = new PrintfFormat(format); // create
201

202       // instance of
203
// PrintfFormat
204
// class
205
return printfFormat.sprintf(o);
206    }
207
208
209    /**
210     * needed by Util.encode(s,enc) to check if URLEncoder.encode(String s,
211     * String enc) is available (from jdk 1.4)
212     *
213     * @param s
214     * the string to encode
215     * @param enc
216     * the desired encoding
217     *
218     * @return the encoded string
219     *
220     * @throws UnsupportedEncodingException
221     * DOCUMENT ME!
222     * @throws NoSuchMethodException
223     * to signal that jdk 1.3 is being used
224     */

225    private static final String JavaDoc decCheck(String JavaDoc s,
226                                         String JavaDoc enc)
227                                  throws UnsupportedEncodingException JavaDoc,
228                                         NoSuchMethodError JavaDoc {
229       if (isNull(enc)) {
230          enc = "UTF-8";
231       }
232
233       return URLDecoder.decode(s, enc);
234    }
235
236
237    /**
238     * needed by Util.encode(s,enc) to check if URLEncoder.encode(String s,
239     * String enc) is available (from jdk 1.4)
240     *
241     * @param s
242     * the string to encode
243     * @param enc
244     * the desired encoding
245     *
246     * @return the encoded string
247     *
248     * @throws UnsupportedEncodingException
249     * DOCUMENT ME!
250     * @throws NoSuchMethodException
251     * to signal that jdk 1.3 is being used
252     */

253    private static final String JavaDoc encCheck(String JavaDoc s,
254                                         String JavaDoc enc)
255                                  throws UnsupportedEncodingException JavaDoc,
256                                         NoSuchMethodError JavaDoc {
257       if (isNull(enc)) {
258          enc = "UTF-8";
259       }
260
261       s = URLEncoder.encode(s, enc);
262
263       return s;
264    }
265
266     public static String JavaDoc getBaseURL(HttpServletRequest JavaDoc request) {
267         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
268         buf.append(request.getScheme());
269         buf.append("://");
270         buf.append(request.getServerName());
271
272         int port = request.getServerPort();
273         if((port!=80) && (port !=443)) {
274             buf.append(":");
275             buf.append(String.valueOf(port));
276         }
277         buf.append(request.getContextPath());
278         buf.append("/");
279         return buf.toString();
280     }
281
282     public static String JavaDoc getRequestURL(HttpServletRequest JavaDoc request) {
283         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
284         buf.append(request.getScheme());
285         buf.append("://");
286         buf.append(request.getServerName());
287         int port = request.getServerPort();
288         if ((port != 80) && (port != 443)) {
289             buf.append(":");
290             buf.append(port);
291         }
292         buf.append(request.getRequestURI());
293         String JavaDoc query = request.getQueryString();
294         if (!isNull(query)) {
295             buf.append("?");
296             buf.append(query);
297         }
298         return buf.toString();
299     }
300
301 }
302
Popular Tags