1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
64 */
65 @Deprecated
66 public final class DatatypeConverterImpl implements DatatypeConverterInterface {
67
68 @Deprecated
69 public static final DatatypeConverterInterface theInstance = new DatatypeConverterImpl();
70
71 protected DatatypeConverterImpl() {
72 // shall not be used
73 }
74
75 public static BigInteger _parseInteger(CharSequence s) {
76 return new BigInteger(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString());
77 }
78
79 public static String _printInteger(BigInteger val) {
80 return val.toString();
81 }
82
83 /**
84 * Faster but less robust String->int conversion.
85 *
86 * Note that:
87 * <ol>
88 * <li>XML Schema allows '+', but {@link Integer#valueOf(String)} is not.
89 * <li>XML Schema allows leading and trailing (but not in-between) whitespaces.
90 * {@link Integer#valueOf(String)} doesn't allow any.
91 * </ol>
92 */
93 public static int _parseInt(CharSequence s) {
94 int len = s.length();
95 int sign = 1;
96
97 int r = 0;
98
99 for (int i = 0; i < len; i++) {
100 char ch = s.charAt(i);
101 if (WhiteSpaceProcessor.isWhiteSpace(ch)) {
102 // skip whitespace
103 } else if ('0' <= ch && ch <= '9') {
104 r = r * 10 + (ch - '0');
105 } else if (ch == '-') {
106 sign = -1;
107 } else if (ch == '+') {
108 // noop
109 } else {
110 throw new NumberFormatException("Not a number: " + s);
111 }
112 }
113
114 return r * sign;
115 }
116
117 public static long _parseLong(CharSequence s) {
118 return Long.valueOf(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString());
119 }
120
121 public static short _parseShort(CharSequence s) {
122 return (short) _parseInt(s);
123 }
124
125 public static String _printShort(short val) {
126 return String.valueOf(val);
127 }
128
129 public static BigDecimal _parseDecimal(CharSequence content) {
130 content = WhiteSpaceProcessor.trim(content);
131
132 if (content.length() <= 0) {
133 return null;
134 }
135
136 return new BigDecimal(content.toString());
137
138 // from purely XML Schema perspective,
|
1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
64 */
65 @Deprecated
66 public final class DatatypeConverterImpl implements DatatypeConverterInterface {
67
68 @Deprecated
69 public static final DatatypeConverterInterface theInstance = new DatatypeConverterImpl();
70
71 protected DatatypeConverterImpl() {
72 // shall not be used
73 }
74
75 public static BigInteger _parseInteger(CharSequence s) {
76 return new BigInteger(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString());
77 }
78
79 public static String _printInteger(BigInteger val) {
80 return val.toString();
81 }
82
83 /**
84 * Faster but less robust {@code String->int} conversion.
85 *
86 * Note that:
87 * <ol>
88 * <li>XML Schema allows '+', but {@link Integer#valueOf(String)} is not.
89 * <li>XML Schema allows leading and trailing (but not in-between) whitespaces.
90 * {@link Integer#valueOf(String)} doesn't allow any.
91 * </ol>
92 */
93 public static int _parseInt(CharSequence s) {
94 int len = s.length();
95 int sign = 1;
96
97 int r = 0;
98
99 for (int i = 0; i < len; i++) {
100 char ch = s.charAt(i);
101 if (WhiteSpaceProcessor.isWhiteSpace(ch)) {
102 // skip whitespace
103 } else if ('0' <= ch && ch <= '9') {
104 r = r * 10 + (ch - '0');
105 } else if (ch == '-') {
106 sign = -1;
107 } else if (ch == '+') {
108 // noop
109 } else {
110 throw new NumberFormatException("Not a number: " + s);
111 }
112 }
113
114 return r * sign;
115 }
116
117 public static long _parseLong(CharSequence s) {
118 return Long.parseLong(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString());
119 }
120
121 public static short _parseShort(CharSequence s) {
122 return (short) _parseInt(s);
123 }
124
125 public static String _printShort(short val) {
126 return String.valueOf(val);
127 }
128
129 public static BigDecimal _parseDecimal(CharSequence content) {
130 content = WhiteSpaceProcessor.trim(content);
131
132 if (content.length() <= 0) {
133 return null;
134 }
135
136 return new BigDecimal(content.toString());
137
138 // from purely XML Schema perspective,
|