提交 ab21dbad 编写于 作者: A Arjen Poutsma

SPR-7621 - AbstractStaxContentHandler sets NamespaceContext on every start/end element

上级 c32c0d56
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -35,8 +35,11 @@ abstract class AbstractStaxContentHandler implements ContentHandler {
private SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
private boolean namespaceContextChanged = false;
public final void startDocument() throws SAXException {
namespaceContext.clear();
namespaceContextChanged = false;
try {
startDocumentInternal();
}
......@@ -49,6 +52,7 @@ abstract class AbstractStaxContentHandler implements ContentHandler {
public final void endDocument() throws SAXException {
namespaceContext.clear();
namespaceContextChanged = false;
try {
endDocumentInternal();
}
......@@ -66,6 +70,7 @@ abstract class AbstractStaxContentHandler implements ContentHandler {
*/
public final void startPrefixMapping(String prefix, String uri) {
namespaceContext.bindNamespaceUri(prefix, uri);
namespaceContextChanged = true;
}
/**
......@@ -75,11 +80,13 @@ abstract class AbstractStaxContentHandler implements ContentHandler {
*/
public final void endPrefixMapping(String prefix) {
namespaceContext.removeBinding(prefix);
namespaceContextChanged = true;
}
public final void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
try {
startElementInternal(toQName(uri, qName), atts, namespaceContext);
startElementInternal(toQName(uri, qName), atts, namespaceContextChanged ? namespaceContext : null);
namespaceContextChanged = false;
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle startElement: " + ex.getMessage(), ex);
......@@ -91,7 +98,8 @@ abstract class AbstractStaxContentHandler implements ContentHandler {
public final void endElement(String uri, String localName, String qName) throws SAXException {
try {
endElementInternal(toQName(uri, qName), namespaceContext);
endElementInternal(toQName(uri, qName), namespaceContextChanged ? namespaceContext : null);
namespaceContextChanged = false;
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle endElement: " + ex.getMessage(), ex);
......
......@@ -95,13 +95,13 @@ class StaxEventContentHandler extends AbstractStaxContentHandler {
throws XMLStreamException {
List attributes = getAttributes(atts);
List namespaces = createNamespaces(namespaceContext);
consumeEvent(eventFactory.createStartElement(name, attributes.iterator(), namespaces.iterator()));
consumeEvent(eventFactory.createStartElement(name, attributes.iterator(), namespaces != null ? namespaces.iterator() : null));
}
@Override
protected void endElementInternal(QName name, SimpleNamespaceContext namespaceContext) throws XMLStreamException {
List namespaces = createNamespaces(namespaceContext);
consumeEvent(eventFactory.createEndElement(name, namespaces.iterator()));
consumeEvent(eventFactory.createEndElement(name, namespaces != null ? namespaces.iterator() : null));
}
@Override
......@@ -128,6 +128,10 @@ class StaxEventContentHandler extends AbstractStaxContentHandler {
/** Creates and returns a list of <code>NameSpace</code> objects from the <code>NamespaceContext</code>. */
private List<Namespace> createNamespaces(SimpleNamespaceContext namespaceContext) {
if (namespaceContext == null) {
return null;
}
List<Namespace> namespaces = new ArrayList<Namespace>();
String defaultNamespaceUri = namespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX);
if (StringUtils.hasLength(defaultNamespaceUri)) {
......@@ -188,4 +192,4 @@ class StaxEventContentHandler extends AbstractStaxContentHandler {
return locator.getSystemId();
}
}
}
\ No newline at end of file
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册