1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Power Of Two S.R.L. (www.pow2.com)
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.util;
25
26 import java.io.PrintWriter;
27 import java.io.StringWriter;
28 import java.io.IOException;
29 import java.util.*;
30
31 import org.apache.log4j.Category;
32
33
34 /***
35 * Utility class.
36 *
37 * @author Luca Fossato
38 * @created 16 June 2002
39 */
40 public class Util
41 {
42 /*** Log4j category. */
43 private static Category cat = Category.getInstance(Util.class);
44
45
46 /***
47 * Get the last token from the input string.
48 *
49 * @param str the string containing the token
50 * @param tokenSeparator the token separator string (i.e.: "'", ":", etc)
51 * @return the last token from the input string
52 */
53 public static String getLastToken(String str, String tokenSeparator)
54 {
55 return str.substring(str.lastIndexOf(tokenSeparator) + 1, str.length());
56 }
57
58
59 /***
60 * Test if the input string is null or empty (has 0 characters)
61 *
62 * @param s the input string to test
63 * @return true if the input string is null; false otherwise
64 */
65 public static boolean isNull(String s)
66 {
67 return ((s == null) || (s.length() < 1));
68 }
69
70
71 /***
72 * Test if the input string is null or empty
73 * or if it's equal to the input <code>val</code> parameter.
74 *
75 * @param s the input string to test
76 * @param val the value string to compare to <code>s</code>
77 * @return true if <code>s</code> is null, or empty, or if it's
78 * equal to the <code>val</code> string;
79 * false otherwise.
80 */
81 public static boolean isNull(String s, String val)
82 {
83 return (isNull(s) || (s.compareTo(val) == 0));
84 }
85
86
87 /***
88 * Return the string representation of the input exception
89 * stack trace.
90 *
91 * @param t the input throwable object
92 * @return the string representation of the stack trace of the input
93 * throwable object
94 */
95 public static String stackTrace(Throwable t)
96 {
97 StringWriter sw = new StringWriter();
98
99 t.printStackTrace(new PrintWriter(sw));
100 String s = sw.toString();
101
102 try
103 {
104 sw.close();
105 }
106 catch (IOException e)
107 {
108 cat.error("::stackTrace - cannot close the StringWriter object", e);
109 }
110
111 return s;
112 }
113
114
115 /***
116 * Delimit the input string with single quote characters (').
117 *
118 * @param v The new string value
119 * @return the input string delimited with single quote characters;
120 */
121 public static String dbString(String v)
122 {
123 StringBuffer sb = new StringBuffer();
124
125 return (isNull(v) ? "" : (sb.append("'").append(v).append("'").toString()));
126 }
127
128
129 /***
130 * Dump the content of the input hash table.
131 *
132 * @param table table the hash table to dump
133 * @param html true to set the eof as "<br>\n", false to set it
134 * as "\n"
135 * @return Description of the Return Value
136 */
137 public static String dumpHashTable(Hashtable table, boolean html)
138 {
139 Enumeration keys = table.keys();
140 Enumeration values = table.elements();
141 StringBuffer sb = new StringBuffer();
142 String eof = "\n";
143
144 if (html)
145 eof = "<br>\n";
146
147 while (keys.hasMoreElements())
148 sb.append(" key [").append(keys.nextElement().toString()).
149 append("] = [").append(values.nextElement().toString()).
150 append("]").append(eof);
151
152 return sb.toString();
153 }
154
155
156 /***
157 * Add a new parameter to the input URL string representation.
158 *
159 * @param URL the URL string representation
160 * @param paramName the parameter name
161 * @param paramValue the parameter value
162 */
163 public static String addURLParameter(String URL, String paramName, String paramValue)
164 {
165 String param = new StringBuffer(paramName)
166 .append("=")
167 .append(paramValue)
168 .toString();
169
170 return addURLParameter(URL, param);
171 }
172
173
174 /***
175 * Add a new parameter to the input URL string representation.
176 *
177 * @param URL the URL string representation
178 * @param parameter the parameter string, encoded as "${paramName}=${paramValue}"
179 */
180 public static String addURLParameter(String URL, String parameter)
181 {
182 StringBuffer sb = new StringBuffer(URL);
183
184 if (URL.lastIndexOf('?') == -1)
185 sb.append("?");
186 else
187 sb.append("&");
188
189 sb.append(parameter);
190 return sb.toString();
191 }
192
193
194 /***
195 * Remove the substring starting from the first character
196 * of the input string to the input <code>until</code> string included.
197 *
198 * @param str the string to process
199 * @param until the string to reach (it will removed, too)
200 * @return the substring starting from the first characther
201 * after the input <code>until</code> string,
202 * or null if <code>until</code> token isn't found
203 */
204 public static String remove(String str, String until)
205 {
206 String val = null;
207 int indx = str.indexOf(until);
208
209 if (indx != -1)
210 val = (str.substring((indx + until.length()), str.length()));
211
212 return val;
213 }
214 }