📄 xmlstreamreaderimpl.java
字号:
/* * 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: XMLStreamReaderImpl.java,v 1.8 2006/06/06 06:28:41 sunithareddy Exp $ * @(#)XMLStreamReaderImpl.java 1.10 06/07/13 * * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. */package com.sun.org.apache.xerces.internal.impl;import com.sun.xml.internal.stream.Entity;import com.sun.xml.internal.stream.StaxErrorReporter;import com.sun.xml.internal.stream.XMLEntityStorage;import com.sun.xml.internal.stream.events.EntityDeclarationImpl;import com.sun.xml.internal.stream.events.NotationDeclarationImpl;import javax.xml.namespace.NamespaceContext;import com.sun.org.apache.xerces.internal.xni.XNIException;import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;import javax.xml.namespace.QName;import javax.xml.stream.Location;import javax.xml.stream.events.XMLEvent;import com.sun.org.apache.xerces.internal.util.NamespaceContextWrapper;import com.sun.org.apache.xerces.internal.util.SymbolTable;import com.sun.xml.internal.stream.dtd.nonvalidating.XMLNotationDecl;import com.sun.xml.internal.stream.dtd.nonvalidating.DTDGrammar;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.util.ArrayList;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;import com.sun.org.apache.xerces.internal.util.XMLChar;import com.sun.org.apache.xerces.internal.util.XMLStringBuffer;import com.sun.org.apache.xerces.internal.util.NamespaceSupport;import com.sun.org.apache.xerces.internal.impl.Constants;import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;import com.sun.xml.internal.stream.dtd.DTDGrammarUtil;/** This class implements javax.xml.stream.XMLStreamReader. It makes use of XML*Scanner classes to * derive most of its functionality. If desired, Application can reuse this instance by calling * reset() and setInputSource(). * * @author Neeraj Bajaj Sun Microsystems,Inc. * @author K.Venugopal Sun Microsystems,Inc. * @author Sunitha Reddy Sun Microsystems,Inc. */public class XMLStreamReaderImpl implements javax.xml.stream.XMLStreamReader { /** Property identifier: entity manager. */ protected static final String ENTITY_MANAGER = Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; /** Property identifier: Error Reporter. */ protected static final String ERROR_REPORTER = Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; /** Property identifier: Symbol table. */ protected static final String SYMBOL_TABLE = Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; protected static final String READER_IN_DEFINED_STATE = Constants.READER_IN_DEFINED_STATE; private SymbolTable fSymbolTable = new SymbolTable(); /** Document scanner. */ protected XMLDocumentScannerImpl fScanner = new XMLNSDocumentScannerImpl(); //make Global NamespaceContextWrapper object, fScanner.getNamespaceContext() is dynamic object and ita value changes //as per the state of the parser. protected NamespaceContextWrapper fNamespaceContextWrapper = new NamespaceContextWrapper((NamespaceSupport)fScanner.getNamespaceContext()) ; protected XMLEntityManager fEntityManager = new XMLEntityManager(); protected StaxErrorReporter fErrorReporter = new StaxErrorReporter(); /** Entity scanner, this alwasy works on last entity that was opened. */ protected XMLEntityScanner fEntityScanner = null; /** Input Source */ protected XMLInputSource fInputSource = null; /** Store properties*/ protected PropertyManager fPropertyManager = null ; /** current event type */ private int fEventType ; /** debug flag*/ static final boolean DEBUG = false ; /** more to scan */ private boolean fReuse = true; private boolean fReaderInDefinedState = true ; private boolean fBindNamespaces = true; private String fDTDDecl = null; private String versionStr = null; /** * @param inputStream * @param props * @throws XMLStreamException */ public XMLStreamReaderImpl(InputStream inputStream, PropertyManager props) throws XMLStreamException { init(props); //publicId, systemid, baseSystemId, inputStream, enocding XMLInputSource inputSource = new XMLInputSource(null,null,null,inputStream,null); //pass the input source to document scanner impl. setInputSource(inputSource); } public XMLDocumentScannerImpl getScanner(){ System.out.println("returning scanner"); return fScanner; } /** * @param systemid * @param props * @throws XMLStreamException */ public XMLStreamReaderImpl(String systemid, PropertyManager props) throws XMLStreamException { init(props); //publicId, systemid, baseSystemId, inputStream, enocding XMLInputSource inputSource = new XMLInputSource(null,systemid,null); //pass the input source to document scanner impl. setInputSource(inputSource); } /** * @param inputStream * @param encoding * @param props * @throws XMLStreamException */ public XMLStreamReaderImpl(InputStream inputStream, String encoding, PropertyManager props ) throws XMLStreamException { init(props); //publicId, systemid, baseSystemId, inputStream, enocding XMLInputSource inputSource = new XMLInputSource(null,null,null, new BufferedInputStream(inputStream),encoding ); //pass the input source to document scanner impl. setInputSource(inputSource); } /** * @param reader * @param props * @throws XMLStreamException */ public XMLStreamReaderImpl(Reader reader, PropertyManager props) throws XMLStreamException { init(props); //publicId, systemid, baseSystemId, inputStream, enocding //xxx: Using buffered reader XMLInputSource inputSource = new XMLInputSource(null,null,null,new BufferedReader(reader),null); //pass the input source to document scanner impl. setInputSource(inputSource); } /** * @param inputSource * @param props * @throws XMLStreamException */ public XMLStreamReaderImpl(XMLInputSource inputSource, PropertyManager props) throws XMLStreamException { init(props); //pass the input source to document scanner impl. setInputSource(inputSource); } /** * @param inputSource * @throws XMLStreamException */ public void setInputSource(XMLInputSource inputSource ) throws XMLStreamException { //once setInputSource() is called this instance is busy parsing the inputsource supplied //this instances is free for reuse if parser has reached END_DOCUMENT state or application has //called close() fReuse = false; try{ fScanner.setInputSource(inputSource) ; //XMLStreamReader should be in defined state if(fReaderInDefinedState){ fEventType = fScanner.next(); if (versionStr == null) versionStr = getVersion(); if (fEventType == XMLStreamConstants.START_DOCUMENT && versionStr != null && versionStr.equals("1.1")){ switchToXML11Scanner(); } } }catch(java.io.IOException ex){ throw new XMLStreamException(ex); } }//setInputSource void init(PropertyManager propertyManager) throws XMLStreamException { fPropertyManager = propertyManager; //set Stax internal properties -- Note that these instances are being created in XMLReaderImpl. //1.SymbolTable //2.XMLMessageFormatter //3.XMLEntityManager //4. call reset() //1. propertyManager.setProperty(SYMBOL_TABLE, fSymbolTable ) ; //2. propertyManager.setProperty(ERROR_REPORTER, fErrorReporter ) ; //3. propertyManager.setProperty(ENTITY_MANAGER, fEntityManager); //4. reset(); } /** This function tells if this instances is available for reuse. * One must call reset() and setInputSource() to be able to reuse * this instance. */ public boolean canReuse(){ if(DEBUG){ System.out.println("fReuse = " + fReuse); System.out.println("fEventType = " + getEventTypeString(fEventType) ); } //when parsing begins, fReuse is set to false //fReuse is set to 'true' when application calls close() return fReuse; } /** * Resets this instance so that this instance is ready for reuse. */ public void reset(){ fReuse = true; fEventType = 0 ; //reset entity manager fEntityManager.reset(fPropertyManager); //reset the scanner fScanner.reset(fPropertyManager); //REVISIT:this is too ugly -- we are getting XMLEntityManager and XMLEntityReader from //property manager, it should be only XMLEntityManager fDTDDecl = null; fEntityScanner = (XMLEntityScanner)fEntityManager.getEntityScanner() ; //default value for this property is true. However, this should be false when using XMLEventReader... Ugh.. //because XMLEventReader should not have defined state. fReaderInDefinedState = ((Boolean)fPropertyManager.getProperty(READER_IN_DEFINED_STATE)).booleanValue(); fBindNamespaces = ((Boolean)fPropertyManager.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue(); versionStr = null; } /** Frees any resources associated with this Reader. This method does not close the underlying input source. * @throws XMLStreamException if there are errors freeing associated resources */ public void close() throws XMLStreamException { //xxx: Check what this function is intended to do. //reset(); fReuse = true ; } /** Returns the character encoding declared on the xml declaration Returns null if none was declared * @return the encoding declared in the document or null */ public String getCharacterEncodingScheme() { return fScanner.getCharacterEncodingScheme(); } /** * @return */ public int getColumnNumber() { return fEntityScanner.getColumnNumber(); }//getColumnNumber /** Return input encoding if known or null if unknown. * @return the encoding of this instance or null */ public String getEncoding() { return fEntityScanner.getEncoding(); }//getEncoding /** Returns the current value of the parse event as a string, this returns the string value of a CHARACTERS event, returns the value of a COMMENT, the replacement value for an ENTITY_REFERENCE, the string value of a CDATA section, the string value for a SPACE event, or the String value of the internal subset of the DTD. If an ENTITY_REFERENCE has been resolved, any character data will be reported as CHARACTERS events. * @return the current text or null */ public int getEventType() { return fEventType ; }//getEventType /** * @return */ public int getLineNumber() { return fEntityScanner.getLineNumber() ; }//getLineNumber /** For START_ELEMENT or END_ELEMENT returns the (local) name of the current element. For ENTITY_REF it returns entity name. For * PROCESSING_INSTRUCTION it returns the target. The current event must be START_ELEMENT or END_ELEMENT, PROCESSING_INSTRUCTION, or * ENTITY_REF, otherwise null is returned. * @return */ public String getLocalName() { if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){ //xxx check whats the value of fCurrentElement return fScanner.getElementQName().localpart ; } else if(fEventType == XMLEvent.PROCESSING_INSTRUCTION){ return fScanner.getPITarget(); } else if(fEventType == XMLEvent.ENTITY_REFERENCE){ return fScanner.getEntityName(); } return null; }//getLocalName()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -