001: /* *****************************************************************************
002: * LZUtils.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.utils;
011:
012: import java.util.Enumeration;
013: import java.util.Properties;
014: import java.util.Date;
015: import java.util.GregorianCalendar;
016: import java.util.Calendar;
017:
018: /**
019: * Class for miscellaneous utility functions.
020: */
021: public class LZUtils {
022: /**
023: * Get int from object.
024: *
025: * @param o number to convert.
026: * @return if null or non-integer, returns 0.
027: */
028: static public int getInt(Object o) {
029: if (o == null)
030: return 0;
031:
032: if (!o.getClass().getName().equals("java.lang.Integer"))
033: return 0;
034:
035: return ((Integer) o).intValue();
036: }
037:
038: /**
039: * Get string from object.
040: *
041: * @param o object to convert.
042: * @return if null or non-string, returns null.
043: */
044: static public String getStr(Object o) {
045: return getStr(o, true);
046: }
047:
048: /**
049: * Get string from object.
050: *
051: * @param o object to convert.
052: * @param isNullOk if true, return null for nulls or non-strings, else
053: * return an empty string
054: * @return if null or non-string, returns null as long as isNullOk is true,
055: * else return empty string.
056: */
057: static public String getStr(Object o, boolean isNullOk) {
058: if (o == null)
059: return (isNullOk ? null : "");
060:
061: if (!o.getClass().getName().equals("java.lang.String"))
062: return (isNullOk ? null : "");
063:
064: return (String) o;
065: }
066:
067: /**
068: * Parse a string that contains a number.
069: *
070: * @return number parsed from string. 0, if invalid.
071: */
072: static public int parseInt(String s) {
073: int n = 0;
074: try {
075: n = Integer.parseInt(s);
076: } catch (Exception e) {
077: // ignore
078: }
079: return n;
080: }
081:
082: /**
083: * Expand system property values enclosed in ${}.
084: *
085: * @param p properties object to expand property values
086: * @return properties object with expanded values
087: * @throws Exception if a system property value doesn't exist or the
088: * property value isn't enclosed properly in ${}
089: */
090: static public Properties expandProperties(Properties p)
091: throws Exception {
092: Properties _p = new Properties();
093: Enumeration keys = p.keys();
094: while (keys.hasMoreElements()) {
095: String k = (String) keys.nextElement();
096: String v = (String) p.getProperty(k);
097: _p.setProperty(k, StringUtils.expandPropertyValues(v));
098: }
099: return _p;
100: }
101: }
|