Improve speed in stax readers (#1748)

This commit is contained in:
Guillaume Nodet 2024-10-01 13:02:58 +02:00 committed by GitHub
parent 3fdc54c975
commit eefe2c73bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 4 deletions

View File

@ -348,6 +348,15 @@ public class ${className} {
DEFAULT_ENTITIES = Collections.unmodifiableMap(entities); DEFAULT_ENTITIES = Collections.unmodifiableMap(entities);
} }
static class InputFactoryHolder {
static XMLInputFactory XML_INPUT_FACTORY;
static {
XMLInputFactory factory = XMLInputFactory.newFactory();
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
XML_INPUT_FACTORY = factory;
}
}
private boolean addDefaultEntities = true; private boolean addDefaultEntities = true;
#if ( $locationTracking ) #if ( $locationTracking )
private boolean addLocationInformation = true; private boolean addLocationInformation = true;
@ -363,6 +372,15 @@ public class ${className} {
this.contentTransformer = contentTransformer; this.contentTransformer = contentTransformer;
} }
/**
* Returns the {@link XMLInputFactory} used by this reader.
*
* @return the {@link XMLInputFactory} used by this reader.
*/
public XMLInputFactory getXMLInputFactory() {
return InputFactoryHolder.XML_INPUT_FACTORY;
}
/** /**
* Returns the state of the "add default entities" flag. * Returns the state of the "add default entities" flag.
* *
@ -421,13 +439,12 @@ public class ${className} {
#else #else
public ${root.name} read(Reader reader, boolean strict) throws XMLStreamException { public ${root.name} read(Reader reader, boolean strict) throws XMLStreamException {
#end #end
XMLInputFactory factory = XMLInputFactory.newFactory();
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
#if ( $locationTracking ) #if ( $locationTracking )
StreamSource streamSource = new StreamSource(reader, source != null ? source.getLocation() : null); StreamSource streamSource = new StreamSource(reader, source != null ? source.getLocation() : null);
#else #else
StreamSource streamSource = new StreamSource(reader); StreamSource streamSource = new StreamSource(reader);
#end #end
XMLInputFactory factory = getXMLInputFactory();
XMLStreamReader parser = factory.createXMLStreamReader(streamSource); XMLStreamReader parser = factory.createXMLStreamReader(streamSource);
#if ( $locationTracking ) #if ( $locationTracking )
return read(parser, strict, source); return read(parser, strict, source);
@ -458,13 +475,12 @@ public class ${className} {
#else #else
public ${root.name} read(InputStream in, boolean strict) throws XMLStreamException { public ${root.name} read(InputStream in, boolean strict) throws XMLStreamException {
#end #end
XMLInputFactory factory = XMLInputFactory.newFactory();
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
#if ( $locationTracking ) #if ( $locationTracking )
StreamSource streamSource = new StreamSource(in, source != null ? source.getLocation() : null); StreamSource streamSource = new StreamSource(in, source != null ? source.getLocation() : null);
#else #else
StreamSource streamSource = new StreamSource(in); StreamSource streamSource = new StreamSource(in);
#end #end
XMLInputFactory factory = getXMLInputFactory();
XMLStreamReader parser = factory.createXMLStreamReader(streamSource); XMLStreamReader parser = factory.createXMLStreamReader(streamSource);
#if ( $locationTracking ) #if ( $locationTracking )
return read(parser, strict, source); return read(parser, strict, source);