286N/A/*
286N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage com.sun.xml.internal.stream;
286N/A
286N/Aimport java.io.InputStream;
286N/Aimport java.io.Reader;
286N/A
286N/Aimport javax.xml.stream.*;
286N/Aimport javax.xml.stream.util.XMLEventAllocator ;
286N/Aimport javax.xml.transform.Source;
286N/Aimport javax.xml.transform.stream.StreamSource;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
286N/Aimport com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl;
286N/Aimport com.sun.org.apache.xerces.internal.impl.PropertyManager;
286N/Aimport com.sun.org.apache.xerces.internal.impl.XMLStreamFilterImpl;
286N/Aimport com.sun.org.apache.xerces.internal.impl.Constants;
286N/A
286N/A/** Factory Implementation for XMLInputFactory.
286N/A * @author Neeraj Bajaj Sun Microsystems
286N/A * @author K.Venugopal Sun Microsystems
286N/A */
286N/A
286N/A//xxx: Should we be reusing the XMLInputSource object
286N/Apublic class XMLInputFactoryImpl extends javax.xml.stream.XMLInputFactory {
286N/A
286N/A
286N/A //List of supported properties and default values.
286N/A private PropertyManager fPropertyManager = new PropertyManager(PropertyManager.CONTEXT_READER) ;
286N/A private static final boolean DEBUG = false;
286N/A
286N/A //Maintain a reference to last reader instantiated.
286N/A private XMLStreamReaderImpl fTempReader = null ;
286N/A
286N/A boolean fPropertyChanged = false;
286N/A //no reader reuse by default
286N/A boolean fReuseInstance = false;
286N/A
286N/A /** Creates a new instance of ZephryParserFactory */
286N/A public XMLInputFactoryImpl() {
286N/A
286N/A }
286N/A
286N/A void initEventReader(){
286N/A fPropertyChanged = true;
286N/A }
286N/A
286N/A /**
286N/A * @param inputstream
286N/A * @throws XMLStreamException
286N/A * @return
286N/A */
286N/A public XMLEventReader createXMLEventReader(InputStream inputstream) throws XMLStreamException {
286N/A initEventReader();
286N/A //delegate everything to XMLStreamReader
286N/A return new XMLEventReaderImpl(createXMLStreamReader(inputstream));
286N/A }
286N/A
286N/A public XMLEventReader createXMLEventReader(Reader reader) throws XMLStreamException {
286N/A initEventReader();
286N/A //delegate everything to XMLStreamReader
286N/A return new XMLEventReaderImpl(createXMLStreamReader(reader));
286N/A }
286N/A
286N/A public XMLEventReader createXMLEventReader(Source source) throws XMLStreamException {
286N/A initEventReader();
286N/A //delegate everything to XMLStreamReader
286N/A return new XMLEventReaderImpl(createXMLStreamReader(source));
286N/A }
286N/A
286N/A public XMLEventReader createXMLEventReader(String systemId, InputStream inputstream) throws XMLStreamException {
286N/A initEventReader();
286N/A //delegate everything to XMLStreamReader
286N/A return new XMLEventReaderImpl(createXMLStreamReader(systemId, inputstream));
286N/A }
286N/A
286N/A public XMLEventReader createXMLEventReader(java.io.InputStream stream, String encoding) throws XMLStreamException {
286N/A initEventReader();
286N/A //delegate everything to XMLStreamReader
286N/A return new XMLEventReaderImpl(createXMLStreamReader(stream, encoding));
286N/A }
286N/A
286N/A public XMLEventReader createXMLEventReader(String systemId, Reader reader) throws XMLStreamException {
286N/A initEventReader();
286N/A //delegate everything to XMLStreamReader
286N/A return new XMLEventReaderImpl(createXMLStreamReader(systemId, reader));
286N/A }
286N/A
286N/A /** Create a new XMLEventReader from an XMLStreamReader. After being used
286N/A * to construct the XMLEventReader instance returned from this method
286N/A * the XMLStreamReader must not be used.
286N/A * @param reader the XMLStreamReader to read from (may not be modified)
286N/A * @return a new XMLEventReader
286N/A * @throws XMLStreamException
286N/A */
286N/A public XMLEventReader createXMLEventReader(XMLStreamReader reader) throws XMLStreamException {
286N/A
286N/A //xxx: what do we do now -- instance is passed from the application
286N/A //probably we should check if the state is at the start document,
286N/A //eventreader call to next() should return START_DOCUMENT and
286N/A //then delegate every call to underlying streamReader
286N/A return new XMLEventReaderImpl(reader) ;
286N/A }
286N/A
286N/A public XMLStreamReader createXMLStreamReader(InputStream inputstream) throws XMLStreamException {
286N/A XMLInputSource inputSource = new XMLInputSource(null, null, null, inputstream, null);
286N/A return getXMLStreamReaderImpl(inputSource);
286N/A }
286N/A
286N/A public XMLStreamReader createXMLStreamReader(Reader reader) throws XMLStreamException {
286N/A XMLInputSource inputSource = new XMLInputSource(null, null, null, reader, null);
286N/A return getXMLStreamReaderImpl(inputSource);
286N/A }
286N/A
286N/A public XMLStreamReader createXMLStreamReader(String systemId, Reader reader) throws XMLStreamException {
286N/A XMLInputSource inputSource = new XMLInputSource(null,systemId,null,reader,null);
286N/A return getXMLStreamReaderImpl(inputSource);
286N/A }
286N/A
286N/A public XMLStreamReader createXMLStreamReader(Source source) throws XMLStreamException {
286N/A return new XMLStreamReaderImpl(jaxpSourcetoXMLInputSource(source),
286N/A new PropertyManager(fPropertyManager));
286N/A }
286N/A
286N/A public XMLStreamReader createXMLStreamReader(String systemId, InputStream inputstream) throws XMLStreamException {
286N/A XMLInputSource inputSource = new XMLInputSource(null,systemId,null,inputstream,null);
286N/A return getXMLStreamReaderImpl(inputSource);
286N/A }
286N/A
286N/A
286N/A public XMLStreamReader createXMLStreamReader(InputStream inputstream, String encoding) throws XMLStreamException {
286N/A XMLInputSource inputSource = new XMLInputSource(null,null,null,inputstream,encoding);
286N/A return getXMLStreamReaderImpl(inputSource);
286N/A }
286N/A
286N/A public XMLEventAllocator getEventAllocator() {
286N/A return (XMLEventAllocator)getProperty(XMLInputFactory.ALLOCATOR);
286N/A }
286N/A
286N/A public XMLReporter getXMLReporter() {
286N/A return (XMLReporter)fPropertyManager.getProperty(XMLInputFactory.REPORTER);
286N/A }
286N/A
286N/A public XMLResolver getXMLResolver() {
286N/A Object object = fPropertyManager.getProperty(XMLInputFactory.RESOLVER);
286N/A return (XMLResolver)object;
286N/A //return (XMLResolver)fPropertyManager.getProperty(XMLInputFactory.RESOLVER);
286N/A }
286N/A
286N/A public void setXMLReporter(XMLReporter xmlreporter) {
286N/A fPropertyManager.setProperty(XMLInputFactory.REPORTER, xmlreporter);
286N/A }
286N/A
286N/A public void setXMLResolver(XMLResolver xmlresolver) {
286N/A fPropertyManager.setProperty(XMLInputFactory.RESOLVER, xmlresolver);
286N/A }
286N/A
286N/A /** Create a filtered event reader that wraps the filter around the event reader
286N/A * @param reader the event reader to wrap
286N/A * @param filter the filter to apply to the event reader
286N/A * @throws XMLStreamException
286N/A */
286N/A public XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) throws XMLStreamException {
286N/A return new EventFilterSupport(reader, filter);
286N/A }
286N/A
286N/A /** Create a filtered reader that wraps the filter around the reader
286N/A * @param reader the reader to filter
286N/A * @param filter the filter to apply to the reader
286N/A * @throws XMLStreamException
286N/A */
286N/A public XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter) throws XMLStreamException {
286N/A if( reader != null && filter != null )
286N/A return new XMLStreamFilterImpl(reader,filter);
286N/A
286N/A return null;
286N/A }
286N/A
286N/A
286N/A
286N/A /** Get the value of a feature/property from the underlying implementation
286N/A * @param name The name of the property (may not be null)
286N/A * @return The value of the property
286N/A * @throws IllegalArgumentException if the property is not supported
286N/A */
286N/A public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException {
286N/A if(name == null){
286N/A throw new IllegalArgumentException("Property not supported");
286N/A }
286N/A if(fPropertyManager.containsProperty(name))
286N/A return fPropertyManager.getProperty(name);
286N/A throw new IllegalArgumentException("Property not supported");
286N/A }
286N/A
286N/A /** Query the set of fProperties that this factory supports.
286N/A *
286N/A * @param name The name of the property (may not be null)
286N/A * @return true if the property is supported and false otherwise
286N/A */
286N/A public boolean isPropertySupported(String name) {
286N/A if(name == null)
286N/A return false ;
286N/A else
286N/A return fPropertyManager.containsProperty(name);
286N/A }
286N/A
286N/A /** Set a user defined event allocator for events
286N/A * @param allocator the user defined allocator
286N/A */
286N/A public void setEventAllocator(XMLEventAllocator allocator) {
286N/A fPropertyManager.setProperty(XMLInputFactory.ALLOCATOR, allocator);
286N/A }
286N/A
286N/A /** Allows the user to set specific feature/property on the underlying implementation. The underlying implementation
286N/A * is not required to support every setting of every property in the specification and may use IllegalArgumentException
286N/A * to signal that an unsupported property may not be set with the specified value.
286N/A * @param name The name of the property (may not be null)
286N/A * @param value The value of the property
286N/A * @throws java.lang.IllegalArgumentException if the property is not supported
286N/A */
286N/A public void setProperty(java.lang.String name, Object value) throws java.lang.IllegalArgumentException {
286N/A
286N/A if(name == null || value == null || !fPropertyManager.containsProperty(name) ){
286N/A throw new IllegalArgumentException("Property "+name+" is not supported");
286N/A }
286N/A if(name == Constants.REUSE_INSTANCE || name.equals(Constants.REUSE_INSTANCE)){
286N/A fReuseInstance = ((Boolean)value).booleanValue();
286N/A if(DEBUG)System.out.println("fReuseInstance is set to " + fReuseInstance);
286N/A }else{//for any other property set the flag
286N/A //REVISIT: Even in this case instance can be reused, by passing PropertyManager
286N/A fPropertyChanged = true;
286N/A }
286N/A fPropertyManager.setProperty(name,value);
286N/A }
286N/A
286N/A XMLStreamReader getXMLStreamReaderImpl(XMLInputSource inputSource) throws javax.xml.stream.XMLStreamException{
286N/A //1. if the temp reader is null -- create the instance and return
286N/A if(fTempReader == null){
286N/A fPropertyChanged = false;
286N/A return fTempReader = new XMLStreamReaderImpl(inputSource,
286N/A new PropertyManager(fPropertyManager));
286N/A }
286N/A //if factory is configured to reuse the instance & this instance can be reused
286N/A //& the setProperty() hasn't been called
286N/A if(fReuseInstance && fTempReader.canReuse() && !fPropertyChanged){
286N/A if(DEBUG)System.out.println("Reusing the instance");
286N/A //we can make setInputSource() call reset() and this way there wont be two function calls
286N/A fTempReader.reset();
286N/A fTempReader.setInputSource(inputSource);
286N/A fPropertyChanged = false;
286N/A return fTempReader;
286N/A }else{
286N/A fPropertyChanged = false;
286N/A //just return the new instance.. note that we are not setting fTempReader to the newly created instance
286N/A return fTempReader = new XMLStreamReaderImpl(inputSource,
286N/A new PropertyManager(fPropertyManager));
286N/A }
286N/A }
286N/A
286N/A XMLInputSource jaxpSourcetoXMLInputSource(Source source){
286N/A if(source instanceof StreamSource){
286N/A StreamSource stSource = (StreamSource)source;
286N/A String systemId = stSource.getSystemId();
286N/A String publicId = stSource.getPublicId();
286N/A InputStream istream = stSource.getInputStream();
286N/A Reader reader = stSource.getReader();
286N/A
286N/A if(istream != null){
286N/A return new XMLInputSource(publicId, systemId, null, istream, null);
286N/A }
286N/A else if(reader != null){
286N/A return new XMLInputSource(publicId, systemId,null, reader, null);
286N/A }else{
286N/A return new XMLInputSource(publicId, systemId, null);
286N/A }
286N/A }
286N/A
286N/A throw new UnsupportedOperationException("Cannot create " +
286N/A "XMLStreamReader or XMLEventReader from a " +
286N/A source.getClass().getName());
286N/A }
286N/A
286N/A}//XMLInputFactoryImpl