Refactored DomUtils into relevant classes.
This commit is contained in:
@@ -41,5 +41,12 @@
|
||||
<groupId>stax</groupId>
|
||||
<artifactId>stax-api</artifactId>
|
||||
</dependency>
|
||||
<!-- Test dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<scope>test</scope>
|
||||
<version>2.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -17,12 +17,13 @@
|
||||
package org.springframework.ws.server.endpoint.adapter;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Properties;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
@@ -33,8 +34,8 @@ import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.server.endpoint.MethodEndpoint;
|
||||
import org.springframework.ws.server.endpoint.annotation.XPathParam;
|
||||
import org.springframework.xml.dom.DomUtils;
|
||||
import org.springframework.xml.namespace.SimpleNamespaceContext;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
@@ -105,25 +106,14 @@ public class XPathParamAnnotationMethodEndpointAdapter extends AbstractMethodEnd
|
||||
}
|
||||
|
||||
protected void invokeInternal(MessageContext messageContext, MethodEndpoint methodEndpoint) throws Exception {
|
||||
try {
|
||||
Element payloadElement =
|
||||
DomUtils.getRootElement(messageContext.getRequest().getPayloadSource(), getTransformerFactory());
|
||||
Object[] args = getMethodArguments(payloadElement, methodEndpoint.getMethod());
|
||||
Object result = methodEndpoint.invoke(args);
|
||||
if (result != null && result instanceof Source) {
|
||||
Source responseSource = (Source) result;
|
||||
WebServiceMessage response = messageContext.getResponse();
|
||||
Transformer transformer = createTransformer();
|
||||
transformer.transform(responseSource, response.getPayloadResult());
|
||||
}
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
if (ex.getTargetException() instanceof Exception) {
|
||||
throw (Exception) ex.getTargetException();
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
Element payloadElement = getRootElement(messageContext.getRequest().getPayloadSource());
|
||||
Object[] args = getMethodArguments(payloadElement, methodEndpoint.getMethod());
|
||||
Object result = methodEndpoint.invoke(args);
|
||||
if (result != null && result instanceof Source) {
|
||||
Source responseSource = (Source) result;
|
||||
WebServiceMessage response = messageContext.getResponse();
|
||||
Transformer transformer = createTransformer();
|
||||
transformer.transform(responseSource, response.getPayloadResult());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,4 +159,19 @@ public class XPathParamAnnotationMethodEndpointAdapter extends AbstractMethodEnd
|
||||
return xpath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root element of the given source.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @return the root element
|
||||
*/
|
||||
private Element getRootElement(Source source) throws TransformerException {
|
||||
Transformer transformer = createTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Marks an endpoint method as the handler for an incoming request. The annotation value signifies the value for the
|
||||
* request payload root element that is handled by the method.
|
||||
* Marks an endpoint method as the handler for an incoming request. The annotation values signify the the request
|
||||
* payload root element that is handled by the method.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping
|
||||
@@ -34,6 +34,18 @@ import java.lang.annotation.Target;
|
||||
@Documented
|
||||
public @interface PayloadRoot {
|
||||
|
||||
String value();
|
||||
/**
|
||||
* Signifies the local part of the payload root element handled by the annotated method.
|
||||
*
|
||||
* @see #namespace()
|
||||
*/
|
||||
String localPart();
|
||||
|
||||
/**
|
||||
* Signifies the namespace of the payload root element handled by the annotated method.
|
||||
*
|
||||
* @see #localPart()
|
||||
*/
|
||||
String namespace() default "";
|
||||
|
||||
}
|
||||
|
||||
@@ -20,12 +20,11 @@ import java.lang.reflect.Method;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.server.EndpointMapping;
|
||||
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
|
||||
import org.springframework.xml.dom.DomUtils;
|
||||
import org.springframework.xml.namespace.QNameUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implementation of the {@link EndpointMapping} interface that uses the {@link PayloadRoot} annotation to map methods
|
||||
@@ -35,7 +34,8 @@ import org.w3c.dom.Element;
|
||||
* <pre>
|
||||
* @Endpoint
|
||||
* public class MyEndpoint{
|
||||
* @Payload("{http://springframework.org/spring-ws}Request")
|
||||
* @Payload(localPart = "Request",
|
||||
* namespace = "http://springframework.org/spring-ws")
|
||||
* public Source doSomethingWithRequest() {
|
||||
* ...
|
||||
* }
|
||||
@@ -53,15 +53,25 @@ public class PayloadRootAnnotationMethodEndpointMapping extends AbstractAnnotati
|
||||
}
|
||||
|
||||
protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
|
||||
Element payloadElement =
|
||||
DomUtils.getRootElement(messageContext.getRequest().getPayloadSource(), transformerFactory);
|
||||
QName qName = QNameUtils.getQNameForNode(payloadElement);
|
||||
QName qName = QNameUtils.getQNameForSource(messageContext.getRequest().getPayloadSource(), transformerFactory);
|
||||
return qName != null ? qName.toString() : null;
|
||||
}
|
||||
|
||||
protected String getLookupKeyForMethod(Method method) {
|
||||
PayloadRoot annotation = method.getAnnotation(PayloadRoot.class);
|
||||
return annotation != null ? annotation.value() : null;
|
||||
if (annotation != null) {
|
||||
QName qname;
|
||||
if (StringUtils.hasLength(annotation.localPart()) && StringUtils.hasLength(annotation.namespace())) {
|
||||
qname = new QName(annotation.namespace(), annotation.localPart());
|
||||
}
|
||||
else {
|
||||
qname = new QName(annotation.localPart());
|
||||
}
|
||||
return qname.toString();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,10 +16,14 @@
|
||||
|
||||
package org.springframework.ws.server.endpoint.adapter;
|
||||
|
||||
import java.util.Properties;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
import static org.easymock.EasyMock.*;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.WebServiceMessageFactory;
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
@@ -28,8 +32,11 @@ import org.springframework.ws.server.endpoint.MethodEndpoint;
|
||||
import org.springframework.ws.server.endpoint.annotation.XPathParam;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
public class XPathParamAnnotationMethodEndpointAdapterTest extends TestCase {
|
||||
|
||||
@@ -41,6 +48,8 @@ public class XPathParamAnnotationMethodEndpointAdapterTest extends TestCase {
|
||||
|
||||
private boolean supportedSourceInvoked;
|
||||
|
||||
private boolean namespacesInvoked;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
adapter = new XPathParamAnnotationMethodEndpointAdapter();
|
||||
adapter.afterPropertiesSet();
|
||||
@@ -84,13 +93,10 @@ public class XPathParamAnnotationMethodEndpointAdapterTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testInvokeTypes() throws Exception {
|
||||
MockControl messageControl = MockControl.createControl(WebServiceMessage.class);
|
||||
WebServiceMessage messageMock = (WebServiceMessage) messageControl.getMock();
|
||||
messageControl.expectAndReturn(messageMock.getPayloadSource(), new StringSource(CONTENTS));
|
||||
messageControl.replay();
|
||||
MockControl factoryControl = MockControl.createControl(WebServiceMessageFactory.class);
|
||||
WebServiceMessageFactory factoryMock = (WebServiceMessageFactory) factoryControl.getMock();
|
||||
factoryControl.replay();
|
||||
WebServiceMessage messageMock = createMock(WebServiceMessage.class);
|
||||
expect(messageMock.getPayloadSource()).andReturn(new StringSource(CONTENTS));
|
||||
WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
|
||||
replay(messageMock, factoryMock);
|
||||
|
||||
MessageContext messageContext = new DefaultMessageContext(messageMock, factoryMock);
|
||||
MethodEndpoint endpoint = new MethodEndpoint(this, "supportedTypes",
|
||||
@@ -98,30 +104,58 @@ public class XPathParamAnnotationMethodEndpointAdapterTest extends TestCase {
|
||||
adapter.invoke(messageContext, endpoint);
|
||||
assertTrue("Method not invoked", supportedTypesInvoked);
|
||||
|
||||
messageControl.verify();
|
||||
factoryControl.verify();
|
||||
|
||||
verify(messageMock, factoryMock);
|
||||
}
|
||||
|
||||
public void testInvokeSource() throws Exception {
|
||||
MockControl messageControl = MockControl.createControl(WebServiceMessage.class);
|
||||
WebServiceMessage requestMock = (WebServiceMessage) messageControl.getMock();
|
||||
WebServiceMessage responseMock = (WebServiceMessage) messageControl.getMock();
|
||||
messageControl.expectAndReturn(requestMock.getPayloadSource(), new StringSource(CONTENTS));
|
||||
messageControl.expectAndReturn(responseMock.getPayloadResult(), new StringResult());
|
||||
messageControl.replay();
|
||||
MockControl factoryControl = MockControl.createControl(WebServiceMessageFactory.class);
|
||||
WebServiceMessageFactory factoryMock = (WebServiceMessageFactory) factoryControl.getMock();
|
||||
factoryControl.expectAndReturn(factoryMock.createWebServiceMessage(), responseMock);
|
||||
factoryControl.replay();
|
||||
WebServiceMessage requestMock = createMock(WebServiceMessage.class);
|
||||
WebServiceMessage responseMock = createMock(WebServiceMessage.class);
|
||||
expect(requestMock.getPayloadSource()).andReturn(new StringSource(CONTENTS));
|
||||
expect(responseMock.getPayloadResult()).andReturn(new StringResult());
|
||||
WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
|
||||
expect(factoryMock.createWebServiceMessage()).andReturn(responseMock);
|
||||
replay(requestMock, responseMock, factoryMock);
|
||||
|
||||
MessageContext messageContext = new DefaultMessageContext(requestMock, factoryMock);
|
||||
MethodEndpoint endpoint = new MethodEndpoint(this, "supportedSource", new Class[]{String.class});
|
||||
adapter.invoke(messageContext, endpoint);
|
||||
assertTrue("Method not invoked", supportedSourceInvoked);
|
||||
|
||||
messageControl.verify();
|
||||
factoryControl.verify();
|
||||
verify(requestMock, responseMock, factoryMock);
|
||||
}
|
||||
|
||||
public void testInvokeVoidDom() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.newDocument();
|
||||
String rootNamespace = "http://rootnamespace";
|
||||
Element rootElement = document.createElementNS(rootNamespace, "root");
|
||||
document.appendChild(rootElement);
|
||||
String childNamespace = "http://childnamespace";
|
||||
Element first = document.createElementNS(childNamespace, "child");
|
||||
rootElement.appendChild(first);
|
||||
Text text = document.createTextNode("value");
|
||||
first.appendChild(text);
|
||||
Element second = document.createElementNS(rootNamespace, "other-child");
|
||||
rootElement.appendChild(second);
|
||||
text = document.createTextNode("other-value");
|
||||
second.appendChild(text);
|
||||
|
||||
WebServiceMessage requestMock = createMock(WebServiceMessage.class);
|
||||
expect(requestMock.getPayloadSource()).andReturn(new DOMSource(first));
|
||||
WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
|
||||
|
||||
replay(requestMock, factoryMock);
|
||||
|
||||
Properties namespaces = new Properties();
|
||||
namespaces.setProperty("root", rootNamespace);
|
||||
namespaces.setProperty("child", childNamespace);
|
||||
adapter.setNamespaces(namespaces);
|
||||
|
||||
MessageContext messageContext = new DefaultMessageContext(requestMock, factoryMock);
|
||||
MethodEndpoint endpoint = new MethodEndpoint(this, "namespaces", new Class[]{Node.class});
|
||||
adapter.invoke(messageContext, endpoint);
|
||||
assertTrue("Method not invoked", namespacesInvoked);
|
||||
}
|
||||
|
||||
public void supportedVoid(@XPathParam("/")String param1) {
|
||||
@@ -160,4 +194,9 @@ public class XPathParamAnnotationMethodEndpointAdapterTest extends TestCase {
|
||||
|
||||
public void unsupportedInvalidParamType(@XPathParam("/")int param1) {
|
||||
}
|
||||
|
||||
public void namespaces(@XPathParam(".")Node param) {
|
||||
namespacesInvoked = true;
|
||||
assertEquals("Invalid parameter", "child", param.getLocalName());
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ public class PayloadRootAnnotationMethodEndpointMappingTest extends TestCase {
|
||||
@Endpoint
|
||||
private static class MyEndpoint {
|
||||
|
||||
@PayloadRoot("{http://springframework.org/spring-ws}Request")
|
||||
@PayloadRoot(localPart = "Request", namespace = "http://springframework.org/spring-ws")
|
||||
public void doIt() {
|
||||
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public class PayloadRootAnnotationMethodEndpointMappingTest extends TestCase {
|
||||
|
||||
private static class OtherBean {
|
||||
|
||||
@PayloadRoot("{http://springframework.org/spring-ws}Request2")
|
||||
@PayloadRoot(localPart = "Request2", namespace = "http://springframework.org/spring-ws")
|
||||
public void doIt() {
|
||||
|
||||
}
|
||||
|
||||
@@ -155,5 +155,12 @@
|
||||
<artifactId>jetty</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Test dependencies -->
|
||||
<dependency>
|
||||
<groupId>easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>1.2_Java1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -21,9 +21,7 @@ import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.xml.dom.DomUtils;
|
||||
import org.springframework.xml.namespace.QNameUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implementation of the <code>EndpointMapping</code> interface to map from the qualified name of the request payload
|
||||
@@ -54,9 +52,7 @@ public class PayloadRootQNameEndpointMapping extends AbstractQNameEndpointMappin
|
||||
}
|
||||
|
||||
protected QName resolveQName(MessageContext messageContext) throws TransformerException {
|
||||
Element payloadElement =
|
||||
DomUtils.getRootElement(messageContext.getRequest().getPayloadSource(), transformerFactory);
|
||||
return QNameUtils.getQNameForNode(payloadElement);
|
||||
return QNameUtils.getQNameForSource(messageContext.getRequest().getPayloadSource(), transformerFactory);
|
||||
}
|
||||
|
||||
|
||||
|
||||
18
pom.xml
18
pom.xml
@@ -126,7 +126,7 @@
|
||||
</profile>
|
||||
</profiles>
|
||||
<properties>
|
||||
<spring.version>2.0.4</spring.version>
|
||||
<spring.version>2.0.5</spring.version>
|
||||
</properties>
|
||||
<repositories>
|
||||
<repository>
|
||||
@@ -429,6 +429,11 @@
|
||||
<artifactId>spring-hibernate3</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jpa</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-dao</artifactId>
|
||||
@@ -635,12 +640,6 @@
|
||||
<version>3.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>1.2_Java1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xmlunit</groupId>
|
||||
<artifactId>xmlunit</artifactId>
|
||||
@@ -681,11 +680,6 @@
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xmlunit</groupId>
|
||||
<artifactId>xmlunit</artifactId>
|
||||
|
||||
@@ -65,5 +65,12 @@
|
||||
<artifactId>wstx-asl</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Test dependencies -->
|
||||
<dependency>
|
||||
<groupId>easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>1.2_Java1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.dom;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Convenient utility methods for dealing with DOM.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class DomUtils {
|
||||
|
||||
/**
|
||||
* Returns the root element of the given source, transforming it if necessary.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
|
||||
* @return the root element
|
||||
*/
|
||||
public static Element getRootElement(Source source, TransformerFactory transformerFactory)
|
||||
throws TransformerException {
|
||||
if (source instanceof DOMSource) {
|
||||
DOMSource domSource = (DOMSource) source;
|
||||
Node node = domSource.getNode();
|
||||
if (node == null) {
|
||||
return null;
|
||||
}
|
||||
else if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
return (Element) node;
|
||||
}
|
||||
else if (node.getNodeType() == Node.DOCUMENT_NODE) {
|
||||
Document document = (Document) node;
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
}
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -17,19 +17,30 @@
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Helper class for using <code>javax.xml.namespace.QName</code>.
|
||||
* Helper class for using {@link QName}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see javax.xml.namespace.QName
|
||||
*/
|
||||
public abstract class QNameUtils {
|
||||
|
||||
/** Indicates whether {@link QName} has a prefix. The first release of the class did not have this. */
|
||||
private static boolean qNameHasPrefix;
|
||||
|
||||
static {
|
||||
@@ -51,7 +62,7 @@ public abstract class QNameUtils {
|
||||
* @param localPart local part of the <code>QName</code>
|
||||
* @param prefix prefix of the <code>QName</code>. May be ignored.
|
||||
* @return the created <code>QName</code>
|
||||
* @see QName#QName(String, String, String)
|
||||
* @see QName#QName(String,String,String)
|
||||
*/
|
||||
public static QName createQName(String namespaceUri, String localPart, String prefix) {
|
||||
if (qNameHasPrefix) {
|
||||
@@ -117,6 +128,44 @@ public abstract class QNameUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root qualified name of the given source, transforming it if necessary.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
|
||||
* @return the root element
|
||||
*/
|
||||
public static QName getQNameForSource(Source source, TransformerFactory transformerFactory)
|
||||
throws TransformerException {
|
||||
if (source instanceof DOMSource) {
|
||||
DOMSource domSource = (DOMSource) source;
|
||||
Node node = domSource.getNode();
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
return getQNameForNode(node);
|
||||
}
|
||||
else if (node.getNodeType() == Node.DOCUMENT_NODE) {
|
||||
Document document = (Document) node;
|
||||
return getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
}
|
||||
else if (source instanceof StaxSource) {
|
||||
StaxSource staxSource = (StaxSource) source;
|
||||
if (staxSource.getXMLStreamReader() != null) {
|
||||
XMLStreamReader streamReader = staxSource.getXMLStreamReader();
|
||||
if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT ||
|
||||
streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
|
||||
return streamReader.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
// we have no other option than to transform
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>QName</code> to a qualified name, as used by DOM and SAX. The returned string has a format of
|
||||
* <code>prefix:localName</code> if the prefix is set, or just <code>localName</code> if not.
|
||||
@@ -185,5 +234,4 @@ public abstract class QNameUtils {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,14 @@ package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.Properties;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
@@ -61,4 +66,19 @@ public abstract class AbstractXPathTemplate extends TransformerObjectSupport imp
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root element of the given source.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @return the root element
|
||||
*/
|
||||
protected Element getRootElement(Source source) throws TransformerException {
|
||||
Transformer transformer = createTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.jaxen.JaxenException;
|
||||
import org.jaxen.SimpleNamespaceContext;
|
||||
import org.jaxen.XPath;
|
||||
import org.jaxen.dom.DOMXPath;
|
||||
import org.springframework.xml.dom.DomUtils;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
@@ -43,7 +42,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public boolean evaluateAsBoolean(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return xpath.booleanValueOf(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -57,7 +56,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public Node evaluateAsNode(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return (Node) xpath.selectSingleNode(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -71,7 +70,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public List evaluateAsNodeList(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return xpath.selectNodes(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -85,7 +84,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public double evaluateAsDouble(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return xpath.numberValueOf(element).doubleValue();
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -99,7 +98,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public String evaluateAsString(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
return xpath.stringValueOf(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
@@ -113,7 +112,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public Object evaluateAsObject(String expression, Source context, NodeMapper nodeMapper) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
Node node = (Node) xpath.selectSingleNode(element);
|
||||
if (node != null) {
|
||||
try {
|
||||
@@ -139,7 +138,7 @@ public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
public List evaluate(String expression, Source context, NodeMapper nodeMapper) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
Element element = getRootElement(context);
|
||||
List nodes = (List) xpath.selectNodes(element);
|
||||
List results = new ArrayList(nodes.size());
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
|
||||
@@ -29,7 +29,6 @@ import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import javax.xml.xpath.XPathFactoryConfigurationException;
|
||||
|
||||
import org.springframework.xml.dom.DomUtils;
|
||||
import org.springframework.xml.namespace.SimpleNamespaceContext;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.DOMException;
|
||||
@@ -128,7 +127,8 @@ public class Jaxp13XPathTemplate extends AbstractXPathTemplate {
|
||||
}
|
||||
try {
|
||||
if (context instanceof StaxSource) {
|
||||
Element element = DomUtils.getRootElement(context, getTransformerFactory());
|
||||
// StaxSource is a subclass of SAXSource, but it has no InputSource, therefore we handle it differently
|
||||
Element element = getRootElement(context);
|
||||
return xpath.evaluate(expression, element, returnType);
|
||||
}
|
||||
else if (context instanceof SAXSource) {
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.dom;
|
||||
|
||||
import java.io.StringReader;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
public class DomUtilsTest extends TestCase {
|
||||
|
||||
private static final String NAMESPACE = "http://springframework.org/spring-ws";
|
||||
|
||||
private static final String LOCAL_NAME = "Root";
|
||||
|
||||
private static final String XML = "<" + LOCAL_NAME + " xmlns='" + NAMESPACE + "'/>";
|
||||
|
||||
private TransformerFactory transformerFactory;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
transformerFactory = TransformerFactory.newInstance();
|
||||
}
|
||||
|
||||
public void testGetRootElementDomSource() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.newDocument();
|
||||
Element rootElement = document.createElementNS(NAMESPACE, LOCAL_NAME);
|
||||
document.appendChild(rootElement);
|
||||
|
||||
testSource(new DOMSource(document));
|
||||
}
|
||||
|
||||
public void testGetRootElementSaxSource() throws Exception {
|
||||
InputSource inputSource = new InputSource(new StringReader(XML));
|
||||
testSource(new SAXSource(inputSource));
|
||||
}
|
||||
|
||||
public void testGetRootElementStreamSource() throws Exception {
|
||||
testSource(new StreamSource(new StringReader(XML)));
|
||||
}
|
||||
|
||||
private void testSource(Source source) throws TransformerException {
|
||||
Element result = DomUtils.getRootElement(source, transformerFactory);
|
||||
assertNotNull("No result", result);
|
||||
assertEquals("Invalid namespace", NAMESPACE, result.getNamespaceURI());
|
||||
assertEquals("Invalid local name", LOCAL_NAME, result.getLocalName());
|
||||
}
|
||||
}
|
||||
@@ -16,15 +16,25 @@
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import java.io.StringReader;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
public class QNameUtilsTest extends TestCase {
|
||||
|
||||
@@ -102,4 +112,54 @@ public class QNameUtilsTest extends TestCase {
|
||||
assertEquals("invalid prefix", "", result.getPrefix());
|
||||
assertEquals("invalid localname", "localName", result.getLocalPart());
|
||||
}
|
||||
|
||||
public void testGetQNameForDomSource() throws Exception {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element element = document.createElementNS("namespace", "prefix:localname");
|
||||
document.appendChild(element);
|
||||
Source source = new DOMSource(document);
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForStaxSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(contents));
|
||||
while (streamReader.getEventType() != XMLStreamConstants.START_ELEMENT) {
|
||||
streamReader.next();
|
||||
}
|
||||
Source source = new StaxSource(streamReader);
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForStreamSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
Source source = new StreamSource(new StringReader(contents));
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForSaxSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
Source source = new SAXSource(new InputSource(new StringReader(contents)));
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user