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