2 * XML Schema datatypes input/output
4 * License : The MIT License
5 * Copyright(c) 2019 MikuToga Partners
8 package jp.sfjp.mikutoga.xml;
11 * XSD datatypes I/O utilities.
13 * <p>This class replaces javax.xml.bind.DatatypeConverter(JAXB) subset.
14 * JAXB is not part of JDK9 or later.
16 * @see <a href="https://www.w3.org/TR/xmlschema-2/">
17 * XML Schema Part 2: Datatypes Second Edition
19 * @see <a href="https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/DatatypeConverter.html">
20 * JavaSE8:DatatypeConverter
23 final class DatatypeIo {
33 * Converts an int value into a string as xsd:int.
35 * @param iVal int value
36 * @return xsd:int type text
38 public static String printInt(int iVal){
40 result = String.valueOf(iVal);
45 * Converts an float value into a string as xsd:float.
47 * <p>Infinite value will be "INF".
49 * @param fVal float value
50 * @return xsd:float type text
52 public static String printFloat(float fVal){
55 if(fVal == Float.POSITIVE_INFINITY){
57 }else if(fVal == Float.NEGATIVE_INFINITY){
60 result = String.valueOf(fVal);
67 * trimming whitespace around XSD datatypes value.
69 * @param txt XSD value
70 * @return trimmed text
72 public static CharSequence xsdTrim(CharSequence txt){
73 int length = txt.length();
77 for(int pt = 0; pt < length; pt++){
78 char ch = txt.charAt(pt);
79 if(!isXsdWhitespace(ch)){
85 for(int pt = length - 1; pt >= 0; pt--){
86 char ch = txt.charAt(pt);
87 if(!isXsdWhitespace(ch)){
93 CharSequence result = txt.subSequence(startPos, endPos);
99 * checking whitespace character around XSD datattypes.
101 * <p>\n, \r, \t, and \0020 are whitespace.
103 * @param ch character
104 * @return true if whitespace
106 public static boolean isXsdWhitespace(char ch){
125 * Converts the xsd:boolean string argument into a boolean value.
127 * <p>{"true", "1"} is true. {"false", "0"} is false.
129 * @param xsdVal xsd:boolean string
130 * @return true if true
131 * @throws IllegalArgumentException illegal xsd:boolean string
133 public static boolean parseBoolean(CharSequence xsdVal)
134 throws IllegalArgumentException{
137 CharSequence trimmed = xsdTrim(xsdVal);
139 if("true".contentEquals(trimmed)){
141 }else if("false".contentEquals(trimmed)){
143 }else if("0".contentEquals(trimmed)){
145 }else if("1".contentEquals(trimmed)){
148 throw new IllegalArgumentException(trimmed.toString());
155 * Converts the xsd:byte string argument into a byte value.
157 * @param xsdVal xsd:byte string
159 * @throws NumberFormatException illegal xsd:byte
161 public static byte parseByte(CharSequence xsdVal)
162 throws NumberFormatException{
163 CharSequence trimmed = xsdTrim(xsdVal);
166 iVal = Integer.parseInt(trimmed.toString());
168 if(iVal < -128 || 127 < iVal){
169 throw new NumberFormatException(xsdVal.toString());
179 * Converts the xsd:int string argument into a int value.
181 * @param xsdVal xsd:int string
183 * @throws NumberFormatException illegal xsd:int
185 public static int parseInt(CharSequence xsdVal)
186 throws NumberFormatException{
187 CharSequence trimmed = xsdTrim(xsdVal);
189 result = Integer.parseInt(trimmed.toString());
194 * Converts the xsd:float string argument into a float value.
196 * @param xsdVal xsd:float string
197 * @return float value
198 * @throws NumberFormatException illegal xsd:float
200 public static float parseFloat(CharSequence xsdVal)
201 throws NumberFormatException{
202 String trimmed = xsdTrim(xsdVal).toString();
205 if("INF".equals(trimmed)){
206 result = Float.POSITIVE_INFINITY;
207 }else if("-INF".equals(trimmed)){
208 result = Float.NEGATIVE_INFINITY;
209 }else if(trimmed.endsWith("Infinity")){
210 throw new NumberFormatException(trimmed);
211 }else if(trimmed.contains("x") || trimmed.contains("X")){
212 // HexFloatingPointLiteral
213 throw new NumberFormatException(trimmed);
215 // zero will happen when underflow.
216 // infinite will happen when overflow.
219 result = Float.parseFloat(trimmed);