1 /*
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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, InitializingBean {
36
37 private Properties namespaces;
38
39 private String expressionString;
40
41 private XPathExpression expression;
42
43 /** Sets the XPath expression. Setting this property is required. */
44 public void setExpression(String expression) {
45 expressionString = expression;
46 }
47
48 /** Sets the namespaces for the expressions. The given properties binds string prefixes to string namespaces. */
49 public void setNamespaces(Properties namespaces) {
50 this.namespaces = namespaces;
51 }
52
53 public void afterPropertiesSet() throws IllegalStateException, XPathParseException {
54 Assert.notNull(expressionString, "expression is required");
55 if (namespaces == null || namespaces.isEmpty()) {
56 expression = XPathExpressionFactory.createXPathExpression(expressionString);
57 }
58 else {
59 expression = XPathExpressionFactory.createXPathExpression(expressionString, namespaces);
60 }
61 }
62
63 public Object getObject() throws Exception {
64 return expression;
65 }
66
67 public Class getObjectType() {
68 return XPathExpression.class;
69 }
70
71 public boolean isSingleton() {
72 return true;
73 }
74 }