staxstream2sax.java

来自「JAVA 所有包」· Java 代码 · 共 548 行 · 第 1/2 页

JAVA
548
字号
/* * 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: StAXStream2SAX.java,v 1.3 2005/11/03 17:53:11 jeffsuttor Exp $ * @(#)StAXStream2SAX.java	1.6 06/01/27 * * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. */package com.sun.org.apache.xalan.internal.xsltc.trax;import java.io.IOException;import java.util.Hashtable;import java.util.Stack;import java.util.Vector;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.ext.Locator2;import org.xml.sax.helpers.AttributesImpl;import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;import javax.xml.namespace.QName;import javax.xml.stream.XMLStreamReader;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.StartDocument;import javax.xml.stream.events.XMLEvent;/** * @author Padmaja Vedula * @author Sunitha Reddy */public class StAXStream2SAX implements XMLReader, Locator {    //private final static String EMPTYSTRING = "";    //private static final String XMLNS_PREFIX = "xmlns";      // StAX Stream source    private final XMLStreamReader staxStreamReader;        //private Node _dom = null;    private ContentHandler _sax = null;    private LexicalHandler _lex = null;    private SAXImpl _saxImpl = null;    //private Hashtable _nsPrefixes = new Hashtable();        public StAXStream2SAX(XMLStreamReader staxSrc) {            staxStreamReader = staxSrc;    }    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();    }       /**     * This class is only used internally so this method should never      * be called.     */    public void parse(String sysId) throws IOException, SAXException {	throw new IOException("This method is not yet implemented.");    }       public void bridge() throws XMLStreamException {        try {            // remembers the nest level of elements to know when we are done.            int depth=0;            // if the parser is at the start tag, proceed to the first element            int event = staxStreamReader.getEventType();            if(event == XMLStreamConstants.START_DOCUMENT) {                 event =  staxStreamReader.nextTag();            }                                          if( event!=XMLStreamConstants.START_ELEMENT)                throw new IllegalStateException("The current event is not START_ELEMENT\n but" + event);                        handleStartDocument();            do {                // These are all of the events listed in the javadoc for                // XMLEvent.                // The spec only really describes 11 of them.                switch (event) {                    case XMLStreamConstants.START_ELEMENT :                        depth++;                        handleStartElement();                        break;                    case XMLStreamConstants.END_ELEMENT :                        handleEndElement();                        depth--;                        break;                    case XMLStreamConstants.CHARACTERS :                        handleCharacters();                        break;                    case XMLStreamConstants.ENTITY_REFERENCE :                        handleEntityReference();                        break;                    case XMLStreamConstants.PROCESSING_INSTRUCTION :                        handlePI();                        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=staxStreamReader.next();            } while (depth!=0);            handleEndDocument();        } catch (SAXException e) {            throw new XMLStreamException(e);        }    }    private void handleEndDocument() throws SAXException {        _sax.endDocument();    }    private void handleStartDocument() throws SAXException {        _sax.setDocumentLocator(new Locator2() {            public int getColumnNumber() {                return staxStreamReader.getLocation().getColumnNumber();            }            public int getLineNumber() {                return staxStreamReader.getLocation().getLineNumber();            }            public String getPublicId() {                return staxStreamReader.getLocation().getPublicId();            }            public String getSystemId() {                return staxStreamReader.getLocation().getSystemId();            }            public String getXMLVersion() {                return staxStreamReader.getVersion();            }            public String getEncoding() {                return staxStreamReader.getEncoding();            }         });        _sax.startDocument();    }    private void handlePI() throws XMLStreamException {        try {            _sax.processingInstruction(                staxStreamReader.getPITarget(),                staxStreamReader.getPIData());        } catch (SAXException e) {            throw new XMLStreamException(e);        }    }    private void handleCharacters() throws XMLStreamException {        // workaround for bugid 5046319 - switch over to commented section        // below when it is fixed.        int textLength = staxStreamReader.getTextLength();        char[] chars = new char[textLength];        staxStreamReader.getTextCharacters(0, chars, 0, textLength);        try {            _sax.characters(chars, 0, chars.length);        } catch (SAXException e) {            throw new XMLStreamException(e);        }        //        int start = 0;//        int len;//        do {//            len = staxStreamReader.getTextCharacters(start, buf, 0, buf.length);//            start += len;//            try {//                _sax.characters(buf, 0, len);//            } catch (SAXException e) {//                throw new XMLStreamException(e);//            }

⌨️ 快捷键说明

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