Added XPathExpressionFactoryBean.

This commit is contained in:
Arjen Poutsma
2007-04-17 14:30:42 +00:00
parent 4bb6f90081
commit 4cfe029eea
9 changed files with 123 additions and 24 deletions

View File

@@ -32,10 +32,6 @@
<artifactId>spring-ws-core</artifactId>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>

View File

@@ -22,10 +22,6 @@
<artifactId>spring-oxm</artifactId>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>

View File

@@ -63,11 +63,6 @@
<groupId>org.springframework.ws</groupId>
<artifactId>spring-xml</artifactId>
</dependency>
<!--Spring dependencies-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<!-- XML handling dependencies -->
<dependency>
<groupId>xml-apis</groupId>

View File

@@ -657,6 +657,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@@ -22,10 +22,6 @@
<scope>test</scope>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>

View File

@@ -6,6 +6,8 @@
</properties>
<body>
<release version="1.0-RC1">
<action dev="poutsma" type="add">Added XPathExpressionFactoryBean, for injection of XPath expressions
</action>
<action dev="poutsma" type="fix" issue="SWS-106">Default message factory for WebServiceTemplate</action>
<action dev="poutsma" type="fix" issue="SWS-79">Add faults into dynamically created WSDL</action>
<action dev="poutsma" type="add" issue="SWS-20">Added MethodEndpoint functionality, invoking methods for

View File

@@ -17,18 +17,16 @@
package org.springframework.xml.xpath;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.springframework.xml.namespace.SimpleNamespaceContext;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.springframework.xml.namespace.SimpleNamespaceContext;
/**
* JAXP 1.3-specific factory creating <code>XPathExpression</code>s.
*
@@ -85,9 +83,7 @@ abstract class Jaxp13XPathExpressionFactory {
}
}
/**
* JAXP 1.3 implementation of the <code>XPathExpression</code> interface.
*/
/** JAXP 1.3 implementation of the <code>XPathExpression</code> interface. */
private static class Jaxp13XPathExpression implements XPathExpression {
javax.xml.xpath.XPathExpression xpathExpression;
@@ -107,7 +103,10 @@ abstract class Jaxp13XPathExpressionFactory {
private Object evaluate(Node node, QName returnType) {
try {
return xpathExpression.evaluate(node, returnType);
// XPathExpression is not thread-safe
synchronized (xpathExpression) {
return xpathExpression.evaluate(node, returnType);
}
}
catch (XPathExpressionException ex) {
throw new XPathException("Could not evaluate XPath expression:" + ex.getMessage(), ex);

View File

@@ -0,0 +1,73 @@
/*
* 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.xpath;
import java.util.Properties;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* Spring {@link FactoryBean} for {@link XPathExpression} object. Facilitates injection of XPath expressions into
* endpoint beans.
* <p/>
* Uses {@link XPathExpressionFactory} underneath, so support is provided for JAXP 1.3, Jaxen, and Xalan XPaths.
*
* @author Arjen Poutsma
* @see #setExpression(String)
*/
public class XPathExpressionFactoryBean implements FactoryBean, InitializingBean {
private Properties namespaces;
private String expressionString;
private XPathExpression expression;
/** Sets the XPath expression. Setting this property is required. */
public void setExpression(String expression) {
expressionString = expression;
}
/** Sets the namespaces for the expressions. The given properties binds string prefixes to string namespaces. */
public void setNamespaces(Properties namespaces) {
this.namespaces = namespaces;
}
public void afterPropertiesSet() throws IllegalStateException, XPathParseException {
Assert.notNull(expressionString, "expression is required");
if (namespaces == null || namespaces.isEmpty()) {
expression = XPathExpressionFactory.createXPathExpression(expressionString);
}
else {
expression = XPathExpressionFactory.createXPathExpression(expressionString, namespaces);
}
}
public Object getObject() throws Exception {
return expression;
}
public Class getObjectType() {
return XPathExpression.class;
}
public boolean isSingleton() {
return true;
}
}

View File

@@ -0,0 +1,38 @@
/*
* 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.xpath;
import junit.framework.TestCase;
public class XPathExpressionFactoryBeanTest extends TestCase {
private XPathExpressionFactoryBean factoryBean;
protected void setUp() throws Exception {
factoryBean = new XPathExpressionFactoryBean();
}
public void testFactoryBean() throws Exception {
factoryBean.setExpression("/root");
factoryBean.afterPropertiesSet();
Object result = factoryBean.getObject();
assertNotNull("No result obtained", result);
assertTrue("No XPathExpression returned", result instanceof XPathExpression);
assertTrue("Not a singleton", factoryBean.isSingleton());
assertEquals("Not a XPathExpresison", XPathExpression.class, factoryBean.getObjectType());
}
}