xmldocumentbuilder.java

来自「java jdk 1.4的源码」· Java 代码 · 共 797 行 · 第 1/2 页

JAVA
797
字号
    final public ElementFactory getElementFactory() {        return factory;    }    /**     * Receive notification of the beginning of a document.     */    public void startDocument () throws SAXException    {	document = createDocument ();	if (locator != null)	    document.setSystemId (locator.getSystemId ());	//        // XXX don't want fixed size limits!  Fix someday.  For	// now, wide trees predominate, not deep ones.  This is	// allowing a _very_ deep tree ... we typically observe	// depths on the order of a dozen.	//        elementStack = new ParentNode [200];        topOfStack = 0;        elementStack [topOfStack] = document;	inDTD = false;    }    /**     * Receive notification of the end of a document.     */    public void endDocument () throws SAXException    {        if (topOfStack != 0)            throw new IllegalStateException (getMessage ("XDB-000"));	document.trimToSize ();    }        /**     * Begin the scope of a prefix-URI Namespace mapping.     */    public void startPrefixMapping(String prefix, String uri)	throws SAXException    {        // No-op    }    /**     * End the scope of a prefix-URI mapping.     */    public void endPrefixMapping(String prefix)	throws SAXException {        // No-op    }    /**     * Receive notification of the beginning of an element.     */    public void startElement(String namespaceURI, String localName,                             String qName, Attributes attributes)	throws SAXException    {	//	// Convert set of attributes to DOM representation.	//        AttributeSet attSet = null;	int length = attributes.getLength();	if (length != 0) {	    try {                attSet = AttributeSet.createAttributeSet1(attributes);	    } catch (DOMException ex) {		throw new SAXParseException(getMessage("XDB-002",                        new Object[] { ex.getMessage() }), locator, ex);	    }	}	//	// Then create the element, associate its attributes, and	// stack it for later addition.	//        ElementNode e = null;	try {            e = (ElementNode) document.createElementEx(qName);	} catch (DOMException ex) {	    throw new SAXParseException(getMessage("XDB-004",                    new Object[] { ex.getMessage() }), locator, ex);	}	if (attributes instanceof AttributesEx) {	    e.setIdAttributeName(		((AttributesEx)attributes).getIdAttributeName());        }	if (length != 0) {	    e.setAttributes(attSet);        }	elementStack[topOfStack++].appendChild(e);	elementStack[topOfStack] = e;    }    /**     * Receive notification of the end of an element.     */    public void endElement(String namespaceURI, String localName,                           String qName)	throws SAXException    {        ParentNode e = (ParentNode) elementStack[topOfStack];        elementStack[topOfStack--] = null;	// Trusting that the SAX parser is correct, and hasn't	// mismatched start/end element callbacks.        // if (!tag.equals (e.getTagName ()))        //     throw new SAXParseException ((getMessage ("XDB-009", new	//         Object[] { tag, e.getTagName ()  })), locator);        e.reduceWaste();	// use less space    }    /**     * Receive notification of character data.     */    public void characters(char buf [], int offset, int len)        throws SAXException    {        ParentNode	top = elementStack [topOfStack];	if (inCDataSection) {	    String		temp = new String (buf, offset, len);	    CDATASection	section;	    section = (CDATASection) top.getLastChild ();	    section.appendData (temp);	    return;	}        	try {	    NodeBase lastChild = (NodeBase) top.getLastChild ();	    if (lastChild != null && lastChild.getClass() == TextNode.class) {                // Merge only TextNode data and not CDataNode data		String tmp  = new String (buf, offset, len);	   	((TextNode)lastChild).appendData (tmp);	    } else {        	TextNode text = document.newText (buf, offset, len);	        top.appendChild (text);	    }	} catch (DOMException ex) {	    throw new SAXParseException(getMessage("XDB-004",                    new Object[] { ex.getMessage() }), locator, ex);	}    }        /**     * Receive notification of ignorable whitespace in element content.     *     * Reports ignorable whitespace; if lexical information is not ignored     * the whitespace reported here is recorded in a DOM text (or CDATA, as     * appropriate) node.     *     * @param buf holds text characters     * @param offset initial index of characters in <em>buf</em>     * @param len how many characters are being passed     * @exception SAXException as appropriate     */    public void ignorableWhitespace(char buf [], int offset, int len)        throws SAXException    {	if (ignoreWhitespace)	    return;        characters(buf, offset, len);    }        /**     * Receive notification of a processing instruction.     */    public void processingInstruction(String name, String instruction)         throws SAXException    {	// Ignore PIs in DTD for DOM support	if (inDTD)	    return;        ParentNode	top = elementStack [topOfStack];        PINode		pi;		try {	    pi = (PINode) document.createProcessingInstruction (name,		    instruction);	    top.appendChild (pi);	} catch (DOMException ex) {	    throw new SAXParseException(getMessage("XDB-004",                    new Object[] { ex.getMessage() }), locator, ex);	}    }    /**     * Receive notification of a skipped entity.     */    public void skippedEntity(String name) throws SAXException {        // No-op    }    //////////////////////////////////////////////////////////////////////    // org.xml.sax.ext.LexicalHandler callbacks    //////////////////////////////////////////////////////////////////////    /**     * Report the start of DTD declarations, if any.     */    public void startDTD(String name, String publicId, String systemId)	throws SAXException    {        DOMImplementation impl = document.getImplementation();        doctype = (Doctype)impl.createDocumentType(name, publicId, systemId);        // Set the owner since DOM2 specifies this to be null        doctype.setOwnerDocument(document);        inDTD = true;    }    /**     * Report the end of DTD declarations.     */    public void endDTD() throws SAXException {        document.appendChild(doctype);        inDTD = false;    }    /**     * Report the beginning of an entity in content.     */    public void startEntity(String name) throws SAXException {    	// Our parser doesn't report Paramater entities. Need to make	// changes for that.        // Ignore entity refs while parsing DTD	if (expandEntityRefs || inDTD) {	    return;        }        EntityReference	e = document.createEntityReference(name);	elementStack[topOfStack++].appendChild(e);	elementStack[topOfStack] = (ParentNode)e;    }    /**     * Report the end of an entity.     */    public void endEntity(String name) throws SAXException {        // Ignore entity refs while parsing DTD	if (inDTD) {            return;        }        ParentNode entity = elementStack[topOfStack];	if (!(entity instanceof EntityReference))	    return;	entity.setReadonly(true);        elementStack[topOfStack--] = null;        if (!name.equals(entity.getNodeName())) {            throw new SAXParseException(getMessage("XDB-011",                    new Object[] { name, entity.getNodeName() }), locator);	}    }    /**     * Report the start of a CDATA section.     *     * <P>If this builder is set to record lexical information then this     * callback arranges that character data (and ignorable whitespace) be     * recorded as part of a CDATA section, until the matching     * <em>endCDATA</em> method is called.     */    public void startCDATA() throws SAXException {	if (putCDATAIntoText) {	    return;        }        CDATASection text = document.createCDATASection("");        ParentNode top = elementStack[topOfStack];        	try {	    inCDataSection = true;	    top.appendChild(text);	} catch (DOMException ex) {	    throw new SAXParseException(getMessage("XDB-004",                    new Object[] { ex.getMessage() }), locator, ex);	}    }        /**     * Report the end of a CDATA section.     */    public void endCDATA() throws SAXException {        inCDataSection = false;    }        /**     * Report an XML comment anywhere in the document.     */    public void comment(char[] ch, int start, int length) throws SAXException {	// Ignore comments if lexical info is to be ignored,	// or if parsing the DTD	if (ignoreComments || inDTD) {	    return;        }        String text = new String(ch, start, length);        Comment comment = document.createComment(text);        ParentNode top = elementStack[topOfStack];        	try {	    top.appendChild(comment);	} catch (DOMException ex) {	    throw new SAXParseException(getMessage("XDB-004",                    new Object[] { ex.getMessage() }), locator, ex);	}    }    //////////////////////////////////////////////////////////////////////    // org.xml.sax.ext.DeclHandler callbacks    //////////////////////////////////////////////////////////////////////    /**     * Report an element type declaration.     */    public void elementDecl(String name, String model) throws SAXException {        // ignored    }    /**     * Report an attribute type declaration.     */    public void attributeDecl(String eName, String aName, String type,                              String valueDefault, String value)	throws SAXException    {        // ignored    }    /**     * Report an internal entity declaration.     */    public void internalEntityDecl(String name, String value)	throws SAXException    {        // SAX2 reports PEDecls which we ignore for DOM2.  SAX2 also reports        // only the first defined GEDecl which matches with DOM2.        if (!name.startsWith("%")) {            doctype.addEntityNode(name, value);        }    }    /**     * Report a parsed external entity declaration.     */    public void externalEntityDecl(String name, String publicId,                                   String systemId)	throws SAXException    {        // SAX2 reports PEDecls which we ignore for DOM2.  SAX2 also reports        // only the first defined GEDecl which matches with DOM2.        if (!name.startsWith("%")) {            doctype.addEntityNode(name, publicId, systemId, null);        }    }    //////////////////////////////////////////////////////////////////////    // org.xml.sax.DTDHandler callbacks    //////////////////////////////////////////////////////////////////////    /**     * Receive notification of a notation declaration event.     */    public void notationDecl(String n, String p, String s)	throws SAXException    {        doctype.addNotation(n, p, s);    }    /**     * Receive notification of an unparsed entity declaration event.     */    public void unparsedEntityDecl(String name, String publicId,                                    String systemId, String notation)	throws SAXException    {        doctype.addEntityNode(name, publicId, systemId, notation);    }}

⌨️ 快捷键说明

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