1 7 8 package com.sun.imageio.plugins.gif; 9 10 import javax.imageio.metadata.IIOInvalidTreeException ; 11 import javax.imageio.metadata.IIOMetadata ; 12 import javax.imageio.metadata.IIOMetadataFormatImpl ; 13 import org.w3c.dom.Node ; 14 15 19 abstract class GIFMetadata extends IIOMetadata { 20 21 24 static final int UNDEFINED_INTEGER_VALUE = -1; 25 26 31 protected static void fatal(Node node, String reason) 33 throws IIOInvalidTreeException { 34 throw new IIOInvalidTreeException (reason, node); 35 } 36 37 protected static String getStringAttribute(Node node, String name, 39 String defaultValue, 40 boolean required, 41 String [] range) 42 throws IIOInvalidTreeException { 43 Node attr = node.getAttributes().getNamedItem(name); 44 if (attr == null) { 45 if (!required) { 46 return defaultValue; 47 } else { 48 fatal(node, "Required attribute " + name + " not present!"); 49 } 50 } 51 String value = attr.getNodeValue(); 52 53 if (range != null) { 54 if (value == null) { 55 fatal(node, 56 "Null value for "+node.getNodeName()+ 57 " attribute "+name+"!"); 58 } 59 boolean validValue = false; 60 int len = range.length; 61 for (int i = 0; i < len; i++) { 62 if (value.equals(range[i])) { 63 validValue = true; 64 break; 65 } 66 } 67 if (!validValue) { 68 fatal(node, 69 "Bad value for "+node.getNodeName()+ 70 " attribute "+name+"!"); 71 } 72 } 73 74 return value; 75 } 76 77 78 protected static int getIntAttribute(Node node, String name, 80 int defaultValue, boolean required, 81 boolean bounded, int min, int max) 82 throws IIOInvalidTreeException { 83 String value = getStringAttribute(node, name, null, required, null); 84 if (value == null || "".equals(value)) { 85 return defaultValue; 86 } 87 88 int intValue = defaultValue; 89 try { 90 intValue = Integer.parseInt(value); 91 } catch (NumberFormatException e) { 92 fatal(node, 93 "Bad value for "+node.getNodeName()+ 94 " attribute "+name+"!"); 95 } 96 if (bounded && (intValue < min || intValue > max)) { 97 fatal(node, 98 "Bad value for "+node.getNodeName()+ 99 " attribute "+name+"!"); 100 } 101 return intValue; 102 } 103 104 protected static float getFloatAttribute(Node node, String name, 106 float defaultValue, 107 boolean required) 108 throws IIOInvalidTreeException { 109 String value = getStringAttribute(node, name, null, required, null); 110 if (value == null) { 111 return defaultValue; 112 } 113 return Float.parseFloat(value); 114 } 115 116 protected static int getIntAttribute(Node node, String name, 118 boolean bounded, int min, int max) 119 throws IIOInvalidTreeException { 120 return getIntAttribute(node, name, -1, true, bounded, min, max); 121 } 122 123 protected static float getFloatAttribute(Node node, String name) 125 throws IIOInvalidTreeException { 126 return getFloatAttribute(node, name, -1.0F, true); 127 } 128 129 protected static boolean getBooleanAttribute(Node node, String name, 131 boolean defaultValue, 132 boolean required) 133 throws IIOInvalidTreeException { 134 Node attr = node.getAttributes().getNamedItem(name); 135 if (attr == null) { 136 if (!required) { 137 return defaultValue; 138 } else { 139 fatal(node, "Required attribute " + name + " not present!"); 140 } 141 } 142 String value = attr.getNodeValue(); 143 if (value.equalsIgnoreCase("TRUE")) { 148 return true; 149 } else if (value.equalsIgnoreCase("FALSE")) { 150 return false; 151 } else { 152 fatal(node, "Attribute " + name + " must be 'TRUE' or 'FALSE'!"); 153 return false; 154 } 155 } 156 157 protected static boolean getBooleanAttribute(Node node, String name) 159 throws IIOInvalidTreeException { 160 return getBooleanAttribute(node, name, false, true); 161 } 162 163 protected static int getEnumeratedAttribute(Node node, 165 String name, 166 String [] legalNames, 167 int defaultValue, 168 boolean required) 169 throws IIOInvalidTreeException { 170 Node attr = node.getAttributes().getNamedItem(name); 171 if (attr == null) { 172 if (!required) { 173 return defaultValue; 174 } else { 175 fatal(node, "Required attribute " + name + " not present!"); 176 } 177 } 178 String value = attr.getNodeValue(); 179 for (int i = 0; i < legalNames.length; i++) { 180 if(value.equals(legalNames[i])) { 181 return i; 182 } 183 } 184 185 fatal(node, "Illegal value for attribute " + name + "!"); 186 return -1; 187 } 188 189 protected static int getEnumeratedAttribute(Node node, 191 String name, 192 String [] legalNames) 193 throws IIOInvalidTreeException { 194 return getEnumeratedAttribute(node, name, legalNames, -1, true); 195 } 196 197 protected static String getAttribute(Node node, String name, 199 String defaultValue, boolean required) 200 throws IIOInvalidTreeException { 201 Node attr = node.getAttributes().getNamedItem(name); 202 if (attr == null) { 203 if (!required) { 204 return defaultValue; 205 } else { 206 fatal(node, "Required attribute " + name + " not present!"); 207 } 208 } 209 return attr.getNodeValue(); 210 } 211 212 protected static String getAttribute(Node node, String name) 214 throws IIOInvalidTreeException { 215 return getAttribute(node, name, null, true); 216 } 217 218 protected GIFMetadata(boolean standardMetadataFormatSupported, 219 String nativeMetadataFormatName, 220 String nativeMetadataFormatClassName, 221 String [] extraMetadataFormatNames, 222 String [] extraMetadataFormatClassNames) { 223 super(standardMetadataFormatSupported, 224 nativeMetadataFormatName, 225 nativeMetadataFormatClassName, 226 extraMetadataFormatNames, 227 extraMetadataFormatClassNames); 228 } 229 230 public void mergeTree(String formatName, Node root) 231 throws IIOInvalidTreeException { 232 if (formatName.equals(nativeMetadataFormatName)) { 233 if (root == null) { 234 throw new IllegalArgumentException ("root == null!"); 235 } 236 mergeNativeTree(root); 237 } else if (formatName.equals 238 (IIOMetadataFormatImpl.standardMetadataFormatName)) { 239 if (root == null) { 240 throw new IllegalArgumentException ("root == null!"); 241 } 242 mergeStandardTree(root); 243 } else { 244 throw new IllegalArgumentException ("Not a recognized format!"); 245 } 246 } 247 248 protected byte[] getColorTable(Node colorTableNode, 249 String entryNodeName, 250 boolean lengthExpected, 251 int expectedLength) 252 throws IIOInvalidTreeException { 253 byte[] red = new byte[256]; 254 byte[] green = new byte[256]; 255 byte[] blue = new byte[256]; 256 int maxIndex = -1; 257 258 Node entry = colorTableNode.getFirstChild(); 259 if (entry == null) { 260 fatal(colorTableNode, "Palette has no entries!"); 261 } 262 263 while (entry != null) { 264 if (!entry.getNodeName().equals(entryNodeName)) { 265 fatal(colorTableNode, 266 "Only a "+entryNodeName+" may be a child of a "+ 267 entry.getNodeName()+"!"); 268 } 269 270 int index = getIntAttribute(entry, "index", true, 0, 255); 271 if (index > maxIndex) { 272 maxIndex = index; 273 } 274 red[index] = (byte)getIntAttribute(entry, "red", true, 0, 255); 275 green[index] = (byte)getIntAttribute(entry, "green", true, 0, 255); 276 blue[index] = (byte)getIntAttribute(entry, "blue", true, 0, 255); 277 278 entry = entry.getNextSibling(); 279 } 280 281 int numEntries = maxIndex + 1; 282 283 if (lengthExpected && numEntries != expectedLength) { 284 fatal(colorTableNode, "Unexpected length for palette!"); 285 } 286 287 byte[] colorTable = new byte[3*numEntries]; 288 for (int i = 0, j = 0; i < numEntries; i++) { 289 colorTable[j++] = red[i]; 290 colorTable[j++] = green[i]; 291 colorTable[j++] = blue[i]; 292 } 293 294 return colorTable; 295 } 296 297 protected abstract void mergeNativeTree(Node root) 298 throws IIOInvalidTreeException ; 299 300 protected abstract void mergeStandardTree(Node root) 301 throws IIOInvalidTreeException ; 302 } 303 | Popular Tags |