001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.text.DateFormat;
024:
025: import java.util.Date;
026:
027: /**
028: * <a href="GetterUtil.java.html"><b><i>View Source</i></b></a>
029: *
030: * @author Brian Wing Shun Chan
031: *
032: */
033: public class GetterUtil {
034:
035: public static final boolean DEFAULT_BOOLEAN = false;
036:
037: public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
038:
039: public static final double DEFAULT_DOUBLE = 0.0;
040:
041: public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
042:
043: public static final float DEFAULT_FLOAT = 0;
044:
045: public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
046:
047: public static final int DEFAULT_INTEGER = 0;
048:
049: public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
050:
051: public static final long DEFAULT_LONG = 0;
052:
053: public static final long[] DEFAULT_LONG_VALUES = new long[0];
054:
055: public static final short DEFAULT_SHORT = 0;
056:
057: public static final short[] DEFAULT_SHORT_VALUES = new short[0];
058:
059: public static final String DEFAULT_STRING = StringPool.BLANK;
060:
061: public static String[] BOOLEANS = { "true", "t", "y", "on", "1" };
062:
063: public static boolean getBoolean(String value) {
064: return getBoolean(value, DEFAULT_BOOLEAN);
065: }
066:
067: public static boolean getBoolean(String value, boolean defaultValue) {
068: return get(value, defaultValue);
069: }
070:
071: public static boolean[] getBooleanValues(String[] values) {
072: return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
073: }
074:
075: public static boolean[] getBooleanValues(String[] values,
076: boolean[] defaultValue) {
077:
078: if (values == null) {
079: return defaultValue;
080: }
081:
082: boolean[] booleanValues = new boolean[values.length];
083:
084: for (int i = 0; i < values.length; i++) {
085: booleanValues[i] = getBoolean(values[i]);
086: }
087:
088: return booleanValues;
089: }
090:
091: public static Date getDate(String value, DateFormat df) {
092: return getDate(value, df, new Date());
093: }
094:
095: public static Date getDate(String value, DateFormat df,
096: Date defaultValue) {
097: return get(value, df, defaultValue);
098: }
099:
100: public static double getDouble(String value) {
101: return getDouble(value, DEFAULT_DOUBLE);
102: }
103:
104: public static double getDouble(String value, double defaultValue) {
105: return get(value, defaultValue);
106: }
107:
108: public static double[] getDoubleValues(String[] values) {
109: return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
110: }
111:
112: public static double[] getDoubleValues(String[] values,
113: double[] defaultValue) {
114:
115: if (values == null) {
116: return defaultValue;
117: }
118:
119: double[] doubleValues = new double[values.length];
120:
121: for (int i = 0; i < values.length; i++) {
122: doubleValues[i] = getDouble(values[i]);
123: }
124:
125: return doubleValues;
126: }
127:
128: public static float getFloat(String value) {
129: return getFloat(value, DEFAULT_FLOAT);
130: }
131:
132: public static float getFloat(String value, float defaultValue) {
133: return get(value, defaultValue);
134: }
135:
136: public static float[] getFloatValues(String[] values) {
137: return getFloatValues(values, DEFAULT_FLOAT_VALUES);
138: }
139:
140: public static float[] getFloatValues(String[] values,
141: float[] defaultValue) {
142:
143: if (values == null) {
144: return defaultValue;
145: }
146:
147: float[] floatValues = new float[values.length];
148:
149: for (int i = 0; i < values.length; i++) {
150: floatValues[i] = getFloat(values[i]);
151: }
152:
153: return floatValues;
154: }
155:
156: public static int getInteger(String value) {
157: return getInteger(value, DEFAULT_INTEGER);
158: }
159:
160: public static int getInteger(String value, int defaultValue) {
161: return get(value, defaultValue);
162: }
163:
164: public static int[] getIntegerValues(String[] values) {
165: return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
166: }
167:
168: public static int[] getIntegerValues(String[] values,
169: int[] defaultValue) {
170: if (values == null) {
171: return defaultValue;
172: }
173:
174: int[] intValues = new int[values.length];
175:
176: for (int i = 0; i < values.length; i++) {
177: intValues[i] = getInteger(values[i]);
178: }
179:
180: return intValues;
181: }
182:
183: public static long getLong(String value) {
184: return getLong(value, DEFAULT_LONG);
185: }
186:
187: public static long getLong(String value, long defaultValue) {
188: return get(value, defaultValue);
189: }
190:
191: public static long[] getLongValues(String[] values) {
192: return getLongValues(values, DEFAULT_LONG_VALUES);
193: }
194:
195: public static long[] getLongValues(String[] values,
196: long[] defaultValue) {
197: if (values == null) {
198: return defaultValue;
199: }
200:
201: long[] longValues = new long[values.length];
202:
203: for (int i = 0; i < values.length; i++) {
204: longValues[i] = getLong(values[i]);
205: }
206:
207: return longValues;
208: }
209:
210: public static short getShort(String value) {
211: return getShort(value, DEFAULT_SHORT);
212: }
213:
214: public static short getShort(String value, short defaultValue) {
215: return get(value, defaultValue);
216: }
217:
218: public static short[] getShortValues(String[] values) {
219: return getShortValues(values, DEFAULT_SHORT_VALUES);
220: }
221:
222: public static short[] getShortValues(String[] values,
223: short[] defaultValue) {
224:
225: if (values == null) {
226: return defaultValue;
227: }
228:
229: short[] shortValues = new short[values.length];
230:
231: for (int i = 0; i < values.length; i++) {
232: shortValues[i] = getShort(values[i]);
233: }
234:
235: return shortValues;
236: }
237:
238: public static String getString(String value) {
239: return getString(value, DEFAULT_STRING);
240: }
241:
242: public static String getString(String value, String defaultValue) {
243: return get(value, defaultValue);
244: }
245:
246: public static boolean get(String value, boolean defaultValue) {
247: if (value != null) {
248: try {
249: value = value.trim();
250:
251: if (value.equalsIgnoreCase(BOOLEANS[0])
252: || value.equalsIgnoreCase(BOOLEANS[1])
253: || value.equalsIgnoreCase(BOOLEANS[2])
254: || value.equalsIgnoreCase(BOOLEANS[3])
255: || value.equalsIgnoreCase(BOOLEANS[4])) {
256:
257: return true;
258: } else {
259: return false;
260: }
261: } catch (Exception e) {
262: }
263: }
264:
265: return defaultValue;
266: }
267:
268: public static Date get(String value, DateFormat df,
269: Date defaultValue) {
270: try {
271: Date date = df.parse(value.trim());
272:
273: if (date != null) {
274: return date;
275: }
276: } catch (Exception e) {
277: }
278:
279: return defaultValue;
280: }
281:
282: public static double get(String value, double defaultValue) {
283: try {
284: return Double.parseDouble(_trim(value));
285: } catch (Exception e) {
286: }
287:
288: return defaultValue;
289: }
290:
291: public static float get(String value, float defaultValue) {
292: try {
293: return Float.parseFloat(_trim(value));
294: } catch (Exception e) {
295: }
296:
297: return defaultValue;
298: }
299:
300: public static int get(String value, int defaultValue) {
301: try {
302: return Integer.parseInt(_trim(value));
303: } catch (Exception e) {
304: }
305:
306: return defaultValue;
307: }
308:
309: public static long get(String value, long defaultValue) {
310: try {
311: return Long.parseLong(_trim(value));
312: } catch (Exception e) {
313: }
314:
315: return defaultValue;
316: }
317:
318: public static short get(String value, short defaultValue) {
319: try {
320: return Short.parseShort(_trim(value));
321: } catch (Exception e) {
322: }
323:
324: return defaultValue;
325: }
326:
327: public static String get(String value, String defaultValue) {
328: if (value != null) {
329: value = value.trim();
330: value = StringUtil.replace(value, "\r\n", "\n");
331:
332: return value;
333: }
334:
335: return defaultValue;
336: }
337:
338: private static String _trim(String value) {
339: if (value != null) {
340: value = value.trim();
341:
342: StringMaker sm = new StringMaker();
343:
344: char[] charArray = value.toCharArray();
345:
346: for (int i = 0; i < charArray.length; i++) {
347: if ((Character.isDigit(charArray[i]))
348: || (charArray[i] == '-' && i == 0)
349: || (charArray[i] == '.')) {
350:
351: sm.append(charArray[i]);
352: }
353: }
354:
355: value = sm.toString();
356: }
357:
358: return value;
359: }
360:
361: }
|