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

@@ -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;
}
}