01: /*
02: * Copyright 2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.xml.xpath;
18:
19: import java.util.Properties;
20:
21: import org.springframework.beans.factory.FactoryBean;
22: import org.springframework.beans.factory.InitializingBean;
23: import org.springframework.util.Assert;
24:
25: /**
26: * Spring {@link FactoryBean} for {@link XPathExpression} object. Facilitates injection of XPath expressions into
27: * endpoint beans.
28: * <p/>
29: * Uses {@link XPathExpressionFactory} underneath, so support is provided for JAXP 1.3, and Jaxen XPaths.
30: *
31: * @author Arjen Poutsma
32: * @see #setExpression(String)
33: * @since 1.0.0
34: */
35: public class XPathExpressionFactoryBean implements FactoryBean,
36: InitializingBean {
37:
38: private Properties namespaces;
39:
40: private String expressionString;
41:
42: private XPathExpression expression;
43:
44: /** Sets the XPath expression. Setting this property is required. */
45: public void setExpression(String expression) {
46: expressionString = expression;
47: }
48:
49: /** Sets the namespaces for the expressions. The given properties binds string prefixes to string namespaces. */
50: public void setNamespaces(Properties namespaces) {
51: this .namespaces = namespaces;
52: }
53:
54: public void afterPropertiesSet() throws IllegalStateException,
55: XPathParseException {
56: Assert.notNull(expressionString, "expression is required");
57: if (namespaces == null || namespaces.isEmpty()) {
58: expression = XPathExpressionFactory
59: .createXPathExpression(expressionString);
60: } else {
61: expression = XPathExpressionFactory.createXPathExpression(
62: expressionString, namespaces);
63: }
64: }
65:
66: public Object getObject() throws Exception {
67: return expression;
68: }
69:
70: public Class getObjectType() {
71: return XPathExpression.class;
72: }
73:
74: public boolean isSingleton() {
75: return true;
76: }
77: }
|