001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.jorphan.util;
019:
020: import java.net.URLEncoder;
021: import java.text.DateFormat;
022: import java.text.DecimalFormat;
023: import java.text.NumberFormat;
024: import java.text.ParseException;
025: import java.text.SimpleDateFormat;
026: import java.util.Calendar;
027: import java.util.Date;
028: import java.util.GregorianCalendar;
029: import java.util.StringTokenizer;
030:
031: /**
032: * @author Michael Stover
033: */
034: public class Converter {
035:
036: /**
037: * Convert the given value object to an object of the given type
038: *
039: * @param value
040: * @param toType
041: * @return Object
042: */
043: public static Object convert(Object value, Class toType) {
044: if (value == null) {
045: value = "";
046: } else if (toType.isAssignableFrom(value.getClass())) {
047: return value;
048: } else if (toType.equals(float.class)
049: || toType.equals(Float.class)) {
050: return new Float(getFloat(value));
051: } else if (toType.equals(double.class)
052: || toType.equals(Double.class)) {
053: return new Double(getDouble(value));
054: } else if (toType.equals(String.class)) {
055: return getString(value);
056: } else if (toType.equals(int.class)
057: || toType.equals(Integer.class)) {
058: return new Integer(getInt(value));
059: } else if (toType.equals(char.class)
060: || toType.equals(Character.class)) {
061: return new Character(getChar(value));
062: } else if (toType.equals(long.class)
063: || toType.equals(Long.class)) {
064: return new Long(getLong(value));
065: } else if (toType.equals(boolean.class)
066: || toType.equals(Boolean.class)) {
067: return Boolean.valueOf(getBoolean(value));
068: } else if (toType.equals(java.util.Date.class)) {
069: return getDate(value);
070: } else if (toType.equals(Calendar.class)) {
071: return getCalendar(value);
072: } else if (toType.equals(Class.class)) {
073: try {
074: return Class.forName(value.toString());
075: } catch (Exception e) {
076: // don't do anything
077: }
078: }
079: return value;
080: }
081:
082: /**
083: * Converts the given object to a calendar object. Defaults to the current
084: * date if the given object can't be converted.
085: *
086: * @param date
087: * @return Calendar
088: */
089: public static Calendar getCalendar(Object date,
090: Calendar defaultValue) {
091: Calendar cal = new GregorianCalendar();
092: if (date != null && date instanceof java.util.Date) {
093: cal.setTime((java.util.Date) date);
094: return cal;
095: } else if (date != null) {
096: DateFormat formatter = DateFormat
097: .getDateInstance(DateFormat.SHORT);
098: java.util.Date d = null;
099: try {
100: d = formatter.parse(date.toString());
101: } catch (ParseException e) {
102: formatter = DateFormat
103: .getDateInstance(DateFormat.MEDIUM);
104: try {
105: d = formatter.parse((String) date);
106: } catch (ParseException e1) {
107: formatter = DateFormat
108: .getDateInstance(DateFormat.LONG);
109: try {
110: d = formatter.parse((String) date);
111: } catch (ParseException e2) {
112: formatter = DateFormat
113: .getDateInstance(DateFormat.FULL);
114: try {
115: d = formatter.parse((String) date);
116: } catch (ParseException e3) {
117: return defaultValue;
118: }
119: }
120: }
121: }
122: cal.setTime(d);
123: } else {
124: cal = defaultValue;
125: }
126: return cal;
127: }
128:
129: public static Calendar getCalendar(Object o) {
130: return getCalendar(o, new GregorianCalendar());
131: }
132:
133: public static Date getDate(Object date) {
134: return getDate(date, Calendar.getInstance().getTime());
135: }
136:
137: public static String urlEncode(Object toEncode) {
138: return URLEncoder.encode(getString(toEncode));
139: }
140:
141: public static Date getDate(Object date, Date defaultValue) {
142: Date val = null;
143: if (date != null && date instanceof java.util.Date) {
144: return (Date) date;
145: } else if (date != null) {
146: DateFormat formatter = DateFormat
147: .getDateInstance(DateFormat.SHORT);
148: // java.util.Date d = null;
149: try {
150: val = formatter.parse(date.toString());
151: } catch (ParseException e) {
152: formatter = DateFormat
153: .getDateInstance(DateFormat.MEDIUM);
154: try {
155: val = formatter.parse((String) date);
156: } catch (ParseException e1) {
157: formatter = DateFormat
158: .getDateInstance(DateFormat.LONG);
159: try {
160: val = formatter.parse((String) date);
161: } catch (ParseException e2) {
162: formatter = DateFormat
163: .getDateInstance(DateFormat.FULL);
164: try {
165: val = formatter.parse((String) date);
166: } catch (ParseException e3) {
167: return defaultValue;
168: }
169: }
170: }
171: }
172: } else {
173: return defaultValue;
174: }
175: return val;
176: }
177:
178: public String formatNumber(float num, String pattern) {
179: NumberFormat format = new DecimalFormat(pattern);
180: return format.format(num);
181: }
182:
183: public static float getFloat(Object o, float defaultValue) {
184: try {
185: if (o == null) {
186: return defaultValue;
187: }
188: if (o instanceof Number) {
189: return ((Number) o).floatValue();
190: } else {
191: return Float.parseFloat(o.toString());
192: }
193: } catch (NumberFormatException e) {
194: return defaultValue;
195: }
196: }
197:
198: public static float getFloat(Object o) {
199: return getFloat(o, 0);
200: }
201:
202: public static double getDouble(Object o, double defaultValue) {
203: try {
204: if (o == null) {
205: return defaultValue;
206: }
207: if (o instanceof Number) {
208: return ((Number) o).doubleValue();
209: } else {
210: return Double.parseDouble(o.toString());
211: }
212: } catch (NumberFormatException e) {
213: return defaultValue;
214: }
215: }
216:
217: public static double getDouble(Object o) {
218: return getDouble(o, 0);
219: }
220:
221: public static boolean getBoolean(Object o) {
222: return getBoolean(o, false);
223: }
224:
225: public static boolean getBoolean(Object o, boolean defaultValue) {
226: if (o == null) {
227: return defaultValue;
228: } else if (o instanceof Boolean) {
229: return ((Boolean) o).booleanValue();
230: } else
231: return Boolean.valueOf(o.toString()).booleanValue();
232: }
233:
234: /**
235: * Convert object to integer, return defaultValue if object is not
236: * convertible or is null.
237: *
238: * @param o
239: * @param defaultValue
240: * @return int
241: */
242: public static int getInt(Object o, int defaultValue) {
243: try {
244: if (o == null) {
245: return defaultValue;
246: }
247: if (o instanceof Number) {
248: return ((Number) o).intValue();
249: } else {
250: return Integer.parseInt(o.toString());
251: }
252: } catch (NumberFormatException e) {
253: return defaultValue;
254: }
255: }
256:
257: public static char getChar(Object o) {
258: return getChar(o, ' ');
259: }
260:
261: public static char getChar(Object o, char defaultValue) {
262: try {
263: if (o == null) {
264: return defaultValue;
265: }
266: if (o instanceof Character) {
267: return ((Character) o).charValue();
268: } else if (o instanceof Byte) {
269: return (char) ((Byte) o).byteValue();
270: } else if (o instanceof Integer) {
271: return (char) ((Integer) o).intValue();
272: } else {
273: String s = o.toString();
274: if (s.length() > 0) {
275: return o.toString().charAt(0);
276: } else
277: return defaultValue;
278: }
279: } catch (Exception e) {
280: return defaultValue;
281: }
282: }
283:
284: /**
285: * Converts object to an integer, defaults to 0 if object is not convertible
286: * or is null.
287: *
288: * @param o
289: * @return int
290: */
291: public static int getInt(Object o) {
292: return getInt(o, 0);
293: }
294:
295: /**
296: * Converts object to a long, return defaultValue if object is not
297: * convertible or is null.
298: *
299: * @param o
300: * @param defaultValue
301: * @return long
302: */
303: public static long getLong(Object o, long defaultValue) {
304: try {
305: if (o == null) {
306: return defaultValue;
307: }
308: if (o instanceof Number) {
309: return ((Number) o).longValue();
310: } else {
311: return Long.parseLong(o.toString());
312: }
313: } catch (NumberFormatException e) {
314: return defaultValue;
315: }
316: }
317:
318: /**
319: * Converts object to a long, defaults to 0 if object is not convertible or
320: * is null
321: *
322: * @param o
323: * @return long
324: */
325: public static long getLong(Object o) {
326: return getLong(o, 0);
327: }
328:
329: public static String formatDate(Date date, String pattern) {
330: if (date == null) {
331: return "";
332: }
333: SimpleDateFormat format = new SimpleDateFormat(pattern);
334: return format.format(date);
335: }
336:
337: public static String formatDate(java.sql.Date date, String pattern) {
338: if (date == null) {
339: return "";
340: }
341: SimpleDateFormat format = new SimpleDateFormat(pattern);
342: return format.format(date);
343: }
344:
345: public static String formatDate(String date, String pattern) {
346: return formatDate(getCalendar(date, null), pattern);
347: }
348:
349: public static String formatDate(Calendar date, String pattern) {
350: return formatCalendar(date, pattern);
351: }
352:
353: public static String formatCalendar(Calendar date, String pattern) {
354: if (date == null) {
355: return "";
356: }
357: SimpleDateFormat format = new SimpleDateFormat(pattern);
358: return format.format(date.getTime());
359: }
360:
361: /**
362: * Converts object to a String, return defaultValue if object is null.
363: *
364: * @param o
365: * @param defaultValue
366: * @return String
367: */
368: public static String getString(Object o, String defaultValue) {
369: if (o == null) {
370: return defaultValue;
371: }
372: return o.toString();
373: }
374:
375: public static String insertLineBreaks(String v, String insertion) {
376: if (v == null) {
377: return "";
378: } else {
379: StringBuffer replacement = new StringBuffer();
380: StringTokenizer tokens = new StringTokenizer(v, "\n", true);
381: while (tokens.hasMoreTokens()) {
382: String token = tokens.nextToken();
383: if (token.compareTo("\n") == 0) {
384: replacement.append(insertion);
385: } else {
386: replacement.append(token);
387: }
388: }
389: return replacement.toString();
390: }
391: }
392:
393: // Does not appear to be used.
394: // Remove as replaceAll() requires 1.4
395: // public static String insertSpaceBreaks(String v, String insertion) {
396: // return v.trim().replaceAll("\\s+", insertion);
397: // }
398:
399: /**
400: * Converts object to a String, defaults to empty string if object is null.
401: *
402: * @param o
403: * @return String
404: */
405: public static String getString(Object o) {
406: return getString(o, "");
407: }
408: }
|