1 | /*
|
---|
2 | * Copyright 2002-2012 Drew Noakes
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | * More information about this project is available at:
|
---|
17 | *
|
---|
18 | * http://drewnoakes.com/code/exif/
|
---|
19 | * http://code.google.com/p/metadata-extractor/
|
---|
20 | */
|
---|
21 | package com.drew.metadata;
|
---|
22 |
|
---|
23 | import com.drew.lang.annotations.NotNull;
|
---|
24 | import com.drew.lang.annotations.Nullable;
|
---|
25 |
|
---|
26 | import java.lang.reflect.Array;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Abstract base class for all tag descriptor classes. Implementations are responsible for
|
---|
30 | * providing the human-readable string representation of tag values stored in a directory.
|
---|
31 | * The directory is provided to the tag descriptor via its constructor.
|
---|
32 | *
|
---|
33 | * @author Drew Noakes http://drewnoakes.com
|
---|
34 | */
|
---|
35 | public abstract class TagDescriptor<T extends Directory>
|
---|
36 | {
|
---|
37 | @NotNull
|
---|
38 | protected final T _directory;
|
---|
39 |
|
---|
40 | public TagDescriptor(@NotNull T directory)
|
---|
41 | {
|
---|
42 | _directory = directory;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Returns a descriptive value of the the specified tag for this image.
|
---|
47 | * Where possible, known values will be substituted here in place of the raw
|
---|
48 | * tokens actually kept in the metadata segment. If no substitution is
|
---|
49 | * available, the value provided by <code>getString(tagType)</code> will be returned.
|
---|
50 | *
|
---|
51 | * @param tagType the tag to find a description for
|
---|
52 | * @return a description of the image's value for the specified tag, or
|
---|
53 | * <code>null</code> if the tag hasn't been defined.
|
---|
54 | */
|
---|
55 | @Nullable
|
---|
56 | public String getDescription(int tagType)
|
---|
57 | {
|
---|
58 | Object object = _directory.getObject(tagType);
|
---|
59 |
|
---|
60 | if (object==null)
|
---|
61 | return null;
|
---|
62 |
|
---|
63 | // special presentation for long arrays
|
---|
64 | if (object.getClass().isArray()) {
|
---|
65 | final int length = Array.getLength(object);
|
---|
66 | if (length > 16) {
|
---|
67 | final String componentTypeName = object.getClass().getComponentType().getName();
|
---|
68 | return String.format("[%d %s%s]", length, componentTypeName, length==1 ? "" : "s");
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | // no special handling required, so use default conversion to a string
|
---|
73 | return _directory.getString(tagType);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Takes a series of 4 bytes from the specified offset, and converts these to a
|
---|
78 | * well-known version number, where possible.
|
---|
79 | * <p/>
|
---|
80 | * Two different formats are processed:
|
---|
81 | * <ul>
|
---|
82 | * <li>[30 32 31 30] -> 2.10</li>
|
---|
83 | * <li>[0 1 0 0] -> 1.00</li>
|
---|
84 | * </ul>
|
---|
85 | * @param components the four version values
|
---|
86 | * @param majorDigits the number of components to be
|
---|
87 | * @return the version as a string of form "2.10" or null if the argument cannot be converted
|
---|
88 | */
|
---|
89 | @Nullable
|
---|
90 | public static String convertBytesToVersionString(@Nullable int[] components, final int majorDigits)
|
---|
91 | {
|
---|
92 | if (components==null)
|
---|
93 | return null;
|
---|
94 | StringBuilder version = new StringBuilder();
|
---|
95 | for (int i = 0; i < 4 && i < components.length; i++) {
|
---|
96 | if (i == majorDigits)
|
---|
97 | version.append('.');
|
---|
98 | char c = (char)components[i];
|
---|
99 | if (c < '0')
|
---|
100 | c += '0';
|
---|
101 | if (i == 0 && c=='0')
|
---|
102 | continue;
|
---|
103 | version.append(c);
|
---|
104 | }
|
---|
105 | return version.toString();
|
---|
106 | }
|
---|
107 | }
|
---|