1 57 58 package com.sun.org.apache.xerces.internal.impl.dv.xs; 59 60 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException; 61 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext; 62 63 70 public class DurationDV extends AbstractDateTimeDV { 71 72 private final static int[][] DATETIMES= { 79 {1696, 9, 1, 0, 0, 0, 0, 'Z'}, 80 {1697, 2, 1, 0, 0, 0, 0, 'Z'}, 81 {1903, 3, 1, 0, 0, 0, 0, 'Z'}, 82 {1903, 7, 1, 0, 0, 0, 0, 'Z'}}; 83 84 public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException{ 85 try{ 86 return new DateTimeData(parse(content), this); 87 } catch (Exception ex) { 88 throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object []{content, "duration"}); 89 } 90 } 91 92 100 protected int[] parse(String str) throws SchemaDateTimeException{ 101 int len = str.length(); 102 int[] date=new int[TOTAL_SIZE]; 103 104 int start = 0; 105 char c=str.charAt(start++); 106 if ( c!='P' && c!='-' ) { 107 throw new SchemaDateTimeException(); 108 } 109 else { 110 date[utc]=(c=='-')?'-':0; 111 if ( c=='-' && str.charAt(start++)!='P' ) { 112 throw new SchemaDateTimeException(); 113 } 114 } 115 116 int negate = 1; 117 if ( date[utc]=='-' ) { 119 negate = -1; 120 121 } 122 boolean designator = false; 124 125 int endDate = indexOf (str, start, len, 'T'); 126 if ( endDate == -1 ) { 127 endDate = len; 128 } 129 int end = indexOf (str, start, endDate, 'Y'); 131 if ( end!=-1 ) { 132 date[CY]=negate * parseInt(str,start,end); 134 start = end+1; 135 designator = true; 136 } 137 138 end = indexOf (str, start, endDate, 'M'); 139 if ( end!=-1 ) { 140 date[M]=negate * parseInt(str,start,end); 142 start = end+1; 143 designator = true; 144 } 145 146 end = indexOf (str, start, endDate, 'D'); 147 if ( end!=-1 ) { 148 date[D]=negate * parseInt(str,start,end); 150 start = end+1; 151 designator = true; 152 } 153 154 if ( len == endDate && start!=len ) { 155 throw new SchemaDateTimeException(); 156 } 157 if ( len !=endDate ) { 158 159 163 end = indexOf (str, ++start, len, 'H'); 164 if ( end!=-1 ) { 165 date[h]=negate * parseInt(str,start,end); 167 start=end+1; 168 designator = true; 169 } 170 171 end = indexOf (str, start, len, 'M'); 172 if ( end!=-1 ) { 173 date[m]=negate * parseInt(str,start,end); 175 start=end+1; 176 designator = true; 177 } 178 179 end = indexOf (str, start, len, 'S'); 180 if ( end!=-1 ) { 181 int mlsec = indexOf (str, start, end, '.'); 183 if ( mlsec >0 ) { 184 date[s] = negate * parseInt (str, start, mlsec); 185 date[ms] = negate * parseInt (str, mlsec+1, end); 186 } 187 else { 188 date[s]=negate * parseInt(str, start,end); 189 } 190 start=end+1; 191 designator = true; 192 } 193 if ( start != len || str.charAt(--start)=='T' ) { 196 throw new SchemaDateTimeException(); 197 } 198 } 199 200 if ( !designator ) { 201 throw new SchemaDateTimeException(); 202 } 203 204 return date; 205 } 206 207 221 protected short compareDates(int[] date1, int[] date2, boolean strict) { 222 223 227 short resultA, resultB= INDETERMINATE; 229 230 resultA = compareOrder (date1, date2); 232 if ( resultA == 0 ) { 233 return 0; 234 } 235 236 int[][] result = new int[2][TOTAL_SIZE]; 237 238 int[] tempA = addDuration (date1, DATETIMES[0], result[0]); 240 int[] tempB = addDuration (date2, DATETIMES[0], result[1]); 241 resultA = compareOrder(tempA, tempB); 242 if ( resultA == INDETERMINATE ) { 243 return INDETERMINATE; 244 } 245 246 tempA = addDuration(date1, DATETIMES[1], result[0]); 247 tempB = addDuration(date2, DATETIMES[1], result[1]); 248 resultB = compareOrder(tempA, tempB); 249 resultA = compareResults(resultA, resultB, strict); 250 if (resultA == INDETERMINATE) { 251 return INDETERMINATE; 252 } 253 254 tempA = addDuration(date1, DATETIMES[2], result[0]); 255 tempB = addDuration(date2, DATETIMES[2], result[1]); 256 resultB = compareOrder(tempA, tempB); 257 resultA = compareResults(resultA, resultB, strict); 258 if (resultA == INDETERMINATE) { 259 return INDETERMINATE; 260 } 261 262 tempA = addDuration(date1, DATETIMES[3], result[0]); 263 tempB = addDuration(date2, DATETIMES[3], result[1]); 264 resultB = compareOrder(tempA, tempB); 265 resultA = compareResults(resultA, resultB, strict); 266 267 return resultA; 268 } 269 270 private short compareResults(short resultA, short resultB, boolean strict){ 271 272 if ( resultB == INDETERMINATE ) { 273 return INDETERMINATE; 274 } 275 else if ( resultA!=resultB && strict ) { 276 return INDETERMINATE; 277 } 278 else if ( resultA!=resultB && !strict ) { 279 if ( resultA!=0 && resultB!=0 ) { 280 return INDETERMINATE; 281 } 282 else { 283 return (resultA!=0)?resultA:resultB; 284 } 285 } 286 return resultA; 287 } 288 289 private int[] addDuration(int[] date, int[] addto, int[] duration) { 290 291 295 resetDateObj(duration); 296 int temp = addto[M] + date[M]; 298 duration[M] = modulo (temp, 1, 13); 299 int carry = fQuotient (temp, 1, 13); 300 301 duration[CY]=addto[CY] + date[CY] + carry; 303 304 temp = addto[s] + date[s]; 306 carry = fQuotient (temp, 60); 307 duration[s] = mod(temp, 60, carry); 308 309 temp = addto[m] +date[m] + carry; 311 carry = fQuotient (temp, 60); 312 duration[m]= mod(temp, 60, carry); 313 314 temp = addto[h] + date[h] + carry; 316 carry = fQuotient(temp, 24); 317 duration[h] = mod(temp, 24, carry); 318 319 320 duration[D]=addto[D] + date[D] + carry; 321 322 while ( true ) { 323 324 temp=maxDayInMonthFor(duration[CY], duration[M]); 325 if ( duration[D] < 1 ) { duration[D] = duration[D] + maxDayInMonthFor(duration[CY], duration[M]-1); 327 carry=-1; 328 } 329 else if ( duration[D] > temp ) { 330 duration[D] = duration[D] - temp; 331 carry=1; 332 } 333 else { 334 break; 335 } 336 temp = duration[M]+carry; 337 duration[M] = modulo(temp, 1, 13); 338 duration[CY] = duration[CY]+fQuotient(temp, 1, 13); 339 } 340 341 duration[utc]='Z'; 342 return duration; 343 } 344 345 protected String dateToString(int[] date) { 346 StringBuffer message = new StringBuffer (30); 347 int negate = 1; 348 if ( date[CY]<0 ) { 349 message.append('-'); 350 negate=-1; 351 } 352 message.append('P'); 353 message.append(negate * date[CY]); 354 message.append('Y'); 355 message.append(negate * date[M]); 356 message.append('M'); 357 message.append(negate * date[D]); 358 message.append('D'); 359 message.append('T'); 360 message.append(negate * date[h]); 361 message.append('H'); 362 message.append(negate * date[m]); 363 message.append('M'); 364 message.append(negate * date[s]); 365 message.append('.'); 366 message.append(negate * date[ms]); 367 message.append('S'); 368 369 return message.toString(); 370 } 371 } 372 | Popular Tags |