domdocument.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,388 行 · 第 1/3 页
JAVA
1,388 行
(c < 0x10000 || c > 0xeffff) && c != 0x00b7 && (c < 0x0300 || c > 0x036f) && (c < 0x203f || c > 0x2040)) { throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, name, null, c); } } else { // XML 1.0 int type = Character.getType(c); switch (type) { case Character.LOWERCASE_LETTER: // Ll case Character.UPPERCASE_LETTER: // Lu case Character.DECIMAL_DIGIT_NUMBER: // Nd case Character.OTHER_LETTER: // Lo case Character.TITLECASE_LETTER: // Lt case Character.LETTER_NUMBER: // Nl case Character.COMBINING_SPACING_MARK: // Mc case Character.ENCLOSING_MARK: // Me case Character.NON_SPACING_MARK: // Mn case Character.MODIFIER_LETTER: // Lm if ((c > 0xf900 && c < 0xfffe) || (c >= 0x20dd && c <= 0x20e0)) { // Compatibility area and Unicode 2.0 exclusions throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, name, null, c); } break; default: if (c != '-' && c != '.' && c != ':' && c != '_' && c != 0x0387 && (c < 0x02bb || c > 0x02c1) && c != 0x0559 && c != 0x06e5 && c != 0x06e6 && c != 0x00b7) { throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, name, null, c); } } } } // FIXME characters with a font or compatibility decomposition (i.e. // those with a "compatibility formatting tag" in field 5 of the // database -- marked by field 5 beginning with a "<") are not allowed. } // package private static void checkNCName(String name, boolean xml11) { checkName(name, xml11); int len = name.length(); int index = name.indexOf(':'); if (index != -1) { if (index == 0 || index == (len - 1) || name.lastIndexOf(':') != index) { throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0); } } } // package private static void checkChar(String value, boolean xml11) { char[] chars = value.toCharArray(); checkChar(chars, 0, chars.length, xml11); } static void checkChar(char[] buf, int off, int len, boolean xml11) { for (int i = 0; i < len; i++) { char c = buf[i]; // assume surrogate pairing checks out OK, for simplicity if ((c >= 0x0020 && c <= 0xd7ff) || (c == 0x000a || c == 0x000d || c == 0x0009) || (c >= 0xe000 && c <= 0xfffd) || (c >= 0x10000 && c <= 0x10ffff)) { continue; } if (xml11) { if ((c >= 0x0001 && c <= 0x001f) || (c >= 0x007f && c <= 0x0084) || (c >= 0x0086 && c <= 0x009f)) { continue; } } throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, new String(buf, off, len), null, c); } } /** * <b>DOM L1</b> * Returns a newly created element with the specified name. */ public Element createElement(String name) { Element element; if (checkingCharacters) { checkName(name, "1.1".equals(version)); } if (name.startsWith("xml:")) { element = createElementNS(null, name); } else { element = new DomElement(this, null, name); } defaultAttributes(element, name); return element; } /** * <b>DOM L2</b> * Returns a newly created element with the specified name * and namespace information. */ public Element createElementNS(String namespaceURI, String name) { if (checkingCharacters) { checkNCName(name, "1.1".equals(version)); } if ("".equals(namespaceURI)) { namespaceURI = null; } if (name.startsWith("xml:")) { if (namespaceURI != null && !XMLConstants.XML_NS_URI.equals(namespaceURI)) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xml namespace is always " + XMLConstants.XML_NS_URI, this, 0); } namespaceURI = XMLConstants.XML_NS_URI; } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || name.startsWith("xmlns:")) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xmlns is reserved", this, 0); } else if (namespaceURI == null && name.indexOf(':') != -1) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "prefixed name '" + name + "' needs a URI", this, 0); } Element element = new DomElement(this, namespaceURI, name); defaultAttributes(element, name); return element; } private void defaultAttributes(Element element, String name) { DomDoctype doctype = (DomDoctype) getDoctype(); if (doctype == null) { return; } // default any attributes that need it DTDElementTypeInfo info = doctype.getElementTypeInfo(name); if (info != null) { for (Iterator i = info.attributes(); i != null && i.hasNext(); ) { DTDAttributeTypeInfo attr = (DTDAttributeTypeInfo) i.next(); DomAttr node = (DomAttr) createAttribute(attr.name); String value = attr.value; if (value == null) { value = ""; } node.setValue(value); node.setSpecified(false); element.setAttributeNode(node); } } } /** * <b>DOM L1</b> * Returns a newly created document fragment. */ public DocumentFragment createDocumentFragment() { return new DomDocumentFragment(this); } /** * <b>DOM L1</b> * Returns a newly created text node with the specified value. */ public Text createTextNode(String value) { if (checkingCharacters) { checkChar(value, "1.1".equals(version)); } return new DomText(this, value); } /** * Returns a newly created text node with the specified value. */ public Text createTextNode(char[] buf, int off, int len) { if (checkingCharacters) { checkChar(buf, off, len, "1.1".equals(version)); } return new DomText(this, buf, off, len); } /** * <b>DOM L1</b> * Returns a newly created comment node with the specified value. */ public Comment createComment(String value) { if (checkingCharacters) { checkChar(value, "1.1".equals(version)); } return new DomComment(this, value); } /** * <b>DOM L1</b> * Returns a newly created CDATA section node with the specified value. */ public CDATASection createCDATASection(String value) { if (checkingCharacters) { checkChar(value, "1.1".equals(version)); } return new DomCDATASection(this, value); } /** * Returns a newly created CDATA section node with the specified value. */ public CDATASection createCDATASection(char[] buf, int off, int len) { if (checkingCharacters) { checkChar(buf, off, len, "1.1".equals(version)); } return new DomCDATASection(this, buf, off, len); } /** * <b>DOM L1</b> * Returns a newly created processing instruction. */ public ProcessingInstruction createProcessingInstruction(String target, String data) { if (checkingCharacters) { boolean xml11 = "1.1".equals(version); checkName(target, xml11); if ("xml".equalsIgnoreCase(target)) { throw new DomDOMException(DOMException.SYNTAX_ERR, "illegal PI target name", this, 0); } checkChar(data, xml11); } return new DomProcessingInstruction(this, target, data); } /** * <b>DOM L1</b> * Returns a newly created attribute with the specified name. */ public Attr createAttribute(String name) { if (checkingCharacters) { checkName(name, "1.1".equals(version)); } if (name.startsWith("xml:")) { return createAttributeNS(XMLConstants.XML_NS_URI, name); } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || name.startsWith("xmlns:")) { return createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, name); } else { return new DomAttr(this, null, name); } } /** * <b>DOM L2</b> * Returns a newly created attribute with the specified name * and namespace information. */ public Attr createAttributeNS(String namespaceURI, String name) { if (checkingCharacters) { checkNCName(name, "1.1".equals(version)); } if ("".equals(namespaceURI)) { namespaceURI = null; } if (name.startsWith ("xml:")) { if (namespaceURI == null) { namespaceURI = XMLConstants.XML_NS_URI; } else if (!XMLConstants.XML_NS_URI.equals(namespaceURI)) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xml namespace is always " + XMLConstants.XML_NS_URI, this, 0); } } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || name.startsWith("xmlns:")) { if (namespaceURI == null) { namespaceURI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI; } else if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xmlns namespace must be " + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, this, 0); } } else if (namespaceURI == null && name.indexOf(':') != -1) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "prefixed name needs a URI: " + name, this, 0); } return new DomAttr(this, namespaceURI, name); } /** * <b>DOM L1</b> * Returns a newly created reference to the specified entity. * The caller should populate this with the appropriate children * and then mark it as readonly. * * @see DomNode#makeReadonly */ public EntityReference createEntityReference(String name) { DomEntityReference ret = new DomEntityReference(this, name); DocumentType doctype = getDoctype(); if (doctype != null) { DomEntity ent = (DomEntity) doctype.getEntities().getNamedItem(name); if (ent != null) { for (DomNode ctx = ent.first; ctx != null; ctx = ctx.next) { ret.appendChild(ctx.cloneNode(true)); } } } ret.makeReadonly(); return ret; } /** * <b>DOM L2</b> * Makes a copy of the specified node, with all nodes "owned" by * this document and with children optionally copied. This type * of standard utility has become, well, a standard utility. * * <p> Note that EntityReference nodes created through this method (either * directly, or recursively) never have children, and that there is no * portable way to associate them with such children. * * <p> Note also that there is no requirement that the specified node * be associated with a different document. This differs from the * <em>cloneNode</em> operation in that the node itself is not given * an opportunity to participate, so that any information managed * by node subclasses will be lost. */ public Node importNode(Node src, boolean deep) { Node dst = null; switch (src.getNodeType()) { case TEXT_NODE: dst = createTextNode(src.getNodeValue()); break; case CDATA_SECTION_NODE: dst = createCDATASection(src.getNodeValue()); break; case COMMENT_NODE: dst = createComment(src.getNodeValue()); break; case PROCESSING_INSTRUCTION_NODE: dst = createProcessingInstruction(src.getNodeName(), src.getNodeValue()); break; case NOTATION_NODE: // NOTE: There's no standard way to create // these, or add them to a doctype. Useless. Notation notation = (Notation) src; dst = new DomNotation(this, notation.getNodeName(), notation.getPublicId(), notation.getSystemId()); break; case ENTITY_NODE: // NOTE: There's no standard way to create // these, or add them to a doctype. Useless. Entity entity = (Entity) src; dst = new DomEntity(this, entity.getNodeName(), entity.getPublicId(), entity.getSystemId(), entity.getNotationName()); if (deep) { for (Node ctx = src.getFirstChild(); ctx != null; ctx = ctx.getNextSibling()) { dst.appendChild(importNode(ctx, deep)); } } break; case ENTITY_REFERENCE_NODE: dst = createEntityReference(src.getNodeName()); break; case DOCUMENT_FRAGMENT_NODE: dst = new DomDocumentFragment(this);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?