|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.xml.internal.stream; |
|
|
|
import java.io.InputStream; |
|
import java.io.Reader; |
|
|
|
import javax.xml.stream.*; |
|
import javax.xml.stream.util.XMLEventAllocator ; |
|
import javax.xml.transform.Source; |
|
import javax.xml.transform.stream.StreamSource; |
|
import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; |
|
import com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl; |
|
import com.sun.org.apache.xerces.internal.impl.PropertyManager; |
|
import com.sun.org.apache.xerces.internal.impl.XMLStreamFilterImpl; |
|
import com.sun.org.apache.xerces.internal.impl.Constants; |
|
|
|
/** Factory Implementation for XMLInputFactory. |
|
* @author Neeraj Bajaj Sun Microsystems |
|
* @author K.Venugopal Sun Microsystems |
|
*/ |
|
|
|
|
|
public class XMLInputFactoryImpl extends javax.xml.stream.XMLInputFactory { |
|
|
|
|
|
|
|
private PropertyManager fPropertyManager = new PropertyManager(PropertyManager.CONTEXT_READER) ; |
|
private static final boolean DEBUG = false; |
|
|
|
|
|
private XMLStreamReaderImpl fTempReader = null ; |
|
|
|
boolean fPropertyChanged = false; |
|
|
|
boolean fReuseInstance = false; |
|
|
|
|
|
public XMLInputFactoryImpl() { |
|
|
|
} |
|
|
|
void initEventReader(){ |
|
fPropertyChanged = true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public XMLEventReader createXMLEventReader(InputStream inputstream) throws XMLStreamException { |
|
initEventReader(); |
|
|
|
return new XMLEventReaderImpl(createXMLStreamReader(inputstream)); |
|
} |
|
|
|
public XMLEventReader createXMLEventReader(Reader reader) throws XMLStreamException { |
|
initEventReader(); |
|
|
|
return new XMLEventReaderImpl(createXMLStreamReader(reader)); |
|
} |
|
|
|
public XMLEventReader createXMLEventReader(Source source) throws XMLStreamException { |
|
initEventReader(); |
|
|
|
return new XMLEventReaderImpl(createXMLStreamReader(source)); |
|
} |
|
|
|
public XMLEventReader createXMLEventReader(String systemId, InputStream inputstream) throws XMLStreamException { |
|
initEventReader(); |
|
|
|
return new XMLEventReaderImpl(createXMLStreamReader(systemId, inputstream)); |
|
} |
|
|
|
public XMLEventReader createXMLEventReader(java.io.InputStream stream, String encoding) throws XMLStreamException { |
|
initEventReader(); |
|
|
|
return new XMLEventReaderImpl(createXMLStreamReader(stream, encoding)); |
|
} |
|
|
|
public XMLEventReader createXMLEventReader(String systemId, Reader reader) throws XMLStreamException { |
|
initEventReader(); |
|
|
|
return new XMLEventReaderImpl(createXMLStreamReader(systemId, reader)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public XMLEventReader createXMLEventReader(XMLStreamReader reader) throws XMLStreamException { |
|
|
|
//xxx: what do we do now -- instance is passed from the application |
|
//probably we should check if the state is at the start document, |
|
//eventreader call to next() should return START_DOCUMENT and |
|
|
|
return new XMLEventReaderImpl(reader) ; |
|
} |
|
|
|
public XMLStreamReader createXMLStreamReader(InputStream inputstream) throws XMLStreamException { |
|
XMLInputSource inputSource = new XMLInputSource(null, null, null, inputstream, null); |
|
return getXMLStreamReaderImpl(inputSource); |
|
} |
|
|
|
public XMLStreamReader createXMLStreamReader(Reader reader) throws XMLStreamException { |
|
XMLInputSource inputSource = new XMLInputSource(null, null, null, reader, null); |
|
return getXMLStreamReaderImpl(inputSource); |
|
} |
|
|
|
public XMLStreamReader createXMLStreamReader(String systemId, Reader reader) throws XMLStreamException { |
|
XMLInputSource inputSource = new XMLInputSource(null,systemId,null,reader,null); |
|
return getXMLStreamReaderImpl(inputSource); |
|
} |
|
|
|
public XMLStreamReader createXMLStreamReader(Source source) throws XMLStreamException { |
|
return new XMLStreamReaderImpl(jaxpSourcetoXMLInputSource(source), |
|
new PropertyManager(fPropertyManager)); |
|
} |
|
|
|
public XMLStreamReader createXMLStreamReader(String systemId, InputStream inputstream) throws XMLStreamException { |
|
XMLInputSource inputSource = new XMLInputSource(null,systemId,null,inputstream,null); |
|
return getXMLStreamReaderImpl(inputSource); |
|
} |
|
|
|
|
|
public XMLStreamReader createXMLStreamReader(InputStream inputstream, String encoding) throws XMLStreamException { |
|
XMLInputSource inputSource = new XMLInputSource(null,null,null,inputstream,encoding); |
|
return getXMLStreamReaderImpl(inputSource); |
|
} |
|
|
|
public XMLEventAllocator getEventAllocator() { |
|
return (XMLEventAllocator)getProperty(XMLInputFactory.ALLOCATOR); |
|
} |
|
|
|
public XMLReporter getXMLReporter() { |
|
return (XMLReporter)fPropertyManager.getProperty(XMLInputFactory.REPORTER); |
|
} |
|
|
|
public XMLResolver getXMLResolver() { |
|
Object object = fPropertyManager.getProperty(XMLInputFactory.RESOLVER); |
|
return (XMLResolver)object; |
|
//return (XMLResolver)fPropertyManager.getProperty(XMLInputFactory.RESOLVER); |
|
} |
|
|
|
public void setXMLReporter(XMLReporter xmlreporter) { |
|
fPropertyManager.setProperty(XMLInputFactory.REPORTER, xmlreporter); |
|
} |
|
|
|
public void setXMLResolver(XMLResolver xmlresolver) { |
|
fPropertyManager.setProperty(XMLInputFactory.RESOLVER, xmlresolver); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) throws XMLStreamException { |
|
return new EventFilterSupport(reader, filter); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter) throws XMLStreamException { |
|
if( reader != null && filter != null ) |
|
return new XMLStreamFilterImpl(reader,filter); |
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException { |
|
if(name == null){ |
|
throw new IllegalArgumentException("Property not supported"); |
|
} |
|
if(fPropertyManager.containsProperty(name)) |
|
return fPropertyManager.getProperty(name); |
|
throw new IllegalArgumentException("Property not supported"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isPropertySupported(String name) { |
|
if(name == null) |
|
return false ; |
|
else |
|
return fPropertyManager.containsProperty(name); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void setEventAllocator(XMLEventAllocator allocator) { |
|
fPropertyManager.setProperty(XMLInputFactory.ALLOCATOR, allocator); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setProperty(java.lang.String name, Object value) throws java.lang.IllegalArgumentException { |
|
|
|
if(name == null || value == null || !fPropertyManager.containsProperty(name) ){ |
|
throw new IllegalArgumentException("Property "+name+" is not supported"); |
|
} |
|
if(name == Constants.REUSE_INSTANCE || name.equals(Constants.REUSE_INSTANCE)){ |
|
fReuseInstance = ((Boolean)value).booleanValue(); |
|
if(DEBUG)System.out.println("fReuseInstance is set to " + fReuseInstance); |
|
}else{//for any other property set the flag |
|
|
|
fPropertyChanged = true; |
|
} |
|
fPropertyManager.setProperty(name,value); |
|
} |
|
|
|
XMLStreamReader getXMLStreamReaderImpl(XMLInputSource inputSource) throws javax.xml.stream.XMLStreamException{ |
|
|
|
if(fTempReader == null){ |
|
fPropertyChanged = false; |
|
return fTempReader = new XMLStreamReaderImpl(inputSource, |
|
new PropertyManager(fPropertyManager)); |
|
} |
|
//if factory is configured to reuse the instance & this instance can be reused |
|
|
|
if(fReuseInstance && fTempReader.canReuse() && !fPropertyChanged){ |
|
if(DEBUG)System.out.println("Reusing the instance"); |
|
|
|
fTempReader.reset(); |
|
fTempReader.setInputSource(inputSource); |
|
fPropertyChanged = false; |
|
return fTempReader; |
|
}else{ |
|
fPropertyChanged = false; |
|
|
|
return fTempReader = new XMLStreamReaderImpl(inputSource, |
|
new PropertyManager(fPropertyManager)); |
|
} |
|
} |
|
|
|
XMLInputSource jaxpSourcetoXMLInputSource(Source source){ |
|
if(source instanceof StreamSource){ |
|
StreamSource stSource = (StreamSource)source; |
|
String systemId = stSource.getSystemId(); |
|
String publicId = stSource.getPublicId(); |
|
InputStream istream = stSource.getInputStream(); |
|
Reader reader = stSource.getReader(); |
|
|
|
if(istream != null){ |
|
return new XMLInputSource(publicId, systemId, null, istream, null); |
|
} |
|
else if(reader != null){ |
|
return new XMLInputSource(publicId, systemId,null, reader, null); |
|
}else{ |
|
return new XMLInputSource(publicId, systemId, null, false); |
|
} |
|
} |
|
|
|
throw new UnsupportedOperationException("Cannot create " + |
|
"XMLStreamReader or XMLEventReader from a " + |
|
source.getClass().getName()); |
|
} |
|
|
|
}//XMLInputFactoryImpl |