📄 utility.java
字号:
int value = parseInt(text); if (value < Character.MIN_VALUE || value > Character.MAX_VALUE) { throw new JiBXException("Value out of range"); } return (char)value; } /** * Serialize char value to text as unsigned 16-bit integer. * * @param value char value to be serialized * @return text representation of value */ public static String serializeChar(char value) { return Integer.toString(value); } /** * Parse char value from text as character value. This requires that the * string must be of length one. * * @param text text to be parsed * @return converted char value * @throws JiBXException on parse error */ public static char parseCharString(String text) throws JiBXException { if (text.length() == 1) { return text.charAt(0); } else { throw new JiBXException("Input must be a single character"); } } /** * Deserialize char value from text as character value. This requires that * the string must be null or of length one. * * @param text text to be parsed (may be <code>null</code>) * @return converted char value * @throws JiBXException on parse error */ public static char deserializeCharString(String text) throws JiBXException { if (text == null) { return 0; } else { return parseCharString(text); } } /** * Serialize char value to text as string of length one. * * @param value char value to be serialized * @return text representation of value */ public static String serializeCharString(char value) { return String.valueOf(value); } /** * Parse float value from text. This uses the W3C XML Schema format for * floats, with the exception that it will accept "+NaN" and "-NaN" as * valid formats. This is not in strict compliance with the specification, * but is included for interoperability with other Java XML processing. * * @param text text to be parsed * @return converted float value * @throws JiBXException on parse error */ public static float parseFloat(String text) throws JiBXException { text = text.trim(); if ("-INF".equals(text)) { return Float.NEGATIVE_INFINITY; } else if ("INF".equals(text)) { return Float.POSITIVE_INFINITY; } else { try { return Float.parseFloat(text); } catch (NumberFormatException ex) { throw new JiBXException(ex.getMessage()); } } } /** * Serialize float value to text. * * @param value float value to be serialized * @return text representation of value */ public static String serializeFloat(float value) { if (Float.isInfinite(value)) { return (value < 0.0f) ? "-INF" : "INF"; } else { return Float.toString(value); } } /** * Parse double value from text. This uses the W3C XML Schema format for * doubles, with the exception that it will accept "+NaN" and "-NaN" as * valid formats. This is not in strict compliance with the specification, * but is included for interoperability with other Java XML processing. * * @param text text to be parsed * @return converted double value * @throws JiBXException on parse error */ public static double parseDouble(String text) throws JiBXException { text = text.trim(); if ("-INF".equals(text)) { return Double.NEGATIVE_INFINITY; } else if ("INF".equals(text)) { return Double.POSITIVE_INFINITY; } else { try { return Double.parseDouble(text); } catch (NumberFormatException ex) { throw new JiBXException(ex.getMessage()); } } } /** * Serialize double value to text. * * @param value double value to be serialized * @return text representation of value */ public static String serializeDouble(double value) { if (Double.isInfinite(value)) { return (value < 0.0f) ? "-INF" : "INF"; } else { return Double.toString(value); } } /** * Convert gYearMonth text to Java date. Date values are expected to be in * W3C XML Schema standard format as CCYY-MM, with optional * leading sign. * * @param text text to be parsed * @return start of month in year date as millisecond value * @throws JiBXException on parse error */ public static long parseYearMonth(String text) throws JiBXException { // start by validating the length and basic format text = text.trim(); boolean valid = true; int minc = 7; char chr = text.charAt(0); if (chr == '-') { minc = 8; } else if (chr == '+') { valid = false; } int split = text.length() - 3; if (text.length() < minc) { valid = false; } else { if (text.charAt(split) != '-') { valid = false; } } if (!valid) { throw new JiBXException("Invalid date format"); } // handle year and month conversion int year = parseInt(text.substring(0, split)); if (year == 0) { throw new JiBXException("Year value 0 is not allowed"); } int month = parseDigits(text, split+1, 2) - 1; if (month < 0 || month > 11) { throw new JiBXException("Month value out of range"); } boolean leap = (year%4 == 0) && !((year%100 == 0) && (year%400 != 0)); if (year > 0) { year--; } long day = ((long)year)*365 + year/4 - year/100 + year/400 + (leap ? MONTHS_LEAP : MONTHS_NONLEAP)[month]; return day*MSPERDAY - TIME_BASE; } /** * Convert date text to Java date. Date values are expected to be in * W3C XML Schema standard format as CCYY-MM-DD, with optional * leading sign and trailing time zone (though the time zone is ignored * in this case). * * @param text text to be parsed * @return start of day in month and year date as millisecond value * @throws JiBXException on parse error */ public static long parseDate(String text) throws JiBXException { // start by validating the length and basic format int split = validateDate(text); // handle year, month, and day conversion int year = parseInt(text.substring(0, split)); if (year == 0) { throw new JiBXException("Year value 0 is not allowed"); } int month = parseDigits(text, split+1, 2) - 1; if (month < 0 || month > 11) { throw new JiBXException("Month value out of range"); } long day = parseDigits(text, split+4, 2) - 1; boolean leap = (year%4 == 0) && !((year%100 == 0) && (year%400 != 0)); int[] starts = leap ? MONTHS_LEAP : MONTHS_NONLEAP; if (day < 0 || day >= (starts[month+1]-starts[month])) { throw new JiBXException("Day value out of range"); } if (year > 0) { year--; } day += ((long)year)*365 + year/4 - year/100 + year/400 + starts[month]; return day*MSPERDAY - TIME_BASE; } /** * Deserialize date from text. Date values are expected to match W3C XML * Schema standard format as CCYY-MM-DD, with optional leading minus sign * if necessary. This method follows standard JiBX deserializer usage * requirements by accepting a <code>null</code> input. * * @param text text to be parsed (may be <code>null</code>) * @return converted date, or <code>null</code> if passed <code>null</code> * input * @throws JiBXException on parse error */ public static Date deserializeDate(String text) throws JiBXException { if (text == null) { return null; } else { return new Date(parseDate(text)); } } /** * Validate a date text string. * * @param text * @return offset past end of year in text * @throws JiBXException on validation error */ private static int validateDate(String text) throws JiBXException { // start by validating the length and basic format boolean valid = true; int minc = 10; char chr = text.charAt(0); if (chr == '-') { minc = 11; } else if (chr == '+') { valid = false; } int split = text.length() - 6; if (text.length() < minc) { valid = false; } else { if (text.charAt(split) != '-' || text.charAt(split+3) != '-') { valid = false; } } if (!valid) { throw new JiBXException("Invalid date format"); } return split; }//#!j2me{ /** * Deserialize SQL date from text. Date values are expected to match W3C XML * Schema standard format as CCYY-MM-DD, with optional leading minus sign * if necessary. This method follows standard JiBX deserializer usage * requirements by accepting a <code>null</code> input. * * @param text text to be parsed (may be <code>null</code>) * @return converted date, or <code>null</code> if passed <code>null</code> * input * @throws JiBXException on parse error */ public static java.sql.Date deserializeSqlDate(String text) throws JiBXException { if (text == null) { return null; } else { // make sure date is avlid int split = validateDate(text); // handle year, month, and day conversion int year = parseInt(text.substring(0, split)); if (year == 0) { throw new JiBXException("Year value 0 is not allowed"); } int month = parseDigits(text, split+1, 2) - 1; if (month < 0 || month > 11) { throw new JiBXException("Month value out of range"); } int day = parseDigits(text, split+4, 2) - 1; boolean leap = (year%4 == 0) && !((year%100 == 0) && (year%400 != 0)); int[] starts = leap ? MONTHS_LEAP : MONTHS_NONLEAP; if (day < 0 || day >= (starts[month+1]-starts[month])) { throw new JiBXException("Day value out of range"); } if (year < 0) { year++; } // set it into a calendar GregorianCalendar cal; if (year < 1800) { cal = new GregorianCalendar(); cal.setGregorianChange(BEGINNING_OF_TIME); cal.clear(); cal.set(year, month, day+1); } else { cal = new GregorianCalendar(year, month, day+1); } return new java.sql.Date(cal.getTime().getTime()); } }//#j2me} /** * Parse general time value from text. Time values are expected to be in W3C * XML Schema standard format as hh:mm:ss.fff, with optional leading sign * and trailing time zone. * * @param text text to be parsed * @param start offset of first character of time value * @param length number of characters in time value * @return converted time as millisecond value * @throws JiBXException on parse error */ public static long parseTime(String text, int start, int length) throws JiBXException { // validate time value following date long milli = 0; boolean valid = length > (start+7) && (text.charAt(start+2) == ':') && (text.charAt(start+5) == ':'); if (valid) { int hour = parseDigits(text, start, 2); int minute = parseDigits(text, start+3, 2); int second = parseDigits(text, start+6, 2); if (hour > 23 || minute > 59 || second > 60) { valid = false; } else { // convert to base millisecond in day milli = (((hour*60)+minute)*60+second)*1000; start += 8; if (length > start) { // adjust for time zone if (text.charAt(length-1) == 'Z') { length--; } else { char chr = text.charAt(length-6); if (chr == '-' || chr == '+') { hour = parseDigits(text, length-5, 2); minute = parseDigits(text, length-2, 2); if (hour > 23 || minute > 59) { valid = false; } else { int offset = ((hour*60)+minute)*60*1000; if (chr == '-') { milli += offset; } else { milli -= offset; } } length -= 6; } } // check for trailing fractional second if (text.charAt(start) == '.') { double fraction = Double.parseDouble (text.substring(start, length)); milli += fraction*1000.0; } else if (length > start) { valid = false; } } } } // check for valid result if (valid) { return milli; } else { throw new JiBXException("Invalid dateTime format"); } } /** * Parse general dateTime value from text. Date values are expected to be in * W3C XML Schema standard format as CCYY-MM-DDThh:mm:ss.fff, with optional * leading sign and trailing time zone. * * @param text text to be parsed * @return converted date as millisecond value * @throws JiBXException on parse error */ public static long parseDateTime(String text) throws JiBXException { // split text to convert portions separately int split = text.indexOf('T'); if (split < 0) { throw new JiBXException("Missing 'T' separator in dateTime"); } return parseDate(text.substring(0, split)) + parseTime(text, split+1, text.length()); } /** * Deserialize date from general dateTime text. Date values are expected to * match W3C XML Schema standard format as CCYY-MM-DDThh:mm:ss, with * optional leading minus sign and trailing seconds decimal, as necessary. * This method follows standard JiBX deserializer usage requirements by * accepting a <code>null</code> input. * * @param text text to be parsed (may be <code>null</code>) * @return converted date, or <code>null</code> if passed <code>null</code> * input * @throws JiBXException on parse error */ public static Date deserializeDateTime(String text) throws JiBXException { if (text == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -