1 | /*
|
---|
2 | * Copyright 2002-2015 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 | * https://drewnoakes.com/code/exif/
|
---|
19 | * https://github.com/drewnoakes/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 | /**
|
---|
27 | * Models a particular tag within a {@link com.drew.metadata.Directory} and provides methods for obtaining its value.
|
---|
28 | * Immutable.
|
---|
29 | *
|
---|
30 | * @author Drew Noakes https://drewnoakes.com
|
---|
31 | */
|
---|
32 | public class Tag
|
---|
33 | {
|
---|
34 | private final int _tagType;
|
---|
35 | @NotNull
|
---|
36 | private final Directory _directory;
|
---|
37 |
|
---|
38 | public Tag(int tagType, @NotNull Directory directory)
|
---|
39 | {
|
---|
40 | _tagType = tagType;
|
---|
41 | _directory = directory;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Gets the tag type as an int
|
---|
46 | *
|
---|
47 | * @return the tag type as an int
|
---|
48 | */
|
---|
49 | public int getTagType()
|
---|
50 | {
|
---|
51 | return _tagType;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Gets the tag type in hex notation as a String with padded leading
|
---|
56 | * zeroes if necessary (i.e. <code>0x100E</code>).
|
---|
57 | *
|
---|
58 | * @return the tag type as a string in hexadecimal notation
|
---|
59 | */
|
---|
60 | @NotNull
|
---|
61 | public String getTagTypeHex()
|
---|
62 | {
|
---|
63 | String hex = Integer.toHexString(_tagType);
|
---|
64 | while (hex.length() < 4) hex = "0" + hex;
|
---|
65 | return "0x" + hex;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Get a description of the tag's value, considering enumerated values
|
---|
70 | * and units.
|
---|
71 | *
|
---|
72 | * @return a description of the tag's value
|
---|
73 | */
|
---|
74 | @Nullable
|
---|
75 | public String getDescription()
|
---|
76 | {
|
---|
77 | return _directory.getDescription(_tagType);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Get whether this tag has a name.
|
---|
82 | *
|
---|
83 | * If <code>true</code>, it may be accessed via {@link #getTagName}.
|
---|
84 | * If <code>false</code>, {@link #getTagName} will return a string resembling <code>"Unknown tag (0x1234)"</code>.
|
---|
85 | *
|
---|
86 | * @return whether this tag has a name
|
---|
87 | */
|
---|
88 | @NotNull
|
---|
89 | public boolean hasTagName()
|
---|
90 | {
|
---|
91 | return _directory.hasTagName(_tagType);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Get the name of the tag, such as <code>Aperture</code>, or
|
---|
96 | * <code>InteropVersion</code>.
|
---|
97 | *
|
---|
98 | * @return the tag's name
|
---|
99 | */
|
---|
100 | @NotNull
|
---|
101 | public String getTagName()
|
---|
102 | {
|
---|
103 | return _directory.getTagName(_tagType);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Get the name of the {@link com.drew.metadata.Directory} in which the tag exists, such as
|
---|
108 | * <code>Exif</code>, <code>GPS</code> or <code>Interoperability</code>.
|
---|
109 | *
|
---|
110 | * @return name of the {@link com.drew.metadata.Directory} in which this tag exists
|
---|
111 | */
|
---|
112 | @NotNull
|
---|
113 | public String getDirectoryName()
|
---|
114 | {
|
---|
115 | return _directory.getName();
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * A basic representation of the tag's type and value. EG: <code>[FNumber] F2.8</code>.
|
---|
120 | *
|
---|
121 | * @return the tag's type and value
|
---|
122 | */
|
---|
123 | @Override
|
---|
124 | @NotNull
|
---|
125 | public String toString()
|
---|
126 | {
|
---|
127 | String description = getDescription();
|
---|
128 | if (description == null)
|
---|
129 | description = _directory.getString(getTagType()) + " (unable to formulate description)";
|
---|
130 | return "[" + _directory.getName() + "] " + getTagName() + " - " + description;
|
---|
131 | }
|
---|
132 | }
|
---|