1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.text.DateFormat;
26  
27  import java.util.Date;
28  
29  /**
30   * <a href="GetterUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   *
34   */
35  public class GetterUtil {
36  
37      public static final boolean DEFAULT_BOOLEAN = false;
38  
39      public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
40  
41      public static final double DEFAULT_DOUBLE = 0.0;
42  
43      public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
44  
45      public static final float DEFAULT_FLOAT = 0;
46  
47      public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
48  
49      public static final int DEFAULT_INTEGER = 0;
50  
51      public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
52  
53      public static final long DEFAULT_LONG = 0;
54  
55      public static final long[] DEFAULT_LONG_VALUES = new long[0];
56  
57      public static final short DEFAULT_SHORT = 0;
58  
59      public static final short[] DEFAULT_SHORT_VALUES = new short[0];
60  
61      public static final String DEFAULT_STRING = StringPool.BLANK;
62  
63      public static String[] BOOLEANS = {"true", "t", "y", "on", "1"};
64  
65      public static boolean getBoolean(String value) {
66          return getBoolean(value, DEFAULT_BOOLEAN);
67      }
68  
69      public static boolean getBoolean(String value, boolean defaultValue) {
70          return get(value, defaultValue);
71      }
72  
73      public static boolean[] getBooleanValues(String[] values) {
74          return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
75      }
76  
77      public static boolean[] getBooleanValues(
78          String[] values, boolean[] defaultValue) {
79  
80          if (values == null) {
81              return defaultValue;
82          }
83  
84          boolean[] booleanValues = new boolean[values.length];
85  
86          for (int i = 0; i < values.length; i++) {
87              booleanValues[i] = getBoolean(values[i]);
88          }
89  
90          return booleanValues;
91      }
92  
93      public static Date getDate(String value, DateFormat df) {
94          return getDate(value, df, new Date());
95      }
96  
97      public static Date getDate(String value, DateFormat df, Date defaultValue) {
98          return get(value, df, defaultValue);
99      }
100 
101     public static double getDouble(String value) {
102         return getDouble(value, DEFAULT_DOUBLE);
103     }
104 
105     public static double getDouble(String value, double defaultValue) {
106         return get(value, defaultValue);
107     }
108 
109     public static double[] getDoubleValues(String[] values) {
110         return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
111     }
112 
113     public static double[] getDoubleValues(
114         String[] values, double[] defaultValue) {
115 
116         if (values == null) {
117             return defaultValue;
118         }
119 
120         double[] doubleValues = new double[values.length];
121 
122         for (int i = 0; i < values.length; i++) {
123             doubleValues[i] = getDouble(values[i]);
124         }
125 
126         return doubleValues;
127     }
128 
129     public static float getFloat(String value) {
130         return getFloat(value, DEFAULT_FLOAT);
131     }
132 
133     public static float getFloat(String value, float defaultValue) {
134         return get(value, defaultValue);
135     }
136 
137     public static float[] getFloatValues(String[] values) {
138         return getFloatValues(values, DEFAULT_FLOAT_VALUES);
139     }
140 
141     public static float[] getFloatValues(
142         String[] values, float[] defaultValue) {
143 
144         if (values == null) {
145             return defaultValue;
146         }
147 
148         float[] floatValues = new float[values.length];
149 
150         for (int i = 0; i < values.length; i++) {
151             floatValues[i] = getFloat(values[i]);
152         }
153 
154         return floatValues;
155     }
156 
157     public static int getInteger(String value) {
158         return getInteger(value, DEFAULT_INTEGER);
159     }
160 
161     public static int getInteger(String value, int defaultValue) {
162         return get(value, defaultValue);
163     }
164 
165     public static int[] getIntegerValues(String[] values) {
166         return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
167     }
168 
169     public static int[] getIntegerValues(String[] values, 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, long[] defaultValue) {
196         if (values == null) {
197             return defaultValue;
198         }
199 
200         long[] longValues = new long[values.length];
201 
202         for (int i = 0; i < values.length; i++) {
203             longValues[i] = getLong(values[i]);
204         }
205 
206         return longValues;
207     }
208 
209     public static short getShort(String value) {
210         return getShort(value, DEFAULT_SHORT);
211     }
212 
213     public static short getShort(String value, short defaultValue) {
214         return get(value, defaultValue);
215     }
216 
217     public static short[] getShortValues(String[] values) {
218         return getShortValues(values, DEFAULT_SHORT_VALUES);
219     }
220 
221     public static short[] getShortValues(
222         String[] values, short[] defaultValue) {
223 
224         if (values == null) {
225             return defaultValue;
226         }
227 
228         short[] shortValues = new short[values.length];
229 
230         for (int i = 0; i < values.length; i++) {
231             shortValues[i] = getShort(values[i]);
232         }
233 
234         return shortValues;
235     }
236 
237     public static String getString(String value) {
238         return getString(value, DEFAULT_STRING);
239     }
240 
241     public static String getString(String value, String defaultValue) {
242         return get(value, defaultValue);
243     }
244 
245     public static boolean get(String value, boolean defaultValue) {
246         if (value != null) {
247             try {
248                 value = value.trim();
249 
250                 if (value.equalsIgnoreCase(BOOLEANS[0]) ||
251                     value.equalsIgnoreCase(BOOLEANS[1]) ||
252                     value.equalsIgnoreCase(BOOLEANS[2]) ||
253                     value.equalsIgnoreCase(BOOLEANS[3]) ||
254                     value.equalsIgnoreCase(BOOLEANS[4])) {
255 
256                     return true;
257                 }
258                 else {
259                     return false;
260                 }
261             }
262             catch (Exception e) {
263             }
264         }
265 
266         return defaultValue;
267     }
268 
269     public static Date get(String value, DateFormat df, Date defaultValue) {
270         try {
271             Date date = df.parse(value.trim());
272 
273             if (date != null) {
274                 return date;
275             }
276         }
277         catch (Exception e) {
278         }
279 
280         return defaultValue;
281     }
282 
283     public static double get(String value, double defaultValue) {
284         try {
285             return Double.parseDouble(_trim(value));
286         }
287         catch (Exception e) {
288         }
289 
290         return defaultValue;
291     }
292 
293     public static float get(String value, float defaultValue) {
294         try {
295             return Float.parseFloat(_trim(value));
296         }
297         catch (Exception e) {
298         }
299 
300         return defaultValue;
301     }
302 
303     public static int get(String value, int defaultValue) {
304         try {
305             return Integer.parseInt(_trim(value));
306         }
307         catch (Exception e) {
308         }
309 
310         return defaultValue;
311     }
312 
313     public static long get(String value, long defaultValue) {
314         try {
315             return Long.parseLong(_trim(value));
316         }
317         catch (Exception e) {
318         }
319 
320         return defaultValue;
321     }
322 
323     public static short get(String value, short defaultValue) {
324         try {
325             return Short.parseShort(_trim(value));
326         }
327         catch (Exception e) {
328         }
329 
330         return defaultValue;
331     }
332 
333     public static String get(String value, String defaultValue) {
334         if (value != null) {
335             value = value.trim();
336             value = StringUtil.replace(
337                 value, StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
338 
339             return value;
340         }
341 
342         return defaultValue;
343     }
344 
345     private static String _trim(String value) {
346         if (value != null) {
347             value = value.trim();
348 
349             StringBuilder sb = new StringBuilder();
350 
351             char[] charArray = value.toCharArray();
352 
353             for (int i = 0; i < charArray.length; i++) {
354                 if ((Character.isDigit(charArray[i])) ||
355                     ((charArray[i] == CharPool.DASH) && (i == 0)) ||
356                     (charArray[i] == CharPool.PERIOD) ||
357                     (charArray[i] == CharPool.UPPER_CASE_E) ||
358                     (charArray[i] == CharPool.LOWER_CASE_E)) {
359 
360                     sb.append(charArray[i]);
361                 }
362             }
363 
364             value = sb.toString();
365         }
366 
367         return value;
368     }
369 
370 }