001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.text.DateFormat;
018    
019    import java.util.Date;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class GetterUtil {
025    
026            public static final String[] BOOLEANS = {"true", "t", "y", "on", "1"};
027    
028            public static final boolean DEFAULT_BOOLEAN = false;
029    
030            public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
031    
032            public static final byte DEFAULT_BYTE = 0;
033    
034            public static final byte[] DEFAULT_BYTE_VALUES = new byte[0];
035    
036            public static final Date[] DEFAULT_DATE_VALUES = new Date[0];
037    
038            public static final double DEFAULT_DOUBLE = 0.0;
039    
040            public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
041    
042            public static final float DEFAULT_FLOAT = 0;
043    
044            public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
045    
046            public static final int DEFAULT_INTEGER = 0;
047    
048            public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
049    
050            public static final long DEFAULT_LONG = 0;
051    
052            public static final long[] DEFAULT_LONG_VALUES = new long[0];
053    
054            public static final Number DEFAULT_NUMBER = 0;
055    
056            public static final Number[] DEFAULT_NUMBER_VALUES = new Number[0];
057    
058            public static final Number DEFAULT_OBJECT = null;
059    
060            public static final short DEFAULT_SHORT = 0;
061    
062            public static final short[] DEFAULT_SHORT_VALUES = new short[0];
063    
064            public static final String DEFAULT_STRING = StringPool.BLANK;
065    
066            public static final String[] DEFAULT_STRING_VALUES = new String[0];
067    
068            public static boolean get(Object value, boolean defaultValue) {
069                    if (value == null) {
070                            return defaultValue;
071                    }
072    
073                    if (value instanceof String) {
074                            return get((String)value, defaultValue);
075                    }
076    
077                    Class<?> clazz = value.getClass();
078    
079                    if (clazz.isAssignableFrom(Boolean.class)) {
080                            return (Boolean)value;
081                    }
082    
083                    return defaultValue;
084            }
085    
086            public static Date get(
087                    Object value, DateFormat dateFormat, Date defaultValue) {
088    
089                    if (value == null) {
090                            return defaultValue;
091                    }
092    
093                    if (value instanceof String) {
094                            return get((String)value, dateFormat, defaultValue);
095                    }
096    
097                    Class<?> clazz = value.getClass();
098    
099                    if (clazz.isAssignableFrom(Date.class)) {
100                            return (Date)value;
101                    }
102    
103                    return defaultValue;
104            }
105    
106            public static double get(Object value, double defaultValue) {
107                    if (value == null) {
108                            return defaultValue;
109                    }
110    
111                    if (value instanceof String) {
112                            return get((String)value, defaultValue);
113                    }
114    
115                    Class<?> clazz = value.getClass();
116    
117                    if (clazz.isAssignableFrom(Double.class)) {
118                            return (Double)value;
119                    }
120    
121                    if (value instanceof Number) {
122                            Number number = (Number)value;
123    
124                            return number.doubleValue();
125                    }
126    
127                    return defaultValue;
128            }
129    
130            public static float get(Object value, float defaultValue) {
131                    if (value == null) {
132                            return defaultValue;
133                    }
134    
135                    if (value instanceof String) {
136                            return get((String)value, defaultValue);
137                    }
138    
139                    Class<?> clazz = value.getClass();
140    
141                    if (clazz.isAssignableFrom(Float.class)) {
142                            return (Float)value;
143                    }
144    
145                    if (value instanceof Number) {
146                            Number number = (Number)value;
147    
148                            return number.floatValue();
149                    }
150    
151                    return defaultValue;
152            }
153    
154            public static int get(Object value, int defaultValue) {
155                    if (value == null) {
156                            return defaultValue;
157                    }
158    
159                    if (value instanceof String) {
160                            return get((String)value, defaultValue);
161                    }
162    
163                    Class<?> clazz = value.getClass();
164    
165                    if (clazz.isAssignableFrom(Integer.class)) {
166                            return (Integer)value;
167                    }
168    
169                    if (value instanceof Number) {
170                            Number number = (Number)value;
171    
172                            return number.intValue();
173                    }
174    
175                    return defaultValue;
176            }
177    
178            public static long get(Object value, long defaultValue) {
179                    if (value == null) {
180                            return defaultValue;
181                    }
182    
183                    if (value instanceof String) {
184                            return get((String)value, defaultValue);
185                    }
186    
187                    Class<?> clazz = value.getClass();
188    
189                    if (clazz.isAssignableFrom(Long.class)) {
190                            return (Long)value;
191                    }
192    
193                    if (value instanceof Number) {
194                            Number number = (Number)value;
195    
196                            return number.longValue();
197                    }
198    
199                    return defaultValue;
200            }
201    
202            public static Number get(Object value, Number defaultValue) {
203                    if (value == null) {
204                            return defaultValue;
205                    }
206    
207                    if (value instanceof String) {
208                            if (Validator.isNull(value)) {
209                                    return defaultValue;
210                            }
211    
212                            if (getFloat(value) == getInteger(value)) {
213                                    return getInteger(value);
214                            }
215                            else {
216                                    return getFloat(value);
217                            }
218                    }
219    
220                    Class<?> clazz = value.getClass();
221    
222                    if (clazz.isAssignableFrom(Byte.class)) {
223                            return (Byte)value;
224                    }
225                    else if (clazz.isAssignableFrom(Double.class)) {
226                            return (Double)value;
227                    }
228                    else if (clazz.isAssignableFrom(Float.class)) {
229                            return (Float)value;
230                    }
231                    else if (clazz.isAssignableFrom(Integer.class)) {
232                            return (Integer)value;
233                    }
234                    else if (clazz.isAssignableFrom(Long.class)) {
235                            return (Long)value;
236                    }
237                    else if (clazz.isAssignableFrom(Short.class)) {
238                            return (Short)value;
239                    }
240    
241                    if (value instanceof Number) {
242                            return (Number)value;
243                    }
244    
245                    return defaultValue;
246            }
247    
248            public static short get(Object value, short defaultValue) {
249                    if (value == null) {
250                            return defaultValue;
251                    }
252    
253                    if (value instanceof String) {
254                            return get((String)value, defaultValue);
255                    }
256    
257                    Class<?> clazz = value.getClass();
258    
259                    if (clazz.isAssignableFrom(Short.class)) {
260                            return (Short)value;
261                    }
262    
263                    if (value instanceof Number) {
264                            Number number = (Number)value;
265    
266                            return number.shortValue();
267                    }
268    
269                    return defaultValue;
270            }
271    
272            public static String get(Object value, String defaultValue) {
273                    if (value == null) {
274                            return defaultValue;
275                    }
276    
277                    if (value instanceof String) {
278                            return get((String)value, defaultValue);
279                    }
280    
281                    return defaultValue;
282            }
283    
284            public static boolean get(String value, boolean defaultValue) {
285                    if (value == null) {
286                            return defaultValue;
287                    }
288    
289                    try {
290                            value = value.trim().toLowerCase();
291    
292                            if (value.equals(BOOLEANS[0]) || value.equals(BOOLEANS[1]) ||
293                                    value.equals(BOOLEANS[2]) || value.equals(BOOLEANS[3]) ||
294                                    value.equals(BOOLEANS[4])) {
295    
296                                    return true;
297                            }
298                            else {
299                                    return false;
300                            }
301                    }
302                    catch (Exception e) {
303                    }
304    
305                    return defaultValue;
306            }
307    
308            public static Date get(
309                    String value, DateFormat dateFormat, Date defaultValue) {
310    
311                    if (value == null) {
312                            return defaultValue;
313                    }
314    
315                    try {
316                            Date date = dateFormat.parse(value.trim());
317    
318                            if (date != null) {
319                                    return date;
320                            }
321                    }
322                    catch (Exception e) {
323                    }
324    
325                    return defaultValue;
326            }
327    
328            public static double get(String value, double defaultValue) {
329                    if (value != null) {
330                            try {
331                                    return Double.parseDouble(_trim(value));
332                            }
333                            catch (Exception e) {
334                            }
335                    }
336    
337                    return defaultValue;
338            }
339    
340            public static float get(String value, float defaultValue) {
341                    if (value == null) {
342                            return defaultValue;
343                    }
344    
345                    try {
346                            return Float.parseFloat(_trim(value));
347                    }
348                    catch (Exception e) {
349                    }
350    
351                    return defaultValue;
352            }
353    
354            public static int get(String value, int defaultValue) {
355                    if (value == null) {
356                            return defaultValue;
357                    }
358    
359                    return _parseInt(_trim(value), defaultValue);
360            }
361    
362            public static long get(String value, long defaultValue) {
363                    if (value == null) {
364                            return defaultValue;
365                    }
366    
367                    return _parseLong(_trim(value), defaultValue);
368            }
369    
370            public static short get(String value, short defaultValue) {
371                    if (value == null) {
372                            return defaultValue;
373                    }
374    
375                    return _parseShort(_trim(value), defaultValue);
376            }
377    
378            public static String get(String value, String defaultValue) {
379                    if (value == null) {
380                            return defaultValue;
381                    }
382    
383                    value = value.trim();
384    
385                    if (value.indexOf(CharPool.RETURN) != -1) {
386                            value = StringUtil.replace(
387                                    value, StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
388                    }
389    
390                    return value;
391            }
392    
393            public static boolean getBoolean(Object value) {
394                    return getBoolean(value, DEFAULT_BOOLEAN);
395            }
396    
397            public static boolean getBoolean(Object value, boolean defaultValue) {
398                    return get(value, defaultValue);
399            }
400    
401            public static boolean getBoolean(String value) {
402                    return getBoolean(value, DEFAULT_BOOLEAN);
403            }
404    
405            public static boolean getBoolean(String value, boolean defaultValue) {
406                    return get(value, defaultValue);
407            }
408    
409            public static boolean[] getBooleanValues(Object value) {
410                    return getBooleanValues(value, DEFAULT_BOOLEAN_VALUES);
411            }
412    
413            public static boolean[] getBooleanValues(
414                    Object value, boolean[] defaultValue) {
415    
416                    Class<?> clazz = value.getClass();
417    
418                    if (clazz.isArray()) {
419                            Class<?> componentType = clazz.getComponentType();
420    
421                            if (componentType.isAssignableFrom(String.class)) {
422                                    return getBooleanValues((String[])value, defaultValue);
423                            }
424                            else if (componentType.isAssignableFrom(Boolean.class)) {
425                                    return (boolean[])value;
426                            }
427                    }
428    
429                    return defaultValue;
430            }
431    
432            public static boolean[] getBooleanValues(String[] values) {
433                    return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
434            }
435    
436            public static boolean[] getBooleanValues(
437                    String[] values, boolean[] defaultValue) {
438    
439                    if (values == null) {
440                            return defaultValue;
441                    }
442    
443                    boolean[] booleanValues = new boolean[values.length];
444    
445                    for (int i = 0; i < values.length; i++) {
446                            booleanValues[i] = getBoolean(values[i]);
447                    }
448    
449                    return booleanValues;
450            }
451    
452            public static Date getDate(Object value, DateFormat dateFormat) {
453                    return getDate(value, dateFormat, new Date());
454            }
455    
456            public static Date getDate(
457                    Object value, DateFormat dateFormat, Date defaultValue) {
458    
459                    return get(value, dateFormat, defaultValue);
460            }
461    
462            public static Date getDate(String value, DateFormat dateFormat) {
463                    return getDate(value, dateFormat, new Date());
464            }
465    
466            public static Date getDate(
467                    String value, DateFormat dateFormat, Date defaultValue) {
468    
469                    return get(value, dateFormat, defaultValue);
470            }
471    
472            public static Date[] getDateValues(Object value, DateFormat dateFormat) {
473                    return getDateValues(value, dateFormat, DEFAULT_DATE_VALUES);
474            }
475    
476            public static Date[] getDateValues(
477                    Object value, DateFormat dateFormat, Date[] defaultValue) {
478    
479                    Class<?> clazz = value.getClass();
480    
481                    if (clazz.isArray()) {
482                            Class<?> componentType = clazz.getComponentType();
483    
484                            if (componentType.isAssignableFrom(String.class)) {
485                                    return getDateValues((String[])value, dateFormat, defaultValue);
486                            }
487                            else if (componentType.isAssignableFrom(Date.class)) {
488                                    return (Date[])value;
489                            }
490                    }
491    
492                    return defaultValue;
493            }
494    
495            public static Date[] getDateValues(String[] values, DateFormat dateFormat) {
496                    return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES);
497            }
498    
499            public static Date[] getDateValues(
500                    String[] values, DateFormat dateFormat, Date[] defaultValue) {
501    
502                    if (values == null) {
503                            return defaultValue;
504                    }
505    
506                    Date[] dateValues = new Date[values.length];
507    
508                    for (int i = 0; i < values.length; i++) {
509                            dateValues[i] = getDate(values[i], dateFormat);
510                    }
511    
512                    return dateValues;
513            }
514    
515            public static double getDouble(Object value) {
516                    return getDouble(value, DEFAULT_DOUBLE);
517            }
518    
519            public static double getDouble(Object value, double defaultValue) {
520                    return get(value, defaultValue);
521            }
522    
523            public static double getDouble(String value) {
524                    return getDouble(value, DEFAULT_DOUBLE);
525            }
526    
527            public static double getDouble(String value, double defaultValue) {
528                    return get(value, defaultValue);
529            }
530    
531            public static double[] getDoubleValues(Object value) {
532                    return getDoubleValues(value, DEFAULT_DOUBLE_VALUES);
533            }
534    
535            public static double[] getDoubleValues(
536                    Object value, double[] defaultValue) {
537    
538                    Class<?> clazz = value.getClass();
539    
540                    if (clazz.isArray()) {
541                            Class<?> componentType = clazz.getComponentType();
542    
543                            if (componentType.isAssignableFrom(String.class)) {
544                                    return getDoubleValues((String[])value, defaultValue);
545                            }
546                            else if (componentType.isAssignableFrom(Double.class)) {
547                                    return (double[])value;
548                            }
549                    }
550    
551                    return defaultValue;
552            }
553    
554            public static double[] getDoubleValues(String[] values) {
555                    return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
556            }
557    
558            public static double[] getDoubleValues(
559                    String[] values, double[] defaultValue) {
560    
561                    if (values == null) {
562                            return defaultValue;
563                    }
564    
565                    double[] doubleValues = new double[values.length];
566    
567                    for (int i = 0; i < values.length; i++) {
568                            doubleValues[i] = getDouble(values[i]);
569                    }
570    
571                    return doubleValues;
572            }
573    
574            public static float getFloat(Object value) {
575                    return getFloat(value, DEFAULT_FLOAT);
576            }
577    
578            public static float getFloat(Object value, float defaultValue) {
579                    return get(value, defaultValue);
580            }
581    
582            public static float getFloat(String value) {
583                    return getFloat(value, DEFAULT_FLOAT);
584            }
585    
586            public static float getFloat(String value, float defaultValue) {
587                    return get(value, defaultValue);
588            }
589    
590            public static float[] getFloatValues(Object value) {
591                    return getFloatValues(value, DEFAULT_FLOAT_VALUES);
592            }
593    
594            public static float[] getFloatValues(Object value, float[] defaultValue) {
595                    Class<?> clazz = value.getClass();
596    
597                    if (clazz.isArray()) {
598                            Class<?> componentType = clazz.getComponentType();
599    
600                            if (componentType.isAssignableFrom(String.class)) {
601                                    return getFloatValues((String[])value, defaultValue);
602                            }
603                            else if (componentType.isAssignableFrom(Float.class)) {
604                                    return (float[])value;
605                            }
606                    }
607    
608                    return defaultValue;
609            }
610    
611            public static float[] getFloatValues(String[] values) {
612                    return getFloatValues(values, DEFAULT_FLOAT_VALUES);
613            }
614    
615            public static float[] getFloatValues(
616                    String[] values, float[] defaultValue) {
617    
618                    if (values == null) {
619                            return defaultValue;
620                    }
621    
622                    float[] floatValues = new float[values.length];
623    
624                    for (int i = 0; i < values.length; i++) {
625                            floatValues[i] = getFloat(values[i]);
626                    }
627    
628                    return floatValues;
629            }
630    
631            public static int getInteger(Object value) {
632                    return getInteger(value, DEFAULT_INTEGER);
633            }
634    
635            public static int getInteger(Object value, int defaultValue) {
636                    return get(value, defaultValue);
637            }
638    
639            public static int getInteger(String value) {
640                    return getInteger(value, DEFAULT_INTEGER);
641            }
642    
643            public static int getInteger(String value, int defaultValue) {
644                    return get(value, defaultValue);
645            }
646    
647            public static int[] getIntegerValues(Object value) {
648                    return getIntegerValues(value, DEFAULT_INTEGER_VALUES);
649            }
650    
651            public static int[] getIntegerValues(Object value, int[] defaultValue) {
652                    Class<?> clazz = value.getClass();
653    
654                    if (clazz.isArray()) {
655                            Class<?> componentType = clazz.getComponentType();
656    
657                            if (componentType.isAssignableFrom(String.class)) {
658                                    return getIntegerValues((String[])value, defaultValue);
659                            }
660                            else if (componentType.isAssignableFrom(Integer.class)) {
661                                    return (int[])value;
662                            }
663                    }
664    
665                    return defaultValue;
666            }
667    
668            public static int[] getIntegerValues(String[] values) {
669                    return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
670            }
671    
672            public static int[] getIntegerValues(String[] values, int[] defaultValue) {
673                    if (values == null) {
674                            return defaultValue;
675                    }
676    
677                    int[] intValues = new int[values.length];
678    
679                    for (int i = 0; i < values.length; i++) {
680                            intValues[i] = getInteger(values[i]);
681                    }
682    
683                    return intValues;
684            }
685    
686            public static long getLong(Object value) {
687                    return getLong(value, DEFAULT_LONG);
688            }
689    
690            public static long getLong(Object value, long defaultValue) {
691                    return get(value, defaultValue);
692            }
693    
694            public static long getLong(String value) {
695                    return getLong(value, DEFAULT_LONG);
696            }
697    
698            public static long getLong(String value, long defaultValue) {
699                    return get(value, defaultValue);
700            }
701    
702            public static long[] getLongValues(Object value) {
703                    return getLongValues(value, DEFAULT_LONG_VALUES);
704            }
705    
706            public static long[] getLongValues(Object value, long[] defaultValue) {
707                    Class<?> clazz = value.getClass();
708    
709                    if (clazz.isArray()) {
710                            Class<?> componentType = clazz.getComponentType();
711    
712                            if (componentType.isAssignableFrom(String.class)) {
713                                    return getLongValues((String[])value, defaultValue);
714                            }
715                            else if (componentType.isAssignableFrom(Long.class)) {
716                                    return (long[])value;
717                            }
718                            else if (Number.class.isAssignableFrom(componentType)) {
719                                    Number[] numbers = (Number[])value;
720    
721                                    long[] values = new long[numbers.length];
722    
723                                    for (int i = 0; i < values.length; i++) {
724                                            values[i] = numbers[i].longValue();
725                                    }
726    
727                                    return values;
728                            }
729                    }
730    
731                    return defaultValue;
732            }
733    
734            public static long[] getLongValues(String[] values) {
735                    return getLongValues(values, DEFAULT_LONG_VALUES);
736            }
737    
738            public static long[] getLongValues(String[] values, long[] defaultValue) {
739                    if (values == null) {
740                            return defaultValue;
741                    }
742    
743                    long[] longValues = new long[values.length];
744    
745                    for (int i = 0; i < values.length; i++) {
746                            longValues[i] = getLong(values[i]);
747                    }
748    
749                    return longValues;
750            }
751    
752            public static Number getNumber(Object value) {
753                    return getNumber(value, DEFAULT_NUMBER);
754            }
755    
756            public static Number getNumber(Object value, Number defaultValue) {
757                    return get(value, defaultValue);
758            }
759    
760            public static Number getNumber(String value) {
761                    return getNumber(value, DEFAULT_NUMBER);
762            }
763    
764            public static Number getNumber(String value, Number defaultValue) {
765                    return get(value, defaultValue);
766            }
767    
768            public static Number[] getNumberValues(Object value) {
769                    return getNumberValues(value, DEFAULT_NUMBER_VALUES);
770            }
771    
772            public static Number[] getNumberValues(
773                    Object value, Number[] defaultValue) {
774    
775                    Class<?> clazz = value.getClass();
776    
777                    if (clazz.isArray()) {
778                            Class<?> componentType = clazz.getComponentType();
779    
780                            if (componentType.isAssignableFrom(String.class)) {
781                                    return getNumberValues((String[])value, defaultValue);
782                            }
783                            else if (componentType.isAssignableFrom(Number.class)) {
784                                    return (Number[])value;
785                            }
786                    }
787    
788                    return defaultValue;
789            }
790    
791            public static Number[] getNumberValues(String[] values) {
792                    return getNumberValues(values, DEFAULT_NUMBER_VALUES);
793            }
794    
795            public static Number[] getNumberValues(
796                    String[] values, Number[] defaultValue) {
797    
798                    if (values == null) {
799                            return defaultValue;
800                    }
801    
802                    Number[] numberValues = new Number[values.length];
803    
804                    for (int i = 0; i < values.length; i++) {
805                            numberValues[i] = getNumber(values[i]);
806                    }
807    
808                    return numberValues;
809            }
810    
811            public static Object getObject(Object value) {
812                    return getObject(value, DEFAULT_OBJECT);
813            }
814    
815            public static Object getObject(Object value, Object defaultValue) {
816                    if (value == null) {
817                            return defaultValue;
818                    }
819    
820                    return value;
821            }
822    
823            public static short getShort(Object value) {
824                    return getShort(value, DEFAULT_SHORT);
825            }
826    
827            public static short getShort(Object value, short defaultValue) {
828                    return get(value, defaultValue);
829            }
830    
831            public static short getShort(String value) {
832                    return getShort(value, DEFAULT_SHORT);
833            }
834    
835            public static short getShort(String value, short defaultValue) {
836                    return get(value, defaultValue);
837            }
838    
839            public static short[] getShortValues(Object value) {
840                    return getShortValues(value, DEFAULT_SHORT_VALUES);
841            }
842    
843            public static short[] getShortValues(Object value, short[] defaultValue) {
844                    Class<?> clazz = value.getClass();
845    
846                    if (clazz.isArray()) {
847                            Class<?> componentType = clazz.getComponentType();
848    
849                            if (componentType.isAssignableFrom(String.class)) {
850                                    return getShortValues((String[])value, defaultValue);
851                            }
852                            else if (componentType.isAssignableFrom(Short.class)) {
853                                    return (short[])value;
854                            }
855                    }
856    
857                    return defaultValue;
858            }
859    
860            public static short[] getShortValues(String[] values) {
861                    return getShortValues(values, DEFAULT_SHORT_VALUES);
862            }
863    
864            public static short[] getShortValues(
865                    String[] values, short[] defaultValue) {
866    
867                    if (values == null) {
868                            return defaultValue;
869                    }
870    
871                    short[] shortValues = new short[values.length];
872    
873                    for (int i = 0; i < values.length; i++) {
874                            shortValues[i] = getShort(values[i]);
875                    }
876    
877                    return shortValues;
878            }
879    
880            public static String getString(Object value) {
881                    return getString(value, DEFAULT_STRING);
882            }
883    
884            public static String getString(Object value, String defaultValue) {
885                    return get(value, defaultValue);
886            }
887    
888            public static String getString(String value) {
889                    return getString(value, DEFAULT_STRING);
890            }
891    
892            public static String getString(String value, String defaultValue) {
893                    return get(value, defaultValue);
894            }
895    
896            public static String[] getStringValues(Object value) {
897                    return getStringValues(value, DEFAULT_STRING_VALUES);
898            }
899    
900            public static String[] getStringValues(
901                    Object value, String[] defaultValue) {
902    
903                    Class<?> clazz = value.getClass();
904    
905                    if (clazz.isArray()) {
906                            Class<?> componentType = clazz.getComponentType();
907    
908                            if (componentType.isAssignableFrom(String.class)) {
909                                    return getStringValues((String[])value, defaultValue);
910                            }
911                    }
912    
913                    return defaultValue;
914            }
915    
916            public static String[] getStringValues(
917                    Object[] values, String[] defaultValue) {
918    
919                    if (values == null) {
920                            return defaultValue;
921                    }
922    
923                    String[] stringValues = new String[values.length];
924    
925                    for (int i = 0; i < values.length; i++) {
926                            stringValues[i] = String.valueOf(values[i]);
927                    }
928    
929                    return stringValues;
930            }
931    
932            public static String[] getStringValues(String[] values) {
933                    return getStringValues(values, DEFAULT_STRING_VALUES);
934            }
935    
936            private static int _parseInt(String value, int defaultValue) {
937                    int length = value.length();
938    
939                    if (length <= 0) {
940                            return defaultValue;
941                    }
942    
943                    int pos = 0;
944                    int limit = -Integer.MAX_VALUE;
945                    boolean negative = false;
946    
947                    char c = value.charAt(0);
948    
949                    if (c < CharPool.NUMBER_0) {
950                            if (c == CharPool.MINUS) {
951                                    limit = Integer.MIN_VALUE;
952                                    negative = true;
953                            }
954                            else if (c != CharPool.PLUS) {
955                                    return defaultValue;
956                            }
957    
958                            if (length == 1) {
959                                    return defaultValue;
960                            }
961    
962                            pos++;
963                    }
964    
965                    int smallLimit = limit / 10;
966    
967                    int result = 0;
968    
969                    while (pos < length) {
970                            if (result < smallLimit) {
971                                    return defaultValue;
972                            }
973    
974                            c = value.charAt(pos++);
975    
976                            if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
977                                    return defaultValue;
978                            }
979    
980                            int number = c - CharPool.NUMBER_0;
981    
982                            result *= 10;
983    
984                            if (result < (limit + number)) {
985                                    return defaultValue;
986                            }
987    
988                            result -= number;
989                    }
990    
991                    if (negative) {
992                            return result;
993                    }
994                    else {
995                            return -result;
996                    }
997            }
998    
999            private static long _parseLong(String value, long defaultValue) {
1000                    if (_useJDKParseLong == null) {
1001                            if (OSDetector.isAIX() && ServerDetector.isWebSphere() &&
1002                                    JavaDetector.isIBM() && JavaDetector.is64bit()) {
1003    
1004                                    _useJDKParseLong = Boolean.TRUE;
1005                            }
1006                            else {
1007                                    _useJDKParseLong = Boolean.FALSE;
1008                            }
1009                    }
1010    
1011                    if (_useJDKParseLong) {
1012                            try {
1013                                    return Long.parseLong(value);
1014                            }
1015                            catch (NumberFormatException nfe) {
1016                                    return defaultValue;
1017                            }
1018                    }
1019    
1020                    int length = value.length();
1021    
1022                    if (length <= 0) {
1023                            return defaultValue;
1024                    }
1025    
1026                    int pos = 0;
1027                    long limit = -Long.MAX_VALUE;
1028                    boolean negative = false;
1029    
1030                    char c = value.charAt(0);
1031    
1032                    if (c < CharPool.NUMBER_0) {
1033                            if (c == CharPool.MINUS) {
1034                                    limit = Long.MIN_VALUE;
1035                                    negative = true;
1036                            }
1037                            else if (c != CharPool.PLUS) {
1038                                    return defaultValue;
1039                            }
1040    
1041                            if (length == 1) {
1042                                    return defaultValue;
1043                            }
1044    
1045                            pos++;
1046                    }
1047    
1048                    long smallLimit = limit / 10;
1049    
1050                    long result = 0;
1051    
1052                    while (pos < length) {
1053                            if (result < smallLimit) {
1054                                    return defaultValue;
1055                            }
1056    
1057                            c = value.charAt(pos++);
1058    
1059                            if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
1060                                    return defaultValue;
1061                            }
1062    
1063                            int number = c - CharPool.NUMBER_0;
1064    
1065                            result *= 10;
1066    
1067                            if (result < (limit + number)) {
1068                                    return defaultValue;
1069                            }
1070    
1071                            result -= number;
1072                    }
1073    
1074                    if (negative) {
1075                            return result;
1076                    }
1077                    else {
1078                            return -result;
1079                    }
1080            }
1081    
1082            private static short _parseShort(String value, short defaultValue) {
1083                    int i = _parseInt(value, defaultValue);
1084    
1085                    if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
1086                            return defaultValue;
1087                    }
1088    
1089                    return (short)i;
1090            }
1091    
1092            private static String _trim(String value) {
1093                    value = value.trim();
1094    
1095                    int length = value.length();
1096    
1097                    StringBuilder sb = new StringBuilder(length);
1098    
1099                    for (int i = 0; i < length; i++) {
1100                            char c = value.charAt(i);
1101    
1102                            if (Character.isDigit(c) ||
1103                                    ((c == CharPool.DASH) &&
1104                                     ((i == 0) || (value.charAt(i - 1) == CharPool.UPPER_CASE_E) ||
1105                                      (value.charAt(i - 1) == CharPool.LOWER_CASE_E))) ||
1106                                    (c == CharPool.PERIOD) || (c == CharPool.UPPER_CASE_E) ||
1107                                    (c == CharPool.LOWER_CASE_E)) {
1108    
1109                                    sb.append(c);
1110                            }
1111                    }
1112    
1113                    return sb.toString();
1114            }
1115    
1116            private static Boolean _useJDKParseLong;
1117    
1118    }