⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 domdocument.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* DomDocument.java --    Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package gnu.xml.dom;import java.util.Iterator;import javax.xml.XMLConstants;import org.w3c.dom.Attr;import org.w3c.dom.CDATASection;import org.w3c.dom.Comment;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.DocumentType;import org.w3c.dom.DOMConfiguration;import org.w3c.dom.DOMImplementation;import org.w3c.dom.DOMException;import org.w3c.dom.Element;import org.w3c.dom.Entity;import org.w3c.dom.EntityReference;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.Notation;import org.w3c.dom.ProcessingInstruction;import org.w3c.dom.Text;import org.w3c.dom.UserDataHandler;import org.w3c.dom.traversal.DocumentTraversal;import org.w3c.dom.traversal.NodeFilter;import org.w3c.dom.traversal.NodeIterator;import org.w3c.dom.traversal.TreeWalker;import org.w3c.dom.xpath.XPathEvaluator;import org.w3c.dom.xpath.XPathException;import org.w3c.dom.xpath.XPathExpression;import org.w3c.dom.xpath.XPathNSResolver;/** * <p> "Document" and "DocumentTraversal" implementation. * * <p> Note that when this checks names for legality, it uses an * approximation of the XML rules, not the real ones.  Specifically, * it uses Unicode rules, with sufficient tweaks to pass a majority * of basic XML conformance tests.  (The huge XML character tables are * hairy to implement.) * * @author David Brownell  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */public class DomDocument  extends DomNode  implements Document, DocumentTraversal, XPathEvaluator{  private final DOMImplementation implementation;  private boolean checkingCharacters = true;  boolean checkingWellformedness = true;  boolean building; // if true, skip mutation events in the tree    DomDocumentConfiguration config;  String inputEncoding;  String encoding;  String version = "1.0";  boolean standalone;  String systemId;    /**   * Constructs a Document node, associating it with an instance   * of the DomImpl class.   *   * <p> Note that this constructor disables character checking.   * It is normally used when connecting a DOM to an XML parser,   * and duplicating such checks is undesirable.  When used for   * purposes other than connecting to a parser, you should   * re-enable that checking.   *   * @see #setCheckingCharacters   */  public DomDocument()  {    this(new DomImpl());  }    /**   * Constructs a Document node, associating it with the specified   * implementation.  This should only be used in conjunction with   * a specialized implementation; it will normally be called by   * that implementation.   *   * @see DomImpl   * @see #setCheckingCharacters   */  protected DomDocument(DOMImplementation impl)  {    super(DOCUMENT_NODE, null);    implementation = impl;  }  /**   * Sets the <code>building</code> flag.   * Mutation events in the document are not reported.   */  public void setBuilding(boolean flag)  {    building = flag;  }  /**   * Sets whether to check for document well-formedness.   * If true, an exception will be raised if a second doctype or root   * element node is added to the document.   */  public void setCheckWellformedness(boolean flag)  {    checkingWellformedness = flag;  }  /**   * <b>DOM L1</b>   * Returns the constant "#document".   */  final public String getNodeName()  {    return "#document";  }  /**   * <b>DOM L1</b>   * Returns the document's root element, or null.   */  final public Element getDocumentElement()  {    for (DomNode ctx = first; ctx != null; ctx = ctx.next)      {        if (ctx.nodeType == ELEMENT_NODE)          {            return (Element) ctx;          }      }    return null;  }  /**   * <b>DOM L1</b>   * Returns the document's DocumentType, or null.   */  final public DocumentType getDoctype()  {    for (DomNode ctx = first; ctx != null; ctx = ctx.next)      {      if (ctx.nodeType == DOCUMENT_TYPE_NODE)          {            return (DocumentType) ctx;          }      }    return null;  }  /**   * <b>DOM L1</b>   * Returns the document's DOMImplementation.   */  final public DOMImplementation getImplementation()  {    return implementation;  }  /**   * <b>DOM L1 (relocated in DOM L2)</b>   * Returns the element with the specified "ID" attribute, or null.   *   * <p>Returns null unless {@link Consumer} was used to populate internal   * DTD declaration information, using package-private APIs.  If that   * internal DTD information is available, the document may be searched for   * the element with that ID.   */  public Element getElementById(String id)  {    if (id == null || id.length() == 0)      {        return null;      }    DomDoctype doctype = (DomDoctype) getDoctype();    if (doctype != null && !doctype.hasIds())      {        doctype = null;      }        // yes, this is linear in size of document.    // it'd be easy enough to maintain a hashtable.    Node current = getDocumentElement();    Node temp;        if (current == null)      {        return null;      }    while (current != this)      {        // done?        if (current.getNodeType() == ELEMENT_NODE)          {            DomElement element = (DomElement) current;            if (doctype != null)              {                DTDElementTypeInfo info =                  doctype.getElementTypeInfo(current.getNodeName());                if (info != null &&                    id.equals(element.getAttribute(info.idAttrName)))                  {                    return element;                  }                else if (element.userIdAttrs != null)                  {                    for (Iterator i = element.userIdAttrs.iterator();                         i.hasNext(); )                      {                        Node idAttr = (Node) i.next();                        if (id.equals(idAttr.getNodeValue()))                          {                            return element;                          }                      }                  }              }            // xml:id            String xmlId = element.getAttribute("xml:id");            if (xmlId == null)              {                xmlId = element.getAttributeNS(XMLConstants.XML_NS_URI,                                               "id");              }            if (id.equals(xmlId))              {                return element;              }          }                // descend?        if (current.hasChildNodes())          {            current = current.getFirstChild();            continue;          }                // lateral?        temp = current.getNextSibling();        if (temp != null)          {            current = temp;            continue;          }                // back up ...         do          {            temp = current.getParentNode();            if (temp == null)              {                return null;              }            current = temp;            temp = current.getNextSibling();          }        while (temp == null);        current = temp;      }    return null;  }  private void checkNewChild(Node newChild)  {    if (newChild.getNodeType() == ELEMENT_NODE        && getDocumentElement() != null)      {        throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,                                  "document element already present: " +                                  getDocumentElement(), newChild, 0);      }    if (newChild.getNodeType() == DOCUMENT_TYPE_NODE        && getDoctype() != null)      {        throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,                                  "document type already present: " +                                  getDoctype(), newChild, 0);      }  }  /**   * <b>DOM L1</b>   * Appends the specified node to this node's list of children,   * enforcing the constraints that there be only one root element   * and one document type child.   */  public Node appendChild(Node newChild)  {    if (checkingWellformedness)      {        checkNewChild(newChild);      }    return super.appendChild(newChild);  }  /**   * <b>DOM L1</b>   * Inserts the specified node in this node's list of children,   * enforcing the constraints that there be only one root element   * and one document type child.   */  public Node insertBefore(Node newChild, Node refChild)  {    if (checkingWellformedness)      {        checkNewChild(newChild);      }    return super.insertBefore(newChild, refChild);  }  /**   * <b>DOM L1</b>   * Replaces the specified node in this node's list of children,   * enforcing the constraints that there be only one root element   * and one document type child.   */  public Node replaceChild(Node newChild, Node refChild)  {    if (checkingWellformedness &&        ((newChild.getNodeType() == ELEMENT_NODE &&          refChild.getNodeType() != ELEMENT_NODE) ||         (newChild.getNodeType() == DOCUMENT_TYPE_NODE &&          refChild.getNodeType() != DOCUMENT_TYPE_NODE)))      {        checkNewChild(newChild);      }    return super.replaceChild(newChild, refChild);  }   // NOTE:  DOM can't really tell when the name of an entity,  // notation, or PI must follow the namespace rules (excluding  // colons) instead of the XML rules (which allow them without  // much restriction).  That's an API issue.  verifyXmlName  // aims to enforce the XML rules, not the namespace rules.    /**   * Throws a DOM exception if the specified name is not a legal XML 1.0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -