1 /*
2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package com.sun.imageio.plugins.tiff;
26 
27 import java.util.HashMap;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.MissingResourceException;
31 import java.util.ResourceBundle;
32 import javax.imageio.metadata.IIOMetadataFormat;
33 
34 public abstract class TIFFMetadataFormat implements IIOMetadataFormat {
35 
36     protected Map<String,TIFFElementInfo> elementInfoMap = new HashMap<String,TIFFElementInfo>();
37     protected Map<String,TIFFAttrInfo> attrInfoMap = new HashMap<String,TIFFAttrInfo>();
38 
39     protected String resourceBaseName;
40     protected String rootName;
41 
getRootName()42     public String getRootName() {
43         return rootName;
44     }
45 
getResource(String key, Locale locale)46     private String getResource(String key, Locale locale) {
47         if (locale == null) {
48             locale = Locale.getDefault();
49         }
50         try {
51             ResourceBundle bundle =
52                 ResourceBundle.getBundle(resourceBaseName, locale,
53                                          this.getClass().getModule());
54             return bundle.getString(key);
55         } catch (MissingResourceException e) {
56             return null;
57         }
58     }
59 
getElementInfo(String elementName)60     private TIFFElementInfo getElementInfo(String elementName) {
61         if (elementName == null) {
62             throw new NullPointerException("elementName == null!");
63         }
64         TIFFElementInfo info =
65             elementInfoMap.get(elementName);
66         if (info == null) {
67             throw new IllegalArgumentException("No such element: " +
68                                                elementName);
69         }
70         return info;
71     }
72 
getAttrInfo(String elementName, String attrName)73     private TIFFAttrInfo getAttrInfo(String elementName, String attrName) {
74         if (elementName == null) {
75             throw new NullPointerException("elementName == null!");
76         }
77         if (attrName == null) {
78             throw new NullPointerException("attrName == null!");
79         }
80         String key = elementName + "/" + attrName;
81         TIFFAttrInfo info = attrInfoMap.get(key);
82         if (info == null) {
83             throw new IllegalArgumentException("No such attribute: " + key);
84         }
85         return info;
86     }
87 
getElementMinChildren(String elementName)88     public int getElementMinChildren(String elementName) {
89         TIFFElementInfo info = getElementInfo(elementName);
90         return info.minChildren;
91     }
92 
getElementMaxChildren(String elementName)93     public int getElementMaxChildren(String elementName) {
94         TIFFElementInfo info = getElementInfo(elementName);
95         return info.maxChildren;
96     }
97 
getElementDescription(String elementName, Locale locale)98     public String getElementDescription(String elementName, Locale locale) {
99         if (!elementInfoMap.containsKey(elementName)) {
100             throw new IllegalArgumentException("No such element: " +
101                                                elementName);
102         }
103         return getResource(elementName, locale);
104     }
105 
getChildPolicy(String elementName)106     public int getChildPolicy(String elementName) {
107         TIFFElementInfo info = getElementInfo(elementName);
108         return info.childPolicy;
109     }
110 
getChildNames(String elementName)111     public String[] getChildNames(String elementName) {
112         TIFFElementInfo info = getElementInfo(elementName);
113         return info.childNames;
114     }
115 
getAttributeNames(String elementName)116     public String[] getAttributeNames(String elementName) {
117         TIFFElementInfo info = getElementInfo(elementName);
118         return info.attributeNames;
119     }
120 
getAttributeValueType(String elementName, String attrName)121     public int getAttributeValueType(String elementName, String attrName) {
122         TIFFAttrInfo info = getAttrInfo(elementName, attrName);
123         return info.valueType;
124     }
125 
getAttributeDataType(String elementName, String attrName)126     public int getAttributeDataType(String elementName, String attrName) {
127         TIFFAttrInfo info = getAttrInfo(elementName, attrName);
128         return info.dataType;
129     }
130 
isAttributeRequired(String elementName, String attrName)131     public boolean isAttributeRequired(String elementName, String attrName) {
132         TIFFAttrInfo info = getAttrInfo(elementName, attrName);
133         return info.isRequired;
134     }
135 
getAttributeDefaultValue(String elementName, String attrName)136     public String getAttributeDefaultValue(String elementName,
137                                            String attrName) {
138         return null;
139     }
140 
getAttributeEnumerations(String elementName, String attrName)141     public String[] getAttributeEnumerations(String elementName,
142                                              String attrName) {
143         throw new IllegalArgumentException("The attribute is not an enumeration.");
144     }
145 
getAttributeMinValue(String elementName, String attrName)146     public String getAttributeMinValue(String elementName, String attrName) {
147         throw new IllegalArgumentException("The attribute is not a range.");
148     }
149 
getAttributeMaxValue(String elementName, String attrName)150     public String getAttributeMaxValue(String elementName, String attrName) {
151         throw new IllegalArgumentException("The attribute is not a range.");
152     }
153 
getAttributeListMinLength(String elementName, String attrName)154     public int getAttributeListMinLength(String elementName, String attrName) {
155         TIFFAttrInfo info = getAttrInfo(elementName, attrName);
156         return info.listMinLength;
157     }
158 
getAttributeListMaxLength(String elementName, String attrName)159     public int getAttributeListMaxLength(String elementName, String attrName) {
160         TIFFAttrInfo info = getAttrInfo(elementName, attrName);
161         return info.listMaxLength;
162     }
163 
getAttributeDescription(String elementName, String attrName, Locale locale)164     public String getAttributeDescription(String elementName, String attrName,
165                                           Locale locale) {
166         String key = elementName + "/" + attrName;
167         if (!attrInfoMap.containsKey(key)) {
168             throw new IllegalArgumentException("No such attribute: " + key);
169         }
170         return getResource(key, locale);
171     }
172 
getObjectValueType(String elementName)173     public int getObjectValueType(String elementName) {
174         TIFFElementInfo info = getElementInfo(elementName);
175         return info.objectValueType;
176     }
177 
getObjectClass(String elementName)178     public Class<?> getObjectClass(String elementName) {
179         TIFFElementInfo info = getElementInfo(elementName);
180         if (info.objectValueType == VALUE_NONE) {
181             throw new IllegalArgumentException(
182                      "Element cannot contain an object value: " + elementName);
183         }
184         return info.objectClass;
185     }
186 
getObjectDefaultValue(String elementName)187     public Object getObjectDefaultValue(String elementName) {
188         TIFFElementInfo info = getElementInfo(elementName);
189         if (info.objectValueType == VALUE_NONE) {
190             throw new IllegalArgumentException(
191                      "Element cannot contain an object value: " + elementName);
192         }
193         return info.objectDefaultValue;
194     }
195 
getObjectEnumerations(String elementName)196     public Object[] getObjectEnumerations(String elementName) {
197         TIFFElementInfo info = getElementInfo(elementName);
198         if (info.objectValueType == VALUE_NONE) {
199             throw new IllegalArgumentException(
200                      "Element cannot contain an object value: " + elementName);
201         }
202         return info.objectEnumerations;
203     }
204 
getObjectMinValue(String elementName)205     public Comparable<Object> getObjectMinValue(String elementName) {
206         TIFFElementInfo info = getElementInfo(elementName);
207         if (info.objectValueType == VALUE_NONE) {
208             throw new IllegalArgumentException(
209                      "Element cannot contain an object value: " + elementName);
210         }
211         return info.objectMinValue;
212     }
213 
getObjectMaxValue(String elementName)214     public Comparable<Object> getObjectMaxValue(String elementName) {
215         TIFFElementInfo info = getElementInfo(elementName);
216         if (info.objectValueType == VALUE_NONE) {
217             throw new IllegalArgumentException(
218                      "Element cannot contain an object value: " + elementName);
219         }
220         return info.objectMaxValue;
221     }
222 
getObjectArrayMinLength(String elementName)223     public int getObjectArrayMinLength(String elementName) {
224         TIFFElementInfo info = getElementInfo(elementName);
225         if (info.objectValueType == VALUE_NONE) {
226             throw new IllegalArgumentException(
227                      "Element cannot contain an object value: " + elementName);
228         }
229         return info.objectArrayMinLength;
230     }
231 
getObjectArrayMaxLength(String elementName)232     public int getObjectArrayMaxLength(String elementName) {
233         TIFFElementInfo info = getElementInfo(elementName);
234         if (info.objectValueType == VALUE_NONE) {
235             throw new IllegalArgumentException(
236                      "Element cannot contain an object value: " + elementName);
237         }
238         return info.objectArrayMaxLength;
239     }
240 
TIFFMetadataFormat()241     public TIFFMetadataFormat() {}
242 }
243