1 | /*
|
---|
2 | * Copyright 2002-2017 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.tiff;
|
---|
22 |
|
---|
23 | import com.drew.imaging.tiff.TiffHandler;
|
---|
24 | import com.drew.lang.Rational;
|
---|
25 | import com.drew.lang.annotations.NotNull;
|
---|
26 | import com.drew.metadata.Directory;
|
---|
27 | import com.drew.metadata.ErrorDirectory;
|
---|
28 | import com.drew.metadata.Metadata;
|
---|
29 | import com.drew.metadata.StringValue;
|
---|
30 |
|
---|
31 | import java.util.Stack;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Adapter between the {@link TiffHandler} interface and the {@link Metadata}/{@link Directory} object model.
|
---|
35 | *
|
---|
36 | * @author Drew Noakes https://drewnoakes.com
|
---|
37 | */
|
---|
38 | public abstract class DirectoryTiffHandler implements TiffHandler
|
---|
39 | {
|
---|
40 | private final Stack<Directory> _directoryStack = new Stack<Directory>();
|
---|
41 |
|
---|
42 | protected Directory _currentDirectory;
|
---|
43 | protected final Metadata _metadata;
|
---|
44 |
|
---|
45 | protected DirectoryTiffHandler(Metadata metadata)
|
---|
46 | {
|
---|
47 | _metadata = metadata;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void endingIFD()
|
---|
51 | {
|
---|
52 | _currentDirectory = _directoryStack.empty() ? null : _directoryStack.pop();
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected void pushDirectory(@NotNull Class<? extends Directory> directoryClass)
|
---|
56 | {
|
---|
57 | Directory newDirectory = null;
|
---|
58 |
|
---|
59 | try {
|
---|
60 | newDirectory = directoryClass.newInstance();
|
---|
61 | } catch (InstantiationException e) {
|
---|
62 | throw new RuntimeException(e);
|
---|
63 | } catch (IllegalAccessException e) {
|
---|
64 | throw new RuntimeException(e);
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (newDirectory != null)
|
---|
68 | {
|
---|
69 | // If this is the first directory, don't add to the stack
|
---|
70 | if (_currentDirectory != null)
|
---|
71 | {
|
---|
72 | _directoryStack.push(_currentDirectory);
|
---|
73 | newDirectory.setParent(_currentDirectory);
|
---|
74 | }
|
---|
75 | _currentDirectory = newDirectory;
|
---|
76 | _metadata.addDirectory(_currentDirectory);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void warn(@NotNull String message)
|
---|
81 | {
|
---|
82 | getCurrentOrErrorDirectory().addError(message);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void error(@NotNull String message)
|
---|
86 | {
|
---|
87 | getCurrentOrErrorDirectory().addError(message);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @NotNull
|
---|
91 | private Directory getCurrentOrErrorDirectory()
|
---|
92 | {
|
---|
93 | if (_currentDirectory != null)
|
---|
94 | return _currentDirectory;
|
---|
95 | ErrorDirectory error = _metadata.getFirstDirectoryOfType(ErrorDirectory.class);
|
---|
96 | if (error != null)
|
---|
97 | return error;
|
---|
98 | pushDirectory(ErrorDirectory.class);
|
---|
99 | return _currentDirectory;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void setByteArray(int tagId, @NotNull byte[] bytes)
|
---|
103 | {
|
---|
104 | _currentDirectory.setByteArray(tagId, bytes);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void setString(int tagId, @NotNull StringValue string)
|
---|
108 | {
|
---|
109 | _currentDirectory.setStringValue(tagId, string);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void setRational(int tagId, @NotNull Rational rational)
|
---|
113 | {
|
---|
114 | _currentDirectory.setRational(tagId, rational);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public void setRationalArray(int tagId, @NotNull Rational[] array)
|
---|
118 | {
|
---|
119 | _currentDirectory.setRationalArray(tagId, array);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void setFloat(int tagId, float float32)
|
---|
123 | {
|
---|
124 | _currentDirectory.setFloat(tagId, float32);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void setFloatArray(int tagId, @NotNull float[] array)
|
---|
128 | {
|
---|
129 | _currentDirectory.setFloatArray(tagId, array);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void setDouble(int tagId, double double64)
|
---|
133 | {
|
---|
134 | _currentDirectory.setDouble(tagId, double64);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void setDoubleArray(int tagId, @NotNull double[] array)
|
---|
138 | {
|
---|
139 | _currentDirectory.setDoubleArray(tagId, array);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void setInt8s(int tagId, byte int8s)
|
---|
143 | {
|
---|
144 | // NOTE Directory stores all integral types as int32s, except for int32u and long
|
---|
145 | _currentDirectory.setInt(tagId, int8s);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void setInt8sArray(int tagId, @NotNull byte[] array)
|
---|
149 | {
|
---|
150 | // NOTE Directory stores all integral types as int32s, except for int32u and long
|
---|
151 | _currentDirectory.setByteArray(tagId, array);
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void setInt8u(int tagId, short int8u)
|
---|
155 | {
|
---|
156 | // NOTE Directory stores all integral types as int32s, except for int32u and long
|
---|
157 | _currentDirectory.setInt(tagId, int8u);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void setInt8uArray(int tagId, @NotNull short[] array)
|
---|
161 | {
|
---|
162 | // TODO create and use a proper setter for short[]
|
---|
163 | _currentDirectory.setObjectArray(tagId, array);
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void setInt16s(int tagId, int int16s)
|
---|
167 | {
|
---|
168 | // TODO create and use a proper setter for int16u?
|
---|
169 | _currentDirectory.setInt(tagId, int16s);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void setInt16sArray(int tagId, @NotNull short[] array)
|
---|
173 | {
|
---|
174 | // TODO create and use a proper setter for short[]
|
---|
175 | _currentDirectory.setObjectArray(tagId, array);
|
---|
176 | }
|
---|
177 |
|
---|
178 | public void setInt16u(int tagId, int int16u)
|
---|
179 | {
|
---|
180 | // TODO create and use a proper setter for
|
---|
181 | _currentDirectory.setInt(tagId, int16u);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public void setInt16uArray(int tagId, @NotNull int[] array)
|
---|
185 | {
|
---|
186 | // TODO create and use a proper setter for short[]
|
---|
187 | _currentDirectory.setObjectArray(tagId, array);
|
---|
188 | }
|
---|
189 |
|
---|
190 | public void setInt32s(int tagId, int int32s)
|
---|
191 | {
|
---|
192 | _currentDirectory.setInt(tagId, int32s);
|
---|
193 | }
|
---|
194 |
|
---|
195 | public void setInt32sArray(int tagId, @NotNull int[] array)
|
---|
196 | {
|
---|
197 | _currentDirectory.setIntArray(tagId, array);
|
---|
198 | }
|
---|
199 |
|
---|
200 | public void setInt32u(int tagId, long int32u)
|
---|
201 | {
|
---|
202 | _currentDirectory.setLong(tagId, int32u);
|
---|
203 | }
|
---|
204 |
|
---|
205 | public void setInt32uArray(int tagId, @NotNull long[] array)
|
---|
206 | {
|
---|
207 | // TODO create and use a proper setter for short[]
|
---|
208 | _currentDirectory.setObjectArray(tagId, array);
|
---|
209 | }
|
---|
210 | }
|
---|