Coverage Report - org.apache.camel.builder.saxon.XQueryBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
XQueryBuilder
42% 
83% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.builder.saxon;
 18  
 
 19  
 import net.sf.saxon.Configuration;
 20  
 import net.sf.saxon.om.DocumentInfo;
 21  
 import net.sf.saxon.query.DynamicQueryContext;
 22  
 import net.sf.saxon.query.StaticQueryContext;
 23  
 import net.sf.saxon.query.XQueryExpression;
 24  
 import net.sf.saxon.trans.XPathException;
 25  
 import org.apache.camel.Exchange;
 26  
 import org.apache.camel.Expression;
 27  
 import org.apache.camel.Predicate;
 28  
 import org.apache.camel.RuntimeExpressionException;
 29  
 import org.apache.camel.converter.IOConverter;
 30  
 import org.apache.camel.converter.jaxp.BytesSource;
 31  
 import org.apache.camel.converter.jaxp.StringSource;
 32  
 import org.apache.camel.converter.jaxp.XmlConverter;
 33  
 import org.apache.camel.util.ObjectHelper;
 34  
 import org.w3c.dom.Node;
 35  
 
 36  
 import javax.xml.transform.Result;
 37  
 import javax.xml.transform.Source;
 38  
 import javax.xml.transform.dom.DOMResult;
 39  
 import javax.xml.transform.stream.StreamResult;
 40  
 import java.io.ByteArrayOutputStream;
 41  
 import java.io.File;
 42  
 import java.io.FileNotFoundException;
 43  
 import java.io.IOException;
 44  
 import java.io.InputStream;
 45  
 import java.io.Reader;
 46  
 import java.io.StringWriter;
 47  
 import java.net.URL;
 48  
 import java.util.HashMap;
 49  
 import java.util.List;
 50  
 import java.util.Map;
 51  
 import java.util.Properties;
 52  
 import java.util.Set;
 53  
 
 54  
 /**
 55  
  * Creates an XQuery builder
 56  
  * 
 57  
  * @version $Revision: 564167 $
 58  
  */
 59  3
 public abstract class XQueryBuilder<E extends Exchange> implements Expression<E>, Predicate<E> {
 60  
     private Configuration configuration;
 61  
     private XQueryExpression expression;
 62  
     private StaticQueryContext staticQueryContext;
 63  3
     private Map<String, Object> parameters = new HashMap<String, Object>();
 64  3
     private XmlConverter converter = new XmlConverter();
 65  3
     private ResultFormat resultsFormat = ResultFormat.DOM;
 66  3
     private Properties properties = new Properties();
 67  
 
 68  
     @Override
 69  
     public String toString() {
 70  2
         return "XQuery[" + expression + "]";
 71  
     }
 72  
 
 73  
     public Object evaluate(E exchange) {
 74  
         try {
 75  1
             switch (resultsFormat) {
 76  
             case Bytes:
 77  0
                 return evaluateAsBytes(exchange);
 78  
             case BytesSource:
 79  0
                 return evaluateAsBytesSource(exchange);
 80  
             case DOM:
 81  1
                 return evaluateAsDOM(exchange);
 82  
             case List:
 83  0
                 return evaluateAsList(exchange);
 84  
             case StringSource:
 85  0
                 return evaluateAsStringSource(exchange);
 86  
             case String:
 87  
             default:
 88  0
                 return evaluateAsString(exchange);
 89  
             }
 90  0
         } catch (Exception e) {
 91  0
             throw new RuntimeExpressionException(e);
 92  
         }
 93  
     }
 94  
 
 95  
     public List evaluateAsList(E exchange) throws Exception {
 96  2
         return getExpression().evaluate(createDynamicContext(exchange));
 97  
     }
 98  
 
 99  
     public Object evaluateAsStringSource(E exchange) throws Exception {
 100  0
         String text = evaluateAsString(exchange);
 101  0
         return new StringSource(text);
 102  
     }
 103  
 
 104  
     public Object evaluateAsBytesSource(E exchange) throws Exception {
 105  0
         byte[] bytes = evaluateAsBytes(exchange);
 106  0
         return new BytesSource(bytes);
 107  
     }
 108  
 
 109  
     public Node evaluateAsDOM(E exchange) throws Exception {
 110  1
         DOMResult result = new DOMResult();
 111  1
         getExpression().pull(createDynamicContext(exchange), result, properties);
 112  1
         return result.getNode();
 113  
     }
 114  
 
 115  
     public byte[] evaluateAsBytes(E exchange) throws Exception {
 116  0
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 117  0
         Result result = new StreamResult(buffer);
 118  0
         getExpression().pull(createDynamicContext(exchange), result, properties);
 119  0
         byte[] bytes = buffer.toByteArray();
 120  0
         return bytes;
 121  
     }
 122  
 
 123  
     public String evaluateAsString(E exchange) throws Exception {
 124  0
         StringWriter buffer = new StringWriter();
 125  0
         Result result = new StreamResult(buffer);
 126  0
         getExpression().pull(createDynamicContext(exchange), result, properties);
 127  0
         String text = buffer.toString();
 128  0
         return text;
 129  
     }
 130  
 
 131  
     public boolean matches(E exchange) {
 132  
         try {
 133  2
             List list = evaluateAsList(exchange);
 134  2
             return matches(exchange, list);
 135  0
         } catch (Exception e) {
 136  0
             throw new RuntimeExpressionException(e);
 137  
         }
 138  
     }
 139  
 
 140  
     public void assertMatches(String text, E exchange) throws AssertionError {
 141  
         try {
 142  0
             List list = evaluateAsList(exchange);
 143  0
             if (!matches(exchange, list)) {
 144  0
                 throw new AssertionError(this + " failed on " + exchange + " as evaluated: " + list);
 145  
             }
 146  0
         } catch (Exception e) {
 147  0
             throw new AssertionError(e);
 148  0
         }
 149  0
     }
 150  
 
 151  
     // Static helper methods
 152  
     //-------------------------------------------------------------------------
 153  
     public static <E extends Exchange> XQueryBuilder<E> xquery(final String queryText) {
 154  3
         return new XQueryBuilder<E>() {
 155  5
             protected XQueryExpression createQueryExpression(StaticQueryContext staticQueryContext) throws XPathException {
 156  3
                 return staticQueryContext.compileQuery(queryText);
 157  
             }
 158  
         };
 159  
     }
 160  
 
 161  
     public static <E extends Exchange> XQueryBuilder<E> xquery(final Reader reader) {
 162  0
         return new XQueryBuilder<E>() {
 163  0
             protected XQueryExpression createQueryExpression(StaticQueryContext staticQueryContext) throws XPathException, IOException {
 164  0
                 return staticQueryContext.compileQuery(reader);
 165  
             }
 166  
         };
 167  
     }
 168  
 
 169  
     public static <E extends Exchange> XQueryBuilder<E> xquery(final InputStream in, final String characterSet) {
 170  0
         return new XQueryBuilder<E>() {
 171  0
             protected XQueryExpression createQueryExpression(StaticQueryContext staticQueryContext) throws XPathException, IOException {
 172  0
                 return staticQueryContext.compileQuery(in, characterSet);
 173  
             }
 174  
         };
 175  
     }
 176  
 
 177  
     public static <E extends Exchange> XQueryBuilder<E> xquery(File file, String characterSet) throws FileNotFoundException {
 178  0
         return xquery(IOConverter.toInputStream(file), characterSet);
 179  
     }
 180  
 
 181  
     public static <E extends Exchange> XQueryBuilder<E> xquery(URL url, String characterSet) throws IOException {
 182  0
         return xquery(IOConverter.toInputStream(url), characterSet);
 183  
     }
 184  
 
 185  
     public static <E extends Exchange> XQueryBuilder<E> xquery(File file) throws FileNotFoundException {
 186  0
         return xquery(IOConverter.toInputStream(file), ObjectHelper.getDefaultCharacterSet());
 187  
     }
 188  
 
 189  
     public static <E extends Exchange> XQueryBuilder<E> xquery(URL url) throws IOException {
 190  0
         return xquery(IOConverter.toInputStream(url), ObjectHelper.getDefaultCharacterSet());
 191  
     }
 192  
 
 193  
 
 194  
     // Fluent API
 195  
     // -------------------------------------------------------------------------
 196  
     public XQueryBuilder<E> asBytes() {
 197  0
         setResultsFormat(ResultFormat.Bytes);
 198  0
         return this;
 199  
     }
 200  
 
 201  
     public XQueryBuilder<E> asBytesSource() {
 202  0
         setResultsFormat(ResultFormat.BytesSource);
 203  0
         return this;
 204  
     }
 205  
 
 206  
     public XQueryBuilder<E> asDOM() {
 207  0
         setResultsFormat(ResultFormat.DOM);
 208  0
         return this;
 209  
     }
 210  
 
 211  
     public XQueryBuilder<E> asDOMSource() {
 212  0
         setResultsFormat(ResultFormat.DOMSource);
 213  0
         return this;
 214  
     }
 215  
 
 216  
     public XQueryBuilder<E> asList() {
 217  0
         setResultsFormat(ResultFormat.List);
 218  0
         return this;
 219  
     }
 220  
 
 221  
     public XQueryBuilder<E> asString() {
 222  0
         setResultsFormat(ResultFormat.String);
 223  0
         return this;
 224  
     }
 225  
 
 226  
     public XQueryBuilder<E> asStringSource() {
 227  0
         setResultsFormat(ResultFormat.StringSource);
 228  0
         return this;
 229  
     }
 230  
 
 231  
     public XQueryBuilder<E> parameter(String name, Object value) {
 232  0
         parameters.put(name, value);
 233  0
         return this;
 234  
     }
 235  
 
 236  
     // Properties
 237  
     // -------------------------------------------------------------------------
 238  
 
 239  
     public XQueryExpression getExpression() throws IOException, XPathException {
 240  3
         if (expression == null) {
 241  3
             expression = createQueryExpression(getStaticQueryContext());
 242  3
             clearBuilderReferences();
 243  
         }
 244  3
         return expression;
 245  
     }
 246  
 
 247  
     public Configuration getConfiguration() {
 248  9
         if (configuration == null) {
 249  6
             configuration = new Configuration();
 250  6
             configuration.setHostLanguage(Configuration.XQUERY);
 251  
         }
 252  9
         return configuration;
 253  
     }
 254  
 
 255  
     public void setConfiguration(Configuration configuration) {
 256  0
         this.configuration = configuration;
 257  0
     }
 258  
 
 259  
     public StaticQueryContext getStaticQueryContext() {
 260  6
         if (staticQueryContext == null) {
 261  6
             staticQueryContext = new StaticQueryContext(getConfiguration());
 262  
         }
 263  6
         return staticQueryContext;
 264  
     }
 265  
 
 266  
     public void setStaticQueryContext(StaticQueryContext staticQueryContext) {
 267  0
         this.staticQueryContext = staticQueryContext;
 268  0
     }
 269  
 
 270  
     public Map<String, Object> getParameters() {
 271  3
         return parameters;
 272  
     }
 273  
 
 274  
     public void setParameters(Map<String, Object> parameters) {
 275  0
         this.parameters = parameters;
 276  0
     }
 277  
 
 278  
     public Properties getProperties() {
 279  0
         return properties;
 280  
     }
 281  
 
 282  
     public void setProperties(Properties properties) {
 283  0
         this.properties = properties;
 284  0
     }
 285  
 
 286  
     public ResultFormat getResultsFormat() {
 287  0
         return resultsFormat;
 288  
     }
 289  
 
 290  
     public void setResultsFormat(ResultFormat resultsFormat) {
 291  0
         this.resultsFormat = resultsFormat;
 292  0
     }
 293  
 
 294  
     // Implementation methods
 295  
     // -------------------------------------------------------------------------
 296  
 
 297  
     /**
 298  
      * A factory method to create the XQuery expression
 299  
      */
 300  
     protected abstract XQueryExpression createQueryExpression(StaticQueryContext staticQueryContext) throws XPathException, IOException;
 301  
 
 302  
     /**
 303  
      * Creates a dynamic context for the given exchange
 304  
      */
 305  
     protected DynamicQueryContext createDynamicContext(E exchange) throws Exception {
 306  3
         Configuration config = getConfiguration();
 307  3
         DynamicQueryContext dynamicQueryContext = new DynamicQueryContext(config);
 308  
 
 309  3
         Source source = exchange.getIn().getBody(Source.class);
 310  3
         if (source == null) {
 311  0
             source = converter.toSource(converter.createDocument());
 312  
         }
 313  
 
 314  3
         DocumentInfo doc = getStaticQueryContext().buildDocument(source);
 315  3
         dynamicQueryContext.setContextItem(doc);
 316  3
         configureQuery(dynamicQueryContext, exchange);
 317  3
         return dynamicQueryContext;
 318  
     }
 319  
 
 320  
     /**
 321  
      * Configures the dynamic context with exchange specific parameters
 322  
      * 
 323  
      * @param dynamicQueryContext
 324  
      * @param exchange
 325  
      * @throws Exception
 326  
      */
 327  
     protected void configureQuery(DynamicQueryContext dynamicQueryContext, Exchange exchange) throws Exception {
 328  3
         addParameters(dynamicQueryContext, exchange.getProperties());
 329  3
         addParameters(dynamicQueryContext, exchange.getIn().getHeaders());
 330  3
         addParameters(dynamicQueryContext, getParameters());
 331  
 
 332  3
         dynamicQueryContext.setParameter("exchange", exchange);
 333  3
         dynamicQueryContext.setParameter("in", exchange.getIn());
 334  3
         dynamicQueryContext.setParameter("out", exchange.getOut());
 335  3
     }
 336  
 
 337  
     protected void addParameters(DynamicQueryContext dynamicQueryContext, Map<String, Object> map) {
 338  9
         Set<Map.Entry<String, Object>> propertyEntries = map.entrySet();
 339  9
         for (Map.Entry<String, Object> entry : propertyEntries) {
 340  0
             dynamicQueryContext.setParameter(entry.getKey(), entry.getValue());
 341  0
         }
 342  9
     }
 343  
 
 344  
     /**
 345  
      * To avoid keeping around any unnecessary objects after the expresion has
 346  
      * been created lets nullify references here
 347  
      */
 348  
     protected void clearBuilderReferences() {
 349  3
         staticQueryContext = null;
 350  3
         configuration = null;
 351  3
     }
 352  
 
 353  
     protected boolean matches(E exchange, List results) {
 354  2
         return ObjectHelper.matches(results);
 355  
     }
 356  
 }