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.Collection; 048import java.util.HashMap; 049import java.util.Iterator; 050import java.util.Locale; 051import java.util.Map; 052import java.util.MissingResourceException; 053import java.util.ResourceBundle; 054 055import javax.imageio.ImageTypeSpecifier; 056import javax.imageio.metadata.IIOMetadataFormat; 057 058import com.github.jaiimageio.plugins.tiff.BaselineTIFFTagSet; 059import com.github.jaiimageio.plugins.tiff.TIFFTag; 060import com.github.jaiimageio.plugins.tiff.TIFFTagSet; 061 062public abstract class TIFFMetadataFormat implements IIOMetadataFormat { 063 064 protected Map elementInfoMap = new HashMap(); 065 protected Map attrInfoMap = new HashMap(); 066 067 protected String resourceBaseName; 068 protected String rootName; 069 070 public String getRootName() { 071 return rootName; 072 } 073 074 private String getResource(String key, Locale locale) { 075 if (locale == null) { 076 locale = Locale.getDefault(); 077 } 078 try { 079 ResourceBundle bundle = 080 ResourceBundle.getBundle(resourceBaseName, locale); 081 return bundle.getString(key); 082 } catch (MissingResourceException e) { 083 return null; 084 } 085 } 086 087 private TIFFElementInfo getElementInfo(String elementName) { 088 if (elementName == null) { 089 throw new IllegalArgumentException("elementName == null!"); 090 } 091 TIFFElementInfo info = 092 (TIFFElementInfo)elementInfoMap.get(elementName); 093 if (info == null) { 094 throw new IllegalArgumentException("No such element: " + 095 elementName); 096 } 097 return info; 098 } 099 100 private TIFFAttrInfo getAttrInfo(String elementName, String attrName) { 101 if (elementName == null) { 102 throw new IllegalArgumentException("elementName == null!"); 103 } 104 if (attrName == null) { 105 throw new IllegalArgumentException("attrName == null!"); 106 } 107 String key = elementName + "/" + attrName; 108 TIFFAttrInfo info = (TIFFAttrInfo)attrInfoMap.get(key); 109 if (info == null) { 110 throw new IllegalArgumentException("No such attribute: " + key); 111 } 112 return info; 113 } 114 115 public int getElementMinChildren(String elementName) { 116 TIFFElementInfo info = getElementInfo(elementName); 117 return info.minChildren; 118 } 119 120 public int getElementMaxChildren(String elementName) { 121 TIFFElementInfo info = getElementInfo(elementName); 122 return info.maxChildren; 123 } 124 125 public String getElementDescription(String elementName, Locale locale) { 126 if (!elementInfoMap.containsKey(elementName)) { 127 throw new IllegalArgumentException("No such element: " + 128 elementName); 129 } 130 return getResource(elementName, locale); 131 } 132 133 public int getChildPolicy(String elementName) { 134 TIFFElementInfo info = getElementInfo(elementName); 135 return info.childPolicy; 136 } 137 138 public String[] getChildNames(String elementName) { 139 TIFFElementInfo info = getElementInfo(elementName); 140 return info.childNames; 141 } 142 143 public String[] getAttributeNames(String elementName) { 144 TIFFElementInfo info = getElementInfo(elementName); 145 return info.attributeNames; 146 } 147 148 public int getAttributeValueType(String elementName, String attrName) { 149 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 150 return info.valueType; 151 } 152 153 public int getAttributeDataType(String elementName, String attrName) { 154 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 155 return info.dataType; 156 } 157 158 public boolean isAttributeRequired(String elementName, String attrName) { 159 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 160 return info.isRequired; 161 } 162 163 public String getAttributeDefaultValue(String elementName, 164 String attrName) { 165 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 166 return info.defaultValue; 167 } 168 169 public String[] getAttributeEnumerations(String elementName, 170 String attrName) { 171 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 172 return info.enumerations; 173 } 174 175 public String getAttributeMinValue(String elementName, String attrName) { 176 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 177 return info.minValue; 178 } 179 180 public String getAttributeMaxValue(String elementName, String attrName) { 181 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 182 return info.maxValue; 183 } 184 185 public int getAttributeListMinLength(String elementName, String attrName) { 186 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 187 return info.listMinLength; 188 } 189 190 public int getAttributeListMaxLength(String elementName, String attrName) { 191 TIFFAttrInfo info = getAttrInfo(elementName, attrName); 192 return info.listMaxLength; 193 } 194 195 public String getAttributeDescription(String elementName, String attrName, 196 Locale locale) { 197 String key = elementName + "/" + attrName; 198 if (!attrInfoMap.containsKey(key)) { 199 throw new IllegalArgumentException("No such attribute: " + key); 200 } 201 return getResource(key, locale); 202 } 203 204 public int getObjectValueType(String elementName) { 205 TIFFElementInfo info = getElementInfo(elementName); 206 return info.objectValueType; 207 } 208 209 public Class getObjectClass(String elementName) { 210 TIFFElementInfo info = getElementInfo(elementName); 211 if (info.objectValueType == VALUE_NONE) { 212 throw new IllegalArgumentException( 213 "Element cannot contain an object value: " + elementName); 214 } 215 return info.objectClass; 216 } 217 218 public Object getObjectDefaultValue(String elementName) { 219 TIFFElementInfo info = getElementInfo(elementName); 220 if (info.objectValueType == VALUE_NONE) { 221 throw new IllegalArgumentException( 222 "Element cannot contain an object value: " + elementName); 223 } 224 return info.objectDefaultValue; 225 } 226 227 public Object[] getObjectEnumerations(String elementName) { 228 TIFFElementInfo info = getElementInfo(elementName); 229 if (info.objectValueType == VALUE_NONE) { 230 throw new IllegalArgumentException( 231 "Element cannot contain an object value: " + elementName); 232 } 233 return info.objectEnumerations; 234 } 235 236 public Comparable getObjectMinValue(String elementName) { 237 TIFFElementInfo info = getElementInfo(elementName); 238 if (info.objectValueType == VALUE_NONE) { 239 throw new IllegalArgumentException( 240 "Element cannot contain an object value: " + elementName); 241 } 242 return info.objectMinValue; 243 } 244 245 public Comparable getObjectMaxValue(String elementName) { 246 TIFFElementInfo info = getElementInfo(elementName); 247 if (info.objectValueType == VALUE_NONE) { 248 throw new IllegalArgumentException( 249 "Element cannot contain an object value: " + elementName); 250 } 251 return info.objectMaxValue; 252 } 253 254 public int getObjectArrayMinLength(String elementName) { 255 TIFFElementInfo info = getElementInfo(elementName); 256 if (info.objectValueType == VALUE_NONE) { 257 throw new IllegalArgumentException( 258 "Element cannot contain an object value: " + elementName); 259 } 260 return info.objectArrayMinLength; 261 } 262 263 public int getObjectArrayMaxLength(String elementName) { 264 TIFFElementInfo info = getElementInfo(elementName); 265 if (info.objectValueType == VALUE_NONE) { 266 throw new IllegalArgumentException( 267 "Element cannot contain an object value: " + elementName); 268 } 269 return info.objectArrayMaxLength; 270 } 271 272 public TIFFMetadataFormat() {} 273}