1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class GetterUtil {
35  
36      public static final boolean DEFAULT_BOOLEAN = false;
37  
38      public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
39  
40      public static final double DEFAULT_DOUBLE = 0.0;
41  
42      public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
43  
44      public static final float DEFAULT_FLOAT = 0;
45  
46      public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
47  
48      public static final int DEFAULT_INTEGER = 0;
49  
50      public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
51  
52      public static final long DEFAULT_LONG = 0;
53  
54      public static final long[] DEFAULT_LONG_VALUES = new long[0];
55  
56      public static final short DEFAULT_SHORT = 0;
57  
58      public static final short[] DEFAULT_SHORT_VALUES = new short[0];
59  
60      public static final String DEFAULT_STRING = StringPool.BLANK;
61  
62      public static String[] BOOLEANS = {"true", "t", "y", "on", "1"};
63  
64      public static boolean getBoolean(String value) {
65          return getBoolean(value, DEFAULT_BOOLEAN);
66      }
67  
68      public static boolean getBoolean(String value, boolean defaultValue) {
69          return get(value, defaultValue);
70      }
71  
72      public static boolean[] getBooleanValues(String[] values) {
73          return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
74      }
75  
76      public static boolean[] getBooleanValues(
77          String[] values, boolean[] defaultValue) {
78  
79          if (values == null) {
80              return defaultValue;
81          }
82  
83          boolean[] booleanValues = new boolean[values.length];
84  
85          for (int i = 0; i < values.length; i++) {
86              booleanValues[i] = getBoolean(values[i]);
87          }
88  
89          return booleanValues;
90      }
91  
92      public static Date getDate(String value, DateFormat df) {
93          return getDate(value, df, new Date());
94      }
95  
96      public static Date getDate(String value, DateFormat df, Date defaultValue) {
97          return get(value, df, defaultValue);
98      }
99  
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(
113         String[] values, 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(
141         String[] values, 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, int[] defaultValue) {
169         if (values == null) {
170             return defaultValue;
171         }
172 
173         int[] intValues = new int[values.length];
174 
175         for (int i = 0; i < values.length; i++) {
176             intValues[i] = getInteger(values[i]);
177         }
178 
179         return intValues;
180     }
181 
182     public static long getLong(String value) {
183         return getLong(value, DEFAULT_LONG);
184     }
185 
186     public static long getLong(String value, long defaultValue) {
187         return get(value, defaultValue);
188     }
189 
190     public static long[] getLongValues(String[] values) {
191         return getLongValues(values, DEFAULT_LONG_VALUES);
192     }
193 
194     public static long[] getLongValues(String[] values, long[] defaultValue) {
195         if (values == null) {
196             return defaultValue;
197         }
198 
199         long[] longValues = new long[values.length];
200 
201         for (int i = 0; i < values.length; i++) {
202             longValues[i] = getLong(values[i]);
203         }
204 
205         return longValues;
206     }
207 
208     public static short getShort(String value) {
209         return getShort(value, DEFAULT_SHORT);
210     }
211 
212     public static short getShort(String value, short defaultValue) {
213         return get(value, defaultValue);
214     }
215 
216     public static short[] getShortValues(String[] values) {
217         return getShortValues(values, DEFAULT_SHORT_VALUES);
218     }
219 
220     public static short[] getShortValues(
221         String[] values, short[] defaultValue) {
222 
223         if (values == null) {
224             return defaultValue;
225         }
226 
227         short[] shortValues = new short[values.length];
228 
229         for (int i = 0; i < values.length; i++) {
230             shortValues[i] = getShort(values[i]);
231         }
232 
233         return shortValues;
234     }
235 
236     public static String getString(String value) {
237         return getString(value, DEFAULT_STRING);
238     }
239 
240     public static String getString(String value, String defaultValue) {
241         return get(value, defaultValue);
242     }
243 
244     public static boolean get(String value, boolean defaultValue) {
245         if (value != null) {
246             try {
247                 value = value.trim();
248 
249                 if (value.equalsIgnoreCase(BOOLEANS[0]) ||
250                     value.equalsIgnoreCase(BOOLEANS[1]) ||
251                     value.equalsIgnoreCase(BOOLEANS[2]) ||
252                     value.equalsIgnoreCase(BOOLEANS[3]) ||
253                     value.equalsIgnoreCase(BOOLEANS[4])) {
254 
255                     return true;
256                 }
257                 else {
258                     return false;
259                 }
260             }
261             catch (Exception e) {
262             }
263         }
264 
265         return defaultValue;
266     }
267 
268     public static Date get(String value, DateFormat df, Date defaultValue) {
269         try {
270             Date date = df.parse(value.trim());
271 
272             if (date != null) {
273                 return date;
274             }
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         }
286         catch (Exception e) {
287         }
288 
289         return defaultValue;
290     }
291 
292     public static float get(String value, float defaultValue) {
293         try {
294             return Float.parseFloat(_trim(value));
295         }
296         catch (Exception e) {
297         }
298 
299         return defaultValue;
300     }
301 
302     public static int get(String value, int defaultValue) {
303         try {
304             return Integer.parseInt(_trim(value));
305         }
306         catch (Exception e) {
307         }
308 
309         return defaultValue;
310     }
311 
312     public static long get(String value, long defaultValue) {
313         try {
314             return Long.parseLong(_trim(value));
315         }
316         catch (Exception e) {
317         }
318 
319         return defaultValue;
320     }
321 
322     public static short get(String value, short defaultValue) {
323         try {
324             return Short.parseShort(_trim(value));
325         }
326         catch (Exception e) {
327         }
328 
329         return defaultValue;
330     }
331 
332     public static String get(String value, String defaultValue) {
333         if (value != null) {
334             value = value.trim();
335             value = StringUtil.replace(
336                 value, StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
337 
338             return value;
339         }
340 
341         return defaultValue;
342     }
343 
344     private static String _trim(String value) {
345         if (value != null) {
346             value = value.trim();
347 
348             StringBuilder sb = new StringBuilder();
349 
350             char[] charArray = value.toCharArray();
351 
352             for (int i = 0; i < charArray.length; i++) {
353                 if ((Character.isDigit(charArray[i])) ||
354                     ((charArray[i] == CharPool.DASH) && (i == 0)) ||
355                     (charArray[i] == CharPool.PERIOD) ||
356                     (charArray[i] == CharPool.UPPER_CASE_E) ||
357                     (charArray[i] == CharPool.LOWER_CASE_E)) {
358 
359                     sb.append(charArray[i]);
360                 }
361             }
362 
363             value = sb.toString();
364         }
365 
366         return value;
367     }
368 
369 }