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 public final class DatatypeIo {
25 private static final String XSD_POS_INF = "INF";
26 private static final String XSD_NEG_INF = "-INF";
27 private static final String JAVA_INF = "Infinity";
38 * Converts an int value into a string as xsd:int.
40 * @param iVal int value
41 * @return xsd:int type text
43 public static String printInt(int iVal){
45 result = String.valueOf(iVal);
50 * Converts an float value into a string as xsd:float.
52 * <p>Infinite value will be "INF".
54 * @param fVal float value
55 * @return xsd:float type text
57 public static String printFloat(float fVal){
60 if(fVal == Float.POSITIVE_INFINITY){
62 }else if(fVal == Float.NEGATIVE_INFINITY){
65 result = String.valueOf(fVal);
72 * trimming whitespace around XSD datatypes value.
74 * @param txt XSD value
75 * @return trimmed text
77 public static CharSequence xsdTrim(CharSequence txt){
78 int length = txt.length();
82 for(int pt = 0; pt < length; pt++){
83 char ch = txt.charAt(pt);
84 if(!isXsdWhitespace(ch)){
90 for(int pt = length - 1; pt >= 0; pt--){
91 char ch = txt.charAt(pt);
92 if(!isXsdWhitespace(ch)){
98 CharSequence result = txt.subSequence(startPos, endPos);
104 * checking whitespace character around XSD datattypes.
106 * <p>\n, \r, \t, and \0020 are whitespace.
108 * @param ch character
109 * @return true if whitespace
111 public static boolean isXsdWhitespace(char ch){
130 * Converts the xsd:boolean string argument into a boolean value.
132 * <p>{"true", "1"} is true. {"false", "0"} is false.
134 * @param xsdVal xsd:boolean string
135 * @return true if true
136 * @throws IllegalArgumentException illegal xsd:boolean string
138 public static boolean parseBoolean(CharSequence xsdVal)
139 throws IllegalArgumentException{
142 CharSequence trimmed = xsdTrim(xsdVal);
144 if("true".contentEquals(trimmed)){
146 }else if("false".contentEquals(trimmed)){
148 }else if("0".contentEquals(trimmed)){
150 }else if("1".contentEquals(trimmed)){
153 throw new IllegalArgumentException(trimmed.toString());
160 * Converts the xsd:byte string argument into a byte value.
162 * @param xsdVal xsd:byte string
164 * @throws NumberFormatException illegal xsd:byte
166 public static byte parseByte(CharSequence xsdVal)
167 throws NumberFormatException{
168 CharSequence trimmed = xsdTrim(xsdVal);
171 iVal = Integer.parseInt(trimmed.toString());
173 if(iVal < -128 || 127 < iVal){
174 throw new NumberFormatException(xsdVal.toString());
184 * Converts the xsd:int string argument into a int value.
186 * @param xsdVal xsd:int string
188 * @throws NumberFormatException illegal xsd:int
190 public static int parseInt(CharSequence xsdVal)
191 throws NumberFormatException{
192 CharSequence trimmed = xsdTrim(xsdVal);
194 result = Integer.parseInt(trimmed.toString());
199 * Converts the xsd:float string argument into a float value.
201 * @param xsdVal xsd:float string
202 * @return float value
203 * @throws NumberFormatException illegal xsd:float
205 public static float parseFloat(CharSequence xsdVal)
206 throws NumberFormatException{
207 String trimmed = xsdTrim(xsdVal).toString();
210 if(XSD_POS_INF.equals(trimmed)){
211 result = Float.POSITIVE_INFINITY;
212 }else if(XSD_NEG_INF.equals(trimmed)){
213 result = Float.NEGATIVE_INFINITY;
214 }else if(trimmed.endsWith(JAVA_INF)){
215 throw new NumberFormatException(trimmed);
216 }else if(trimmed.contains("x") || trimmed.contains("X")){
217 // HexFloatingPointLiteral
218 throw new NumberFormatException(trimmed);
220 // zero will happen when underflow.
221 // infinite will happen when overflow.
224 result = Float.parseFloat(trimmed);