Path: blob/jdk8u272-b10-aarch32-20201026/jaxp/src/com/sun/xml/internal/stream/XMLInputFactoryImpl.java
83408 views
/*1* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.xml.internal.stream;2627import java.io.InputStream;28import java.io.Reader;2930import javax.xml.stream.*;31import javax.xml.stream.util.XMLEventAllocator ;32import javax.xml.transform.Source;33import javax.xml.transform.stream.StreamSource;34import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;35import com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl;36import com.sun.org.apache.xerces.internal.impl.PropertyManager;37import com.sun.org.apache.xerces.internal.impl.XMLStreamFilterImpl;38import com.sun.org.apache.xerces.internal.impl.Constants;3940/** Factory Implementation for XMLInputFactory.41* @author Neeraj Bajaj Sun Microsystems42* @author K.Venugopal Sun Microsystems43*/4445//xxx: Should we be reusing the XMLInputSource object46public class XMLInputFactoryImpl extends javax.xml.stream.XMLInputFactory {474849//List of supported properties and default values.50private PropertyManager fPropertyManager = new PropertyManager(PropertyManager.CONTEXT_READER) ;51private static final boolean DEBUG = false;5253//Maintain a reference to last reader instantiated.54private XMLStreamReaderImpl fTempReader = null ;5556boolean fPropertyChanged = false;57//no reader reuse by default58boolean fReuseInstance = false;5960/** Creates a new instance of ZephryParserFactory */61public XMLInputFactoryImpl() {6263}6465void initEventReader(){66fPropertyChanged = true;67}6869/**70* @param inputstream71* @throws XMLStreamException72* @return73*/74public XMLEventReader createXMLEventReader(InputStream inputstream) throws XMLStreamException {75initEventReader();76//delegate everything to XMLStreamReader77return new XMLEventReaderImpl(createXMLStreamReader(inputstream));78}7980public XMLEventReader createXMLEventReader(Reader reader) throws XMLStreamException {81initEventReader();82//delegate everything to XMLStreamReader83return new XMLEventReaderImpl(createXMLStreamReader(reader));84}8586public XMLEventReader createXMLEventReader(Source source) throws XMLStreamException {87initEventReader();88//delegate everything to XMLStreamReader89return new XMLEventReaderImpl(createXMLStreamReader(source));90}9192public XMLEventReader createXMLEventReader(String systemId, InputStream inputstream) throws XMLStreamException {93initEventReader();94//delegate everything to XMLStreamReader95return new XMLEventReaderImpl(createXMLStreamReader(systemId, inputstream));96}9798public XMLEventReader createXMLEventReader(java.io.InputStream stream, String encoding) throws XMLStreamException {99initEventReader();100//delegate everything to XMLStreamReader101return new XMLEventReaderImpl(createXMLStreamReader(stream, encoding));102}103104public XMLEventReader createXMLEventReader(String systemId, Reader reader) throws XMLStreamException {105initEventReader();106//delegate everything to XMLStreamReader107return new XMLEventReaderImpl(createXMLStreamReader(systemId, reader));108}109110/** Create a new XMLEventReader from an XMLStreamReader. After being used111* to construct the XMLEventReader instance returned from this method112* the XMLStreamReader must not be used.113* @param reader the XMLStreamReader to read from (may not be modified)114* @return a new XMLEventReader115* @throws XMLStreamException116*/117public XMLEventReader createXMLEventReader(XMLStreamReader reader) throws XMLStreamException {118119//xxx: what do we do now -- instance is passed from the application120//probably we should check if the state is at the start document,121//eventreader call to next() should return START_DOCUMENT and122//then delegate every call to underlying streamReader123return new XMLEventReaderImpl(reader) ;124}125126public XMLStreamReader createXMLStreamReader(InputStream inputstream) throws XMLStreamException {127XMLInputSource inputSource = new XMLInputSource(null, null, null, inputstream, null);128return getXMLStreamReaderImpl(inputSource);129}130131public XMLStreamReader createXMLStreamReader(Reader reader) throws XMLStreamException {132XMLInputSource inputSource = new XMLInputSource(null, null, null, reader, null);133return getXMLStreamReaderImpl(inputSource);134}135136public XMLStreamReader createXMLStreamReader(String systemId, Reader reader) throws XMLStreamException {137XMLInputSource inputSource = new XMLInputSource(null,systemId,null,reader,null);138return getXMLStreamReaderImpl(inputSource);139}140141public XMLStreamReader createXMLStreamReader(Source source) throws XMLStreamException {142return new XMLStreamReaderImpl(jaxpSourcetoXMLInputSource(source),143new PropertyManager(fPropertyManager));144}145146public XMLStreamReader createXMLStreamReader(String systemId, InputStream inputstream) throws XMLStreamException {147XMLInputSource inputSource = new XMLInputSource(null,systemId,null,inputstream,null);148return getXMLStreamReaderImpl(inputSource);149}150151152public XMLStreamReader createXMLStreamReader(InputStream inputstream, String encoding) throws XMLStreamException {153XMLInputSource inputSource = new XMLInputSource(null,null,null,inputstream,encoding);154return getXMLStreamReaderImpl(inputSource);155}156157public XMLEventAllocator getEventAllocator() {158return (XMLEventAllocator)getProperty(XMLInputFactory.ALLOCATOR);159}160161public XMLReporter getXMLReporter() {162return (XMLReporter)fPropertyManager.getProperty(XMLInputFactory.REPORTER);163}164165public XMLResolver getXMLResolver() {166Object object = fPropertyManager.getProperty(XMLInputFactory.RESOLVER);167return (XMLResolver)object;168//return (XMLResolver)fPropertyManager.getProperty(XMLInputFactory.RESOLVER);169}170171public void setXMLReporter(XMLReporter xmlreporter) {172fPropertyManager.setProperty(XMLInputFactory.REPORTER, xmlreporter);173}174175public void setXMLResolver(XMLResolver xmlresolver) {176fPropertyManager.setProperty(XMLInputFactory.RESOLVER, xmlresolver);177}178179/** Create a filtered event reader that wraps the filter around the event reader180* @param reader the event reader to wrap181* @param filter the filter to apply to the event reader182* @throws XMLStreamException183*/184public XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) throws XMLStreamException {185return new EventFilterSupport(reader, filter);186}187188/** Create a filtered reader that wraps the filter around the reader189* @param reader the reader to filter190* @param filter the filter to apply to the reader191* @throws XMLStreamException192*/193public XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter) throws XMLStreamException {194if( reader != null && filter != null )195return new XMLStreamFilterImpl(reader,filter);196197return null;198}199200201202/** Get the value of a feature/property from the underlying implementation203* @param name The name of the property (may not be null)204* @return The value of the property205* @throws IllegalArgumentException if the property is not supported206*/207public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException {208if(name == null){209throw new IllegalArgumentException("Property not supported");210}211if(fPropertyManager.containsProperty(name))212return fPropertyManager.getProperty(name);213throw new IllegalArgumentException("Property not supported");214}215216/** Query the set of fProperties that this factory supports.217*218* @param name The name of the property (may not be null)219* @return true if the property is supported and false otherwise220*/221public boolean isPropertySupported(String name) {222if(name == null)223return false ;224else225return fPropertyManager.containsProperty(name);226}227228/** Set a user defined event allocator for events229* @param allocator the user defined allocator230*/231public void setEventAllocator(XMLEventAllocator allocator) {232fPropertyManager.setProperty(XMLInputFactory.ALLOCATOR, allocator);233}234235/** Allows the user to set specific feature/property on the underlying implementation. The underlying implementation236* is not required to support every setting of every property in the specification and may use IllegalArgumentException237* to signal that an unsupported property may not be set with the specified value.238* @param name The name of the property (may not be null)239* @param value The value of the property240* @throws java.lang.IllegalArgumentException if the property is not supported241*/242public void setProperty(java.lang.String name, Object value) throws java.lang.IllegalArgumentException {243244if(name == null || value == null || !fPropertyManager.containsProperty(name) ){245throw new IllegalArgumentException("Property "+name+" is not supported");246}247if(name == Constants.REUSE_INSTANCE || name.equals(Constants.REUSE_INSTANCE)){248fReuseInstance = ((Boolean)value).booleanValue();249if(DEBUG)System.out.println("fReuseInstance is set to " + fReuseInstance);250}else{//for any other property set the flag251//REVISIT: Even in this case instance can be reused, by passing PropertyManager252fPropertyChanged = true;253}254fPropertyManager.setProperty(name,value);255}256257XMLStreamReader getXMLStreamReaderImpl(XMLInputSource inputSource) throws javax.xml.stream.XMLStreamException{258//1. if the temp reader is null -- create the instance and return259if(fTempReader == null){260fPropertyChanged = false;261return fTempReader = new XMLStreamReaderImpl(inputSource,262new PropertyManager(fPropertyManager));263}264//if factory is configured to reuse the instance & this instance can be reused265//& the setProperty() hasn't been called266if(fReuseInstance && fTempReader.canReuse() && !fPropertyChanged){267if(DEBUG)System.out.println("Reusing the instance");268//we can make setInputSource() call reset() and this way there wont be two function calls269fTempReader.reset();270fTempReader.setInputSource(inputSource);271fPropertyChanged = false;272return fTempReader;273}else{274fPropertyChanged = false;275//just return the new instance.. note that we are not setting fTempReader to the newly created instance276return fTempReader = new XMLStreamReaderImpl(inputSource,277new PropertyManager(fPropertyManager));278}279}280281XMLInputSource jaxpSourcetoXMLInputSource(Source source){282if(source instanceof StreamSource){283StreamSource stSource = (StreamSource)source;284String systemId = stSource.getSystemId();285String publicId = stSource.getPublicId();286InputStream istream = stSource.getInputStream();287Reader reader = stSource.getReader();288289if(istream != null){290return new XMLInputSource(publicId, systemId, null, istream, null);291}292else if(reader != null){293return new XMLInputSource(publicId, systemId,null, reader, null);294}else{295return new XMLInputSource(publicId, systemId, null);296}297}298299throw new UnsupportedOperationException("Cannot create " +300"XMLStreamReader or XMLEventReader from a " +301source.getClass().getName());302}303304}//XMLInputFactoryImpl305306307