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

📄 staxevent2sax.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the "License").  You may not use this file except * in compliance with the License. * * You can obtain a copy of the license at * https://jaxp.dev.java.net/CDDLv1.0.html. * See the License for the specific language governing * permissions and limitations under the License. * * When distributing Covered Code, include this CDDL * HEADER in each file and include the License file at * https://jaxp.dev.java.net/CDDLv1.0.html * If applicable add the following below this CDDL HEADER * with the fields enclosed by brackets "[]" replaced with * your own identifying information: Portions Copyright * [year] [name of copyright owner] *//* * $Id: StAXEvent2SAX.java,v 1.4 2006/01/25 05:09:46 sunithareddy Exp $ * @(#)StAXEvent2SAX.java	1.8 06/04/07 * * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. */package com.sun.org.apache.xalan.internal.xsltc.trax;import java.io.IOException;import java.util.Iterator;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.DTDHandler;import org.xml.sax.EntityResolver;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.XMLReader;import org.xml.sax.ext.LexicalHandler;import org.xml.sax.helpers.AttributesImpl;import org.xml.sax.ext.Locator2;import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;import javax.xml.namespace.QName;import javax.xml.stream.XMLEventReader;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.events.Attribute;import javax.xml.stream.events.Characters;import javax.xml.stream.events.EndElement;import javax.xml.stream.events.Namespace;import javax.xml.stream.events.ProcessingInstruction;import javax.xml.stream.events.StartElement;import javax.xml.stream.events.XMLEvent;import javax.xml.stream.events.StartDocument;/** * @author Suresh Kumar * @author Sunitha Reddy * @since 1.6 */public class StAXEvent2SAX implements XMLReader, Locator {    //private final static String EMPTYSTRING = "";    //private static final String XMLNS_PREFIX = "xmlns";      // StAX event source    private final XMLEventReader staxEventReader;        //private Node _dom = null;    private ContentHandler _sax = null;    private LexicalHandler _lex = null;    private SAXImpl _saxImpl = null;    //private Hashtable _nsPrefixes = new Hashtable();    private String version = null;    private String encoding = null;            public StAXEvent2SAX(XMLEventReader staxCore) {	staxEventReader = staxCore;    }    public ContentHandler getContentHandler() { 	return _sax;    }    public void setContentHandler(ContentHandler handler) throws 	NullPointerException     {	_sax = handler;	if (handler instanceof LexicalHandler) {	    _lex = (LexicalHandler) handler;	}		if (handler instanceof SAXImpl) {	    _saxImpl = (SAXImpl)handler;	}    }        public void parse(InputSource unused) throws IOException, SAXException {       try {            bridge();        } catch (XMLStreamException e) {            throw new SAXException(e);        }    }    //Main Work Starts Here.    public void parse() throws IOException, SAXException, XMLStreamException {        bridge();    }        /*  public void parse() throws IOException, SAXException {	if (_dom != null) {	    boolean isIncomplete = 		(_dom.getNodeType() != org.w3c.dom.Node.DOCUMENT_NODE);	    if (isIncomplete) {		_sax.startDocument();		parse(_dom);		_sax.endDocument();	    }	    else {		parse(_dom);	    }	}    }    */        /*     * @see StAXReaderToContentHandler#bridge()     */    private void bridge() throws XMLStreamException {        try {            // remembers the nest level of elements to know when we are done.            int depth=0;            XMLEvent event = staxEventReader.peek();            if (!event.isStartDocument() && !event.isStartElement()) {                throw new IllegalStateException();            }            // if the parser is on START_DOCUMENT, skip ahead to the first element            while( !event.isStartElement() ) {                if (event.getEventType() == XMLStreamConstants.START_DOCUMENT){                    version = ((StartDocument)event).getVersion();                    if (((StartDocument)event).encodingSet())                        encoding = ((StartDocument)event).getCharacterEncodingScheme();                }                                event = staxEventReader.nextEvent();            }                       handleStartDocument(event);            do {                // These are all of the events listed in the javadoc for                // XMLEvent.                // The spec only really describes 11 of them.                switch (event.getEventType()) {                    case XMLStreamConstants.START_ELEMENT :                        depth++;                        handleStartElement(event.asStartElement());                        break;                    case XMLStreamConstants.END_ELEMENT :                        handleEndElement(event.asEndElement());                        depth--;                        break;                    case XMLStreamConstants.CHARACTERS :                        handleCharacters(event.asCharacters());                        break;                    case XMLStreamConstants.ENTITY_REFERENCE :                        handleEntityReference();                        break;                    case XMLStreamConstants.PROCESSING_INSTRUCTION :                        handlePI((ProcessingInstruction)event);                        break;                    case XMLStreamConstants.COMMENT :                        handleComment();                        break;                    case XMLStreamConstants.DTD :                        handleDTD();                        break;                    case XMLStreamConstants.ATTRIBUTE :                        handleAttribute();                        break;                    case XMLStreamConstants.NAMESPACE :                        handleNamespace();                        break;                    case XMLStreamConstants.CDATA :                        handleCDATA();                        break;                    case XMLStreamConstants.ENTITY_DECLARATION :                        handleEntityDecl();                        break;                    case XMLStreamConstants.NOTATION_DECLARATION :                        handleNotationDecl();                        break;                    case XMLStreamConstants.SPACE :                        handleSpace();                        break;                    default :                        throw new InternalError("processing event: " + event);                }                event=staxEventReader.nextEvent();            } while (depth!=0);            handleEndDocument();        } catch (SAXException e) {            throw new XMLStreamException(e);        }    }        private void handleEndDocument() throws SAXException {        _sax.endDocument();    }    private void handleStartDocument(final XMLEvent event) throws SAXException {        _sax.setDocumentLocator(new Locator2() {            public int getColumnNumber() {                return event.getLocation().getColumnNumber();            }            public int getLineNumber() {                return event.getLocation().getLineNumber();            }            public String getPublicId() {                return event.getLocation().getPublicId();            }            public String getSystemId() {                return event.getLocation().getSystemId();            }            public String getXMLVersion(){                return version;            }            public String getEncoding(){                return encoding;            }                    });        _sax.startDocument();    }    private void handlePI(ProcessingInstruction event)        throws XMLStreamException {        try {            _sax.processingInstruction(                event.getTarget(),                event.getData());        } catch (SAXException e) {            throw new XMLStreamException(e);        }    }    private void handleCharacters(Characters event) throws XMLStreamException {        try {            _sax.characters(                event.getData().toCharArray(),                0,                event.getData().length());        } catch (SAXException e) {            throw new XMLStreamException(e);        }    }    private void handleEndElement(EndElement event) throws XMLStreamException {        QName qName = event.getName();

⌨️ 快捷键说明

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