FileDocCategorySizeDatePackage
GIFMetadata.javaAPI DocJava SE 6 API11045Tue Jun 10 00:21:52 BST 2008com.sun.imageio.plugins.gif

GIFMetadata

public abstract class GIFMetadata extends IIOMetadata
Class which adds utility DOM element attribute access methods to IIOMetadata for subclass use.

Fields Summary
static final int
UNDEFINED_INTEGER_VALUE
Represents an undefined value of integer attributes.
Constructors Summary
protected GIFMetadata(boolean standardMetadataFormatSupported, String nativeMetadataFormatName, String nativeMetadataFormatClassName, String[] extraMetadataFormatNames, String[] extraMetadataFormatClassNames)

        super(standardMetadataFormatSupported,
              nativeMetadataFormatName,
              nativeMetadataFormatClassName,
              extraMetadataFormatNames,
              extraMetadataFormatClassNames);
    
Methods Summary
protected static voidfatal(org.w3c.dom.Node node, java.lang.String reason)


    //
    // Note: These attribute methods were shamelessly lifted from
    // com.sun.imageio.plugins.png.PNGMetadata and modified.
    //

    // Shorthand for throwing an IIOInvalidTreeException
          
        
        throw new IIOInvalidTreeException(reason, node);
    
protected static java.lang.StringgetAttribute(org.w3c.dom.Node node, java.lang.String name, java.lang.String defaultValue, boolean required)

        Node attr = node.getAttributes().getNamedItem(name);
        if (attr == null) {
            if (!required) {
                return defaultValue;
            } else {
                fatal(node, "Required attribute " + name + " not present!");
            }
        }
        return attr.getNodeValue();
    
protected static java.lang.StringgetAttribute(org.w3c.dom.Node node, java.lang.String name)

        return getAttribute(node, name, null, true);
    
protected static booleangetBooleanAttribute(org.w3c.dom.Node node, java.lang.String name, boolean defaultValue, boolean required)

        Node attr = node.getAttributes().getNamedItem(name);
        if (attr == null) {
            if (!required) {
                return defaultValue;
            } else {
                fatal(node, "Required attribute " + name + " not present!");
            }
        }
        String value = attr.getNodeValue();
        // XXX Should be able to use equals() here instead of
        // equalsIgnoreCase() but some boolean attributes are incorrectly
        // set to "true" or "false" by the J2SE core metadata classes
        // getAsTree() method (which are duplicated above). See bug 5082756.
        if (value.equalsIgnoreCase("TRUE")) {
            return true;
        } else if (value.equalsIgnoreCase("FALSE")) {
            return false;
        } else {
            fatal(node, "Attribute " + name + " must be 'TRUE' or 'FALSE'!");
            return false;
        }
    
protected static booleangetBooleanAttribute(org.w3c.dom.Node node, java.lang.String name)

        return getBooleanAttribute(node, name, false, true);
    
protected byte[]getColorTable(org.w3c.dom.Node colorTableNode, java.lang.String entryNodeName, boolean lengthExpected, int expectedLength)

        byte[] red = new byte[256];
        byte[] green  = new byte[256];
        byte[] blue = new byte[256];
        int maxIndex = -1;

        Node entry = colorTableNode.getFirstChild();
        if (entry == null) {
            fatal(colorTableNode, "Palette has no entries!");
        }

        while (entry != null) {
            if (!entry.getNodeName().equals(entryNodeName)) {
                fatal(colorTableNode,
                      "Only a "+entryNodeName+" may be a child of a "+
                      entry.getNodeName()+"!");
            }

            int index = getIntAttribute(entry, "index", true, 0, 255);
            if (index > maxIndex) {
                maxIndex = index;
            }
            red[index] = (byte)getIntAttribute(entry, "red", true, 0, 255);
            green[index] = (byte)getIntAttribute(entry, "green", true, 0, 255);
            blue[index] = (byte)getIntAttribute(entry, "blue", true, 0, 255);

            entry = entry.getNextSibling();
        }

        int numEntries = maxIndex + 1;

        if (lengthExpected && numEntries != expectedLength) {
            fatal(colorTableNode, "Unexpected length for palette!");
        }

        byte[] colorTable = new byte[3*numEntries];
        for (int i = 0, j = 0; i < numEntries; i++) {
            colorTable[j++] = red[i];
            colorTable[j++] = green[i];
            colorTable[j++] = blue[i];
        }

        return colorTable;
    
