Coverage Report - org.apache.camel.builder.xml.XPathBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
XPathBuilder
59% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.builder.xml;
 19  
 
 20  
 import static org.apache.camel.converter.ObjectConverter.toBoolean;
 21  
 import org.apache.camel.Exchange;
 22  
 import org.apache.camel.Expression;
 23  
 import org.apache.camel.Predicate;
 24  
 import org.apache.camel.RuntimeExpressionException;
 25  
 import org.apache.camel.Message;
 26  
 import org.w3c.dom.Document;
 27  
 import org.w3c.dom.Element;
 28  
 import org.xml.sax.InputSource;
 29  
 
 30  
 import javax.xml.namespace.QName;
 31  
 import javax.xml.xpath.XPath;
 32  
 import javax.xml.xpath.XPathConstants;
 33  
 import javax.xml.xpath.XPathExpression;
 34  
 import javax.xml.xpath.XPathExpressionException;
 35  
 import javax.xml.xpath.XPathFactory;
 36  
 import javax.xml.xpath.XPathFactoryConfigurationException;
 37  
 import javax.xml.xpath.XPathFunctionResolver;
 38  
 import java.io.StringReader;
 39  
 
 40  
 /**
 41  
  * Creates an XPath expression builder
 42  
  *
 43  
  * @version $Revision: 531854 $
 44  
  */
 45  16
 public class XPathBuilder<E extends Exchange> implements Expression<E>, Predicate<E> {
 46  
     private final String text;
 47  
     private XPathFactory xpathFactory;
 48  16
     private Class documentType = Document.class;
 49  16
     private QName resultType = null;
 50  16
     private String objectModelUri = null;
 51  
     private DefaultNamespaceContext namespaceContext;
 52  
     private XPathFunctionResolver functionResolver;
 53  
     private XPathExpression expression;
 54  16
     private MessageVariableResolver variableResolver = new MessageVariableResolver();
 55  
 
 56  
     public static XPathBuilder xpath(String text) {
 57  16
         return new XPathBuilder(text);
 58  
     }
 59  
 
 60  16
     public XPathBuilder(String text) {
 61  16
         this.text = text;
 62  16
     }
 63  
 
 64  
     @Override
 65  
     public String toString() {
 66  29
         return "XPath: " + text;
 67  
     }
 68  
 
 69  
     public boolean matches(E exchange) {
 70  12
         Object booleanResult = evaluateAs(exchange, XPathConstants.BOOLEAN);
 71  12
         return toBoolean(booleanResult);
 72  
     }
 73  
 
 74  
     public void assertMatches(String text, E exchange) throws AssertionError {
 75  4
         Object booleanResult = evaluateAs(exchange, XPathConstants.BOOLEAN);
 76  4
         if (!toBoolean(booleanResult)) {
 77  1
             throw new AssertionError(this + " failed on " + exchange + " as returned <" + booleanResult + ">");
 78  
         }
 79  3
     }
 80  
 
 81  
     public Object evaluate(E exchange) {
 82  5
         return evaluateAs(exchange, resultType);
 83  
     }
 84  
 
 85  
 
 86  
     // Builder methods
 87  
     //-------------------------------------------------------------------------
 88  
 
 89  
     /**
 90  
      * Sets the expression result type to boolean
 91  
      *
 92  
      * @return the current builder
 93  
      */
 94  
     public XPathBuilder<E> booleanResult() {
 95  0
         resultType = XPathConstants.BOOLEAN;
 96  0
         return this;
 97  
     }
 98  
 
 99  
     /**
 100  
      * Sets the expression result type to boolean
 101  
      *
 102  
      * @return the current builder
 103  
      */
 104  
     public XPathBuilder<E> nodeResult() {
 105  0
         resultType = XPathConstants.NODE;
 106  0
         return this;
 107  
     }
 108  
 
 109  
     /**
 110  
      * Sets the expression result type to boolean
 111  
      *
 112  
      * @return the current builder
 113  
      */
 114  
     public XPathBuilder<E> nodeSetResult() {
 115  0
         resultType = XPathConstants.NODESET;
 116  0
         return this;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Sets the expression result type to boolean
 121  
      *
 122  
      * @return the current builder
 123  
      */
 124  
     public XPathBuilder<E> numberResult() {
 125  0
         resultType = XPathConstants.NUMBER;
 126  0
         return this;
 127  
     }
 128  
 
 129  
     /**
 130  
      * Sets the expression result type to boolean
 131  
      *
 132  
      * @return the current builder
 133  
      */
 134  
     public XPathBuilder<E> stringResult() {
 135  0
         resultType = XPathConstants.STRING;
 136  0
         return this;
 137  
     }
 138  
 
 139  
     /**
 140  
      * Sets the object model URI to use
 141  
      *
 142  
      * @return the current builder
 143  
      */
 144  
     public XPathBuilder<E> objectModel(String uri) {
 145  0
         this.objectModelUri = uri;
 146  0
         return this;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Sets the {@link XPathFunctionResolver} instance to use on these XPath expressions
 151  
      *
 152  
      * @return the current builder
 153  
      */
 154  
     public XPathBuilder<E> functionResolver(XPathFunctionResolver functionResolver) {
 155  0
         this.functionResolver = functionResolver;
 156  0
         return this;
 157  
     }
 158  
 
 159  
     /**
 160  
      * Registers the namespace prefix and URI with the builder so that the prefix can be used in XPath expressions
 161  
      *
 162  
      * @param prefix is the namespace prefix that can be used in the XPath expressions
 163  
      * @param uri is the namespace URI to which the prefix refers
 164  
      * @return the current builder
 165  
      */
 166  
     public XPathBuilder<E> namespace(String prefix, String uri) {
 167  6
         getNamespaceContext().add(prefix, uri);
 168  6
         return this;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Registers a variable (in the global namespace) which can be referred to from XPath expressions
 173  
      */
 174  
     public XPathBuilder<E> variable(String name, Object value) {
 175  1
         variableResolver.addVariable(name, value);
 176  1
         return this;
 177  
     }
 178  
 
 179  
 
 180  
     // Properties
 181  
     //-------------------------------------------------------------------------
 182  
     public XPathFactory getXPathFactory() throws XPathFactoryConfigurationException {
 183  32
         if (xpathFactory == null) {
 184  27
             if (objectModelUri != null) {
 185  0
                 xpathFactory = XPathFactory.newInstance(objectModelUri);
 186  
             }
 187  27
             xpathFactory = XPathFactory.newInstance();
 188  
         }
 189  32
         return xpathFactory;
 190  
     }
 191  
 
 192  
     public void setXPathFactory(XPathFactory xpathFactory) {
 193  0
         this.xpathFactory = xpathFactory;
 194  0
     }
 195  
 
 196  
     public Class getDocumentType() {
 197  21
         return documentType;
 198  
     }
 199  
 
 200  
     public void setDocumentType(Class documentType) {
 201  0
         this.documentType = documentType;
 202  0
     }
 203  
 
 204  
     public String getText() {
 205  0
         return text;
 206  
     }
 207  
 
 208  
     public QName getResultType() {
 209  0
         return resultType;
 210  
     }
 211  
 
 212  
     public DefaultNamespaceContext getNamespaceContext() {
 213  24
         if (namespaceContext == null) {
 214  
             try {
 215  16
                 namespaceContext = new DefaultNamespaceContext(getXPathFactory());
 216  
             }
 217  0
             catch (XPathFactoryConfigurationException e) {
 218  0
                 throw new RuntimeExpressionException(e);
 219  16
             }
 220  
         }
 221  24
         return namespaceContext;
 222  
     }
 223  
 
 224  
     public void setNamespaceContext(DefaultNamespaceContext namespaceContext) {
 225  0
         this.namespaceContext = namespaceContext;
 226  0
     }
 227  
 
 228  
     public XPathFunctionResolver getFunctionResolver() {
 229  0
         return functionResolver;
 230  
     }
 231  
 
 232  
     public void setFunctionResolver(XPathFunctionResolver functionResolver) {
 233  0
         this.functionResolver = functionResolver;
 234  0
     }
 235  
 
 236  
     public XPathExpression getExpression() throws XPathFactoryConfigurationException, XPathExpressionException {
 237  21
         if (expression == null) {
 238  16
             expression = createXPathExpression();
 239  
         }
 240  21
         return expression;
 241  
     }
 242  
 
 243  
     public void setNamespacesFromDom(Element node) {
 244  1
         getNamespaceContext().setNamespacesFromDom(node);        
 245  1
     }
 246  
 
 247  
     // Implementation methods
 248  
     //-------------------------------------------------------------------------
 249  
 
 250  
 
 251  
     /**
 252  
      * Evaluates the expression as the given result type
 253  
      */
 254  
     protected synchronized Object evaluateAs(E exchange, QName resultType) {
 255  21
         variableResolver.setExchange(exchange);
 256  
         try {
 257  21
             Object document = getDocument(exchange);
 258  21
             if (resultType != null) {
 259  16
                 if (document instanceof InputSource) {
 260  0
                     InputSource inputSource = (InputSource) document;
 261  0
                     return getExpression().evaluate(inputSource, resultType);
 262  
                 }
 263  
                 else {
 264  16
                     return getExpression().evaluate(document, resultType);
 265  
                 }
 266  
             }
 267  
             else {
 268  5
                 if (document instanceof InputSource) {
 269  0
                     InputSource inputSource = (InputSource) document;
 270  0
                     return getExpression().evaluate(inputSource);
 271  
                 }
 272  
                 else {
 273  5
                     return getExpression().evaluate(document);
 274  
                 }
 275  
             }
 276  
         }
 277  0
         catch (XPathExpressionException e) {
 278  0
             throw new InvalidXPathExpression(getText(), e);
 279  
         }
 280  0
         catch (XPathFactoryConfigurationException e) {
 281  0
             throw new InvalidXPathExpression(getText(), e);
 282  
         }
 283  
     }
 284  
 
 285  
     protected XPathExpression createXPathExpression() throws XPathExpressionException, XPathFactoryConfigurationException {
 286  16
         XPath xPath = getXPathFactory().newXPath();
 287  
 
 288  
         // lets now clear any factory references to avoid keeping them around
 289  16
         xpathFactory = null;
 290  
 
 291  16
         xPath.setNamespaceContext(getNamespaceContext());
 292  16
         xPath.setXPathVariableResolver(variableResolver);
 293  16
         if (functionResolver != null) {
 294  0
             xPath.setXPathFunctionResolver(functionResolver);
 295  
         }
 296  16
         return xPath.compile(text);
 297  
     }
 298  
 
 299  
     /**
 300  
      * Strategy method to extract the document from the exchange
 301  
      */
 302  
     protected Object getDocument(E exchange) {
 303  21
         Message in = exchange.getIn();
 304  21
         Class type = getDocumentType();
 305  21
         Object answer = null;
 306  21
         if (type != null) {
 307  21
             answer = in.getBody(type);
 308  
         }
 309  21
         if (answer == null) {
 310  0
             answer = in.getBody();
 311  
         }
 312  
 
 313  
         // lets try coerce some common types into something JAXP can deal with
 314  21
         if (answer instanceof String) {
 315  0
             answer = new InputSource(new StringReader(answer.toString()));
 316  
         }
 317  21
         return answer;
 318  
     }
 319  
 
 320  
 
 321  
 }