1 /* 2 * Copyright 2005-2012 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.Collections; 20 import java.util.Map; 21 22 import org.springframework.util.Assert; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 /** 28 * Factory for compiled <code>XPathExpression</code>s, being aware of JAXP 1.3+ XPath functionality, and Jaxen. Mainly 29 * for internal use of the framework. 30 * <p/> 31 * The goal of this class is to avoid runtime dependencies a specific XPath engine, simply using the best XPath 32 * implementation that is available. Prefers JAXP 1.3+ XPath implementations to Jaxen. 33 * 34 * @author Arjen Poutsma 35 * @see XPathExpression 36 * @since 1.0.0 37 */ 38 public abstract class XPathExpressionFactory { 39 40 private static final Log logger = LogFactory.getLog(XPathExpressionFactory.class); 41 42 /** 43 * Create a compiled XPath expression using the given string. 44 * 45 * @param expression the XPath expression 46 * @return the compiled XPath expression 47 * @throws IllegalStateException if neither JAXP 1.3+, or Jaxen are available 48 * @throws XPathParseException if the given expression cannot be parsed 49 */ 50 public static XPathExpression createXPathExpression(String expression) 51 throws IllegalStateException, XPathParseException { 52 return createXPathExpression(expression, Collections.<String, String>emptyMap()); 53 } 54 55 /** 56 * Create a compiled XPath expression using the given string and namespaces. The namespace map should consist of 57 * string prefixes mapped to string namespaces. 58 * 59 * @param expression the XPath expression 60 * @param namespaces a map that binds string prefixes to string namespaces 61 * @return the compiled XPath expression 62 * @throws IllegalStateException if neither JAXP 1.3+, or Jaxen are available 63 * @throws XPathParseException if the given expression cannot be parsed 64 */ 65 public static XPathExpression createXPathExpression(String expression, Map<String, String> namespaces) 66 throws IllegalStateException, XPathParseException { 67 Assert.hasLength(expression, "expression is empty"); 68 if (namespaces == null) { 69 namespaces = Collections.emptyMap(); 70 } 71 try { 72 logger.trace("Creating [javax.xml.xpath.XPathExpression]"); 73 return Jaxp13XPathExpressionFactory.createXPathExpression(expression, namespaces); 74 } 75 catch (XPathException e) { 76 throw e; 77 } 78 } 79 80 81 }