public static float _parseFloat(CharSequence _val) { String s = WhiteSpaceProcessor.trim(_val).toString(); /* Incompatibilities of XML Schema's float "xfloat" and Java's float "jfloat" * jfloat.valueOf ignores leading and trailing whitespaces, whereas this is not allowed in xfloat. * jfloat.valueOf allows "float type suffix" (f, F) to be appended after float literal (e.g., 1.52e-2f), whereare this is not the case of xfloat. gray zone --------- * jfloat allows ".523". And there is no clear statement that mentions this case in xfloat. Although probably this is allowed. * */ if (s.equals("NaN")) return Float.NaN; if (s.equals("INF")) return Float.POSITIVE_INFINITY; if (s.equals("-INF")) return Float.NEGATIVE_INFINITY; if (s.length() == 0 || !isDigitOrPeriodOrSign(s.charAt(0)) || !isDigitOrPeriodOrSign(s.charAt(s.length() - 1))) throw new NumberFormatException(); // these screening process is necessary due to the wobble of Float.valueOf method return Float.parseFloat(s); }
/** @return null if fails to convert. */ public static QName _parseQName(CharSequence text, NamespaceContext nsc) { int length = text.length(); // trim whitespace int start = 0; while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) start++; int end = length; while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) end--; if (end == start) throw new IllegalArgumentException("input is empty"); String uri; String localPart; String prefix; // search ':' int idx = start + 1; // no point in searching the first char. that's not valid. while (idx < end && text.charAt(idx) != ':') idx++; if (idx == end) { uri = nsc.getNamespaceURI(""); localPart = text.subSequence(start, end).toString(); prefix = ""; } else { // Prefix exists, check everything prefix = text.subSequence(start, idx).toString(); localPart = text.subSequence(idx + 1, end).toString(); uri = nsc.getNamespaceURI(prefix); // uri can never be null according to javadoc, // but some users reported that there are implementations that return null. if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken. // error: unbound prefix throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace"); } return new QName(uri, localPart, prefix); }
public static boolean _parseBoolean(CharSequence literal) { int i = 0; int len = literal.length(); char ch; do { ch = literal.charAt(i++); } while (WhiteSpaceProcessor.isWhiteSpace(ch) && i < len); // if we are strict about errors, check i==len. and report an error if (ch == 't' || ch == '1') return true; if (ch == 'f' || ch == '0') return false; return false; }
public static double _parseDouble(CharSequence _val) { String val = WhiteSpaceProcessor.trim(_val).toString(); if (val.equals("NaN")) return Double.NaN; if (val.equals("INF")) return Double.POSITIVE_INFINITY; if (val.equals("-INF")) return Double.NEGATIVE_INFINITY; if (val.length() == 0 || !isDigitOrPeriodOrSign(val.charAt(0)) || !isDigitOrPeriodOrSign(val.charAt(val.length() - 1))) throw new NumberFormatException(val); // these screening process is necessary due to the wobble of Float.valueOf method return Double.parseDouble(val); }
public static BigDecimal _parseDecimal(CharSequence content) { content = WhiteSpaceProcessor.trim(content); return new BigDecimal(content.toString()); // from purely XML Schema perspective, // this implementation has a problem, since // in xs:decimal "1.0" and "1" is equal whereas the above // code will return different values for those two forms. // // the code was originally using com.sun.msv.datatype.xsd.NumberType.load, // but a profiling showed that the process of normalizing "1.0" into "1" // could take non-trivial time. // // also, from the user's point of view, one might be surprised if // 1 (not 1.0) is returned from "1.000" }
/** * Faster but less robust String->int conversion. * * <p>Note that: * * <ol> * <li>XML Schema allows '+', but {@link Integer#valueOf(String)} is not. * <li>XML Schema allows leading and trailing (but not in-between) whitespaces.. {@link * Integer#valueOf(String)} doesn't allow any. * </ol> */ public static int _parseInt(CharSequence s) { int len = s.length(); int sign = 1; int r = 0; for (int i = 0; i < len; i++) { char ch = s.charAt(i); if (WhiteSpaceProcessor.isWhiteSpace(ch)) { // skip whitespace } else if ('0' <= ch && ch <= '9') { r = r * 10 + (ch - '0'); } else if (ch == '-') { sign = -1; } else if (ch == '+') { // noop } else throw new NumberFormatException("Not a number: " + s); } return r * sign; }
public static BigInteger _parseInteger(CharSequence s) { return new BigInteger(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString()); }
public static GregorianCalendar _parseDateTime(CharSequence s) { String val = WhiteSpaceProcessor.trim(s).toString(); return datatypeFactory.newXMLGregorianCalendar(val).toGregorianCalendar(); }
public static long _parseLong(CharSequence s) { return Long.valueOf(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString()); }