StaxEventContentHandler uses static inner class for Location adapter, in order to avoid leaks when caching events
Issue: SPR-9305
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2012 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -32,17 +32,16 @@ import javax.xml.stream.util.XMLEventConsumer;
|
|||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.Locator;
|
import org.xml.sax.Locator;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SAX <code>ContentHandler</code> that transforms callback calls to <code>XMLEvent</code>s and writes them to a
|
* SAX <code>ContentHandler</code> that transforms callback calls to <code>XMLEvent</code>s
|
||||||
* <code>XMLEventConsumer</code>.
|
* and writes them to a <code>XMLEventConsumer</code>.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @since 3.0
|
||||||
* @see XMLEvent
|
* @see XMLEvent
|
||||||
* @see XMLEventConsumer
|
* @see XMLEventConsumer
|
||||||
* @since 3.0
|
|
||||||
*/
|
*/
|
||||||
class StaxEventContentHandler extends AbstractStaxContentHandler {
|
class StaxEventContentHandler extends AbstractStaxContentHandler {
|
||||||
|
|
||||||
@@ -50,101 +49,82 @@ class StaxEventContentHandler extends AbstractStaxContentHandler {
|
|||||||
|
|
||||||
private final XMLEventConsumer eventConsumer;
|
private final XMLEventConsumer eventConsumer;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new instance of the <code>StaxEventContentHandler</code> that writes to the given
|
* Construct a new instance of the <code>StaxEventContentHandler</code> that writes to the given
|
||||||
* <code>XMLEventConsumer</code>. A default <code>XMLEventFactory</code> will be created.
|
* <code>XMLEventConsumer</code>. A default <code>XMLEventFactory</code> will be created.
|
||||||
*
|
|
||||||
* @param consumer the consumer to write events to
|
* @param consumer the consumer to write events to
|
||||||
*/
|
*/
|
||||||
StaxEventContentHandler(XMLEventConsumer consumer) {
|
StaxEventContentHandler(XMLEventConsumer consumer) {
|
||||||
Assert.notNull(consumer, "'consumer' must not be null");
|
this.eventFactory = XMLEventFactory.newInstance();
|
||||||
eventFactory = XMLEventFactory.newInstance();
|
this.eventConsumer = consumer;
|
||||||
eventConsumer = consumer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new instance of the <code>StaxEventContentHandler</code> that uses the given event factory to create
|
* Construct a new instance of the <code>StaxEventContentHandler</code> that uses the given
|
||||||
* events and writes to the given <code>XMLEventConsumer</code>.
|
* event factory to create events and writes to the given <code>XMLEventConsumer</code>.
|
||||||
*
|
|
||||||
* @param consumer the consumer to write events to
|
* @param consumer the consumer to write events to
|
||||||
* @param factory the factory used to create events
|
* @param factory the factory used to create events
|
||||||
*/
|
*/
|
||||||
StaxEventContentHandler(XMLEventConsumer consumer, XMLEventFactory factory) {
|
StaxEventContentHandler(XMLEventConsumer consumer, XMLEventFactory factory) {
|
||||||
eventFactory = factory;
|
this.eventFactory = factory;
|
||||||
eventConsumer = consumer;
|
this.eventConsumer = consumer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDocumentLocator(final Locator locator) {
|
public void setDocumentLocator(Locator locator) {
|
||||||
if (locator != null) {
|
if (locator != null) {
|
||||||
eventFactory.setLocation(new Location() {
|
this.eventFactory.setLocation(new LocatorLocationAdapter(locator));
|
||||||
|
|
||||||
public int getLineNumber() {
|
|
||||||
return locator.getLineNumber();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getColumnNumber() {
|
|
||||||
return locator.getColumnNumber();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCharacterOffset() {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPublicId() {
|
|
||||||
return locator.getPublicId();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSystemId() {
|
|
||||||
return locator.getSystemId();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void startDocumentInternal() throws XMLStreamException {
|
protected void startDocumentInternal() throws XMLStreamException {
|
||||||
consumeEvent(eventFactory.createStartDocument());
|
consumeEvent(this.eventFactory.createStartDocument());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void endDocumentInternal() throws XMLStreamException {
|
protected void endDocumentInternal() throws XMLStreamException {
|
||||||
consumeEvent(eventFactory.createEndDocument());
|
consumeEvent(this.eventFactory.createEndDocument());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void startElementInternal(QName name, Attributes atts, SimpleNamespaceContext namespaceContext)
|
protected void startElementInternal(QName name, Attributes atts, SimpleNamespaceContext namespaceContext)
|
||||||
throws XMLStreamException {
|
throws XMLStreamException {
|
||||||
|
|
||||||
List attributes = getAttributes(atts);
|
List attributes = getAttributes(atts);
|
||||||
List namespaces = createNamespaces(namespaceContext);
|
List namespaces = createNamespaces(namespaceContext);
|
||||||
consumeEvent(eventFactory.createStartElement(name, attributes.iterator(), namespaces != null ? namespaces.iterator() : null));
|
consumeEvent(this.eventFactory.createStartElement(name, attributes.iterator(),
|
||||||
|
(namespaces != null ? namespaces.iterator() : null)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void endElementInternal(QName name, SimpleNamespaceContext namespaceContext) throws XMLStreamException {
|
protected void endElementInternal(QName name, SimpleNamespaceContext namespaceContext) throws XMLStreamException {
|
||||||
List namespaces = createNamespaces(namespaceContext);
|
List namespaces = createNamespaces(namespaceContext);
|
||||||
consumeEvent(eventFactory.createEndElement(name, namespaces != null ? namespaces.iterator() : null));
|
consumeEvent(this.eventFactory.createEndElement(name, namespaces != null ? namespaces.iterator() : null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void charactersInternal(char[] ch, int start, int length) throws XMLStreamException {
|
protected void charactersInternal(char[] ch, int start, int length) throws XMLStreamException {
|
||||||
consumeEvent(eventFactory.createCharacters(new String(ch, start, length)));
|
consumeEvent(this.eventFactory.createCharacters(new String(ch, start, length)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void ignorableWhitespaceInternal(char[] ch, int start, int length) throws XMLStreamException {
|
protected void ignorableWhitespaceInternal(char[] ch, int start, int length) throws XMLStreamException {
|
||||||
consumeEvent(eventFactory.createIgnorableSpace(new String(ch, start, length)));
|
consumeEvent(this.eventFactory.createIgnorableSpace(new String(ch, start, length)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void processingInstructionInternal(String target, String data) throws XMLStreamException {
|
protected void processingInstructionInternal(String target, String data) throws XMLStreamException {
|
||||||
consumeEvent(eventFactory.createProcessingInstruction(target, data));
|
consumeEvent(this.eventFactory.createProcessingInstruction(target, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void consumeEvent(XMLEvent event) throws XMLStreamException {
|
private void consumeEvent(XMLEvent event) throws XMLStreamException {
|
||||||
eventConsumer.add(event);
|
this.eventConsumer.add(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Creates and returns a list of <code>NameSpace</code> objects from the <code>NamespaceContext</code>. */
|
/**
|
||||||
|
* Create and return a list of <code>NameSpace</code> objects from the <code>NamespaceContext</code>.
|
||||||
|
*/
|
||||||
private List<Namespace> createNamespaces(SimpleNamespaceContext namespaceContext) {
|
private List<Namespace> createNamespaces(SimpleNamespaceContext namespaceContext) {
|
||||||
if (namespaceContext == null) {
|
if (namespaceContext == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -153,12 +133,12 @@ class StaxEventContentHandler extends AbstractStaxContentHandler {
|
|||||||
List<Namespace> namespaces = new ArrayList<Namespace>();
|
List<Namespace> namespaces = new ArrayList<Namespace>();
|
||||||
String defaultNamespaceUri = namespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX);
|
String defaultNamespaceUri = namespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX);
|
||||||
if (StringUtils.hasLength(defaultNamespaceUri)) {
|
if (StringUtils.hasLength(defaultNamespaceUri)) {
|
||||||
namespaces.add(eventFactory.createNamespace(defaultNamespaceUri));
|
namespaces.add(this.eventFactory.createNamespace(defaultNamespaceUri));
|
||||||
}
|
}
|
||||||
for (Iterator iterator = namespaceContext.getBoundPrefixes(); iterator.hasNext();) {
|
for (Iterator iterator = namespaceContext.getBoundPrefixes(); iterator.hasNext();) {
|
||||||
String prefix = (String) iterator.next();
|
String prefix = (String) iterator.next();
|
||||||
String namespaceUri = namespaceContext.getNamespaceURI(prefix);
|
String namespaceUri = namespaceContext.getNamespaceURI(prefix);
|
||||||
namespaces.add(eventFactory.createNamespace(prefix, namespaceUri));
|
namespaces.add(this.eventFactory.createNamespace(prefix, namespaceUri));
|
||||||
}
|
}
|
||||||
return namespaces;
|
return namespaces;
|
||||||
}
|
}
|
||||||
@@ -168,18 +148,45 @@ class StaxEventContentHandler extends AbstractStaxContentHandler {
|
|||||||
for (int i = 0; i < attributes.getLength(); i++) {
|
for (int i = 0; i < attributes.getLength(); i++) {
|
||||||
QName name = toQName(attributes.getURI(i), attributes.getQName(i));
|
QName name = toQName(attributes.getURI(i), attributes.getQName(i));
|
||||||
if (!("xmlns".equals(name.getLocalPart()) || "xmlns".equals(name.getPrefix()))) {
|
if (!("xmlns".equals(name.getLocalPart()) || "xmlns".equals(name.getPrefix()))) {
|
||||||
list.add(eventFactory.createAttribute(name, attributes.getValue(i)));
|
list.add(this.eventFactory.createAttribute(name, attributes.getValue(i)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
/* No operation */
|
||||||
// No operation
|
|
||||||
//
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void skippedEntityInternal(String name) throws XMLStreamException {
|
protected void skippedEntityInternal(String name) throws XMLStreamException {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static final class LocatorLocationAdapter implements Location {
|
||||||
|
|
||||||
|
private final Locator locator;
|
||||||
|
|
||||||
|
public LocatorLocationAdapter(Locator locator) {
|
||||||
|
this.locator = locator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLineNumber() {
|
||||||
|
return this.locator.getLineNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getColumnNumber() {
|
||||||
|
return this.locator.getColumnNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCharacterOffset() {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPublicId() {
|
||||||
|
return this.locator.getPublicId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSystemId() {
|
||||||
|
return this.locator.getSystemId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user