protected static intgetEnumeratedAttribute(org.w3c.dom.Node node, java.lang.String name, java.lang.String[] legalNames)

        return getEnumeratedAttribute(node, name, legalNames, -1, true);
    
protected static intgetEnumeratedAttribute(org.w3c.dom.Node node, java.lang.String name, java.lang.String[] legalNames, int defaultValue, boolean required)

        Node attr = node.getAttributes().getNamedItem(name);
        if (attr == null) {
            if (!required) {
                return defaultValue;
            } else {
                fatal(node, "Required attribute " + name + " not present!");
            }
        }
        String value = attr.getNodeValue();
        for (int i = 0; i < legalNames.length; i++) {
            if(value.equals(legalNames[i])) {
                return i;
            }
        }

        fatal(node, "Illegal value for attribute " + name + "!");
        return -1;
    
protected static floatgetFloatAttribute(org.w3c.dom.Node node, java.lang.String name, float defaultValue, boolean required)

        String value = getStringAttribute(node, name, null, required, null);
        if (value == null) {
            return defaultValue;
        }
        return Float.parseFloat(value);
    
protected static floatgetFloatAttribute(org.w3c.dom.Node node, java.lang.String name)

        return getFloatAttribute(node, name, -1.0F, true);
    
protected static intgetIntAttribute(org.w3c.dom.Node node, java.lang.String name, int defaultValue, boolean required, boolean bounded, int min, int max)

        String value = getStringAttribute(node, name, null, required, null);
        if (value == null || "".equals(value)) {
            return defaultValue;
        }
        
        int intValue = defaultValue;
        try {
            intValue = Integer.parseInt(value);
        } catch (NumberFormatException e) {
            fatal(node,
                  "Bad value for "+node.getNodeName()+
                  " attribute "+name+"!");
        }
        if (bounded && (intValue < min || intValue > max)) {
            fatal(node,
                  "Bad value for "+node.getNodeName()+
                  " attribute "+name+"!");
        }
        return intValue;
    
protected static intgetIntAttribute(org.w3c.dom.Node node, java.lang.String name, boolean bounded, int min, int max)

        return getIntAttribute(node, name, -1, true, bounded, min, max);
    
protected static java.lang.StringgetStringAttribute(org.w3c.dom.Node node, java.lang.String name, java.lang.String defaultValue, boolean required, java.lang.String[] range)

        Node attr = node.getAttributes().getNamedItem(name);
        if (attr == null) {
            if (!required) {
                return defaultValue;
            } else {
                fatal(node, "Required attribute " + name + " not present!");
            }
        }
        String value = attr.getNodeValue();

        if (range != null) {
            if (value == null) {
                fatal(node,
                      "Null value for "+node.getNodeName()+
                      " attribute "+name+"!");
            }
            boolean validValue = false;
            int len = range.length;
            for (int i = 0; i < len; i++) {
                if (value.equals(range[i])) {
                    validValue = true;
                    break;
                }
            }
            if (!validValue) {
                fatal(node,
                      "Bad value for "+node.getNodeName()+
                      " attribute "+name+"!");
            }
        }

        return value;
    
protected abstract voidmergeNativeTree(org.w3c.dom.Node root)

protected abstract voidmergeStandardTree(org.w3c.dom.Node root)

public voidmergeTree(java.lang.String formatName, org.w3c.dom.Node root)

        if (formatName.equals(nativeMetadataFormatName)) {
            if (root == null) {
                throw new IllegalArgumentException("root == null!");
            }
            mergeNativeTree(root);
        } else if (formatName.equals
                  (IIOMetadataFormatImpl.standardMetadataFormatName)) {
            if (root == null) {
                throw new IllegalArgumentException("root == null!");
            }
            mergeStandardTree(root);
        } else {
            throw new IllegalArgumentException("Not a recognized format!");
        }