KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > gif > GIFMetadata


1 /*
2  * @(#)GIFMetadata.java 1.3 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.gif;
9
10 import javax.imageio.metadata.IIOInvalidTreeException JavaDoc;
11 import javax.imageio.metadata.IIOMetadata JavaDoc;
12 import javax.imageio.metadata.IIOMetadataFormatImpl JavaDoc;
13 import org.w3c.dom.Node JavaDoc;
14
15 /**
16  * Class which adds utility DOM element attribute access methods to
17  * <code>IIOMetadata</code> for subclass use.
18  */

19 abstract class GIFMetadata extends IIOMetadata JavaDoc {
20
21     /**
22      * Represents an undefined value of integer attributes.
23      */

24     static final int UNDEFINED_INTEGER_VALUE = -1;
25
26     //
27
// Note: These attribute methods were shamelessly lifted from
28
// com.sun.imageio.plugins.png.PNGMetadata and modified.
29
//
30

31     // Shorthand for throwing an IIOInvalidTreeException
32
protected static void fatal(Node JavaDoc node, String JavaDoc reason)
33       throws IIOInvalidTreeException JavaDoc {
34         throw new IIOInvalidTreeException JavaDoc(reason, node);
35     }
36
37     // Get an integer-valued attribute
38
protected static String JavaDoc getStringAttribute(Node JavaDoc node, String JavaDoc name,
39                                                String JavaDoc defaultValue,
40                                                boolean required,
41                                                String JavaDoc[] range)
42       throws IIOInvalidTreeException JavaDoc {
43         Node JavaDoc 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 JavaDoc 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     // Get an integer-valued attribute
79
protected static int getIntAttribute(Node JavaDoc node, String JavaDoc name,
80                                          int defaultValue, boolean required,
81                                          boolean bounded, int min, int max)
82       throws IIOInvalidTreeException JavaDoc {
83         String JavaDoc 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 JavaDoc 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     // Get a float-valued attribute
105
protected static float getFloatAttribute(Node JavaDoc node, String JavaDoc name,
106                                              float defaultValue,
107                                              boolean required)
108       throws IIOInvalidTreeException JavaDoc {
109         String JavaDoc value = getStringAttribute(node, name, null, required, null);
110         if (value == null) {
111             return defaultValue;
112         }
113         return Float.parseFloat(value);
114     }
115
116     // Get a required integer-valued attribute
117
protected static int getIntAttribute(Node JavaDoc node, String JavaDoc name,
118                                          boolean bounded, int min, int max)
119       throws IIOInvalidTreeException JavaDoc {
120         return getIntAttribute(node, name, -1, true, bounded, min, max);
121     }
122
123     // Get a required float-valued attribute
124
protected static float getFloatAttribute(Node JavaDoc node, String JavaDoc name)
125       throws IIOInvalidTreeException JavaDoc {
126         return getFloatAttribute(node, name, -1.0F, true);
127     }
128
129     // Get a boolean-valued attribute
130
protected static boolean getBooleanAttribute(Node JavaDoc node, String JavaDoc name,
131                                                  boolean defaultValue,
132                                                  boolean required)
133       throws IIOInvalidTreeException JavaDoc {
134         Node JavaDoc 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 JavaDoc value = attr.getNodeValue();
143         // XXX Should be able to use equals() here instead of
144
// equalsIgnoreCase() but some boolean attributes are incorrectly
145
// set to "true" or "false" by the J2SE core metadata classes
146
// getAsTree() method (which are duplicated above). See bug 5082756.
147
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     // Get a required boolean-valued attribute
158
protected static boolean getBooleanAttribute(Node JavaDoc node, String JavaDoc name)
159       throws IIOInvalidTreeException JavaDoc {
160         return getBooleanAttribute(node, name, false, true);
161     }
162
163     // Get an enumerated attribute as an index into a String array
164
protected static int getEnumeratedAttribute(Node JavaDoc node,
165                                                 String JavaDoc name,
166                                                 String JavaDoc[] legalNames,
167                                                 int defaultValue,
168                                                 boolean required)
169       throws IIOInvalidTreeException JavaDoc {
170         Node JavaDoc 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 JavaDoc 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     // Get a required enumerated attribute as an index into a String array
190
protected static int getEnumeratedAttribute(Node JavaDoc node,
191                                                 String JavaDoc name,
192                                                 String JavaDoc[] legalNames)
193       throws IIOInvalidTreeException JavaDoc {
194         return getEnumeratedAttribute(node, name, legalNames, -1, true);
195     }
196
197     // Get a String-valued attribute
198
protected static String JavaDoc getAttribute(Node JavaDoc node, String JavaDoc name,
199                                          String JavaDoc defaultValue, boolean required)
200       throws IIOInvalidTreeException JavaDoc {
201         Node JavaDoc 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     // Get a required String-valued attribute
213
protected static String JavaDoc getAttribute(Node JavaDoc node, String JavaDoc name)
214       throws IIOInvalidTreeException JavaDoc {
215         return getAttribute(node, name, null, true);
216     }
217
218     protected GIFMetadata(boolean standardMetadataFormatSupported,
219                           String JavaDoc nativeMetadataFormatName,
220                           String JavaDoc nativeMetadataFormatClassName,
221                           String JavaDoc[] extraMetadataFormatNames,
222                           String JavaDoc[] extraMetadataFormatClassNames) {
223         super(standardMetadataFormatSupported,
224               nativeMetadataFormatName,
225               nativeMetadataFormatClassName,
226               extraMetadataFormatNames,
227               extraMetadataFormatClassNames);
228     }
229
230     public void mergeTree(String JavaDoc formatName, Node JavaDoc root)
231       throws IIOInvalidTreeException JavaDoc {
232         if (formatName.equals(nativeMetadataFormatName)) {
233             if (root == null) {
234                 throw new IllegalArgumentException JavaDoc("root == null!");
235             }
236             mergeNativeTree(root);
237         } else if (formatName.equals
238                   (IIOMetadataFormatImpl.standardMetadataFormatName)) {
239             if (root == null) {
240                 throw new IllegalArgumentException JavaDoc("root == null!");
241             }
242             mergeStandardTree(root);
243         } else {
244             throw new IllegalArgumentException JavaDoc("Not a recognized format!");
245         }
246     }
247
248     protected byte[] getColorTable(Node JavaDoc colorTableNode,
249                                    String JavaDoc entryNodeName,
250                                    boolean lengthExpected,
251                                    int expectedLength)
252       throws IIOInvalidTreeException JavaDoc {
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 JavaDoc 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 JavaDoc root)
298       throws IIOInvalidTreeException JavaDoc;
299
300    protected abstract void mergeStandardTree(Node JavaDoc root)
301       throws IIOInvalidTreeException JavaDoc;
302 }
303
Popular Tags