/* * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.imageio.metadata; import java.util.ArrayList;import java.util.Iterator;import java.util.List; import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.DOMException;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.TypeInfo;import org.w3c.dom.UserDataHandler; class IIODOMException extends DOMException { public IIODOMException(short code, String message) { super(code, message); }} class IIONamedNodeMap implements NamedNodeMap { List nodes; public IIONamedNodeMap(List nodes) { this.nodes = nodes; } public int getLength() { return nodes.size(); } public Node getNamedItem(String name) { Iterator iter = nodes.iterator(); while (iter.hasNext()) { Node node = (Node)iter.next(); if (name.equals(node.getNodeName())) { return node; } } return null; } public Node item(int index) { Node node = (Node)nodes.get(index); return node; } public Node removeNamedItem(java.lang.String name) { throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "This NamedNodeMap is read-only!"); } public Node setNamedItem(Node arg) { throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "This NamedNodeMap is read-only!"); } /** * Equivalent to <code>getNamedItem(localName)</code>. */ public Node getNamedItemNS(String namespaceURI, String localName) { return getNamedItem(localName); } /** * Equivalent to <code>setNamedItem(arg)</code>. */ public Node setNamedItemNS(Node arg) { return setNamedItem(arg); } /** * Equivalent to <code>removeNamedItem(localName)</code>. */ public Node removeNamedItemNS(String namespaceURI, String localName) { return removeNamedItem(localName); }} class IIONodeList implements NodeList { List nodes; public IIONodeList(List nodes) { this.nodes = nodes; } public int getLength() { return nodes.size(); } public Node item(int index) { if (index < 0 || index > nodes.size()) { return null; } return (Node)nodes.get(index); }} class IIOAttr extends IIOMetadataNode implements Attr { Element owner; String name; String value; public IIOAttr(Element owner, String name, String value) { this.owner = owner; this.name = name; this.value = value; } public String getName() { return name; } public String getNodeName() { return name; } public short getNodeType() { return ATTRIBUTE_NODE; } public boolean getSpecified() { return true; } public String getValue() { return value; } public String getNodeValue() { return value; } public void setValue(String value) { this.value = value; } public void setNodeValue(String value) { this.value = value; } public Element getOwnerElement() { return owner; } public void setOwnerElement(Element owner) { this.owner = owner; } /** This method is new in the DOM L3 for Attr interface. * Could throw DOMException here, but its probably OK * to always return false. One reason for this, is we have no good * way to document this exception, since this class, IIOAttr, * is not a public class. The rest of the methods that throw * DOMException are publically documented as such on IIOMetadataNode. * @return false */ public boolean isId() { return false; } } /** * A class representing a node in a meta-data tree, which implements * the <a * href="../../../../api/org/w3c/dom/Element.html"> * <code>org.w3c.dom.Element</code></a> interface and additionally allows * for the storage of non-textual objects via the * <code>getUserObject</code> and <code>setUserObject</code> methods. * * <p> This class is not intended to be used for general XML * processing. In particular, <code>Element</code> nodes created * within the Image I/O API are not compatible with those created by * Sun's standard implementation of the <code>org.w3.dom</code> API. * In particular, the implementation is tuned for simple uses and may * not perform well for intensive processing. * * <p> Namespaces are ignored in this implementation. The terms "tag * name" and "node name" are always considered to be synonymous. * * <em>Note:</em> * The DOM Level 3 specification added a number of new methods to the * {@code Node}, {@code Element} and {@code Attr} interfaces that are not * of value to the {@code IIOMetadataNode} implementation or specification. * * Calling such methods on an {@code IIOMetadataNode}, or an {@code Attr} * instance returned from an {@code IIOMetadataNode} will result in a * {@code DOMException} being thrown. * * @see IIOMetadata#getAsTree * @see IIOMetadata#setFromTree * @see IIOMetadata#mergeTree * */public class IIOMetadataNode implements Element, NodeList { /** * The name of the node as a <code>String</code>. */ private String nodeName = null; /** * The value of the node as a <code>String</code>. The Image I/O * API typically does not make use of the node value. */ private String nodeValue = null; /** * The <code>Object</code> value associated with this node. */ private Object userObject = null; /** * The parent node of this node, or <code>null</code> if this node * forms the root of its own tree. */ private IIOMetadataNode parent = null; /** * The number of child nodes. */ private int numChildren = 0; /** * The first (leftmost) child node of this node, or * <code>null</code> if this node is a leaf node. */ private IIOMetadataNode firstChild = null; /** * The last (rightmost) child node of this node, or * <code>null</code> if this node is a leaf node. */ private IIOMetadataNode lastChild = null; /** * The next (right) sibling node of this node, or * <code>null</code> if this node is its parent's last child node. */ private IIOMetadataNode nextSibling = null; /** * The previous (left) sibling node of this node, or * <code>null</code> if this node is its parent's first child node. */ private IIOMetadataNode previousSibling = null; /** * A <code>List</code> of <code>IIOAttr</code> nodes representing * attributes. */ private List attributes = new ArrayList(); /** * Constructs an empty <code>IIOMetadataNode</code>. */ public IIOMetadataNode() {} /** * Constructs an <code>IIOMetadataNode</code> with a given node * name. * * @param nodeName the name of the node, as a <code>String</code>. */ public IIOMetadataNode(String nodeName) { this.nodeName = nodeName; } /** * Check that the node is either <code>null</code> or an * <code>IIOMetadataNode</code>. */ private void checkNode(Node node) throws DOMException { if (node == null) { return; } if (!(node instanceof IIOMetadataNode)) { throw new IIODOMException(DOMException.WRONG_DOCUMENT_ERR, "Node not an IIOMetadataNode!"); } } // Methods from Node /** * Returns the node name associated with this node. * * @return the node name, as a <code>String</code>. */ public String getNodeName() { return nodeName; } /** * Returns the value associated with this node. * * @return the node value, as a <code>String</code>. */ public String getNodeValue(){ return nodeValue; } /** * Sets the <code>String</code> value associated with this node. */ public void setNodeValue(String nodeValue) { this.nodeValue = nodeValue; } /** * Returns the node type, which is always * <code>ELEMENT_NODE</code>. * * @return the <code>short</code> value <code>ELEMENT_NODE</code>. */ public short getNodeType() { return ELEMENT_NODE; } /** * Returns the parent of this node. A <code>null</code> value * indicates that the node is the root of its own tree. To add a * node to an existing tree, use one of the * <code>insertBefore</code>, <code>replaceChild</code>, or * <code>appendChild</code> methods. * * @return the parent, as a <code>Node</code>. * * @see #insertBefore * @see #replaceChild * @see #appendChild */ public Node getParentNode() { return parent; } /** * Returns a <code>NodeList</code> that contains all children of this node. * If there are no children, this is a <code>NodeList</code> containing * no nodes. * * @return the children as a <code>NodeList</code> */ public NodeList getChildNodes() { return this; } /** * Returns the first child of this node, or <code>null</code> if * the node has no children. * * @return the first child, as a <code>Node</code>, or * <code>null</code> */ public Node getFirstChild() { return firstChild; } /** * Returns the last child of this node, or <code>null</code> if * the node has no children. * * @return the last child, as a <code>Node</code>, or * <code>null</code>. */ public Node getLastChild() { return lastChild; } /** * Returns the previous sibling of this node, or <code>null</code> * if this node has no previous sibling. * * @return the previous sibling, as a <code>Node</code>, or * <code>null</code>. */ public Node getPreviousSibling() { return previousSibling; } /** * Returns the next sibling of this node, or <code>null</code> if * the node has no next sibling. * * @return the next sibling, as a <code>Node</code>, or * <code>null</code>. */ public Node getNextSibling() { return nextSibling; } /** * Returns a <code>NamedNodeMap</code> containing the attributes of * this node. * * @return a <code>NamedNodeMap</code> containing the attributes of * this node. */ public NamedNodeMap getAttributes() { return new IIONamedNodeMap(attributes); } /** * Returns <code>null</code>, since <code>IIOMetadataNode</code>s * do not belong to any <code>Document</code>. * * @return <code>null</code>. */ public Document getOwnerDocument() { return null; } /** * Inserts the node <code>newChild</code> before the existing * child node <code>refChild</code>. If <code>refChild</code> is * <code>null</code>, insert <code>newChild</code> at the end of * the list of children. * * @param newChild the <code>Node</code> to insert. * @param refChild the reference <code>Node</code>. * * @return the node being inserted. * * @exception IllegalArgumentException if <code>newChild</code> is * <code>null</code>. */ public Node insertBefore(Node newChild, Node refChild) { if (newChild == null) { throw new IllegalArgumentException("newChild == null!"); } checkNode(newChild); checkNode(refChild); IIOMetadataNode newChildNode = (IIOMetadataNode)newChild; IIOMetadataNode refChildNode = (IIOMetadataNode)refChild; // Siblings, can be null. IIOMetadataNode previous = null; IIOMetadataNode next = null; if (refChild == null) { previous = this.lastChild; next = null; this.lastChild = newChildNode; } else { previous = refChildNode.previousSibling; next = refChildNode; } if (previous != null) { previous.nextSibling = newChildNode; } if (next != null) { next.previousSibling = newChildNode; } newChildNode.parent = this; newChildNode.previousSibling = previous; newChildNode.nextSibling = next; // N.B.: O.K. if refChild == null if (this.firstChild == refChildNode) { this.firstChild = newChildNode;Lines 1–500 of 1132. Load more
/* * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.imageio.metadata; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.DOMException; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.TypeInfo; import org.w3c.dom.UserDataHandler; class IIODOMException extends DOMException { public IIODOMException(short code, String message) { super(code, message); } } class IIONamedNodeMap implements NamedNodeMap { List nodes; public IIONamedNodeMap(List nodes) { this.nodes = nodes; } public int getLength() { return nodes.size(); } public Node getNamedItem(String name) { Iterator iter = nodes.iterator(); while (iter.hasNext()) { Node node = (Node)iter.next(); if (name.equals(node.getNodeName())) { return node; } } return null; } public Node item(int index) { Node node = (Node)nodes.get(index); return node; } public Node removeNamedItem(java.lang.String name) { throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "This NamedNodeMap is read-only!"); } public Node setNamedItem(Node arg) { throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "This NamedNodeMap is read-only!"); } /** * Equivalent to <code>getNamedItem(localName)</code>. */ public Node getNamedItemNS(String namespaceURI, String localName) { return getNamedItem(localName); } /** * Equivalent to <code>setNamedItem(arg)</code>. */ public Node setNamedItemNS(Node arg) { return setNamedItem(arg); } /** * Equivalent to <code>removeNamedItem(localName)</code>. */ public Node removeNamedItemNS(String namespaceURI, String localName) { return removeNamedItem(localName); } } class IIONodeList implements NodeList { List nodes; public IIONodeList(List nodes) { this.nodes = nodes; } public int getLength() { return nodes.size(); } public Node item(int index) { if (index < 0 || index > nodes.size()) { return null; } return (Node)nodes.get(index); } } class IIOAttr extends IIOMetadataNode implements Attr { Element owner; String name; String value; public IIOAttr(Element owner, String name, String value) { this.owner = owner; this.name = name; this.value = value; } public String getName() { return name; } public String getNodeName() { return name; } public short getNodeType() { return ATTRIBUTE_NODE; } public boolean getSpecified() { return true; } public String getValue() { return value; } public String getNodeValue() { return value; } public void setValue(String value) { this.value = value; } public void setNodeValue(String value) { this.value = value; } public Element getOwnerElement() { return owner; } public void setOwnerElement(Element owner) { this.owner = owner; } /** This method is new in the DOM L3 for Attr interface. * Could throw DOMException here, but its probably OK * to always return false. One reason for this, is we have no good * way to document this exception, since this class, IIOAttr, * is not a public class. The rest of the methods that throw * DOMException are publically documented as such on IIOMetadataNode. * @return false */ public boolean isId() { return false; } } /** * A class representing a node in a meta-data tree, which implements * the <a * href="../../../../api/org/w3c/dom/Element.html"> * <code>org.w3c.dom.Element</code></a> interface and additionally allows * for the storage of non-textual objects via the * <code>getUserObject</code> and <code>setUserObject</code> methods. * * <p> This class is not intended to be used for general XML * processing. In particular, <code>Element</code> nodes created * within the Image I/O API are not compatible with those created by * Sun's standard implementation of the <code>org.w3.dom</code> API. * In particular, the implementation is tuned for simple uses and may * not perform well for intensive processing. * * <p> Namespaces are ignored in this implementation. The terms "tag * name" and "node name" are always considered to be synonymous. * * <em>Note:</em> * The DOM Level 3 specification added a number of new methods to the * {@code Node}, {@code Element} and {@code Attr} interfaces that are not * of value to the {@code IIOMetadataNode} implementation or specification. * * Calling such methods on an {@code IIOMetadataNode}, or an {@code Attr} * instance returned from an {@code IIOMetadataNode} will result in a * {@code DOMException} being thrown. * * @see IIOMetadata#getAsTree * @see IIOMetadata#setFromTree * @see IIOMetadata#mergeTree * */ public class IIOMetadataNode implements Element, NodeList { /** * The name of the node as a <code>String</code>. */ private String nodeName = null; /** * The value of the node as a <code>String</code>. The Image I/O * API typically does not make use of the node value. */ private String nodeValue = null; /** * The <code>Object</code> value associated with this node. */ private Object userObject = null; /** * The parent node of this node, or <code>null</code> if this node * forms the root of its own tree. */ private IIOMetadataNode parent = null; /** * The number of child nodes. */ private int numChildren = 0; /** * The first (leftmost) child node of this node, or * <code>null</code> if this node is a leaf node. */ private IIOMetadataNode firstChild = null; /** * The last (rightmost) child node of this node, or * <code>null</code> if this node is a leaf node. */ private IIOMetadataNode lastChild = null; /** * The next (right) sibling node of this node, or * <code>null</code> if this node is its parent's last child node. */ private IIOMetadataNode nextSibling = null; /** * The previous (left) sibling node of this node, or * <code>null</code> if this node is its parent's first child node. */ private IIOMetadataNode previousSibling = null; /** * A <code>List</code> of <code>IIOAttr</code> nodes representing * attributes. */ private List attributes = new ArrayList(); /** * Constructs an empty <code>IIOMetadataNode</code>. */ public IIOMetadataNode() {} /** * Constructs an <code>IIOMetadataNode</code> with a given node * name. * * @param nodeName the name of the node, as a <code>String</code>. */ public IIOMetadataNode(String nodeName) { this.nodeName = nodeName; } /** * Check that the node is either <code>null</code> or an * <code>IIOMetadataNode</code>. */ private void checkNode(Node node) throws DOMException { if (node == null) { return; } if (!(node instanceof IIOMetadataNode)) { throw new IIODOMException(DOMException.WRONG_DOCUMENT_ERR, "Node not an IIOMetadataNode!"); } } // Methods from Node /** * Returns the node name associated with this node. * * @return the node name, as a <code>String</code>. */ public String getNodeName() { return nodeName; } /** * Returns the value associated with this node. * * @return the node value, as a <code>String</code>. */ public String getNodeValue(){ return nodeValue; } /** * Sets the <code>String</code> value associated with this node. */ public void setNodeValue(String nodeValue) { this.nodeValue = nodeValue; } /** * Returns the node type, which is always * <code>ELEMENT_NODE</code>. * * @return the <code>short</code> value <code>ELEMENT_NODE</code>. */ public short getNodeType() { return ELEMENT_NODE; } /** * Returns the parent of this node. A <code>null</code> value * indicates that the node is the root of its own tree. To add a * node to an existing tree, use one of the * <code>insertBefore</code>, <code>replaceChild</code>, or * <code>appendChild</code> methods. * * @return the parent, as a <code>Node</code>. * * @see #insertBefore * @see #replaceChild * @see #appendChild */ public Node getParentNode() { return parent; } /** * Returns a <code>NodeList</code> that contains all children of this node. * If there are no children, this is a <code>NodeList</code> containing * no nodes. * * @return the children as a <code>NodeList</code> */ public NodeList getChildNodes() { return this; } /** * Returns the first child of this node, or <code>null</code> if * the node has no children. * * @return the first child, as a <code>Node</code>, or * <code>null</code> */ public Node getFirstChild() { return firstChild; } /** * Returns the last child of this node, or <code>null</code> if * the node has no children. * * @return the last child, as a <code>Node</code>, or * <code>null</code>. */ public Node getLastChild() { return lastChild; } /** * Returns the previous sibling of this node, or <code>null</code> * if this node has no previous sibling. * * @return the previous sibling, as a <code>Node</code>, or * <code>null</code>. */ public Node getPreviousSibling() { return previousSibling; } /** * Returns the next sibling of this node, or <code>null</code> if * the node has no next sibling. * * @return the next sibling, as a <code>Node</code>, or * <code>null</code>. */ public Node getNextSibling() { return nextSibling; } /** * Returns a <code>NamedNodeMap</code> containing the attributes of * this node. * * @return a <code>NamedNodeMap</code> containing the attributes of * this node. */ public NamedNodeMap getAttributes() { return new IIONamedNodeMap(attributes); } /** * Returns <code>null</code>, since <code>IIOMetadataNode</code>s * do not belong to any <code>Document</code>. * * @return <code>null</code>. */ public Document getOwnerDocument() { return null; } /** * Inserts the node <code>newChild</code> before the existing * child node <code>refChild</code>. If <code>refChild</code> is * <code>null</code>, insert <code>newChild</code> at the end of * the list of children. * * @param newChild the <code>Node</code> to insert. * @param refChild the reference <code>Node</code>. * * @return the node being inserted. * * @exception IllegalArgumentException if <code>newChild</code> is * <code>null</code>. */ public Node insertBefore(Node newChild, Node refChild) { if (newChild == null) { throw new IllegalArgumentException("newChild == null!"); } checkNode(newChild); checkNode(refChild); IIOMetadataNode newChildNode = (IIOMetadataNode)newChild; IIOMetadataNode refChildNode = (IIOMetadataNode)refChild; // Siblings, can be null. IIOMetadataNode previous = null; IIOMetadataNode next = null; if (refChild == null) { previous = this.lastChild; next = null; this.lastChild = newChildNode; } else { previous = refChildNode.previousSibling; next = refChildNode; } if (previous != null) { previous.nextSibling = newChildNode; } if (next != null) { next.previousSibling = newChildNode; } newChildNode.parent = this; newChildNode.previousSibling = previous; newChildNode.nextSibling = next; // N.B.: O.K. if refChild == null if (this.firstChild == refChildNode) { this.firstChild = newChildNode;