Upgraded to Spring 4.0.3 and XMLUnit 1.5
Upgraded to XMLUnit 1.5, which broke some tests in SWS as well as SPR. Spring 4.0.3 fixed those, so upgraded to that as well.
This commit is contained in:
@@ -20,12 +20,10 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.namespace.QNameUtils;
|
||||
|
||||
import org.apache.axiom.om.OMAttribute;
|
||||
import org.apache.axiom.om.OMContainer;
|
||||
import org.apache.axiom.om.OMElement;
|
||||
@@ -37,145 +35,190 @@ import org.xml.sax.Locator;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.namespace.QNameUtils;
|
||||
|
||||
/**
|
||||
* Specific SAX {@link ContentHandler} and {@link LexicalHandler} that adds the resulting AXIOM OMElement to a specified
|
||||
* parent element when <code>endDocument</code> is called. Used for returing <code>SAXResult</code>s from Axiom
|
||||
* elements.
|
||||
*
|
||||
* Specific SAX {@link ContentHandler} and {@link LexicalHandler} that adds the resulting
|
||||
* AXIOM OMElement to a specified parent element when <code>endDocument</code> is called.
|
||||
* Used for returing <code>SAXResult</code>s from Axiom elements.
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class AxiomHandler implements ContentHandler, LexicalHandler {
|
||||
|
||||
private final OMFactory factory;
|
||||
private final OMFactory factory;
|
||||
|
||||
private final List<OMContainer> elements = new ArrayList<OMContainer>();
|
||||
private final List<OMContainer> elements = new ArrayList<OMContainer>();
|
||||
|
||||
private Map<String, String> namespaces = new HashMap<String, String>();
|
||||
private final OMContainer container;
|
||||
|
||||
private final OMContainer container;
|
||||
private int charactersType = XMLStreamConstants.CHARACTERS;
|
||||
|
||||
private int charactersType = XMLStreamConstants.CHARACTERS;
|
||||
private List<Map<String, String>> namespaceMappings =
|
||||
new ArrayList<Map<String, String>>();
|
||||
|
||||
AxiomHandler(OMContainer container, OMFactory factory) {
|
||||
Assert.notNull(container, "'container' must not be null");
|
||||
Assert.notNull(factory, "'factory' must not be null");
|
||||
this.factory = factory;
|
||||
this.container = container;
|
||||
}
|
||||
AxiomHandler(OMContainer container, OMFactory factory) {
|
||||
Assert.notNull(container, "'container' must not be null");
|
||||
Assert.notNull(factory, "'factory' must not be null");
|
||||
this.factory = factory;
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
private OMContainer getParent() {
|
||||
if (!elements.isEmpty()) {
|
||||
return elements.get(elements.size() - 1);
|
||||
}
|
||||
else {
|
||||
return container;
|
||||
}
|
||||
}
|
||||
private OMContainer getParent() {
|
||||
if (!elements.isEmpty()) {
|
||||
return elements.get(elements.size() - 1);
|
||||
}
|
||||
else {
|
||||
return container;
|
||||
}
|
||||
}
|
||||
|
||||
public void startPrefixMapping(String prefix, String uri) throws SAXException {
|
||||
namespaces.put(prefix, uri);
|
||||
}
|
||||
public void startDocument() throws SAXException {
|
||||
removeAllNamespaceMappings();
|
||||
newNamespaceMapping();
|
||||
}
|
||||
|
||||
public void endPrefixMapping(String prefix) throws SAXException {
|
||||
namespaces.remove(prefix);
|
||||
}
|
||||
public void endDocument() throws SAXException {
|
||||
removeAllNamespaceMappings();
|
||||
}
|
||||
|
||||
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
|
||||
OMContainer parent = getParent();
|
||||
OMNamespace ns = factory.createOMNamespace(uri, QNameUtils.toQName(uri, qName).getPrefix());
|
||||
OMElement element = factory.createOMElement(localName, ns, parent);
|
||||
for (Map.Entry<String, String> entry : namespaces.entrySet()) {
|
||||
String prefix = entry.getKey();
|
||||
if (prefix.length() == 0) {
|
||||
element.declareDefaultNamespace((String) entry.getValue());
|
||||
}
|
||||
else {
|
||||
element.declareNamespace((String) entry.getValue(), prefix);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < atts.getLength(); i++) {
|
||||
QName attrName = QNameUtils.toQName(atts.getURI(i), atts.getQName(i));
|
||||
String value = atts.getValue(i);
|
||||
if (!atts.getQName(i).startsWith("xmlns")) {
|
||||
OMNamespace namespace = factory.createOMNamespace(attrName.getNamespaceURI(), attrName.getPrefix());
|
||||
OMAttribute attribute = factory.createOMAttribute(attrName.getLocalPart(), namespace, value);
|
||||
element.addAttribute(attribute);
|
||||
}
|
||||
}
|
||||
public void startPrefixMapping(String prefix, String uri) throws SAXException {
|
||||
currentNamespaceMapping().put(prefix, uri);
|
||||
}
|
||||
|
||||
elements.add(element);
|
||||
}
|
||||
public void endPrefixMapping(String prefix) throws SAXException {
|
||||
}
|
||||
|
||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||
elements.remove(elements.size() - 1);
|
||||
}
|
||||
public void startElement(String uri, String localName, String qName,
|
||||
Attributes attributes) throws SAXException {
|
||||
OMContainer parent = getParent();
|
||||
OMNamespace ns = factory.createOMNamespace(uri,
|
||||
QNameUtils.toQName(uri, qName).getPrefix());
|
||||
OMElement element = factory.createOMElement(localName, ns, parent);
|
||||
|
||||
public void characters(char ch[], int start, int length) throws SAXException {
|
||||
String data = new String(ch, start, length);
|
||||
OMContainer parent = getParent();
|
||||
factory.createOMText(parent, data, charactersType);
|
||||
}
|
||||
// declare namespaces
|
||||
Map<String, String> namespaceMappings = currentNamespaceMapping();
|
||||
for (Map.Entry<String, String> entry : namespaceMappings.entrySet()) {
|
||||
String prefix = entry.getKey();
|
||||
String namespaceUri = entry.getValue();
|
||||
|
||||
public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
|
||||
charactersType = XMLStreamConstants.SPACE;
|
||||
characters(ch, start, length);
|
||||
charactersType = XMLStreamConstants.CHARACTERS;
|
||||
}
|
||||
if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
|
||||
element.declareDefaultNamespace(namespaceUri);
|
||||
} else {
|
||||
element.declareNamespace(namespaceUri, prefix);
|
||||
}
|
||||
}
|
||||
// declare attributes
|
||||
for (int i = 0; i < attributes.getLength(); i++) {
|
||||
QName attrName =
|
||||
QNameUtils.toQName(attributes.getURI(i), attributes.getQName(i));
|
||||
if (!isNamespaceDeclaration(attrName)) {
|
||||
OMNamespace namespace =
|
||||
factory.createOMNamespace(attrName.getNamespaceURI(),
|
||||
attrName.getPrefix());
|
||||
OMAttribute attribute =
|
||||
factory.createOMAttribute(attrName.getLocalPart(), namespace,
|
||||
attributes.getValue(i));
|
||||
element.addAttribute(attribute);
|
||||
}
|
||||
}
|
||||
elements.add(element);
|
||||
newNamespaceMapping();
|
||||
}
|
||||
|
||||
public void processingInstruction(String target, String data) throws SAXException {
|
||||
OMContainer parent = getParent();
|
||||
factory.createOMProcessingInstruction(parent, target, data);
|
||||
}
|
||||
private boolean isNamespaceDeclaration(QName qName) {
|
||||
String prefix = qName.getPrefix();
|
||||
String localPart = qName.getLocalPart();
|
||||
return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.length() == 0) ||
|
||||
(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && localPart.length() != 0);
|
||||
}
|
||||
|
||||
public void comment(char ch[], int start, int length) throws SAXException {
|
||||
String content = new String(ch, start, length);
|
||||
OMContainer parent = getParent();
|
||||
factory.createOMComment(parent, content);
|
||||
}
|
||||
public void endElement(String uri, String localName, String qName)
|
||||
throws SAXException {
|
||||
elements.remove(elements.size() - 1);
|
||||
removeNamespaceMapping();
|
||||
}
|
||||
|
||||
public void startCDATA() throws SAXException {
|
||||
charactersType = XMLStreamConstants.CDATA;
|
||||
}
|
||||
public void characters(char ch[], int start, int length) throws SAXException {
|
||||
String data = new String(ch, start, length);
|
||||
OMContainer parent = getParent();
|
||||
factory.createOMText(parent, data, charactersType);
|
||||
}
|
||||
|
||||
public void endCDATA() throws SAXException {
|
||||
charactersType = XMLStreamConstants.CHARACTERS;
|
||||
}
|
||||
public void ignorableWhitespace(char ch[], int start, int length)
|
||||
throws SAXException {
|
||||
charactersType = XMLStreamConstants.SPACE;
|
||||
characters(ch, start, length);
|
||||
charactersType = XMLStreamConstants.CHARACTERS;
|
||||
}
|
||||
|
||||
public void startEntity(String name) throws SAXException {
|
||||
if (!isPredefinedEntityReference(name)) {
|
||||
charactersType = XMLStreamConstants.ENTITY_REFERENCE;
|
||||
}
|
||||
}
|
||||
public void processingInstruction(String target, String data) throws SAXException {
|
||||
OMContainer parent = getParent();
|
||||
factory.createOMProcessingInstruction(parent, target, data);
|
||||
}
|
||||
|
||||
public void endEntity(String name) throws SAXException {
|
||||
charactersType = XMLStreamConstants.CHARACTERS;
|
||||
}
|
||||
public void comment(char ch[], int start, int length) throws SAXException {
|
||||
String content = new String(ch, start, length);
|
||||
OMContainer parent = getParent();
|
||||
factory.createOMComment(parent, content);
|
||||
}
|
||||
|
||||
private boolean isPredefinedEntityReference(String name) {
|
||||
return "lt".equals(name) || "gt".equals(name) || "amp".equals(name) || "quot".equals(name) ||
|
||||
"apos".equals(name);
|
||||
}
|
||||
public void startCDATA() throws SAXException {
|
||||
charactersType = XMLStreamConstants.CDATA;
|
||||
}
|
||||
|
||||
public void endCDATA() throws SAXException {
|
||||
charactersType = XMLStreamConstants.CHARACTERS;
|
||||
}
|
||||
|
||||
public void startEntity(String name) throws SAXException {
|
||||
if (!isPredefinedEntityReference(name)) {
|
||||
charactersType = XMLStreamConstants.ENTITY_REFERENCE;
|
||||
}
|
||||
}
|
||||
|
||||
public void endEntity(String name) throws SAXException {
|
||||
charactersType = XMLStreamConstants.CHARACTERS;
|
||||
}
|
||||
|
||||
private boolean isPredefinedEntityReference(String name) {
|
||||
return "lt".equals(name) || "gt".equals(name) || "amp".equals(name) ||
|
||||
"quot".equals(name) ||
|
||||
"apos".equals(name);
|
||||
}
|
||||
|
||||
/*
|
||||
* Unsupported
|
||||
*/
|
||||
|
||||
public void setDocumentLocator(Locator locator) {
|
||||
}
|
||||
public void setDocumentLocator(Locator locator) {
|
||||
}
|
||||
|
||||
public void startDocument() throws SAXException {
|
||||
}
|
||||
public void skippedEntity(String name) throws SAXException {
|
||||
}
|
||||
|
||||
public void endDocument() throws SAXException {
|
||||
}
|
||||
public void startDTD(String name, String publicId, String systemId)
|
||||
throws SAXException {
|
||||
}
|
||||
|
||||
public void skippedEntity(String name) throws SAXException {
|
||||
}
|
||||
public void endDTD() throws SAXException {
|
||||
}
|
||||
|
||||
public void startDTD(String name, String publicId, String systemId) throws SAXException {
|
||||
}
|
||||
private Map<String, String> currentNamespaceMapping() {
|
||||
return namespaceMappings.get(namespaceMappings.size() - 1);
|
||||
}
|
||||
|
||||
private void newNamespaceMapping() {
|
||||
namespaceMappings.add(new HashMap<String, String>());
|
||||
}
|
||||
|
||||
private void removeNamespaceMapping() {
|
||||
namespaceMappings.remove(namespaceMappings.size() - 1);
|
||||
}
|
||||
|
||||
private void removeAllNamespaceMappings() {
|
||||
namespaceMappings.clear();
|
||||
}
|
||||
|
||||
public void endDTD() throws SAXException {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,24 +16,33 @@
|
||||
|
||||
package org.springframework.ws.soap.axiom;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.StringReader;
|
||||
import java.util.Iterator;
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.namespace.NamespaceContext;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
import org.apache.axiom.om.OMAbstractFactory;
|
||||
import org.apache.axiom.om.OMDocument;
|
||||
import org.apache.axiom.om.OMElement;
|
||||
import org.apache.axiom.om.OMFactory;
|
||||
import org.apache.axiom.om.OMNamespace;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
public class AxiomHandlerTest {
|
||||
|
||||
@@ -158,4 +167,48 @@ public class AxiomHandlerTest {
|
||||
result.serialize(bos);
|
||||
assertXMLEqual("Invalid result", XML_3_ENTITY, bos.toString("UTF-8"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransformDom() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
String XML = "<root xmlns=\"http://www.springframework.org/spring-ws\" xmlns:attr=\"http://www.springframework.org/spring-ws/attr\" attr:attribute=\"value\">" +
|
||||
"<prefix:child xmlns:prefix=\"http://www.springframework.org/spring-ws/child\"/>" +
|
||||
"</root>";
|
||||
|
||||
Document document =
|
||||
documentBuilder.parse(new ByteArrayInputStream(XML.getBytes("UTF-8")));
|
||||
|
||||
DOMSource domSource = new DOMSource(document);
|
||||
handler = new AxiomHandler(result, factory);
|
||||
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
SAXResult saxResult = new SAXResult(handler);
|
||||
transformer.transform(domSource, saxResult);
|
||||
|
||||
OMElement root = result.getOMDocumentElement();
|
||||
assertEquals(2, getNamespaceCount(root));
|
||||
NamespaceContext namespaceContext = root.getNamespaceContext(false);
|
||||
assertEquals("http://www.springframework.org/spring-ws", namespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
|
||||
assertEquals("http://www.springframework.org/spring-ws/attr",
|
||||
namespaceContext.getNamespaceURI("attr"));
|
||||
|
||||
OMElement child = root.getFirstElement();
|
||||
assertEquals(1, getNamespaceCount(child));
|
||||
namespaceContext = child.getNamespaceContext(false);
|
||||
assertEquals("http://www.springframework.org/spring-ws", namespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
|
||||
assertEquals("http://www.springframework.org/spring-ws/child", namespaceContext.getNamespaceURI("prefix"));
|
||||
}
|
||||
|
||||
private int getNamespaceCount(OMElement element) {
|
||||
int i = 0;
|
||||
Iterator namespaces = element.getAllDeclaredNamespaces();
|
||||
while (namespaces.hasNext()) {
|
||||
namespaces.next();
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user