1 11 package com.hp.hpl.jena.datatypes.xsd.impl; 12 13 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; 14 import com.hp.hpl.jena.graph.impl.LiteralLabel; 15 16 23 public class XSDAbstractDateTimeType extends XSDDatatype { 24 25 28 public XSDAbstractDateTimeType(String typename) { 29 super(typename); 30 } 31 32 37 public boolean isEqual(LiteralLabel value1, LiteralLabel value2) { 38 return value1.getValue().equals(value2.getValue()); 39 } 40 41 42 public static final short YEAR_MASK = 0x1; 43 44 45 public static final short MONTH_MASK = 0x2; 46 47 48 public static final short DAY_MASK = 0x4; 49 50 51 public static final short TIME_MASK = 0x8; 52 53 54 public static final short FULL_MASK = 0xf; 55 56 57 63 protected final static int CY = 0, M = 1, D = 2, h = 3, 65 m = 4, s = 5, ms = 6, utc=7, hh=0, mm=1; 66 67 protected final static int TOTAL_SIZE = 8; 70 71 protected final static int YEAR=2000; 74 protected final static int MONTH=01; 75 protected final static int DAY = 15; 76 77 78 86 protected void getTime (String buffer, int start, int end, int[] data, int[] timeZone) throws RuntimeException { 87 88 int stop = start+2; 89 90 data[h]=parseInt(buffer, start,stop); 92 93 95 if (buffer.charAt(stop++)!=':') { 96 throw new RuntimeException ("Error in parsing time zone" ); 97 } 98 start = stop; 99 stop = stop+2; 100 data[m]=parseInt(buffer, start,stop); 101 102 if (buffer.charAt(stop++)!=':') { 104 throw new RuntimeException ("Error in parsing time zone" ); 105 } 106 start = stop; 107 stop = stop+2; 108 data[s]=parseInt(buffer, start,stop); 109 110 if (stop == end) 111 return; 112 113 start = stop; 115 int milisec = buffer.charAt(start) == '.' ? start : -1; 116 117 int sign = findUTCSign(buffer, start, end); 119 120 if ( milisec != -1 ) { 122 start = sign < 0 ? end : sign; 125 data[ms]=parseInt(buffer, milisec+1, start); 126 } 127 128 if ( sign>0 ) { 130 if (start != sign) 131 throw new RuntimeException ("Error in parsing time zone" ); 132 getTimeZone(buffer, data, sign, end, timeZone); 133 } 134 else if (start != end) { 135 throw new RuntimeException ("Error in parsing time zone" ); 136 } 137 } 138 139 147 protected int getDate (String buffer, int start, int end, int[] date) throws RuntimeException { 148 149 start = getYearMonth(buffer, start, end, date); 150 151 if (buffer.charAt(start++) !='-') { 152 throw new RuntimeException ("CCYY-MM must be followed by '-' sign"); 153 } 154 int stop = start + 2; 155 date[D]=parseInt(buffer, start, stop); 156 return stop; 157 } 158 159 167 protected int getYearMonth (String buffer, int start, int end, int[] date) throws RuntimeException { 168 169 if ( buffer.charAt(0)=='-' ) { 170 start++; 174 } 175 int i = indexOf(buffer, start, end, '-'); 176 if ( i==-1 ) throw new RuntimeException ("Year separator is missing or misplaced"); 177 int length = i-start; 178 if (length<4) { 179 throw new RuntimeException ("Year must have 'CCYY' format"); 180 } 181 else if (length > 4 && buffer.charAt(start)=='0'){ 182 throw new RuntimeException ("Leading zeros are required if the year value would otherwise have fewer than four digits; otherwise they are forbidden"); 183 } 184 date[CY]= parseIntYear(buffer, i); 185 if (buffer.charAt(i)!='-') { 186 throw new RuntimeException ("CCYY must be followed by '-' sign"); 187 } 188 start = ++i; 189 i = start +2; 190 date[M]=parseInt(buffer, start, i); 191 return i; } 193 194 202 protected void parseTimeZone (String buffer, int start, int end, int[] date, int[] timeZone) throws RuntimeException { 203 204 206 if ( start<end ) { 207 int sign = findUTCSign(buffer, start, end); 208 if ( sign<0 ) { 209 throw new RuntimeException ("Error in month parsing"); 210 } 211 else { 212 getTimeZone(buffer, date, sign, end, timeZone); 213 } 214 } 215 } 216 217 224 protected void getTimeZone (String buffer, int[] data, int sign, int end, int[] timeZone) throws RuntimeException { 225 data[utc]=buffer.charAt(sign); 226 227 if ( buffer.charAt(sign) == 'Z' ) { 228 if (end>(++sign)) { 229 throw new RuntimeException ("Error in parsing time zone"); 230 } 231 return; 232 } 233 if ( sign<=(end-6) ) { 234 235 int stop = ++sign+2; 237 timeZone[hh]=parseInt(buffer, sign, stop); 238 if (buffer.charAt(stop++)!=':') { 239 throw new RuntimeException ("Error in parsing time zone" ); 240 } 241 242 timeZone[mm]=parseInt(buffer, stop, stop+2); 244 245 if ( stop+2!=end ) { 246 throw new RuntimeException ("Error in parsing time zone"); 247 } 248 249 } 250 else { 251 throw new RuntimeException ("Error in parsing time zone"); 252 } 253 } 254 255 263 protected int indexOf (String buffer, int start, int end, char ch) { 264 for ( int i=start;i<end;i++ ) { 265 if ( buffer.charAt(i) == ch ) { 266 return i; 267 } 268 } 269 return -1; 270 } 271 272 public static final boolean isDigit(char ch) { 274 return ch >= '0' && ch <= '9'; 275 } 276 277 public static final int getDigit(char ch) { 280 return isDigit(ch) ? ch - '0' : -1; 281 } 282 283 284 291 protected int findUTCSign (String buffer, int start, int end) { 292 int c; 293 for ( int i=start;i<end;i++ ) { 294 c=buffer.charAt(i); 295 if ( c == 'Z' || c=='+' || c=='-' ) { 296 return i; 297 } 298 299 } 300 return -1; 301 } 302 303 311 protected int parseInt (String buffer, int start, int end) 312 throws NumberFormatException { 313 int radix=10; 315 int result = 0; 316 int digit=0; 317 int limit = -Integer.MAX_VALUE; 318 int multmin = limit / radix; 319 int i = start; 320 do { 321 digit = getDigit(buffer.charAt(i)); 322 if ( digit < 0 ) throw new NumberFormatException ("'"+buffer.toString()+"' has wrong format"); 323 if ( result < multmin ) throw new NumberFormatException ("'"+buffer.toString()+"' has wrong format"); 324 result *= radix; 325 if ( result < limit + digit ) throw new NumberFormatException ("'"+buffer.toString()+"' has wrong format"); 326 result -= digit; 327 328 }while ( ++i < end ); 329 return -result; 330 } 331 332 protected int parseIntYear (String buffer, int end){ 334 int radix=10; 335 int result = 0; 336 boolean negative = false; 337 int i=0; 338 int limit; 339 int multmin; 340 int digit=0; 341 342 if (buffer.charAt(0) == '-'){ 343 negative = true; 344 limit = Integer.MIN_VALUE; 345 i++; 346 347 } 348 else{ 349 limit = -Integer.MAX_VALUE; 350 } 351 multmin = limit / radix; 352 while (i < end) 353 { 354 digit = getDigit(buffer.charAt(i++)); 355 if (digit < 0) throw new NumberFormatException ("'"+buffer.toString()+"' has wrong format"); 356 if (result < multmin) throw new NumberFormatException ("'"+buffer.toString()+"' has wrong format"); 357 result *= radix; 358 if (result < limit + digit) throw new NumberFormatException ("'"+buffer.toString()+"' has wrong format"); 359 result -= digit; 360 } 361 362 if (negative) 363 { 364 if (i > 1) return result; 365 else throw new NumberFormatException ("'"+buffer.toString()+"' has wrong format"); 366 } 367 return -result; 368 369 } 370 371 public String dateToString(int[] date) { 372 StringBuffer message = new StringBuffer (25); 373 append(message, date[CY], 4); 374 message.append('-'); 375 append(message, date[M], 2); 376 message.append('-'); 377 append(message, date[D], 2); 378 message.append('T'); 379 append(message, date[h], 2); 380 message.append(':'); 381 append(message, date[m], 2); 382 message.append(':'); 383 append(message, date[s], 2); 384 message.append('.'); 385 message.append(date[ms]); 386 append(message, (char)date[utc], 0); 387 return message.toString(); 388 } 389 390 protected void append(StringBuffer message, int value, int nch) { 391 if (value < 0) { 392 message.append('-'); 393 value = -value; 394 } 395 if (nch == 4) { 396 if (value < 10) 397 message.append("000"); 398 else if (value < 100) 399 message.append("00"); 400 else if (value < 1000) 401 message.append("0"); 402 message.append(value); 403 } 404 else if (nch == 2) { 405 if (value < 10) 406 message.append('0'); 407 message.append(value); 408 } 409 else { 410 if (value != 0) 411 message.append((char)value); 412 } 413 } 414 415 416 420 } 421 422 423 424 453 | Popular Tags |