| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package com.adobe.internal.xmp.impl; |
| 11 |
|
| 12 |
import java.text.DecimalFormat; |
| 13 |
import java.text.DecimalFormatSymbols; |
| 14 |
import java.util.Locale; |
| 15 |
import java.util.SimpleTimeZone; |
| 16 |
|
| 17 |
import com.adobe.internal.xmp.XMPDateTime; |
| 18 |
import com.adobe.internal.xmp.XMPError; |
| 19 |
import com.adobe.internal.xmp.XMPException; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public final class ISO8601Converter |
| 30 |
{ |
| 31 |
|
| 32 |
private ISO8601Converter() |
| 33 |
{ |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static XMPDateTime parse(String iso8601String) throws XMPException |
| 79 |
{ |
| 80 |
return parse(iso8601String, new XMPDateTimeImpl()); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static XMPDateTime parse(String iso8601String, XMPDateTime binValue) throws XMPException |
| 91 |
{ |
| 92 |
if (iso8601String == null) |
| 93 |
{ |
| 94 |
throw new XMPException("Parameter must not be null", XMPError.BADPARAM); |
| 95 |
} |
| 96 |
else if (iso8601String.length() == 0) |
| 97 |
{ |
| 98 |
return binValue; |
| 99 |
} |
| 100 |
|
| 101 |
ParseState input = new ParseState(iso8601String); |
| 102 |
int value; |
| 103 |
|
| 104 |
if (input.ch(0) == '-') |
| 105 |
{ |
| 106 |
input.skip(); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
value = input.gatherInt("Invalid year in date string", 9999); |
| 111 |
if (input.hasNext() && input.ch() != '-') |
| 112 |
{ |
| 113 |
throw new XMPException("Invalid date string, after year", XMPError.BADVALUE); |
| 114 |
} |
| 115 |
|
| 116 |
if (input.ch(0) == '-') |
| 117 |
{ |
| 118 |
value = -value; |
| 119 |
} |
| 120 |
binValue.setYear(value); |
| 121 |
if (!input.hasNext()) |
| 122 |
{ |
| 123 |
return binValue; |
| 124 |
} |
| 125 |
input.skip(); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
value = input.gatherInt("Invalid month in date string", 12); |
| 130 |
if (input.hasNext() && input.ch() != '-') |
| 131 |
{ |
| 132 |
throw new XMPException("Invalid date string, after month", XMPError.BADVALUE); |
| 133 |
} |
| 134 |
binValue.setMonth(value); |
| 135 |
if (!input.hasNext()) |
| 136 |
{ |
| 137 |
return binValue; |
| 138 |
} |
| 139 |
input.skip(); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
value = input.gatherInt("Invalid day in date string", 31); |
| 144 |
if (input.hasNext() && input.ch() != 'T') |
| 145 |
{ |
| 146 |
throw new XMPException("Invalid date string, after day", XMPError.BADVALUE); |
| 147 |
} |
| 148 |
binValue.setDay(value); |
| 149 |
if (!input.hasNext()) |
| 150 |
{ |
| 151 |
return binValue; |
| 152 |
} |
| 153 |
input.skip(); |
| 154 |
|
| 155 |
|
| 156 |
value = input.gatherInt("Invalid hour in date string", 23); |
| 157 |
binValue.setHour(value); |
| 158 |
if (!input.hasNext()) |
| 159 |
{ |
| 160 |
return binValue; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
if (input.ch() == ':') |
| 165 |
{ |
| 166 |
input.skip(); |
| 167 |
value = input.gatherInt("Invalid minute in date string", 59); |
| 168 |
if (input.hasNext() && |
| 169 |
input.ch() != ':' && input.ch() != 'Z' && input.ch() != '+' && input.ch() != '-') |
| 170 |
{ |
| 171 |
throw new XMPException("Invalid date string, after minute", XMPError.BADVALUE); |
| 172 |
} |
| 173 |
binValue.setMinute(value); |
| 174 |
} |
| 175 |
|
| 176 |
if (!input.hasNext()) |
| 177 |
{ |
| 178 |
return binValue; |
| 179 |
} |
| 180 |
else if (input.hasNext() && input.ch() == ':') |
| 181 |
{ |
| 182 |
input.skip(); |
| 183 |
value = input.gatherInt("Invalid whole seconds in date string", 59); |
| 184 |
if (input.hasNext() && input.ch() != '.' && input.ch() != 'Z' && |
| 185 |
input.ch() != '+' && input.ch() != '-') |
| 186 |
{ |
| 187 |
throw new XMPException("Invalid date string, after whole seconds", |
| 188 |
XMPError.BADVALUE); |
| 189 |
} |
| 190 |
binValue.setSecond(value); |
| 191 |
if (input.ch() == '.') |
| 192 |
{ |
| 193 |
input.skip(); |
| 194 |
int digits = input.pos(); |
| 195 |
value = input.gatherInt("Invalid fractional seconds in date string", 999999999); |
| 196 |
if (input.hasNext() && |
| 197 |
(input.ch() != 'Z' && input.ch() != '+' && input.ch() != '-')) |
| 198 |
{ |
| 199 |
throw new XMPException("Invalid date string, after fractional second", |
| 200 |
XMPError.BADVALUE); |
| 201 |
} |
| 202 |
digits = input.pos() - digits; |
| 203 |
for (; digits > 9; --digits) |
| 204 |
{ |
| 205 |
value = value / 10; |
| 206 |
} |
| 207 |
for (; digits < 9; ++digits) |
| 208 |
{ |
| 209 |
value = value * 10; |
| 210 |
} |
| 211 |
binValue.setNanoSecond(value); |
| 212 |
} |
| 213 |
} |
| 214 |
else if (input.ch() != 'Z' && input.ch() != '+' && input.ch() != '-') |
| 215 |
{ |
| 216 |
throw new XMPException("Invalid date string, after time", XMPError.BADVALUE); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
int tzSign = 0; |
| 221 |
int tzHour = 0; |
| 222 |
int tzMinute = 0; |
| 223 |
|
| 224 |
if (!input.hasNext()) |
| 225 |
{ |
| 226 |
|
| 227 |
return binValue; |
| 228 |
} |
| 229 |
else if (input.ch() == 'Z') |
| 230 |
{ |
| 231 |
input.skip(); |
| 232 |
} |
| 233 |
else if (input.hasNext()) |
| 234 |
{ |
| 235 |
if (input.ch() == '+') |
| 236 |
{ |
| 237 |
tzSign = 1; |
| 238 |
} |
| 239 |
else if (input.ch() == '-') |
| 240 |
{ |
| 241 |
tzSign = -1; |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
throw new XMPException("Time zone must begin with 'Z', '+', or '-'", |
| 246 |
XMPError.BADVALUE); |
| 247 |
} |
| 248 |
|
| 249 |
input.skip(); |
| 250 |
|
| 251 |
tzHour = input.gatherInt("Invalid time zone hour in date string", 23); |
| 252 |
if (input.hasNext()) |
| 253 |
{ |
| 254 |
if (input.ch() == ':') |
| 255 |
{ |
| 256 |
input.skip(); |
| 257 |
|
| 258 |
|
| 259 |
tzMinute = input.gatherInt("Invalid time zone minute in date string", 59); |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
throw new XMPException("Invalid date string, after time zone hour", |
| 264 |
XMPError.BADVALUE); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
int offset = (tzHour * 3600 * 1000 + tzMinute * 60 * 1000) * tzSign; |
| 272 |
binValue.setTimeZone(new SimpleTimeZone(offset, "")); |
| 273 |
|
| 274 |
if (input.hasNext()) |
| 275 |
{ |
| 276 |
throw new XMPException( |
| 277 |
"Invalid date string, extra chars at end", XMPError.BADVALUE); |
| 278 |
} |
| 279 |
|
| 280 |
return binValue; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public static String render(XMPDateTime dateTime) |
| 317 |
{ |
| 318 |
StringBuffer buffer = new StringBuffer(); |
| 319 |
|
| 320 |
if (dateTime.hasDate()) |
| 321 |
{ |
| 322 |
|
| 323 |
DecimalFormat df = new DecimalFormat("0000", new DecimalFormatSymbols(Locale.ENGLISH)); |
| 324 |
buffer.append(df.format(dateTime.getYear())); |
| 325 |
if (dateTime.getMonth() == 0) |
| 326 |
{ |
| 327 |
return buffer.toString(); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
df.applyPattern("'-'00"); |
| 332 |
buffer.append(df.format(dateTime.getMonth())); |
| 333 |
if (dateTime.getDay() == 0) |
| 334 |
{ |
| 335 |
return buffer.toString(); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
buffer.append(df.format(dateTime.getDay())); |
| 340 |
|
| 341 |
|
| 342 |
if (dateTime.hasTime()) |
| 343 |
{ |
| 344 |
|
| 345 |
buffer.append('T'); |
| 346 |
df.applyPattern("00"); |
| 347 |
buffer.append(df.format(dateTime.getHour())); |
| 348 |
buffer.append(':'); |
| 349 |
buffer.append(df.format(dateTime.getMinute())); |
| 350 |
|
| 351 |
|
| 352 |
if (dateTime.getSecond() != 0 || dateTime.getNanoSecond() != 0) |
| 353 |
{ |
| 354 |
double seconds = dateTime.getSecond() + dateTime.getNanoSecond() / 1e9d; |
| 355 |
|
| 356 |
df.applyPattern(":00.#########"); |
| 357 |
buffer.append(df.format(seconds)); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
if (dateTime.hasTimeZone()) |
| 362 |
{ |
| 363 |
|
| 364 |
long timeInMillis = dateTime.getCalendar().getTimeInMillis(); |
| 365 |
int offset = dateTime.getTimeZone().getOffset(timeInMillis); |
| 366 |
if (offset == 0) |
| 367 |
{ |
| 368 |
|
| 369 |
buffer.append('Z'); |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
int thours = offset / 3600000; |
| 374 |
int tminutes = Math.abs(offset % 3600000 / 60000); |
| 375 |
df.applyPattern("+00;-00"); |
| 376 |
buffer.append(df.format(thours)); |
| 377 |
df.applyPattern(":00"); |
| 378 |
buffer.append(df.format(tminutes)); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
return buffer.toString(); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
class ParseState |
| 396 |
{ |
| 397 |
|
| 398 |
private String str; |
| 399 |
|
| 400 |
private int pos = 0; |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public ParseState(String str) |
| 407 |
{ |
| 408 |
this.str = str; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public int length() |
| 416 |
{ |
| 417 |
return str.length(); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public boolean hasNext() |
| 425 |
{ |
| 426 |
return pos < str.length(); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public char ch(int index) |
| 435 |
{ |
| 436 |
return index < str.length() ? |
| 437 |
str.charAt(index) : |
| 438 |
0x0000; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public char ch() |
| 446 |
{ |
| 447 |
return pos < str.length() ? |
| 448 |
str.charAt(pos) : |
| 449 |
0x0000; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public void skip() |
| 457 |
{ |
| 458 |
pos++; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public int pos() |
| 466 |
{ |
| 467 |
return pos; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
public int gatherInt(String errorMsg, int maxValue) throws XMPException |
| 479 |
{ |
| 480 |
int value = 0; |
| 481 |
boolean success = false; |
| 482 |
char ch = ch(pos); |
| 483 |
while ('0' <= ch && ch <= '9') |
| 484 |
{ |
| 485 |
value = (value * 10) + (ch - '0'); |
| 486 |
success = true; |
| 487 |
pos++; |
| 488 |
ch = ch(pos); |
| 489 |
} |
| 490 |
|
| 491 |
if (success) |
| 492 |
{ |
| 493 |
if (value > maxValue) |
| 494 |
{ |
| 495 |
return maxValue; |
| 496 |
} |
| 497 |
else if (value < 0) |
| 498 |
{ |
| 499 |
return 0; |
| 500 |
} |
| 501 |
else |
| 502 |
{ |
| 503 |
return value; |
| 504 |
} |
| 505 |
} |
| 506 |
else |
| 507 |
{ |
| 508 |
throw new XMPException(errorMsg, XMPError.BADVALUE); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|