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