KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > utils > LZUtils


1 /* *****************************************************************************
2  * LZUtils.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.utils;
11
12 import java.util.Enumeration JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.Date JavaDoc;
15 import java.util.GregorianCalendar JavaDoc;
16 import java.util.Calendar JavaDoc;
17
18 /**
19  * Class for miscellaneous utility functions.
20  */

21 public class LZUtils
22 {
23     /**
24      * Get int from object.
25      *
26      * @param o number to convert.
27      * @return if null or non-integer, returns 0.
28      */

29     static public int getInt(Object JavaDoc o)
30     {
31         if (o == null)
32             return 0;
33
34         if (! o.getClass().getName().equals("java.lang.Integer"))
35             return 0;
36
37         return ((Integer JavaDoc)o).intValue();
38     }
39
40
41     /**
42      * Get string from object.
43      *
44      * @param o object to convert.
45      * @return if null or non-string, returns null.
46      */

47     static public String JavaDoc getStr(Object JavaDoc o)
48     {
49         return getStr(o, true);
50     }
51
52
53     /**
54      * Get string from object.
55      *
56      * @param o object to convert.
57      * @param isNullOk if true, return null for nulls or non-strings, else
58      * return an empty string
59      * @return if null or non-string, returns null as long as isNullOk is true,
60      * else return empty string.
61      */

62     static public String JavaDoc getStr(Object JavaDoc o, boolean isNullOk)
63     {
64         if (o == null)
65             return (isNullOk?null:"");
66
67         if (! o.getClass().getName().equals("java.lang.String"))
68             return (isNullOk?null:"");
69
70         return (String JavaDoc)o;
71     }
72
73
74     /**
75      * Parse a string that contains a number.
76      *
77      * @return number parsed from string. 0, if invalid.
78      */

79     static public int parseInt(String JavaDoc s)
80     {
81         int n = 0;
82         try {
83             n = Integer.parseInt(s);
84         } catch (Exception JavaDoc e) {
85             // ignore
86
}
87         return n;
88     }
89
90     /**
91      * Expand system property values enclosed in ${}.
92      *
93      * @param p properties object to expand property values
94      * @return properties object with expanded values
95      * @throws Exception if a system property value doesn't exist or the
96      * property value isn't enclosed properly in ${}
97      */

98     static public Properties JavaDoc expandProperties(Properties JavaDoc p)
99         throws Exception JavaDoc
100     {
101         Properties JavaDoc _p = new Properties JavaDoc();
102         Enumeration JavaDoc keys = p.keys();
103         while (keys.hasMoreElements()) {
104             String JavaDoc k = (String JavaDoc)keys.nextElement();
105             String JavaDoc v = (String JavaDoc)p.getProperty(k);
106             _p.setProperty(k, StringUtils.expandPropertyValues(v));
107         }
108         return _p;
109     }
110 }
111
Popular Tags