-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathDirectory.java
1194 lines (1087 loc) · 41.2 KB
/
Directory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2002-2019 Drew Noakes and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* /~https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.metadata;
import com.drew.lang.Rational;
import com.drew.lang.annotations.NotNull;
import com.drew.lang.annotations.Nullable;
import com.drew.lang.annotations.SuppressWarnings;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Abstract base class for all directory implementations, having methods for getting and setting tag values of various
* data types.
*
* @author Drew Noakes https://drewnoakes.com
*/
@java.lang.SuppressWarnings("WeakerAccess")
public abstract class Directory
{
private static final String _floatFormatPattern = "0.###";
/** Map of values hashed by type identifiers. */
@NotNull
protected final Map<Integer, Object> _tagMap = new HashMap<Integer, Object>();
/**
* A convenient list holding tag values in the order in which they were stored.
* This is used for creation of an iterator, and for counting the number of
* defined tags.
*/
@NotNull
protected final Collection<Tag> _definedTagList = new ArrayList<Tag>();
@NotNull
private final Collection<String> _errorList = new ArrayList<String>(4);
/** The descriptor used to interpret tag values. */
protected TagDescriptor<?> _descriptor;
@Nullable
private Directory _parent;
// ABSTRACT METHODS
/**
* Provides the name of the directory, for display purposes. E.g. <code>Exif</code>
*
* @return the name of the directory
*/
@NotNull
public abstract String getName();
/**
* Provides the map of tag names, hashed by tag type identifier.
*
* @return the map of tag names
*/
@NotNull
protected abstract HashMap<Integer, String> getTagNameMap();
protected Directory()
{}
// VARIOUS METHODS
/**
* Gets a value indicating whether the directory is empty, meaning it contains no errors and no tag values.
*/
public boolean isEmpty()
{
return _errorList.isEmpty() && _definedTagList.isEmpty();
}
/**
* Indicates whether the specified tag type has been set.
*
* @param tagType the tag type to check for
* @return true if a value exists for the specified tag type, false if not
*/
@java.lang.SuppressWarnings({ "UnnecessaryBoxing" })
public boolean containsTag(int tagType)
{
return _tagMap.containsKey(Integer.valueOf(tagType));
}
/**
* Returns an Iterator of Tag instances that have been set in this Directory.
*
* @return an Iterator of Tag instances
*/
@NotNull
public Collection<Tag> getTags()
{
return Collections.unmodifiableCollection(_definedTagList);
}
/**
* Returns the number of tags set in this Directory.
*
* @return the number of tags set in this Directory
*/
public int getTagCount()
{
return _definedTagList.size();
}
/**
* Sets the descriptor used to interpret tag values.
*
* @param descriptor the descriptor used to interpret tag values
*/
@java.lang.SuppressWarnings({ "ConstantConditions" })
public void setDescriptor(@NotNull TagDescriptor<?> descriptor)
{
if (descriptor == null)
throw new NullPointerException("cannot set a null descriptor");
_descriptor = descriptor;
}
/**
* Registers an error message with this directory.
*
* @param message an error message.
*/
public void addError(@NotNull String message)
{
_errorList.add(message);
}
/**
* Gets a value indicating whether this directory has any error messages.
*
* @return true if the directory contains errors, otherwise false
*/
public boolean hasErrors()
{
return _errorList.size() > 0;
}
/**
* Used to iterate over any error messages contained in this directory.
*
* @return an iterable collection of error message strings.
*/
@NotNull
public Iterable<String> getErrors()
{
return Collections.unmodifiableCollection(_errorList);
}
/** Returns the count of error messages in this directory. */
public int getErrorCount()
{
return _errorList.size();
}
@Nullable
public Directory getParent()
{
return _parent;
}
public void setParent(@NotNull Directory parent)
{
_parent = parent;
}
// TAG SETTERS
/**
* Sets an <code>int</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag as an int
*/
public void setInt(int tagType, int value)
{
setObject(tagType, value);
}
/**
* Sets an <code>int[]</code> (array) for the specified tag.
*
* @param tagType the tag identifier
* @param ints the int array to store
*/
public void setIntArray(int tagType, @NotNull int[] ints)
{
setObjectArray(tagType, ints);
}
/**
* Sets a <code>float</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag as a float
*/
public void setFloat(int tagType, float value)
{
setObject(tagType, value);
}
/**
* Sets a <code>float[]</code> (array) for the specified tag.
*
* @param tagType the tag identifier
* @param floats the float array to store
*/
public void setFloatArray(int tagType, @NotNull float[] floats)
{
setObjectArray(tagType, floats);
}
/**
* Sets a <code>double</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag as a double
*/
public void setDouble(int tagType, double value)
{
setObject(tagType, value);
}
/**
* Sets a <code>double[]</code> (array) for the specified tag.
*
* @param tagType the tag identifier
* @param doubles the double array to store
*/
public void setDoubleArray(int tagType, @NotNull double[] doubles)
{
setObjectArray(tagType, doubles);
}
/**
* Sets a <code>StringValue</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag as a StringValue
*/
@java.lang.SuppressWarnings({ "ConstantConditions" })
public void setStringValue(int tagType, @NotNull StringValue value)
{
if (value == null)
throw new NullPointerException("cannot set a null StringValue");
setObject(tagType, value);
}
/**
* Sets a <code>String</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag as a String
*/
@java.lang.SuppressWarnings({ "ConstantConditions" })
public void setString(int tagType, @NotNull String value)
{
if (value == null)
throw new NullPointerException("cannot set a null String");
setObject(tagType, value);
}
/**
* Sets a <code>String[]</code> (array) for the specified tag.
*
* @param tagType the tag identifier
* @param strings the String array to store
*/
public void setStringArray(int tagType, @NotNull String[] strings)
{
setObjectArray(tagType, strings);
}
/**
* Sets a <code>StringValue[]</code> (array) for the specified tag.
*
* @param tagType the tag identifier
* @param strings the StringValue array to store
*/
public void setStringValueArray(int tagType, @NotNull StringValue[] strings)
{
setObjectArray(tagType, strings);
}
/**
* Sets a <code>boolean</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag as a boolean
*/
public void setBoolean(int tagType, boolean value)
{
setObject(tagType, value);
}
/**
* Sets a <code>long</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag as a long
*/
public void setLong(int tagType, long value)
{
setObject(tagType, value);
}
/**
* Sets a <code>java.util.Date</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag as a java.util.Date
*/
public void setDate(int tagType, @NotNull java.util.Date value)
{
setObject(tagType, value);
}
/**
* Sets a <code>Rational</code> value for the specified tag.
*
* @param tagType the tag's value as an int
* @param rational rational number
*/
public void setRational(int tagType, @NotNull Rational rational)
{
setObject(tagType, rational);
}
/**
* Sets a <code>Rational[]</code> (array) for the specified tag.
*
* @param tagType the tag identifier
* @param rationals the Rational array to store
*/
public void setRationalArray(int tagType, @NotNull Rational[] rationals)
{
setObjectArray(tagType, rationals);
}
/**
* Sets a <code>byte[]</code> (array) for the specified tag.
*
* @param tagType the tag identifier
* @param bytes the byte array to store
*/
public void setByteArray(int tagType, @NotNull byte[] bytes)
{
setObjectArray(tagType, bytes);
}
/**
* Sets a <code>Object</code> for the specified tag.
*
* @param tagType the tag's value as an int
* @param value the value for the specified tag
* @throws NullPointerException if value is <code>null</code>
*/
@java.lang.SuppressWarnings( { "ConstantConditions", "UnnecessaryBoxing" })
public void setObject(int tagType, @NotNull Object value)
{
if (value == null)
throw new NullPointerException("cannot set a null object");
if (!_tagMap.containsKey(Integer.valueOf(tagType))) {
_definedTagList.add(new Tag(tagType, this));
}
// else {
// final Object oldValue = _tagMap.get(tagType);
// if (!oldValue.equals(value))
// addError(String.format("Overwritten tag 0x%s (%s). Old=%s, New=%s", Integer.toHexString(tagType), getTagName(tagType), oldValue, value));
// }
_tagMap.put(tagType, value);
}
/**
* Sets an array <code>Object</code> for the specified tag.
*
* @param tagType the tag's value as an int
* @param array the array of values for the specified tag
*/
public void setObjectArray(int tagType, @NotNull Object array)
{
// for now, we don't do anything special -- this method might be a candidate for removal once the dust settles
setObject(tagType, array);
}
// TAG GETTERS
/**
* Returns the specified tag's value as an int, if possible. Every attempt to represent the tag's value as an int
* is taken. Here is a list of the action taken depending upon the tag's original type:
* <ul>
* <li> int - Return unchanged.
* <li> Number - Return an int value (real numbers are truncated).
* <li> Rational - Truncate any fractional part and returns remaining int.
* <li> String - Attempt to parse string as an int. If this fails, convert the char[] to an int (using shifts and OR).
* <li> Rational[] - Return int value of first item in array.
* <li> byte[] - Return int value of first item in array.
* <li> int[] - Return int value of first item in array.
* </ul>
*
* @throws MetadataException if no value exists for tagType or if it cannot be converted to an int.
*/
public int getInt(int tagType) throws MetadataException
{
Integer integer = getInteger(tagType);
if (integer!=null)
return integer;
Object o = getObject(tagType);
if (o == null)
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first");
throw new MetadataException("Tag '" + tagType + "' cannot be converted to int. It is of type '" + o.getClass() + "'.");
}
/**
* Returns the specified tag's value as an Integer, if possible. Every attempt to represent the tag's value as an
* Integer is taken. Here is a list of the action taken depending upon the tag's original type:
* <ul>
* <li> int - Return unchanged
* <li> Number - Return an int value (real numbers are truncated)
* <li> Rational - Truncate any fractional part and returns remaining int
* <li> String - Attempt to parse string as an int. If this fails, convert the char[] to an int (using shifts and OR)
* <li> Rational[] - Return int value of first item in array if length > 0
* <li> byte[] - Return int value of first item in array if length > 0
* <li> int[] - Return int value of first item in array if length > 0
* </ul>
*
* If the value is not found or cannot be converted to int, <code>null</code> is returned.
*/
@Nullable
public Integer getInteger(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof Number) {
return ((Number)o).intValue();
} else if (o instanceof String || o instanceof StringValue) {
try {
return Integer.parseInt(o.toString());
} catch (NumberFormatException nfe) {
// convert the char array to an int
String s = o.toString();
byte[] bytes = s.getBytes();
long val = 0;
for (byte aByte : bytes) {
val = val << 8;
val += (aByte & 0xff);
}
return (int)val;
}
} else if (o instanceof Rational[]) {
Rational[] rationals = (Rational[])o;
if (rationals.length == 1)
return rationals[0].intValue();
} else if (o instanceof byte[]) {
byte[] bytes = (byte[])o;
if (bytes.length == 1)
return (int)bytes[0];
} else if (o instanceof int[]) {
int[] ints = (int[])o;
if (ints.length == 1)
return ints[0];
} else if (o instanceof short[]) {
short[] shorts = (short[])o;
if (shorts.length == 1)
return (int)shorts[0];
}
return null;
}
/**
* Gets the specified tag's value as a String array, if possible. Only supported
* where the tag is set as StringValue[], String[], StringValue, String, int[], byte[] or Rational[].
*
* @param tagType the tag identifier
* @return the tag's value as an array of Strings. If the value is unset or cannot be converted, <code>null</code> is returned.
*/
@Nullable
public String[] getStringArray(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof String[])
return (String[])o;
if (o instanceof String)
return new String[] { (String)o };
if (o instanceof StringValue)
return new String[] { o.toString() };
if (o instanceof StringValue[]) {
StringValue[] stringValues = (StringValue[])o;
String[] strings = new String[stringValues.length];
for (int i = 0; i < strings.length; i++)
strings[i] = stringValues[i].toString();
return strings;
}
if (o instanceof int[]) {
int[] ints = (int[])o;
String[] strings = new String[ints.length];
for (int i = 0; i < strings.length; i++)
strings[i] = Integer.toString(ints[i]);
return strings;
}
if (o instanceof byte[]) {
byte[] bytes = (byte[])o;
String[] strings = new String[bytes.length];
for (int i = 0; i < strings.length; i++)
strings[i] = Byte.toString(bytes[i]);
return strings;
}
if (o instanceof Rational[]) {
Rational[] rationals = (Rational[])o;
String[] strings = new String[rationals.length];
for (int i = 0; i < strings.length; i++)
strings[i] = rationals[i].toSimpleString(false);
return strings;
}
return null;
}
/**
* Gets the specified tag's value as a StringValue array, if possible.
* Only succeeds if the tag is set as StringValue[], or StringValue.
*
* @param tagType the tag identifier
* @return the tag's value as an array of StringValues. If the value is unset or cannot be converted, <code>null</code> is returned.
*/
@Nullable
public StringValue[] getStringValueArray(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof StringValue[])
return (StringValue[])o;
if (o instanceof StringValue)
return new StringValue[] {(StringValue) o};
return null;
}
/**
* Gets the specified tag's value as an int array, if possible. Only supported
* where the tag is set as String, Integer, int[], byte[] or Rational[].
*
* @param tagType the tag identifier
* @return the tag's value as an int array
*/
@Nullable
public int[] getIntArray(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof int[])
return (int[])o;
if (o instanceof Rational[]) {
Rational[] rationals = (Rational[])o;
int[] ints = new int[rationals.length];
for (int i = 0; i < ints.length; i++) {
ints[i] = rationals[i].intValue();
}
return ints;
}
if (o instanceof short[]) {
short[] shorts = (short[])o;
int[] ints = new int[shorts.length];
for (int i = 0; i < shorts.length; i++) {
ints[i] = shorts[i];
}
return ints;
}
if (o instanceof byte[]) {
byte[] bytes = (byte[])o;
int[] ints = new int[bytes.length];
for (int i = 0; i < bytes.length; i++) {
ints[i] = bytes[i];
}
return ints;
}
if (o instanceof CharSequence) {
CharSequence str = (CharSequence)o;
int[] ints = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
ints[i] = str.charAt(i);
}
return ints;
}
if (o instanceof Integer)
return new int[] { (Integer)o };
return null;
}
/**
* Gets the specified tag's value as an byte array, if possible. Only supported
* where the tag is set as String, Integer, int[], byte[] or Rational[].
*
* @param tagType the tag identifier
* @return the tag's value as a byte array
*/
@Nullable
public byte[] getByteArray(int tagType)
{
Object o = getObject(tagType);
if (o == null) {
return null;
} else if (o instanceof StringValue) {
return ((StringValue)o).getBytes();
} else if (o instanceof Rational[]) {
Rational[] rationals = (Rational[])o;
byte[] bytes = new byte[rationals.length];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = rationals[i].byteValue();
}
return bytes;
} else if (o instanceof byte[]) {
return (byte[])o;
} else if (o instanceof int[]) {
int[] ints = (int[])o;
byte[] bytes = new byte[ints.length];
for (int i = 0; i < ints.length; i++) {
bytes[i] = (byte)ints[i];
}
return bytes;
} else if (o instanceof short[]) {
short[] shorts = (short[])o;
byte[] bytes = new byte[shorts.length];
for (int i = 0; i < shorts.length; i++) {
bytes[i] = (byte)shorts[i];
}
return bytes;
} else if (o instanceof CharSequence) {
CharSequence str = (CharSequence)o;
byte[] bytes = new byte[str.length()];
for (int i = 0; i < str.length(); i++) {
bytes[i] = (byte)str.charAt(i);
}
return bytes;
}
if (o instanceof Integer)
return new byte[] { ((Integer)o).byteValue() };
return null;
}
/** Returns the specified tag's value as a double, if possible. */
public double getDouble(int tagType) throws MetadataException
{
Double value = getDoubleObject(tagType);
if (value!=null)
return value;
Object o = getObject(tagType);
if (o == null)
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first");
throw new MetadataException("Tag '" + tagType + "' cannot be converted to a double. It is of type '" + o.getClass() + "'.");
}
/** Returns the specified tag's value as a Double. If the tag is not set or cannot be converted, <code>null</code> is returned. */
@Nullable
public Double getDoubleObject(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof String || o instanceof StringValue) {
try {
return Double.parseDouble(o.toString());
} catch (NumberFormatException nfe) {
return null;
}
}
if (o instanceof Number)
return ((Number)o).doubleValue();
return null;
}
/** Returns the specified tag's value as a float, if possible. */
public float getFloat(int tagType) throws MetadataException
{
Float value = getFloatObject(tagType);
if (value!=null)
return value;
Object o = getObject(tagType);
if (o == null)
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first");
throw new MetadataException("Tag '" + tagType + "' cannot be converted to a float. It is of type '" + o.getClass() + "'.");
}
/** Returns the specified tag's value as a float. If the tag is not set or cannot be converted, <code>null</code> is returned. */
@Nullable
public Float getFloatObject(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof String || o instanceof StringValue) {
try {
return Float.parseFloat(o.toString());
} catch (NumberFormatException nfe) {
return null;
}
}
if (o instanceof Number)
return ((Number)o).floatValue();
return null;
}
/** Returns the specified tag's value as a long, if possible. */
public long getLong(int tagType) throws MetadataException
{
Long value = getLongObject(tagType);
if (value != null)
return value;
Object o = getObject(tagType);
if (o == null)
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first");
throw new MetadataException("Tag '" + tagType + "' cannot be converted to a long. It is of type '" + o.getClass() + "'.");
}
/** Returns the specified tag's value as a long. If the tag is not set or cannot be converted, <code>null</code> is returned. */
@Nullable
public Long getLongObject(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof Number)
return ((Number)o).longValue();
if (o instanceof String || o instanceof StringValue) {
try {
return Long.parseLong(o.toString());
} catch (NumberFormatException nfe) {
// convert the char array to an int
String s = o.toString();
byte[] bytes = s.getBytes();
long val = 0;
for (byte aByte : bytes) {
val = val << 8;
val += (aByte & 0xff);
}
return val;
}
} else if (o instanceof Rational[]) {
Rational[] rationals = (Rational[])o;
if (rationals.length == 1)
return rationals[0].longValue();
} else if (o instanceof byte[]) {
byte[] bytes = (byte[])o;
if (bytes.length == 1)
return (long)bytes[0];
} else if (o instanceof int[]) {
int[] ints = (int[])o;
if (ints.length == 1)
return (long)ints[0];
} else if (o instanceof short[]) {
short[] shorts = (short[])o;
if (shorts.length == 1)
return (long)shorts[0];
}
return null;
}
/** Returns the specified tag's value as a boolean, if possible. */
public boolean getBoolean(int tagType) throws MetadataException
{
Boolean value = getBooleanObject(tagType);
if (value != null)
return value;
Object o = getObject(tagType);
if (o == null)
throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first");
throw new MetadataException("Tag '" + tagType + "' cannot be converted to a boolean. It is of type '" + o.getClass() + "'.");
}
/** Returns the specified tag's value as a boolean. If the tag is not set or cannot be converted, <code>null</code> is returned. */
@Nullable
@SuppressWarnings(value = "NP_BOOLEAN_RETURN_NULL", justification = "keep API interface consistent")
public Boolean getBooleanObject(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof Boolean)
return (Boolean)o;
if (o instanceof String || o instanceof StringValue) {
try {
return Boolean.getBoolean(o.toString());
} catch (NumberFormatException nfe) {
return null;
}
}
if (o instanceof Number)
return (((Number)o).doubleValue() != 0);
return null;
}
/**
* Returns the specified tag's value as a java.util.Date. If the value is unset or cannot be converted, <code>null</code> is returned.
* <p>
* If the underlying value is a {@link String}, then attempts will be made to parse the string as though it is in
* the GMT {@link TimeZone}. If the {@link TimeZone} is known, call the overload that accepts one as an argument.
*/
@Nullable
public java.util.Date getDate(int tagType)
{
return getDate(tagType, null, null);
}
/**
* Returns the specified tag's value as a java.util.Date. If the value is unset or cannot be converted, <code>null</code> is returned.
* <p>
* If the underlying value is a {@link String}, then attempts will be made to parse the string as though it is in
* the {@link TimeZone} represented by the {@code timeZone} parameter (if it is non-null). Note that this parameter
* is only considered if the underlying value is a string and it has no time zone information, otherwise it has no effect.
*/
@Nullable
public java.util.Date getDate(int tagType, @Nullable TimeZone timeZone)
{
return getDate(tagType, null, timeZone);
}
/**
* Returns the specified tag's value as a java.util.Date. If the value is unset or cannot be converted, <code>null</code> is returned.
* <p>
* If the underlying value is a {@link String}, then attempts will be made to parse the string as though it is in
* the {@link TimeZone} represented by the {@code timeZone} parameter (if it is non-null). Note that this parameter
* is only considered if the underlying value is a string and it has no time zone information, otherwise it has no effect.
* In addition, the {@code subsecond} parameter, which specifies the number of digits after the decimal point in the seconds,
* is set to the returned Date. This parameter is only considered if the underlying value is a string and is has
* no subsecond information, otherwise it has no effect.
*
* @param tagType the tag identifier
* @param subsecond the subsecond value for the Date
* @param timeZone the time zone to use
* @return a Date representing the time value
*/
@Nullable
public java.util.Date getDate(int tagType, @Nullable String subsecond, @Nullable TimeZone timeZone)
{
Object o = getObject(tagType);
if (o instanceof java.util.Date)
return (java.util.Date)o;
java.util.Date date = null;
if ((o instanceof String) || (o instanceof StringValue)) {
// This seems to cover all known Exif and Xmp date strings
// 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
String datePatterns[] = {
"yyyy:MM:dd HH:mm:ss",
"yyyy:MM:dd HH:mm",
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm",
"yyyy.MM.dd HH:mm:ss",
"yyyy.MM.dd HH:mm",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd'T'HH:mm",
"yyyy-MM-dd",
"yyyy-MM",
"yyyyMMdd", // as used in IPTC data
"yyyy" };
String dateString = o.toString();
/*
* This is a common NULL value known for cameras like Olympus C750UZ.
*/
if (dateString.equals("0000:00:00 00:00:00"))
return null;
// if the date string has subsecond information, it supersedes the subsecond parameter
Pattern subsecondPattern = Pattern.compile("(\\d\\d:\\d\\d:\\d\\d)(\\.\\d+)");
Matcher subsecondMatcher = subsecondPattern.matcher(dateString);
if (subsecondMatcher.find()) {
subsecond = subsecondMatcher.group(2).substring(1);
dateString = subsecondMatcher.replaceAll("$1");
}
// if the date string has time zone information, it supersedes the timeZone parameter
Pattern timeZonePattern = Pattern.compile("(Z|[+-]\\d\\d:\\d\\d|[+-]\\d\\d\\d\\d)$");
Matcher timeZoneMatcher = timeZonePattern.matcher(dateString);
if (timeZoneMatcher.find()) {
timeZone = TimeZone.getTimeZone("GMT" + timeZoneMatcher.group().replaceAll("Z", ""));
dateString = timeZoneMatcher.replaceAll("");
}
for (String datePattern : datePatterns) {
try {
DateFormat parser = new SimpleDateFormat(datePattern);
/*
* Some older digital cameras, such as the Olympus C750UZ, may not have recorded a proper
* exif:DateTimeOriginal value. Instead of leaving this field empty, they used
* 0000:00:00 00:00:00 as a placeholder.
*
* "0000:00:00 00:00:00" will result in "Sun Nov 30 00:00:00 GMT 2",
* which is not what a user would expect.
*
* Any illegal formats should result in an exception and not be parsed.
*
* It's best to turn lenient mode off.
*/
parser.setLenient(false);
/*
* If the metadata has set a time zone we use that, and otherwise
* we assume that computer and image belong to the same geographical area.
*/
if (timeZone != null)
parser.setTimeZone(timeZone);
else
parser.setTimeZone(TimeZone.getDefault());
date = parser.parse(dateString);
break;
} catch (ParseException ex) {
// simply try the next pattern
}
}
}
if (date == null)
return null;
if (subsecond == null)
return date;
try {
int millisecond = (int) (Double.parseDouble("." + subsecond) * 1000);
if (millisecond >= 0 && millisecond < 1000) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MILLISECOND, millisecond);
return calendar.getTime();
}
return date;
} catch (NumberFormatException e) {
return date;
}
}
/** Returns the specified tag's value as a Rational. If the value is unset or cannot be converted, <code>null</code> is returned. */
@Nullable
public Rational getRational(int tagType)
{
Object o = getObject(tagType);
if (o == null)
return null;
if (o instanceof Rational)
return (Rational)o;
if (o instanceof Integer)
return new Rational((Integer)o, 1);
if (o instanceof Long)
return new Rational((Long)o, 1);
// NOTE not doing conversions for real number types
return null;
}
/** Returns the specified tag's value as an array of Rational. If the value is unset or cannot be converted, <code>null</code> is returned. */
@Nullable
public Rational[] getRationalArray(int tagType)
{