📄 abstractdatetimedv.java
字号:
/** * Given start and end position, parses string value * * @param buffer string to parse * @param start start position * @param end end position * @return return integer representation of characters */ protected int parseInt (String buffer, int start, int end) throws NumberFormatException{ //REVISIT: more testing on this parsing needs to be done. int radix=10; int result = 0; int digit=0; int limit = -Integer.MAX_VALUE; int multmin = limit / radix; int i = start; do { digit = getDigit(buffer.charAt(i)); if ( digit < 0 ) throw new NumberFormatException("'" + buffer + "' has wrong format"); if ( result < multmin ) throw new NumberFormatException("'" + buffer + "' has wrong format"); result *= radix; if ( result < limit + digit ) throw new NumberFormatException("'" + buffer + "' has wrong format"); result -= digit; }while ( ++i < end ); return -result; } // parse Year differently to support negative value. protected int parseIntYear (String buffer, int end){ int radix=10; int result = 0; boolean negative = false; int i=0; int limit; int multmin; int digit=0; if (buffer.charAt(0) == '-'){ negative = true; limit = Integer.MIN_VALUE; i++; } else{ limit = -Integer.MAX_VALUE; } multmin = limit / radix; while (i < end) { digit = getDigit(buffer.charAt(i++)); if (digit < 0) throw new NumberFormatException("'" + buffer + "' has wrong format"); if (result < multmin) throw new NumberFormatException("'" + buffer + "' has wrong format"); result *= radix; if (result < limit + digit) throw new NumberFormatException("'" + buffer + "' has wrong format"); result -= digit; } if (negative) { if (i > 1) return result; else throw new NumberFormatException("'" + buffer + "' has wrong format"); } return -result; } /** * If timezone present - normalize dateTime [E Adding durations to dateTimes] * * @param date CCYY-MM-DDThh:mm:ss+03 * @return CCYY-MM-DDThh:mm:ssZ */ protected void normalize(DateTimeData date) { // REVISIT: we have common code in addDuration() for durations // should consider reorganizing it. // //add minutes (from time zone) int negate = -1; if ( DEBUG ) { System.out.println("==>date.minute"+date.minute); System.out.println("==>date.timezoneMin" +date.timezoneMin); } int temp = date.minute + negate * date.timezoneMin; int carry = fQuotient (temp, 60); date.minute= mod(temp, 60, carry); if ( DEBUG ) { System.out.println("==>carry: " + carry); } //add hours temp = date.hour + negate * date.timezoneHr + carry; carry = fQuotient(temp, 24); date.hour=mod(temp, 24, carry); if ( DEBUG ) { System.out.println("==>date.hour"+date.hour); System.out.println("==>carry: " + carry); } date.day=date.day+carry; while ( true ) { temp=maxDayInMonthFor(date.year, date.month); if (date.day<1) { date.day = date.day + maxDayInMonthFor(date.year, date.month-1); carry=-1; } else if ( date.day>temp ) { date.day=date.day-temp; carry=1; } else { break; } temp=date.month+carry; date.month=modulo(temp, 1, 13); date.year=date.year+fQuotient(temp, 1, 13); if(date.year == 0 && !Constants.SCHEMA_1_1_SUPPORT) { date.year = (date.timezoneHr < 0 || date.timezoneMin < 0)?1:-1; } } date.utc='Z'; } /** * @param date */ protected void saveUnnormalized(DateTimeData date) { date.unNormYear = date.year; date.unNormMonth = date.month; date.unNormDay = date.day; date.unNormHour = date.hour; date.unNormMinute = date.minute; date.unNormSecond = date.second; } /** * Resets object representation of date/time * * @param data date/time object */ protected void resetDateObj(DateTimeData data) { data.year = 0; data.month = 0; data.day = 0; data.hour = 0; data.minute = 0; data.second = 0; data.utc = 0; data.timezoneHr = 0; data.timezoneMin = 0; } /** * Given {year,month} computes maximum * number of days for given month * * @param year * @param month * @return integer containg the number of days in a given month */ protected int maxDayInMonthFor(int year, int month) { //validate days if ( month==4 || month==6 || month==9 || month==11 ) { return 30; } else if ( month==2 ) { if ( isLeapYear(year) ) { return 29; } else { return 28; } } else { return 31; } } private boolean isLeapYear(int year) { //REVISIT: should we take care about Julian calendar? return((year%4 == 0) && ((year%100 != 0) || (year%400 == 0))); } // // help function described in W3C PR Schema [E Adding durations to dateTimes] // protected int mod (int a, int b, int quotient) { //modulo(a, b) = a - fQuotient(a,b)*b return (a - quotient*b) ; } // // help function described in W3C PR Schema [E Adding durations to dateTimes] // protected int fQuotient (int a, int b) { //fQuotient(a, b) = the greatest integer less than or equal to a/b return (int)Math.floor((float)a/b); } // // help function described in W3C PR Schema [E Adding durations to dateTimes] // protected int modulo (int temp, int low, int high) { //modulo(a - low, high - low) + low int a = temp - low; int b = high - low; return (mod (a, b, fQuotient(a, b)) + low) ; } // // help function described in W3C PR Schema [E Adding durations to dateTimes] // protected int fQuotient (int temp, int low, int high) { //fQuotient(a - low, high - low) return fQuotient(temp - low, high - low); } protected String dateToString(DateTimeData date) { StringBuffer message = new StringBuffer(25); append(message, date.year, 4); message.append('-'); append(message, date.month, 2); message.append('-'); append(message, date.day, 2); message.append('T'); append(message, date.hour, 2); message.append(':'); append(message, date.minute, 2); message.append(':'); append(message, date.second); append(message, (char)date.utc, 0); return message.toString(); } protected void append(StringBuffer message, int value, int nch) { if (value == Integer.MIN_VALUE) { message.append(value); return; } if (value < 0) { message.append('-'); value = -value; } if (nch == 4) { if (value < 10) message.append("000"); else if (value < 100) message.append("00"); else if (value < 1000) message.append("0"); message.append(value); } else if (nch == 2) { if (value < 10) message.append('0'); message.append(value); } else { if (value != 0) message.append((char)value); } } protected void append(StringBuffer message, double value) { if (value < 0) { message.append('-'); value = -value; } if (value < 10) message.append('0'); message.append(value); } protected double parseSecond(String buffer, int start, int end) throws NumberFormatException { int dot = -1; for (int i = start; i < end; i++) { char ch = buffer.charAt(i); if (ch == '.') dot = i; else if (ch > '9' || ch < '0') throw new NumberFormatException("'" + buffer + "' has wrong format"); } if (dot == -1) { if (start+2 != end) throw new NumberFormatException("'" + buffer + "' has wrong format"); } else if (start+2 != dot || dot+1 == end) { throw new NumberFormatException("'" + buffer + "' has wrong format"); } return Double.parseDouble(buffer.substring(start, end)); } // //Private help functions // private void cloneDate (DateTimeData finalValue, DateTimeData tempDate) { tempDate.year = finalValue.year; tempDate.month = finalValue.month; tempDate.day = finalValue.day; tempDate.hour = finalValue.hour; tempDate.minute = finalValue.minute; tempDate.second = finalValue.second; tempDate.utc = finalValue.utc; tempDate.timezoneHr = finalValue.timezoneHr; tempDate.timezoneMin = finalValue.timezoneMin; } /** * Represents date time data */ static final class DateTimeData implements XSDateTime { int year, month, day, hour, minute, utc; double second; int timezoneHr, timezoneMin; private String originalValue; boolean normalized = true; int unNormYear; int unNormMonth; int unNormDay; int unNormHour; int unNormMinute; double unNormSecond; // used for comparisons - to decide the 'interesting' portions of // a date/time based data type. int position; // a pointer to the type that was used go generate this data // note that this is not the actual simple type, but one of the // statically created XXXDV objects, so this won't cause any GC problem. final AbstractDateTimeDV type; private String canonical; public DateTimeData(String originalValue, AbstractDateTimeDV type) { this.originalValue = originalValue; this.type = type; } public DateTimeData(int year, int month, int day, int hour, int minute, double second, int utc, String originalValue, boolean normalized, AbstractDateTimeDV type) { this.year = year; this.month = month; this.day = day; this.hour = hour; this.minute = minute; this.second = second; this.utc = utc; this.type = type; this.originalValue = originalValue; } public boolean equals(Object obj) { if (!(obj instanceof DateTimeData)) return false; return type.compareDates(this, (DateTimeData)obj, true)==0; } public synchronized String toString() { if (canonical == null) { canonical = type.dateToString(this); } return canonical; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getYear() */ public int getYears() { if(type instanceof DurationDV) return 0; return normalized?year:unNormYear; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getMonth() */ public int getMonths() { if(type instanceof DurationDV) { return year*12 + month; } return normalized?month:unNormMonth; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getDay() */ public int getDays() { if(type instanceof DurationDV) return 0; return normalized?day:unNormDay; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getHour() */ public int getHours() { if(type instanceof DurationDV) return 0; return normalized?hour:unNormHour; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getMinutes() */ public int getMinutes() { if(type instanceof DurationDV) return 0; return normalized?minute:unNormMinute; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getSeconds() */ public double getSeconds() { if(type instanceof DurationDV) { return day*24*60*60 + hour*60*60 + minute*60 + second; } return normalized?second:unNormSecond; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#hasTimeZone() */ public boolean hasTimeZone() { return utc != 0; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getTimeZoneHours() */ public int getTimeZoneHours() { return timezoneHr; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getTimeZoneMinutes() */ public int getTimeZoneMinutes() { return timezoneMin; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getLexicalValue() */ public String getLexicalValue() { return originalValue; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#normalize() */ public XSDateTime normalize() { if(!normalized) { DateTimeData dt = (DateTimeData)this.clone(); dt.normalized = true; return dt; } return this; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#isNormalized() */ public boolean isNormalized() { return normalized; } public Object clone() { DateTimeData dt = new DateTimeData(this.year, this.month, this.day, this.hour, this.minute, this.second, this.utc, this.originalValue, this.normalized, this.type); dt.canonical = this.canonical; dt.position = position; dt.timezoneHr = this.timezoneHr; dt.timezoneMin = this.timezoneMin; dt.unNormYear = this.unNormYear; dt.unNormMonth = this.unNormMonth; dt.unNormDay = this.unNormDay; dt.unNormHour = this.unNormHour; dt.unNormMinute = this.unNormMinute; dt.unNormSecond = this.unNormSecond; return dt; } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getXMLGregorianCalendar() */ public XMLGregorianCalendar getXMLGregorianCalendar() { return type.getXMLGregorianCalendar(this); } /* (non-Javadoc) * @see com.sun.org.apache.xerces.internal.xs.datatypes.XSDateTime#getDuration() */ public Duration getDuration() { return type.getDuration(this); } } protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData data) { return null; } protected Duration getDuration(DateTimeData data) { return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -