• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
17 
18 package javax.imageio.metadata;
19 
20 import java.util.ArrayList;
21 
22 import org.apache.harmony.x.imageio.metadata.IIOMetadataUtils;
23 import org.w3c.dom.Node;
24 
25 /**
26  * The class IIOMetadata represents the metadata (bundled with an image) as a
27  * Dom-type tree.
28  *
29  * @since Android 1.0
30  */
31 public abstract class IIOMetadata {
32 
33     /**
34      * Whether the standard metadata format is supported.
35      */
36     protected boolean standardFormatSupported;
37 
38     /**
39      * The native metadata format name.
40      */
41     protected String nativeMetadataFormatName;
42 
43     /**
44      * The native metadata format class name.
45      */
46     protected String nativeMetadataFormatClassName;
47 
48     /**
49      * The extra metadata format names.
50      */
51     protected String[] extraMetadataFormatNames;
52 
53     /**
54      * The extra metadata format class names.
55      */
56     protected String[] extraMetadataFormatClassNames;
57 
58     /**
59      * The default controller.
60      */
61     protected IIOMetadataController defaultController;
62 
63     /**
64      * The controller.
65      */
66     protected IIOMetadataController controller;
67 
68     /**
69      * Instantiates a new IIOMetadata with no data set.
70      */
IIOMetadata()71     protected IIOMetadata() {
72     }
73 
74     /**
75      * Instantiates a new IIOMetadata with the specified data parameters.
76      *
77      * @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.
87      */
IIOMetadata(boolean standardMetadataFormatSupported, String nativeMetadataFormatName, String nativeMetadataFormatClassName, String[] extraMetadataFormatNames, String[] extraMetadataFormatClassNames)88     protected IIOMetadata(boolean standardMetadataFormatSupported, String nativeMetadataFormatName,
89             String nativeMetadataFormatClassName, String[] extraMetadataFormatNames,
90             String[] extraMetadataFormatClassNames) {
91         standardFormatSupported = standardMetadataFormatSupported;
92         this.nativeMetadataFormatName = nativeMetadataFormatName;
93         this.nativeMetadataFormatClassName = nativeMetadataFormatClassName;
94         if (extraMetadataFormatNames == null) {
95             if (extraMetadataFormatClassNames != null) {
96                 throw new IllegalArgumentException(
97                         "extraMetadataFormatNames == null && extraMetadataFormatClassNames != null!");
98             }
99         } else {
100             if (extraMetadataFormatClassNames == null) {
101                 throw new IllegalArgumentException(
102                         "extraMetadataFormatNames != null && extraMetadataFormatClassNames == null!");
103             }
104             if (extraMetadataFormatNames.length == 0) {
105                 throw new IllegalArgumentException("extraMetadataFormatNames.length == 0!");
106             }
107             if (extraMetadataFormatClassNames.length != extraMetadataFormatNames.length) {
108                 throw new IllegalArgumentException(
109                         "extraMetadataFormatClassNames.length != extraMetadataFormatNames.length!");
110             }
111             this.extraMetadataFormatNames = extraMetadataFormatNames.clone();
112             this.extraMetadataFormatClassNames = extraMetadataFormatClassNames.clone();
113         }
114     }
115 
116     /**
117      * Gets the metadata as tree-type document.
118      *
119      * @param formatName
120      *            the format name.
121      * @return the node in tree format.
122      */
getAsTree(String formatName)123     public abstract Node getAsTree(String formatName);
124 
125     /**
126      * Checks if the metadata is read only.
127      *
128      * @return true, if the metadata is read only.
129      */
isReadOnly()130     public abstract boolean isReadOnly();
131 
132     /**
133      * Merges the specified tree with this metadata tree.
134      *
135      * @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.
142      */
mergeTree(String formatName, Node root)143     public abstract void mergeTree(String formatName, Node root) throws IIOInvalidTreeException;
144 
145     /**
146      * Resets the controller.
147      */
reset()148     public abstract void reset();
149 
150     /**
151      * Gets the controller associated with this metadata document.
152      *
153      * @return the controller.
154      */
getController()155     public IIOMetadataController getController() {
156         return controller;
157     }
158 
159     /**
160      * Checks whether this metadata has a controller.
161      *
162      * @return true, if this metadata has a controller.
163      */
hasController()164     public boolean hasController() {
165         return getController() != null;
166     }
167 
168     /**
169      * Activate the controller.
170      *
171      * @return true, if successful.
172      */
activateController()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      *
183      * @return the default controller.
184      */
getDefaultController()185     public IIOMetadataController getDefaultController() {
186         return defaultController;
187     }
188 
189     /**
190      * Gets the extra metadata format names.
191      *
192      * @return the extra metadata format names.
193      */
getExtraMetadataFormatNames()194     public String[] getExtraMetadataFormatNames() {
195         return extraMetadataFormatNames == null ? null : extraMetadataFormatNames.clone();
196     }
197 
198     /**
199      * Gets the metadata format.
200      *
201      * @param formatName
202      *            the format name.
203      * @return the metadata format.
204      */
getMetadataFormat(String formatName)205     public IIOMetadataFormat getMetadataFormat(String formatName) {
206         return IIOMetadataUtils.instantiateMetadataFormat(formatName, standardFormatSupported,
207                 nativeMetadataFormatName, nativeMetadataFormatClassName, extraMetadataFormatNames,
208                 extraMetadataFormatClassNames);
209     }
210 
211     /**
212      * Gets the native metadata format name.
213      *
214      * @return the native metadata format name.
215      */
getNativeMetadataFormatName()216     public String getNativeMetadataFormatName() {
217         return nativeMetadataFormatName;
218     }
219 
220     /**
221      * Checks if the standard metadata format is supported.
222      *
223      * @return true, if the standard metadata format is supported.
224      */
isStandardMetadataFormatSupported()225     public boolean isStandardMetadataFormatSupported() {
226         return standardFormatSupported;
227     }
228 
229     /**
230      * Gets the metadata format names.
231      *
232      * @return the metadata format names.
233      */
getMetadataFormatNames()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      *
259      * @return the standard chroma node.
260      */
getStandardChromaNode()261     protected IIOMetadataNode getStandardChromaNode() {
262         return null;
263     }
264 
265     /**
266      * Gets the standard compression node.
267      *
268      * @return the standard compression node.
269      */
getStandardCompressionNode()270     protected IIOMetadataNode getStandardCompressionNode() {
271         return null;
272     }
273 
274     /**
275      * Gets the standard data node.
276      *
277      * @return the standard data node.
278      */
getStandardDataNode()279     protected IIOMetadataNode getStandardDataNode() {
280         return null;
281     }
282 
283     /**
284      * Gets the standard dimension node.
285      *
286      * @return the standard dimension node.
287      */
getStandardDimensionNode()288     protected IIOMetadataNode getStandardDimensionNode() {
289         return null;
290     }
291 
292     /**
293      * Gets the standard document node.
294      *
295      * @return the standard document node.
296      */
getStandardDocumentNode()297     protected IIOMetadataNode getStandardDocumentNode() {
298         return null;
299     }
300 
301     /**
302      * Gets the standard text node.
303      *
304      * @return the standard text node.
305      */
getStandardTextNode()306     protected IIOMetadataNode getStandardTextNode() {
307         return null;
308     }
309 
310     /**
311      * Gets the standard tile node.
312      *
313      * @return the standard tile node.
314      */
getStandardTileNode()315     protected IIOMetadataNode getStandardTileNode() {
316         return null;
317     }
318 
319     /**
320      * Gets the standard transparency node.
321      *
322      * @return the standard transparency node.
323      */
getStandardTransparencyNode()324     protected IIOMetadataNode getStandardTransparencyNode() {
325         return null;
326     }
327 
328     /**
329      * Gets the metadata as a tree in standard format.
330      *
331      * @return the metadata as a tree in standard format.
332      */
getStandardTree()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         }
362 
363         return root;
364     }
365 
366     /**
367      * Sets the controller.
368      *
369      * @param controller
370      *            the new controller.
371      */
setController(IIOMetadataController controller)372     public void setController(IIOMetadataController controller) {
373         this.controller = controller;
374     }
375 
376     /**
377      * Sets the from tree.
378      *
379      * @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.
386      */
setFromTree(String formatName, Node root)387     public void setFromTree(String formatName, Node root) throws IIOInvalidTreeException {
388         reset();
389         mergeTree(formatName, root);
390     }
391 }
392