blob: 96cebf98d63bfb4481d527307253d3e3dd202b87 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080017
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070018package javax.imageio.metadata;
19
20import java.util.ArrayList;
21
22import org.apache.harmony.x.imageio.metadata.IIOMetadataUtils;
23import org.w3c.dom.Node;
24
25/**
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080026 * The class IIOMetadata represents the metadata (bundled with an image) as a
27 * Dom-type tree.
28 *
29 * @since Android 1.0
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070030 */
31public abstract class IIOMetadata {
32
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080033 /**
34 * Whether the standard metadata format is supported.
35 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070036 protected boolean standardFormatSupported;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080037
38 /**
39 * The native metadata format name.
40 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041 protected String nativeMetadataFormatName;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080042
43 /**
44 * The native metadata format class name.
45 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046 protected String nativeMetadataFormatClassName;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080047
48 /**
49 * The extra metadata format names.
50 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051 protected String[] extraMetadataFormatNames;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080052
53 /**
54 * The extra metadata format class names.
55 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056 protected String[] extraMetadataFormatClassNames;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080057
58 /**
59 * The default controller.
60 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061 protected IIOMetadataController defaultController;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080062
63 /**
64 * The controller.
65 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070066 protected IIOMetadataController controller;
67
68 /**
69 * Instantiates a new IIOMetadata with no data set.
70 */
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080071 protected IIOMetadata() {
72 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070073
74 /**
75 * Instantiates a new IIOMetadata with the specified data parameters.
76 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080077 * @param standardMetadataFormatSupported
78 * whether the standard metadata format is supported.
79 * @param nativeMetadataFormatName
80 * the native metadata format name.
81 * @param nativeMetadataFormatClassName
82 * the native metadata format class name.
83 * @param extraMetadataFormatNames
84 * the extra metadata format names.
85 * @param extraMetadataFormatClassNames
86 * the extra metadata format class names.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070087 */
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080088 protected IIOMetadata(boolean standardMetadataFormatSupported, String nativeMetadataFormatName,
89 String nativeMetadataFormatClassName, String[] extraMetadataFormatNames,
90 String[] extraMetadataFormatClassNames) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070091 standardFormatSupported = standardMetadataFormatSupported;
92 this.nativeMetadataFormatName = nativeMetadataFormatName;
93 this.nativeMetadataFormatClassName = nativeMetadataFormatClassName;
94 if (extraMetadataFormatNames == null) {
95 if (extraMetadataFormatClassNames != null) {
96 throw new IllegalArgumentException(
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080097 "extraMetadataFormatNames == null && extraMetadataFormatClassNames != null!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098 }
99 } else {
100 if (extraMetadataFormatClassNames == null) {
101 throw new IllegalArgumentException(
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800102 "extraMetadataFormatNames != null && extraMetadataFormatClassNames == null!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 }
104 if (extraMetadataFormatNames.length == 0) {
105 throw new IllegalArgumentException("extraMetadataFormatNames.length == 0!");
106 }
107 if (extraMetadataFormatClassNames.length != extraMetadataFormatNames.length) {
108 throw new IllegalArgumentException(
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800109 "extraMetadataFormatClassNames.length != extraMetadataFormatNames.length!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700110 }
111 this.extraMetadataFormatNames = extraMetadataFormatNames.clone();
112 this.extraMetadataFormatClassNames = extraMetadataFormatClassNames.clone();
113 }
114 }
115
116 /**
117 * Gets the metadata as tree-type document.
118 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800119 * @param formatName
120 * the format name.
121 * @return the node in tree format.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700122 */
123 public abstract Node getAsTree(String formatName);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800124
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125 /**
126 * Checks if the metadata is read only.
127 *
128 * @return true, if the metadata is read only.
129 */
130 public abstract boolean isReadOnly();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800131
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132 /**
133 * Merges the specified tree with this metadata tree.
134 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800135 * @param formatName
136 * the format of the specified tree.
137 * @param root
138 * the root node of the metadata tree.
139 * @throws IIOInvalidTreeException
140 * if the specified tree is incompatible with the this metadata
141 * tree.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 */
143 public abstract void mergeTree(String formatName, Node root) throws IIOInvalidTreeException;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800144
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145 /**
146 * Resets the controller.
147 */
148 public abstract void reset();
149
150 /**
151 * Gets the controller associated with this metadata document.
152 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800153 * @return the controller.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154 */
155 public IIOMetadataController getController() {
156 return controller;
157 }
158
159 /**
160 * Checks whether this metadata has a controller.
161 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800162 * @return true, if this metadata has a controller.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163 */
164 public boolean hasController() {
165 return getController() != null;
166 }
167
168 /**
169 * Activate the controller.
170 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800171 * @return true, if successful.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172 */
173 public boolean activateController() {
174 if (!hasController()) {
175 throw new IllegalStateException("hasController() == false!");
176 }
177 return getController().activate(this);
178 }
179
180 /**
181 * Gets the default controller.
182 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800183 * @return the default controller.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184 */
185 public IIOMetadataController getDefaultController() {
186 return defaultController;
187 }
188
189 /**
190 * Gets the extra metadata format names.
191 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800192 * @return the extra metadata format names.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 */
194 public String[] getExtraMetadataFormatNames() {
195 return extraMetadataFormatNames == null ? null : extraMetadataFormatNames.clone();
196 }
197
198 /**
199 * Gets the metadata format.
200 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800201 * @param formatName
202 * the format name.
203 * @return the metadata format.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 */
205 public IIOMetadataFormat getMetadataFormat(String formatName) {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800206 return IIOMetadataUtils.instantiateMetadataFormat(formatName, standardFormatSupported,
207 nativeMetadataFormatName, nativeMetadataFormatClassName, extraMetadataFormatNames,
208 extraMetadataFormatClassNames);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 }
210
211 /**
212 * Gets the native metadata format name.
213 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800214 * @return the native metadata format name.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 */
216 public String getNativeMetadataFormatName() {
217 return nativeMetadataFormatName;
218 }
219
220 /**
221 * Checks if the standard metadata format is supported.
222 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800223 * @return true, if the standard metadata format is supported.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 */
225 public boolean isStandardMetadataFormatSupported() {
226 return standardFormatSupported;
227 }
228
229 /**
230 * Gets the metadata format names.
231 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800232 * @return the metadata format names.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 */
234 public String[] getMetadataFormatNames() {
235 ArrayList<String> res = new ArrayList<String>();
236
237 String nativeMetadataFormatName = getNativeMetadataFormatName();
238 boolean standardFormatSupported = isStandardMetadataFormatSupported();
239 String extraMetadataFormatNames[] = getExtraMetadataFormatNames();
240
241 if (standardFormatSupported) {
242 res.add(IIOMetadataFormatImpl.standardMetadataFormatName);
243 }
244 if (nativeMetadataFormatName != null) {
245 res.add(nativeMetadataFormatName);
246 }
247 if (extraMetadataFormatNames != null) {
248 for (String extraMetadataFormatName : extraMetadataFormatNames) {
249 res.add(extraMetadataFormatName);
250 }
251 }
252
253 return res.size() > 0 ? res.toArray(new String[0]) : null;
254 }
255
256 /**
257 * Gets the standard chroma node.
258 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800259 * @return the standard chroma node.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 */
261 protected IIOMetadataNode getStandardChromaNode() {
262 return null;
263 }
264
265 /**
266 * Gets the standard compression node.
267 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800268 * @return the standard compression node.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 */
270 protected IIOMetadataNode getStandardCompressionNode() {
271 return null;
272 }
273
274 /**
275 * Gets the standard data node.
276 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800277 * @return the standard data node.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 */
279 protected IIOMetadataNode getStandardDataNode() {
280 return null;
281 }
282
283 /**
284 * Gets the standard dimension node.
285 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800286 * @return the standard dimension node.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287 */
288 protected IIOMetadataNode getStandardDimensionNode() {
289 return null;
290 }
291
292 /**
293 * Gets the standard document node.
294 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800295 * @return the standard document node.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296 */
297 protected IIOMetadataNode getStandardDocumentNode() {
298 return null;
299 }
300
301 /**
302 * Gets the standard text node.
303 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800304 * @return the standard text node.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700305 */
306 protected IIOMetadataNode getStandardTextNode() {
307 return null;
308 }
309
310 /**
311 * Gets the standard tile node.
312 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800313 * @return the standard tile node.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 */
315 protected IIOMetadataNode getStandardTileNode() {
316 return null;
317 }
318
319 /**
320 * Gets the standard transparency node.
321 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800322 * @return the standard transparency node.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 */
324 protected IIOMetadataNode getStandardTransparencyNode() {
325 return null;
326 }
327
328 /**
329 * Gets the metadata as a tree in standard format.
330 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800331 * @return the metadata as a tree in standard format.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700332 */
333 protected final IIOMetadataNode getStandardTree() {
334 // Create root node
335 IIOMetadataNode root = new IIOMetadataNode(IIOMetadataFormatImpl.standardMetadataFormatName);
336
337 Node node;
338 if ((node = getStandardChromaNode()) != null) {
339 root.appendChild(node);
340 }
341 if ((node = getStandardCompressionNode()) != null) {
342 root.appendChild(node);
343 }
344 if ((node = getStandardDataNode()) != null) {
345 root.appendChild(node);
346 }
347 if ((node = getStandardDimensionNode()) != null) {
348 root.appendChild(node);
349 }
350 if ((node = getStandardDocumentNode()) != null) {
351 root.appendChild(node);
352 }
353 if ((node = getStandardTextNode()) != null) {
354 root.appendChild(node);
355 }
356 if ((node = getStandardTileNode()) != null) {
357 root.appendChild(node);
358 }
359 if ((node = getStandardTransparencyNode()) != null) {
360 root.appendChild(node);
361 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 return root;
364 }
365
366 /**
367 * Sets the controller.
368 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800369 * @param controller
370 * the new controller.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700371 */
372 public void setController(IIOMetadataController controller) {
373 this.controller = controller;
374 }
375
376 /**
377 * Sets the from tree.
378 *
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800379 * @param formatName
380 * the name of the metatdata format of the from tree.
381 * @param root
382 * the root node of the from tree.
383 * @throws IIOInvalidTreeException
384 * if the tree or its format is not compatible with this
385 * metadata.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700386 */
387 public void setFromTree(String formatName, Node root) throws IIOInvalidTreeException {
388 reset();
389 mergeTree(formatName, root);
390 }
391}