001/* 002 * $RCSfile: TIFFMetadataFormat.java,v $ 003 * 004 * 005 * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without 008 * modification, are permitted provided that the following conditions 009 * are met: 010 * 011 * - Redistribution of source code must retain the above copyright 012 * notice, this list of conditions and the following disclaimer. 013 * 014 * - Redistribution in binary form must reproduce the above copyright 015 * notice, this list of conditions and the following disclaimer in 016 * the documentation and/or other materials provided with the 017 * distribution. 018 * 019 * Neither the name of Sun Microsystems, Inc. or the names of 020 * contributors may be used to endorse or promote products derived 021 * from this software without specific prior written permission. 022 * 023 * This software is provided "AS IS," without a warranty of any 024 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 025 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 026 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY 027 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 028 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 029 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS 030 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 031 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, 032 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND 033 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR 034 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 035 * POSSIBILITY OF SUCH DAMAGES. 036 * 037 * You acknowledge that this software is not designed or intended for 038 * use in the design, construction, operation or maintenance of any 039 * nuclear facility. 040 * 041 * $Revision: 1.1 $ 042 * $Date: 2005/02/11 05:01:48 $ 043 * $State: Exp $ 044 */ 045package com.github.jaiimageio.impl.plugins.tiff; 046 047import java.util.HashMap; 048import java.util.Locale; 049import java.util.Map; 050import java.util.MissingResourceException; 051import java.util.ResourceBundle; 052 053import javax.imageio.metadata.IIOMetadataFormat; 054 055public abstract class TIFFMetadataFormat implements IIOMetadataFormat { 056 057 protected Map elementInfoMap = new HashMap(); 058 protected Map attrInfoMap = new HashMap(); 059 060 protected String resourceBaseName; 061 protected String rootName; 062 063 public String getRootName() { 064 return rootName; 065 } 066 067 private String getResource(String key, Locale locale) { 068 if (locale == null) { 069 locale = Locale.getDefault(); 070 } 071 try { 072 ResourceBundle bundle = 073 ResourceBundle.getBundle(resourceBaseName, locale); 074 return bundle.getString(key); 075 } catch (MissingResourceException e) { 076 return null; 077 } 078 } 079 080 private TIFFElementInfo getElementInfo(String elementName) { 081 if (elementName == null) { 082 throw new IllegalArgumentException("elementName == null!"); 083 } 084 TIFFElementInfo info = 085 (TIFFElementInfo)elementInfoMap.get(elementName); 086 if (info == null) { 087 throw new IllegalArgumentException("No such element: " + 088 elementName); 089 } 090 return info; 091 } 092 093 private TIFFAttrInfo getAttrInfo(String elementName, String attrName) { 094 if (elementName == null) { 095 throw new IllegalArgumentException("elementName == null!"); 096 } 097 if (attrName == null) { 098 throw new IllegalArgumentException("attrName == null!"); 099 } 100 String key = elementName + "/" + attrName; 101 TIFFAttrInfo info = (TIFFAttrInfo)attrInfoMap.get(key); 102 if (info == null) { 103 throw new IllegalArgumentException("No such attribute: " + key); 104 } 105 return info; 106 } 107 108 public int getElementMinChildren(String elementName) { 109 TIFFElementInfo info = getElementInfo(elementName); 110 return info.minChildren; 111 } 112 113 public int getElementMaxChildren(String elementName) { 114 TIFFElementInfo info = getElementInfo(elementName); 115 return info.maxChildren; 116 } 117 118 public String getElementDescription(String elementName, Locale locale) { 119 if (!elementInfoMap.containsKey(elementName)) { 120 throw new IllegalArgumentException("No such element: " + 121 elementName); 122 } 123 return getResource(elementName, locale); 124 } 125 126 public int getChildPolicy(String elementName) { 127 TIFFElementInfo info = getElementInfo(elementName); 128 return info.childPolicy; 129 } 130 131 public String[] getChildNames(String elementName) { 132 TIFFElementInfo info = getElementInfo(elementName); 133 return info.childNames; 134 } 135 136 public String[] getAttributeNames(String elementName) { 137 TIFFElementInfo info = getElementInfo(elementName); 138 return info.attributeNames; 139 } 140 141 public int getAttributeValueType(String elementName, String attrName) { 142 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 143 return info.valueType; 144 } 145 146 public int getAttributeDataType(String elementName, String attrName) { 147 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 148 return info.dataType; 149 } 150 151 public boolean isAttributeRequired(String elementName, String attrName) { 152 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 153 return info.isRequired; 154 } 155 156 public String getAttributeDefaultValue(String elementName, 157 String attrName) { 158 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 159 return info.defaultValue; 160 } 161 162 public String[] getAttributeEnumerations(String elementName, 163 String attrName) { 164 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 165 return info.enumerations; 166 } 167 168 public String getAttributeMinValue(String elementName, String attrName) { 169 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 170 return info.minValue; 171 } 172 173 public String getAttributeMaxValue(String elementName, String attrName) { 174 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 175 return info.maxValue; 176 } 177 178 public int getAttributeListMinLength(String elementName, String attrName) { 179 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 180 return info.listMinLength; 181 } 182 183 public int getAttributeListMaxLength(String elementName, String attrName) { 184 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 185 return info.listMaxLength; 186 } 187 188 public String getAttributeDescription(String elementName, String attrName, 189 Locale locale) { 190 String key = elementName + "/" + attrName; 191 if (!attrInfoMap.containsKey(key)) { 192 throw new IllegalArgumentException("No such attribute: " + key); 193 } 194 return getResource(key, locale); 195 } 196 197 public int getObjectValueType(String elementName) { 198 TIFFElementInfo info = getElementInfo(elementName); 199 return info.objectValueType; 200 } 201 202 public Class getObjectClass(String elementName) { 203 TIFFElementInfo info = getElementInfo(elementName); 204 if (info.objectValueType == VALUE_NONE) { 205 throw new IllegalArgumentException( 206 "Element cannot contain an object value: " + elementName); 207 } 208 return info.objectClass; 209 } 210 211 public Object getObjectDefaultValue(String elementName) { 212 TIFFElementInfo info = getElementInfo(elementName); 213 if (info.objectValueType == VALUE_NONE) { 214 throw new IllegalArgumentException( 215 "Element cannot contain an object value: " + elementName); 216 } 217 return info.objectDefaultValue; 218 } 219 220 public Object[] getObjectEnumerations(String elementName) { 221 TIFFElementInfo info = getElementInfo(elementName); 222 if (info.objectValueType == VALUE_NONE) { 223 throw new IllegalArgumentException( 224 "Element cannot contain an object value: " + elementName); 225 } 226 return info.objectEnumerations; 227 } 228 229 public Comparable getObjectMinValue(String elementName) { 230 TIFFElementInfo info = getElementInfo(elementName); 231 if (info.objectValueType == VALUE_NONE) { 232 throw new IllegalArgumentException( 233 "Element cannot contain an object value: " + elementName); 234 } 235 return info.objectMinValue; 236 } 237 238 public Comparable getObjectMaxValue(String elementName) { 239 TIFFElementInfo info = getElementInfo(elementName); 240 if (info.objectValueType == VALUE_NONE) { 241 throw new IllegalArgumentException( 242 "Element cannot contain an object value: " + elementName); 243 } 244 return info.objectMaxValue; 245 } 246 247 public int getObjectArrayMinLength(String elementName) { 248 TIFFElementInfo info = getElementInfo(elementName); 249 if (info.objectValueType == VALUE_NONE) { 250 throw new IllegalArgumentException( 251 "Element cannot contain an object value: " + elementName); 252 } 253 return info.objectArrayMinLength; 254 } 255 256 public int getObjectArrayMaxLength(String elementName) { 257 TIFFElementInfo info = getElementInfo(elementName); 258 if (info.objectValueType == VALUE_NONE) { 259 throw new IllegalArgumentException( 260 "Element cannot contain an object value: " + elementName); 261 } 262 return info.objectArrayMaxLength; 263 } 264 265 public TIFFMetadataFormat() {} 266}