| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata; |
| 22 |
|
| 23 |
import com.drew.lang.Rational; |
| 24 |
import com.drew.lang.annotations.NotNull; |
| 25 |
import com.drew.lang.annotations.Nullable; |
| 26 |
import com.drew.lang.annotations.SuppressWarnings; |
| 27 |
|
| 28 |
import java.io.UnsupportedEncodingException; |
| 29 |
import java.lang.reflect.Array; |
| 30 |
import java.text.DateFormat; |
| 31 |
import java.text.DecimalFormat; |
| 32 |
import java.text.ParseException; |
| 33 |
import java.text.SimpleDateFormat; |
| 34 |
import java.util.*; |
| 35 |
import java.util.regex.Matcher; |
| 36 |
import java.util.regex.Pattern; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
@java.lang.SuppressWarnings("WeakerAccess") |
| 45 |
public abstract class Directory |
| 46 |
{ |
| 47 |
private static final String _floatFormatPattern = "0.###"; |
| 48 |
|
| 49 |
|
| 50 |
@NotNull |
| 51 |
protected final Map<Integer, Object> _tagMap = new HashMap<Integer, Object>(); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@NotNull |
| 59 |
protected final Collection<Tag> _definedTagList = new ArrayList<Tag>(); |
| 60 |
|
| 61 |
@NotNull |
| 62 |
private final Collection<String> _errorList = new ArrayList<String>(4); |
| 63 |
|
| 64 |
|
| 65 |
protected TagDescriptor<?> _descriptor; |
| 66 |
|
| 67 |
@Nullable |
| 68 |
private Directory _parent; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@NotNull |
| 78 |
public abstract String getName(); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@NotNull |
| 86 |
protected abstract HashMap<Integer, String> getTagNameMap(); |
| 87 |
|
| 88 |
protected Directory() |
| 89 |
{} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public boolean isEmpty() |
| 97 |
{ |
| 98 |
return _errorList.isEmpty() && _definedTagList.isEmpty(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@java.lang.SuppressWarnings({ "UnnecessaryBoxing" }) |
| 108 |
public boolean containsTag(int tagType) |
| 109 |
{ |
| 110 |
return _tagMap.containsKey(Integer.valueOf(tagType)); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
@NotNull |
| 119 |
public Collection<Tag> getTags() |
| 120 |
{ |
| 121 |
return Collections.unmodifiableCollection(_definedTagList); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public int getTagCount() |
| 130 |
{ |
| 131 |
return _definedTagList.size(); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
@java.lang.SuppressWarnings({ "ConstantConditions" }) |
| 140 |
public void setDescriptor(@NotNull TagDescriptor<?> descriptor) |
| 141 |
{ |
| 142 |
if (descriptor == null) |
| 143 |
throw new NullPointerException("cannot set a null descriptor"); |
| 144 |
_descriptor = descriptor; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public void addError(@NotNull String message) |
| 153 |
{ |
| 154 |
_errorList.add(message); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public boolean hasErrors() |
| 163 |
{ |
| 164 |
return _errorList.size() > 0; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
@NotNull |
| 173 |
public Iterable<String> getErrors() |
| 174 |
{ |
| 175 |
return Collections.unmodifiableCollection(_errorList); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
public int getErrorCount() |
| 180 |
{ |
| 181 |
return _errorList.size(); |
| 182 |
} |
| 183 |
|
| 184 |
@Nullable |
| 185 |
public Directory getParent() |
| 186 |
{ |
| 187 |
return _parent; |
| 188 |
} |
| 189 |
|
| 190 |
public void setParent(@NotNull Directory parent) |
| 191 |
{ |
| 192 |
_parent = parent; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public void setInt(int tagType, int value) |
| 204 |
{ |
| 205 |
setObject(tagType, value); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public void setIntArray(int tagType, @NotNull int[] ints) |
| 215 |
{ |
| 216 |
setObjectArray(tagType, ints); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public void setFloat(int tagType, float value) |
| 226 |
{ |
| 227 |
setObject(tagType, value); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public void setFloatArray(int tagType, @NotNull float[] floats) |
| 237 |
{ |
| 238 |
setObjectArray(tagType, floats); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public void setDouble(int tagType, double value) |
| 248 |
{ |
| 249 |
setObject(tagType, value); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public void setDoubleArray(int tagType, @NotNull double[] doubles) |
| 259 |
{ |
| 260 |
setObjectArray(tagType, doubles); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
@java.lang.SuppressWarnings({ "ConstantConditions" }) |
| 270 |
public void setStringValue(int tagType, @NotNull StringValue value) |
| 271 |
{ |
| 272 |
if (value == null) |
| 273 |
throw new NullPointerException("cannot set a null StringValue"); |
| 274 |
setObject(tagType, value); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
@java.lang.SuppressWarnings({ "ConstantConditions" }) |
| 284 |
public void setString(int tagType, @NotNull String value) |
| 285 |
{ |
| 286 |
if (value == null) |
| 287 |
throw new NullPointerException("cannot set a null String"); |
| 288 |
setObject(tagType, value); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public void setStringArray(int tagType, @NotNull String[] strings) |
| 298 |
{ |
| 299 |
setObjectArray(tagType, strings); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public void setStringValueArray(int tagType, @NotNull StringValue[] strings) |
| 309 |
{ |
| 310 |
setObjectArray(tagType, strings); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public void setBoolean(int tagType, boolean value) |
| 320 |
{ |
| 321 |
setObject(tagType, value); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public void setLong(int tagType, long value) |
| 331 |
{ |
| 332 |
setObject(tagType, value); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public void setDate(int tagType, @NotNull java.util.Date value) |
| 342 |
{ |
| 343 |
setObject(tagType, value); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public void setRational(int tagType, @NotNull Rational rational) |
| 353 |
{ |
| 354 |
setObject(tagType, rational); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public void setRationalArray(int tagType, @NotNull Rational[] rationals) |
| 364 |
{ |
| 365 |
setObjectArray(tagType, rationals); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public void setByteArray(int tagType, @NotNull byte[] bytes) |
| 375 |
{ |
| 376 |
setObjectArray(tagType, bytes); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
@java.lang.SuppressWarnings( { "ConstantConditions", "UnnecessaryBoxing" }) |
| 387 |
public void setObject(int tagType, @NotNull Object value) |
| 388 |
{ |
| 389 |
if (value == null) |
| 390 |
throw new NullPointerException("cannot set a null object"); |
| 391 |
|
| 392 |
if (!_tagMap.containsKey(Integer.valueOf(tagType))) { |
| 393 |
_definedTagList.add(new Tag(tagType, this)); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
_tagMap.put(tagType, value); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public void setObjectArray(int tagType, @NotNull Object array) |
| 410 |
{ |
| 411 |
|
| 412 |
setObject(tagType, array); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public int getInt(int tagType) throws MetadataException |
| 433 |
{ |
| 434 |
Integer integer = getInteger(tagType); |
| 435 |
if (integer!=null) |
| 436 |
return integer; |
| 437 |
|
| 438 |
Object o = getObject(tagType); |
| 439 |
if (o == null) |
| 440 |
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first"); |
| 441 |
throw new MetadataException("Tag '" + tagType + "' cannot be converted to int. It is of type '" + o.getClass() + "'."); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
@Nullable |
| 460 |
public Integer getInteger(int tagType) |
| 461 |
{ |
| 462 |
Object o = getObject(tagType); |
| 463 |
|
| 464 |
if (o == null) |
| 465 |
return null; |
| 466 |
|
| 467 |
if (o instanceof Number) { |
| 468 |
return ((Number)o).intValue(); |
| 469 |
} else if (o instanceof String || o instanceof StringValue) { |
| 470 |
try { |
| 471 |
return Integer.parseInt(o.toString()); |
| 472 |
} catch (NumberFormatException nfe) { |
| 473 |
|
| 474 |
String s = o.toString(); |
| 475 |
byte[] bytes = s.getBytes(); |
| 476 |
long val = 0; |
| 477 |
for (byte aByte : bytes) { |
| 478 |
val = val << 8; |
| 479 |
val += (aByte & 0xff); |
| 480 |
} |
| 481 |
return (int)val; |
| 482 |
} |
| 483 |
} else if (o instanceof Rational[]) { |
| 484 |
Rational[] rationals = (Rational[])o; |
| 485 |
if (rationals.length == 1) |
| 486 |
return rationals[0].intValue(); |
| 487 |
} else if (o instanceof byte[]) { |
| 488 |
byte[] bytes = (byte[])o; |
| 489 |
if (bytes.length == 1) |
| 490 |
return (int)bytes[0]; |
| 491 |
} else if (o instanceof int[]) { |
| 492 |
int[] ints = (int[])o; |
| 493 |
if (ints.length == 1) |
| 494 |
return ints[0]; |
| 495 |
} else if (o instanceof short[]) { |
| 496 |
short[] shorts = (short[])o; |
| 497 |
if (shorts.length == 1) |
| 498 |
return (int)shorts[0]; |
| 499 |
} |
| 500 |
return null; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
@Nullable |
| 511 |
public String[] getStringArray(int tagType) |
| 512 |
{ |
| 513 |
Object o = getObject(tagType); |
| 514 |
if (o == null) |
| 515 |
return null; |
| 516 |
if (o instanceof String[]) |
| 517 |
return (String[])o; |
| 518 |
if (o instanceof String) |
| 519 |
return new String[] { (String)o }; |
| 520 |
if (o instanceof StringValue) |
| 521 |
return new String[] { o.toString() }; |
| 522 |
if (o instanceof StringValue[]) { |
| 523 |
StringValue[] stringValues = (StringValue[])o; |
| 524 |
String[] strings = new String[stringValues.length]; |
| 525 |
for (int i = 0; i < strings.length; i++) |
| 526 |
strings[i] = stringValues[i].toString(); |
| 527 |
return strings; |
| 528 |
} |
| 529 |
if (o instanceof int[]) { |
| 530 |
int[] ints = (int[])o; |
| 531 |
String[] strings = new String[ints.length]; |
| 532 |
for (int i = 0; i < strings.length; i++) |
| 533 |
strings[i] = Integer.toString(ints[i]); |
| 534 |
return strings; |
| 535 |
} |
| 536 |
if (o instanceof byte[]) { |
| 537 |
byte[] bytes = (byte[])o; |
| 538 |
String[] strings = new String[bytes.length]; |
| 539 |
for (int i = 0; i < strings.length; i++) |
| 540 |
strings[i] = Byte.toString(bytes[i]); |
| 541 |
return strings; |
| 542 |
} |
| 543 |
if (o instanceof Rational[]) { |
| 544 |
Rational[] rationals = (Rational[])o; |
| 545 |
String[] strings = new String[rationals.length]; |
| 546 |
for (int i = 0; i < strings.length; i++) |
| 547 |
strings[i] = rationals[i].toSimpleString(false); |
| 548 |
return strings; |
| 549 |
} |
| 550 |
return null; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
@Nullable |
| 561 |
public StringValue[] getStringValueArray(int tagType) |
| 562 |
{ |
| 563 |
Object o = getObject(tagType); |
| 564 |
if (o == null) |
| 565 |
return null; |
| 566 |
if (o instanceof StringValue[]) |
| 567 |
return (StringValue[])o; |
| 568 |
if (o instanceof StringValue) |
| 569 |
return new StringValue[] {(StringValue) o}; |
| 570 |
return null; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
@Nullable |
| 581 |
public int[] getIntArray(int tagType) |
| 582 |
{ |
| 583 |
Object o = getObject(tagType); |
| 584 |
if (o == null) |
| 585 |
return null; |
| 586 |
if (o instanceof int[]) |
| 587 |
return (int[])o; |
| 588 |
if (o instanceof Rational[]) { |
| 589 |
Rational[] rationals = (Rational[])o; |
| 590 |
int[] ints = new int[rationals.length]; |
| 591 |
for (int i = 0; i < ints.length; i++) { |
| 592 |
ints[i] = rationals[i].intValue(); |
| 593 |
} |
| 594 |
return ints; |
| 595 |
} |
| 596 |
if (o instanceof short[]) { |
| 597 |
short[] shorts = (short[])o; |
| 598 |
int[] ints = new int[shorts.length]; |
| 599 |
for (int i = 0; i < shorts.length; i++) { |
| 600 |
ints[i] = shorts[i]; |
| 601 |
} |
| 602 |
return ints; |
| 603 |
} |
| 604 |
if (o instanceof byte[]) { |
| 605 |
byte[] bytes = (byte[])o; |
| 606 |
int[] ints = new int[bytes.length]; |
| 607 |
for (int i = 0; i < bytes.length; i++) { |
| 608 |
ints[i] = bytes[i]; |
| 609 |
} |
| 610 |
return ints; |
| 611 |
} |
| 612 |
if (o instanceof CharSequence) { |
| 613 |
CharSequence str = (CharSequence)o; |
| 614 |
int[] ints = new int[str.length()]; |
| 615 |
for (int i = 0; i < str.length(); i++) { |
| 616 |
ints[i] = str.charAt(i); |
| 617 |
} |
| 618 |
return ints; |
| 619 |
} |
| 620 |
if (o instanceof Integer) |
| 621 |
return new int[] { (Integer)o }; |
| 622 |
|
| 623 |
return null; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
@Nullable |
| 634 |
public byte[] getByteArray(int tagType) |
| 635 |
{ |
| 636 |
Object o = getObject(tagType); |
| 637 |
if (o == null) { |
| 638 |
return null; |
| 639 |
} else if (o instanceof StringValue) { |
| 640 |
return ((StringValue)o).getBytes(); |
| 641 |
} else if (o instanceof Rational[]) { |
| 642 |
Rational[] rationals = (Rational[])o; |
| 643 |
byte[] bytes = new byte[rationals.length]; |
| 644 |
for (int i = 0; i < bytes.length; i++) { |
| 645 |
bytes[i] = rationals[i].byteValue(); |
| 646 |
} |
| 647 |
return bytes; |
| 648 |
} else if (o instanceof byte[]) { |
| 649 |
return (byte[])o; |
| 650 |
} else if (o instanceof int[]) { |
| 651 |
int[] ints = (int[])o; |
| 652 |
byte[] bytes = new byte[ints.length]; |
| 653 |
for (int i = 0; i < ints.length; i++) { |
| 654 |
bytes[i] = (byte)ints[i]; |
| 655 |
} |
| 656 |
return bytes; |
| 657 |
} else if (o instanceof short[]) { |
| 658 |
short[] shorts = (short[])o; |
| 659 |
byte[] bytes = new byte[shorts.length]; |
| 660 |
for (int i = 0; i < shorts.length; i++) { |
| 661 |
bytes[i] = (byte)shorts[i]; |
| 662 |
} |
| 663 |
return bytes; |
| 664 |
} else if (o instanceof CharSequence) { |
| 665 |
CharSequence str = (CharSequence)o; |
| 666 |
byte[] bytes = new byte[str.length()]; |
| 667 |
for (int i = 0; i < str.length(); i++) { |
| 668 |
bytes[i] = (byte)str.charAt(i); |
| 669 |
} |
| 670 |
return bytes; |
| 671 |
} |
| 672 |
if (o instanceof Integer) |
| 673 |
return new byte[] { ((Integer)o).byteValue() }; |
| 674 |
|
| 675 |
return null; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
public double getDouble(int tagType) throws MetadataException |
| 680 |
{ |
| 681 |
Double value = getDoubleObject(tagType); |
| 682 |
if (value!=null) |
| 683 |
return value; |
| 684 |
Object o = getObject(tagType); |
| 685 |
if (o == null) |
| 686 |
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first"); |
| 687 |
throw new MetadataException("Tag '" + tagType + "' cannot be converted to a double. It is of type '" + o.getClass() + "'."); |
| 688 |
} |
| 689 |
|
| 690 |
@Nullable |
| 691 |
public Double getDoubleObject(int tagType) |
| 692 |
{ |
| 693 |
Object o = getObject(tagType); |
| 694 |
if (o == null) |
| 695 |
return null; |
| 696 |
if (o instanceof String || o instanceof StringValue) { |
| 697 |
try { |
| 698 |
return Double.parseDouble(o.toString()); |
| 699 |
} catch (NumberFormatException nfe) { |
| 700 |
return null; |
| 701 |
} |
| 702 |
} |
| 703 |
if (o instanceof Number) |
| 704 |
return ((Number)o).doubleValue(); |
| 705 |
|
| 706 |
return null; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
public float getFloat(int tagType) throws MetadataException |
| 711 |
{ |
| 712 |
Float value = getFloatObject(tagType); |
| 713 |
if (value!=null) |
| 714 |
return value; |
| 715 |
Object o = getObject(tagType); |
| 716 |
if (o == null) |
| 717 |
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first"); |
| 718 |
throw new MetadataException("Tag '" + tagType + "' cannot be converted to a float. It is of type '" + o.getClass() + "'."); |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
@Nullable |
| 723 |
public Float getFloatObject(int tagType) |
| 724 |
{ |
| 725 |
Object o = getObject(tagType); |
| 726 |
if (o == null) |
| 727 |
return null; |
| 728 |
if (o instanceof String || o instanceof StringValue) { |
| 729 |
try { |
| 730 |
return Float.parseFloat(o.toString()); |
| 731 |
} catch (NumberFormatException nfe) { |
| 732 |
return null; |
| 733 |
} |
| 734 |
} |
| 735 |
if (o instanceof Number) |
| 736 |
return ((Number)o).floatValue(); |
| 737 |
return null; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
public long getLong(int tagType) throws MetadataException |
| 742 |
{ |
| 743 |
Long value = getLongObject(tagType); |
| 744 |
if (value != null) |
| 745 |
return value; |
| 746 |
Object o = getObject(tagType); |
| 747 |
if (o == null) |
| 748 |
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first"); |
| 749 |
throw new MetadataException("Tag '" + tagType + "' cannot be converted to a long. It is of type '" + o.getClass() + "'."); |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
@Nullable |
| 754 |
public Long getLongObject(int tagType) |
| 755 |
{ |
| 756 |
Object o = getObject(tagType); |
| 757 |
if (o == null) |
| 758 |
return null; |
| 759 |
if (o instanceof Number) |
| 760 |
return ((Number)o).longValue(); |
| 761 |
if (o instanceof String || o instanceof StringValue) { |
| 762 |
try { |
| 763 |
return Long.parseLong(o.toString()); |
| 764 |
} catch (NumberFormatException nfe) { |
| 765 |
return null; |
| 766 |
} |
| 767 |
} else if (o instanceof Rational[]) { |
| 768 |
Rational[] rationals = (Rational[])o; |
| 769 |
if (rationals.length == 1) |
| 770 |
return rationals[0].longValue(); |
| 771 |
} else if (o instanceof byte[]) { |
| 772 |
byte[] bytes = (byte[])o; |
| 773 |
if (bytes.length == 1) |
| 774 |
return (long)bytes[0]; |
| 775 |
} else if (o instanceof int[]) { |
| 776 |
int[] ints = (int[])o; |
| 777 |
if (ints.length == 1) |
| 778 |
return (long)ints[0]; |
| 779 |
} else if (o instanceof short[]) { |
| 780 |
short[] shorts = (short[])o; |
| 781 |
if (shorts.length == 1) |
| 782 |
return (long)shorts[0]; |
| 783 |
} |
| 784 |
return null; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
public boolean getBoolean(int tagType) throws MetadataException |
| 789 |
{ |
| 790 |
Boolean value = getBooleanObject(tagType); |
| 791 |
if (value != null) |
| 792 |
return value; |
| 793 |
Object o = getObject(tagType); |
| 794 |
if (o == null) |
| 795 |
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first"); |
| 796 |
throw new MetadataException("Tag '" + tagType + "' cannot be converted to a boolean. It is of type '" + o.getClass() + "'."); |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
@Nullable |
| 801 |
@SuppressWarnings(value = "NP_BOOLEAN_RETURN_NULL", justification = "keep API interface consistent") |
| 802 |
public Boolean getBooleanObject(int tagType) |
| 803 |
{ |
| 804 |
Object o = getObject(tagType); |
| 805 |
if (o == null) |
| 806 |
return null; |
| 807 |
if (o instanceof Boolean) |
| 808 |
return (Boolean)o; |
| 809 |
if (o instanceof String || o instanceof StringValue) { |
| 810 |
try { |
| 811 |
return Boolean.getBoolean(o.toString()); |
| 812 |
} catch (NumberFormatException nfe) { |
| 813 |
return null; |
| 814 |
} |
| 815 |
} |
| 816 |
if (o instanceof Number) |
| 817 |
return (((Number)o).doubleValue() != 0); |
| 818 |
return null; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
@Nullable |
| 828 |
public java.util.Date getDate(int tagType) |
| 829 |
{ |
| 830 |
return getDate(tagType, null, null); |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
@Nullable |
| 841 |
public java.util.Date getDate(int tagType, @Nullable TimeZone timeZone) |
| 842 |
{ |
| 843 |
return getDate(tagType, null, timeZone); |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
@Nullable |
| 862 |
public java.util.Date getDate(int tagType, @Nullable String subsecond, @Nullable TimeZone timeZone) |
| 863 |
{ |
| 864 |
Object o = getObject(tagType); |
| 865 |
|
| 866 |
if (o instanceof java.util.Date) |
| 867 |
return (java.util.Date)o; |
| 868 |
|
| 869 |
java.util.Date date = null; |
| 870 |
|
| 871 |
if ((o instanceof String) || (o instanceof StringValue)) { |
| 872 |
|
| 873 |
|
| 874 |
String datePatterns[] = { |
| 875 |
"yyyy:MM:dd HH:mm:ss", |
| 876 |
"yyyy:MM:dd HH:mm", |
| 877 |
"yyyy-MM-dd HH:mm:ss", |
| 878 |
"yyyy-MM-dd HH:mm", |
| 879 |
"yyyy.MM.dd HH:mm:ss", |
| 880 |
"yyyy.MM.dd HH:mm", |
| 881 |
"yyyy-MM-dd'T'HH:mm:ss", |
| 882 |
"yyyy-MM-dd'T'HH:mm", |
| 883 |
"yyyy-MM-dd", |
| 884 |
"yyyy-MM", |
| 885 |
"yyyyMMdd", |
| 886 |
"yyyy" }; |
| 887 |
|
| 888 |
String dateString = o.toString(); |
| 889 |
|
| 890 |
|
| 891 |
Pattern subsecondPattern = Pattern.compile("(\\d\\d:\\d\\d:\\d\\d)(\\.\\d+)"); |
| 892 |
Matcher subsecondMatcher = subsecondPattern.matcher(dateString); |
| 893 |
if (subsecondMatcher.find()) { |
| 894 |
subsecond = subsecondMatcher.group(2).substring(1); |
| 895 |
dateString = subsecondMatcher.replaceAll("$1"); |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
Pattern timeZonePattern = Pattern.compile("(Z|[+-]\\d\\d:\\d\\d|[+-]\\d\\d\\d\\d)$"); |
| 900 |
Matcher timeZoneMatcher = timeZonePattern.matcher(dateString); |
| 901 |
if (timeZoneMatcher.find()) { |
| 902 |
timeZone = TimeZone.getTimeZone("GMT" + timeZoneMatcher.group().replaceAll("Z", "")); |
| 903 |
dateString = timeZoneMatcher.replaceAll(""); |
| 904 |
} |
| 905 |
|
| 906 |
for (String datePattern : datePatterns) { |
| 907 |
try { |
| 908 |
DateFormat parser = new SimpleDateFormat(datePattern); |
| 909 |
if (timeZone != null) |
| 910 |
parser.setTimeZone(timeZone); |
| 911 |
else |
| 912 |
parser.setTimeZone(TimeZone.getTimeZone("GMT")); |
| 913 |
|
| 914 |
date = parser.parse(dateString); |
| 915 |
break; |
| 916 |
} catch (ParseException ex) { |
| 917 |
|
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
if (date == null) |
| 923 |
return null; |
| 924 |
|
| 925 |
if (subsecond == null) |
| 926 |
return date; |
| 927 |
|
| 928 |
try { |
| 929 |
int millisecond = (int) (Double.parseDouble("." + subsecond) * 1000); |
| 930 |
if (millisecond >= 0 && millisecond < 1000) { |
| 931 |
Calendar calendar = Calendar.getInstance(); |
| 932 |
calendar.setTime(date); |
| 933 |
calendar.set(Calendar.MILLISECOND, millisecond); |
| 934 |
return calendar.getTime(); |
| 935 |
} |
| 936 |
return date; |
| 937 |
} catch (NumberFormatException e) { |
| 938 |
return date; |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
@Nullable |
| 944 |
public Rational getRational(int tagType) |
| 945 |
{ |
| 946 |
Object o = getObject(tagType); |
| 947 |
|
| 948 |
if (o == null) |
| 949 |
return null; |
| 950 |
|
| 951 |
if (o instanceof Rational) |
| 952 |
return (Rational)o; |
| 953 |
if (o instanceof Integer) |
| 954 |
return new Rational((Integer)o, 1); |
| 955 |
if (o instanceof Long) |
| 956 |
return new Rational((Long)o, 1); |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
return null; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
@Nullable |
| 965 |
public Rational[] getRationalArray(int tagType) |
| 966 |
{ |
| 967 |
Object o = getObject(tagType); |
| 968 |
if (o == null) |
| 969 |
return null; |
| 970 |
|
| 971 |
if (o instanceof Rational[]) |
| 972 |
return (Rational[])o; |
| 973 |
|
| 974 |
return null; |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
@Nullable |
| 985 |
public String getString(int tagType) |
| 986 |
{ |
| 987 |
Object o = getObject(tagType); |
| 988 |
if (o == null) |
| 989 |
return null; |
| 990 |
|
| 991 |
if (o instanceof Rational) |
| 992 |
return ((Rational)o).toSimpleString(true); |
| 993 |
|
| 994 |
if (o.getClass().isArray()) { |
| 995 |
|
| 996 |
int arrayLength = Array.getLength(o); |
| 997 |
final Class<?> componentType = o.getClass().getComponentType(); |
| 998 |
|
| 999 |
StringBuilder string = new StringBuilder(); |
| 1000 |
|
| 1001 |
if (Object.class.isAssignableFrom(componentType)) { |
| 1002 |
|
| 1003 |
for (int i = 0; i < arrayLength; i++) { |
| 1004 |
if (i != 0) |
| 1005 |
string.append(' '); |
| 1006 |
string.append(Array.get(o, i).toString()); |
| 1007 |
} |
| 1008 |
} else if (componentType.getName().equals("int")) { |
| 1009 |
for (int i = 0; i < arrayLength; i++) { |
| 1010 |
if (i != 0) |
| 1011 |
string.append(' '); |
| 1012 |
string.append(Array.getInt(o, i)); |
| 1013 |
} |
| 1014 |
} else if (componentType.getName().equals("short")) { |
| 1015 |
for (int i = 0; i < arrayLength; i++) { |
| 1016 |
if (i != 0) |
| 1017 |
string.append(' '); |
| 1018 |
string.append(Array.getShort(o, i)); |
| 1019 |
} |
| 1020 |
} else if (componentType.getName().equals("long")) { |
| 1021 |
for (int i = 0; i < arrayLength; i++) { |
| 1022 |
if (i != 0) |
| 1023 |
string.append(' '); |
| 1024 |
string.append(Array.getLong(o, i)); |
| 1025 |
} |
| 1026 |
} else if (componentType.getName().equals("float")) { |
| 1027 |
DecimalFormat format = new DecimalFormat(_floatFormatPattern); |
| 1028 |
for (int i = 0; i < arrayLength; i++) { |
| 1029 |
if (i != 0) |
| 1030 |
string.append(' '); |
| 1031 |
String s = format.format(Array.getFloat(o, i)); |
| 1032 |
string.append(s.equals("-0") ? "0" : s); |
| 1033 |
} |
| 1034 |
} else if (componentType.getName().equals("double")) { |
| 1035 |
DecimalFormat format = new DecimalFormat(_floatFormatPattern); |
| 1036 |
for (int i = 0; i < arrayLength; i++) { |
| 1037 |
if (i != 0) |
| 1038 |
string.append(' '); |
| 1039 |
String s = format.format(Array.getDouble(o, i)); |
| 1040 |
string.append(s.equals("-0") ? "0" : s); |
| 1041 |
} |
| 1042 |
} else if (componentType.getName().equals("byte")) { |
| 1043 |
for (int i = 0; i < arrayLength; i++) { |
| 1044 |
if (i != 0) |
| 1045 |
string.append(' '); |
| 1046 |
string.append(Array.getByte(o, i) & 0xff); |
| 1047 |
} |
| 1048 |
} else { |
| 1049 |
addError("Unexpected array component type: " + componentType.getName()); |
| 1050 |
} |
| 1051 |
|
| 1052 |
return string.toString(); |
| 1053 |
} |
| 1054 |
|
| 1055 |
if (o instanceof Double) |
| 1056 |
return new DecimalFormat(_floatFormatPattern).format(((Double)o).doubleValue()); |
| 1057 |
|
| 1058 |
if (o instanceof Float) |
| 1059 |
return new DecimalFormat(_floatFormatPattern).format(((Float)o).floatValue()); |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
return o.toString(); |
| 1066 |
} |
| 1067 |
|
| 1068 |
@Nullable |
| 1069 |
public String getString(int tagType, String charset) |
| 1070 |
{ |
| 1071 |
byte[] bytes = getByteArray(tagType); |
| 1072 |
if (bytes==null) |
| 1073 |
return null; |
| 1074 |
try { |
| 1075 |
return new String(bytes, charset); |
| 1076 |
} catch (UnsupportedEncodingException e) { |
| 1077 |
return null; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
@Nullable |
| 1082 |
public StringValue getStringValue(int tagType) |
| 1083 |
{ |
| 1084 |
Object o = getObject(tagType); |
| 1085 |
if (o instanceof StringValue) |
| 1086 |
return (StringValue)o; |
| 1087 |
return null; |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
@java.lang.SuppressWarnings({ "UnnecessaryBoxing" }) |
| 1097 |
@Nullable |
| 1098 |
public Object getObject(int tagType) |
| 1099 |
{ |
| 1100 |
return _tagMap.get(Integer.valueOf(tagType)); |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
@NotNull |
| 1112 |
public String getTagName(int tagType) |
| 1113 |
{ |
| 1114 |
HashMap<Integer, String> nameMap = getTagNameMap(); |
| 1115 |
if (!nameMap.containsKey(tagType)) { |
| 1116 |
String hex = Integer.toHexString(tagType); |
| 1117 |
while (hex.length() < 4) { |
| 1118 |
hex = "0" + hex; |
| 1119 |
} |
| 1120 |
return "Unknown tag (0x" + hex + ")"; |
| 1121 |
} |
| 1122 |
return nameMap.get(tagType); |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
public boolean hasTagName(int tagType) |
| 1132 |
{ |
| 1133 |
return getTagNameMap().containsKey(tagType); |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
@Nullable |
| 1144 |
public String getDescription(int tagType) |
| 1145 |
{ |
| 1146 |
assert(_descriptor != null); |
| 1147 |
return _descriptor.getDescription(tagType); |
| 1148 |
} |
| 1149 |
|
| 1150 |
@Override |
| 1151 |
public String toString() |
| 1152 |
{ |
| 1153 |
return String.format("%s Directory (%d %s)", |
| 1154 |
getName(), |
| 1155 |
_tagMap.size(), |
| 1156 |
_tagMap.size() == 1 |
| 1157 |
? "tag" |
| 1158 |
: "tags"); |
| 1159 |
} |
| 1160 |
} |