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