Changeset 13061 in josm for trunk/src/com/drew/metadata/Directory.java
- Timestamp:
- 2017-10-30T22:46:09+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/metadata/Directory.java
r10862 r13061 1 1 /* 2 * Copyright 2002-201 6Drew Noakes2 * Copyright 2002-2017 Drew Noakes 3 3 * 4 4 * Licensed under the Apache License, Version 2.0 (the "License"); … … 24 24 import com.drew.lang.annotations.NotNull; 25 25 import com.drew.lang.annotations.Nullable; 26 import com.drew.lang.annotations.SuppressWarnings; 26 27 27 28 import java.io.UnsupportedEncodingException; … … 41 42 * @author Drew Noakes https://drewnoakes.com 42 43 */ 44 @java.lang.SuppressWarnings("WeakerAccess") 43 45 public abstract class Directory 44 46 { 45 private static final DecimalFormat _floatFormat = new DecimalFormat("0.###");47 private static final String _floatFormatPattern = "0.###"; 46 48 47 49 /** Map of values hashed by type identifiers. */ … … 260 262 261 263 /** 264 * Sets a <code>StringValue</code> value for the specified tag. 265 * 266 * @param tagType the tag's value as an int 267 * @param value the value for the specified tag as a StringValue 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 /** 262 278 * Sets a <code>String</code> value for the specified tag. 263 279 * … … 280 296 */ 281 297 public void setStringArray(int tagType, @NotNull String[] strings) 298 { 299 setObjectArray(tagType, strings); 300 } 301 302 /** 303 * Sets a <code>StringValue[]</code> (array) for the specified tag. 304 * 305 * @param tagType the tag identifier 306 * @param strings the StringValue array to store 307 */ 308 public void setStringValueArray(int tagType, @NotNull StringValue[] strings) 282 309 { 283 310 setObjectArray(tagType, strings); … … 440 467 if (o instanceof Number) { 441 468 return ((Number)o).intValue(); 442 } else if (o instanceof String ) {469 } else if (o instanceof String || o instanceof StringValue) { 443 470 try { 444 return Integer.parseInt( (String)o);471 return Integer.parseInt(o.toString()); 445 472 } catch (NumberFormatException nfe) { 446 473 // convert the char array to an int 447 String s = (String)o;474 String s = o.toString(); 448 475 byte[] bytes = s.getBytes(); 449 476 long val = 0; … … 466 493 if (ints.length == 1) 467 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]; 468 499 } 469 500 return null; … … 472 503 /** 473 504 * Gets the specified tag's value as a String array, if possible. Only supported 474 * where the tag is set as String [], String, int[], byte[] or Rational[].505 * where the tag is set as StringValue[], String[], StringValue, String, int[], byte[] or Rational[]. 475 506 * 476 507 * @param tagType the tag identifier … … 487 518 if (o instanceof String) 488 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 } 489 529 if (o instanceof int[]) { 490 530 int[] ints = (int[])o; … … 493 533 strings[i] = Integer.toString(ints[i]); 494 534 return strings; 495 } else if (o instanceof byte[]) { 535 } 536 if (o instanceof byte[]) { 496 537 byte[] bytes = (byte[])o; 497 538 String[] strings = new String[bytes.length]; … … 499 540 strings[i] = Byte.toString(bytes[i]); 500 541 return strings; 501 } else if (o instanceof Rational[]) { 542 } 543 if (o instanceof Rational[]) { 502 544 Rational[] rationals = (Rational[])o; 503 545 String[] strings = new String[rationals.length]; … … 506 548 return strings; 507 549 } 550 return null; 551 } 552 553 /** 554 * Gets the specified tag's value as a StringValue array, if possible. 555 * Only succeeds if the tag is set as StringValue[], or StringValue. 556 * 557 * @param tagType the tag identifier 558 * @return the tag's value as an array of StringValues. If the value is unset or cannot be converted, <code>null</code> is returned. 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}; 508 570 return null; 509 571 } … … 575 637 if (o == null) { 576 638 return null; 639 } else if (o instanceof StringValue) { 640 return ((StringValue)o).getBytes(); 577 641 } else if (o instanceof Rational[]) { 578 642 Rational[] rationals = (Rational[])o; … … 630 694 if (o == null) 631 695 return null; 632 if (o instanceof String ) {696 if (o instanceof String || o instanceof StringValue) { 633 697 try { 634 return Double.parseDouble( (String)o);698 return Double.parseDouble(o.toString()); 635 699 } catch (NumberFormatException nfe) { 636 700 return null; … … 662 726 if (o == null) 663 727 return null; 664 if (o instanceof String ) {728 if (o instanceof String || o instanceof StringValue) { 665 729 try { 666 return Float.parseFloat( (String)o);730 return Float.parseFloat(o.toString()); 667 731 } catch (NumberFormatException nfe) { 668 732 return null; … … 678 742 { 679 743 Long value = getLongObject(tagType); 680 if (value !=null)744 if (value != null) 681 745 return value; 682 746 Object o = getObject(tagType); … … 693 757 if (o == null) 694 758 return null; 695 if (o instanceof String ) {759 if (o instanceof String || o instanceof StringValue) { 696 760 try { 697 return Long.parseLong( (String)o);761 return Long.parseLong(o.toString()); 698 762 } catch (NumberFormatException nfe) { 699 763 return null; … … 709 773 { 710 774 Boolean value = getBooleanObject(tagType); 711 if (value !=null)775 if (value != null) 712 776 return value; 713 777 Object o = getObject(tagType); … … 719 783 /** Returns the specified tag's value as a boolean. If the tag is not set or cannot be converted, <code>null</code> is returned. */ 720 784 @Nullable 785 @SuppressWarnings(value = "NP_BOOLEAN_RETURN_NULL", justification = "keep API interface consistent") 721 786 public Boolean getBooleanObject(int tagType) 722 787 { … … 726 791 if (o instanceof Boolean) 727 792 return (Boolean)o; 728 if (o instanceof String ) {793 if (o instanceof String || o instanceof StringValue) { 729 794 try { 730 return Boolean.getBoolean( (String)o);795 return Boolean.getBoolean(o.toString()); 731 796 } catch (NumberFormatException nfe) { 732 797 return null; … … 788 853 java.util.Date date = null; 789 854 790 if ( o instanceof String) {855 if ((o instanceof String) || (o instanceof StringValue)) { 791 856 // This seems to cover all known Exif and Xmp date strings 792 857 // Note that " : : : : " is a valid date string according to the Exif spec (which means 'unknown date'): http://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif/datetimeoriginal.html … … 802 867 "yyyy-MM-dd", 803 868 "yyyy-MM", 869 "yyyyMMdd", // as used in IPTC data 804 870 "yyyy" }; 805 String dateString = (String)o; 871 872 String dateString = o.toString(); 806 873 807 874 // if the date string has subsecond information, it supersedes the subsecond parameter … … 945 1012 if (i != 0) 946 1013 string.append(' '); 947 string.append( _floatFormat.format(Array.getFloat(o, i)));1014 string.append(new DecimalFormat(_floatFormatPattern).format(Array.getFloat(o, i))); 948 1015 } 949 1016 } else if (componentType.getName().equals("double")) { … … 951 1018 if (i != 0) 952 1019 string.append(' '); 953 string.append( _floatFormat.format(Array.getDouble(o, i)));1020 string.append(new DecimalFormat(_floatFormatPattern).format(Array.getDouble(o, i))); 954 1021 } 955 1022 } else if (componentType.getName().equals("byte")) { … … 967 1034 968 1035 if (o instanceof Double) 969 return _floatFormat.format(((Double)o).doubleValue());1036 return new DecimalFormat(_floatFormatPattern).format(((Double)o).doubleValue()); 970 1037 971 1038 if (o instanceof Float) 972 return _floatFormat.format(((Float)o).floatValue());1039 return new DecimalFormat(_floatFormatPattern).format(((Float)o).floatValue()); 973 1040 974 1041 // Note that several cameras leave trailing spaces (Olympus, Nikon) but this library is intended to show … … 990 1057 return null; 991 1058 } 1059 } 1060 1061 @Nullable 1062 public StringValue getStringValue(int tagType) 1063 { 1064 Object o = getObject(tagType); 1065 if (o instanceof StringValue) 1066 return (StringValue)o; 1067 return null; 992 1068 } 993 1069
Note:
See TracChangeset
for help on using the changeset viewer.