elementimpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,165 行 · 第 1/3 页
JAVA
1,165 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package com.sun.org.apache.xerces.internal.dom;import org.w3c.dom.Attr;import org.w3c.dom.DOMException;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;import org.w3c.dom.TypeInfo;import com.sun.org.apache.xerces.internal.util.URI;/** * Elements represent most of the "markup" and structure of the * document. They contain both the data for the element itself * (element name and attributes), and any contained nodes, including * document text (as children). * <P> * Elements may have Attributes associated with them; the API for this is * defined in Node, but the function is implemented here. In general, XML * applications should retrive Attributes as Nodes, since they may contain * entity references and hence be a fairly complex sub-tree. HTML users will * be dealing with simple string values, and convenience methods are provided * to work in terms of Strings. * <P> * ElementImpl does not support Namespaces. ElementNSImpl, which inherits from * it, does. * @see ElementNSImpl * * @author Arnaud Le Hors, IBM * @author Joe Kesselman, IBM * @author Andy Clark, IBM * @author Ralf Pfeiffer, IBM * @version $Id: ElementImpl.java,v 1.1.2.2 2006/10/11 19:21:01 spericas Exp $ * @since PR-DOM-Level-1-19980818. */public class ElementImpl extends ParentNode implements Element { // // Constants // /** Serialization version. */ static final long serialVersionUID = 3717253516652722278L; // // Data // /** Element name. */ protected String name; /** Attributes. */ protected AttributeMap attributes; /** DOM3: type information */ // REVISIT: we are losing the type information in DOM during serialization transient TypeInfo type; // // Constructors // /** Factory constructor. */ public ElementImpl(CoreDocumentImpl ownerDoc, String name) { super(ownerDoc); this.name = name; needsSyncData(true); // synchronizeData will initialize attributes } // for ElementNSImpl protected ElementImpl() {} // Support for DOM Level 3 renameNode method. // Note: This only deals with part of the pb. CoreDocumentImpl // does all the work. void rename(String name) { if (needsSyncData()) { synchronizeData(); } this.name = name; reconcileDefaultAttributes(); } // // Node methods // /** * A short integer indicating what type of node this is. The named * constants for this value are defined in the org.w3c.dom.Node interface. */ public short getNodeType() { return Node.ELEMENT_NODE; } /** * Returns the element name */ public String getNodeName() { if (needsSyncData()) { synchronizeData(); } return name; } /** * Retrieve all the Attributes as a set. Note that this API is inherited * from Node rather than specified on Element; in fact only Elements will * ever have Attributes, but they want to allow folks to "blindly" operate * on the tree as a set of Nodes. */ public NamedNodeMap getAttributes() { if (needsSyncData()) { synchronizeData(); } if (attributes == null) { attributes = new AttributeMap(this, null); } return attributes; } // getAttributes():NamedNodeMap /** * Return a duplicate copy of this Element. Note that its children * will not be copied unless the "deep" flag is true, but Attributes * are <i>always</i> replicated. * * @see org.w3c.dom.Node#cloneNode(boolean) */ public Node cloneNode(boolean deep) { ElementImpl newnode = (ElementImpl) super.cloneNode(deep); // Replicate NamedNodeMap rather than sharing it. if (attributes != null) { newnode.attributes = (AttributeMap) attributes.cloneMap(newnode); } return newnode; } // cloneNode(boolean):Node /** * DOM Level 3 WD - Experimental. * Retrieve baseURI */ public String getBaseURI() { if (needsSyncData()) { synchronizeData(); } // Absolute base URI is computed according to // XML Base (http://www.w3.org/TR/xmlbase/#granularity) // 1. The base URI specified by an xml:base attribute on the element, // if one exists if (attributes != null) { Attr attrNode = (Attr)attributes.getNamedItem("xml:base"); if (attrNode != null) { String uri = attrNode.getNodeValue(); if (uri.length() != 0 ) {// attribute value is always empty string try { uri = new URI(uri).toString(); } catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){ return null; } return uri; } } } // 2.the base URI of the element's parent element within the // document or external entity, if one exists // 3. the base URI of the document entity or external entity // containing the element // ownerNode serves as a parent or as document String baseURI = (this.ownerNode != null) ? this.ownerNode.getBaseURI() : null ; //base URI of parent element is not null if(baseURI != null){ try { //return valid absolute base URI return new URI(baseURI).toString(); } catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){ return null; } } return null; } //getBaseURI /** * NON-DOM * set the ownerDocument of this node, its children, and its attributes */ void setOwnerDocument(CoreDocumentImpl doc) { super.setOwnerDocument(doc); if (attributes != null) { attributes.setOwnerDocument(doc); } } // // Element methods // /** * Look up a single Attribute by name. Returns the Attribute's * string value, or an empty string (NOT null!) to indicate that the * name did not map to a currently defined attribute. * <p> * Note: Attributes may contain complex node trees. This method * returns the "flattened" string obtained from Attribute.getValue(). * If you need the structure information, see getAttributeNode(). */ public String getAttribute(String name) { if (needsSyncData()) { synchronizeData(); } if (attributes == null) { return ""; } Attr attr = (Attr)(attributes.getNamedItem(name)); return (attr == null) ? "" : attr.getValue(); } // getAttribute(String):String /** * Look up a single Attribute by name. Returns the Attribute Node, * so its complete child tree is available. This could be important in * XML, where the string rendering may not be sufficient information. * <p> * If no matching attribute is available, returns null. */ public Attr getAttributeNode(String name) { if (needsSyncData()) { synchronizeData(); } if (attributes == null) { return null; } return (Attr)attributes.getNamedItem(name); } // getAttributeNode(String):Attr /** * Returns a NodeList of all descendent nodes (children, * grandchildren, and so on) which are Elements and which have the * specified tag name. * <p> * Note: NodeList is a "live" view of the DOM. Its contents will * change as the DOM changes, and alterations made to the NodeList * will be reflected in the DOM. * * @param tagname The type of element to gather. To obtain a list of * all elements no matter what their names, use the wild-card tag * name "*". * * @see DeepNodeListImpl */ public NodeList getElementsByTagName(String tagname) { return new DeepNodeListImpl(this,tagname); } /** * Returns the name of the Element. Note that Element.nodeName() is * defined to also return the tag name. * <p> * This is case-preserving in XML. HTML should uppercasify it on the * way in. */ public String getTagName() { if (needsSyncData()) { synchronizeData(); } return name; } /** * In "normal form" (as read from a source file), there will never be two * Text children in succession. But DOM users may create successive Text * nodes in the course of manipulating the document. Normalize walks the * sub-tree and merges adjacent Texts, as if the DOM had been written out * and read back in again. This simplifies implementation of higher-level * functions that may want to assume that the document is in standard form. * <p> * To normalize a Document, normalize its top-level Element child. * <p> * As of PR-DOM-Level-1-19980818, CDATA -- despite being a subclass of * Text -- is considered "markup" and will _not_ be merged either with * normal Text or with other CDATASections. */ public void normalize() { // No need to normalize if already normalized. if (isNormalized()) { return; } if (needsSyncChildren()) { synchronizeChildren(); } ChildNode kid, next; for (kid = firstChild; kid != null; kid = next) { next = kid.nextSibling; // If kid is a text node, we need to check for one of two // conditions: // 1) There is an adjacent text node // 2) There is no adjacent text node, but kid is // an empty text node. if ( kid.getNodeType() == Node.TEXT_NODE ) { // If an adjacent text node, merge it with kid if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) { ((Text)kid).appendData(next.getNodeValue()); removeChild( next ); next = kid; // Don't advance; there might be another. } else { // If kid is empty, remove it if ( kid.getNodeValue().length()==0 ) removeChild( kid );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?