Coverage Report - org.apache.camel.builder.saxon.XQueryBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
XQueryBuilder
44% 
83% 
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.saxon;
 19  
 
 20  
 import net.sf.saxon.Configuration;
 21  
 import net.sf.saxon.om.DocumentInfo;
 22  
 import net.sf.saxon.query.DynamicQueryContext;
 23  
 import net.sf.saxon.query.StaticQueryContext;
 24  
 import net.sf.saxon.query.XQueryExpression;
 25  
 import net.sf.saxon.trans.XPathException;
 26  
 import org.apache.camel.Exchange;
 27  
 import org.apache.camel.Expression;
 28  
 import org.apache.camel.Predicate;
 29  
 import org.apache.camel.RuntimeExpressionException;
 30  
 import org.apache.camel.util.ObjectHelper;
 31  
 import org.apache.camel.converter.jaxp.BytesSource;
 32  
 import org.apache.camel.converter.jaxp.XmlConverter;
 33  
 import org.apache.camel.converter.jaxp.StringSource;
 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.IOException;
 42  
 import java.io.InputStream;
 43  
 import java.io.Reader;
 44  
 import java.io.StringWriter;
 45  
 import java.util.HashMap;
 46  
 import java.util.List;
 47  
 import java.util.Map;
 48  
 import java.util.Properties;
 49  
 import java.util.Set;
 50  
 
 51  
 /**
 52  
  * Creates an XQuery builder
 53  
  *
 54  
  * @version $Revision: 534053 $
 55  
  */
 56  3
 public abstract class XQueryBuilder<E extends Exchange> implements Expression<E>, Predicate<E> {
 57  
     private Configuration configuration;
 58  
     private XQueryExpression expression;
 59  
     private StaticQueryContext staticQueryContext;
 60  3
     private Map<String, Object> parameters = new HashMap<String, Object>();
 61  3
     private XmlConverter converter = new XmlConverter();
 62  3
     private ResultFormat resultsFormat = ResultFormat.DOM;
 63  3
     private Properties properties = new Properties();
 64  
 
 65  
     @Override
 66  
     public String toString() {
 67  2
         return "XQuery[" + expression + "]";
 68  
     }
 69  
 
 70  
     public Object evaluate(E exchange) {
 71  
         try {
 72  1
             switch (resultsFormat) {
 73  
                 case Bytes:
 74  0
                     return evaluateAsBytes(exchange);
 75  
                 case BytesSource:
 76  0
                     return evaluateAsBytesSource(exchange);
 77  
                 case DOM:
 78  1
                     return evaluateAsDOM(exchange);
 79  
                 case List:
 80  0
                     return evaluateAsList(exchange);
 81  
                 case StringSource:
 82  0
                     return evaluateAsStringSource(exchange);
 83  
                 case String:
 84  
                 default:
 85  0
                     return evaluateAsString(exchange);
 86  
             }
 87  
         }
 88  0
         catch (Exception e) {
 89  0
             throw new RuntimeExpressionException(e);
 90  
         }
 91  
     }
 92  
 
 93  
     public List evaluateAsList(E exchange) throws Exception {
 94  2
         return getExpression().evaluate(createDynamicContext(exchange));
 95  
     }
 96  
 
 97  
     public Object evaluateAsStringSource(E exchange) throws Exception {
 98  0
         String text = evaluateAsString(exchange);
 99  0
         return new StringSource(text);
 100  
     }
 101  
 
 102  
     public Object evaluateAsBytesSource(E exchange) throws Exception {
 103  0
         byte[] bytes = evaluateAsBytes(exchange);
 104  0
         return new BytesSource(bytes);
 105  
     }
 106  
 
 107  
     public Node evaluateAsDOM(E exchange) throws Exception {
 108  1
         DOMResult result = new DOMResult();
 109  1
         getExpression().pull(createDynamicContext(exchange), result, properties);
 110  1
         return result.getNode();
 111  
     }
 112  
 
 113  
     public byte[] evaluateAsBytes(E exchange) throws Exception {
 114  0
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 115  0
         Result result = new StreamResult(buffer);
 116  0
         getExpression().pull(createDynamicContext(exchange), result, properties);
 117  0
         byte[] bytes = buffer.toByteArray();
 118  0
         return bytes;
 119  
     }
 120  
 
 121  
     public String evaluateAsString(E exchange) throws Exception {
 122  0
         StringWriter buffer = new StringWriter();
 123  0
         Result result = new StreamResult(buffer);
 124  0
         getExpression().pull(createDynamicContext(exchange), result, properties);
 125  0
         String text = buffer.toString();
 126  0
         return text;
 127  
     }
 128  
 
 129  
     public boolean matches(E exchange) {
 130  
         try {
 131  2
             List list = evaluateAsList(exchange);
 132  2
             return matches(exchange, list);
 133  
         }
 134  0
         catch (Exception e) {
 135  0
             throw new RuntimeExpressionException(e);
 136  
         }
 137  
     }
 138  
 
 139  
     public void assertMatches(String text, E exchange) throws AssertionError {
 140  
         try {
 141  0
             List list = evaluateAsList(exchange);
 142  0
             if (!matches(exchange, list)) {
 143  0
                 throw new AssertionError(this + " failed on " + exchange + " as evaluated: " + list);
 144  
             }
 145  
         }
 146  0
         catch (Exception e) {
 147  0
             throw new AssertionError(e);
 148  0
         }
 149  0
     }
 150  
 
 151  
     // Builder 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 XQueryBuilder<E> asBytes() {
 178  0
         setResultsFormat(ResultFormat.Bytes);
 179  0
         return this;
 180  
     }
 181  
 
 182  
     public XQueryBuilder<E> asBytesSource() {
 183  0
         setResultsFormat(ResultFormat.BytesSource);
 184  0
         return this;
 185  
     }
 186  
 
 187  
     public XQueryBuilder<E> asDOM() {
 188  0
         setResultsFormat(ResultFormat.DOM);
 189  0
         return this;
 190  
     }
 191  
 
 192  
     public XQueryBuilder<E> asDOMSource() {
 193  0
         setResultsFormat(ResultFormat.DOMSource);
 194  0
         return this;
 195  
     }
 196  
 
 197  
     public XQueryBuilder<E> asList() {
 198  0
         setResultsFormat(ResultFormat.List);
 199  0
         return this;
 200  
     }
 201  
 
 202  
     public XQueryBuilder<E> asString() {
 203  0
         setResultsFormat(ResultFormat.String);
 204  0
         return this;
 205  
     }
 206  
 
 207  
     public XQueryBuilder<E> asStringSource() {
 208  0
         setResultsFormat(ResultFormat.StringSource);
 209  0
         return this;
 210  
     }
 211  
 
 212  
     public XQueryBuilder<E> parameter(String name, Object value) {
 213  0
         parameters.put(name, value);
 214  0
         return this;
 215  
     }
 216  
 
 217  
     // Properties
 218  
     //-------------------------------------------------------------------------
 219  
 
 220  
     public XQueryExpression getExpression() throws IOException, XPathException {
 221  3
         if (expression == null) {
 222  3
             expression = createQueryExpression(getStaticQueryContext());
 223  3
             clearBuilderReferences();
 224  
         }
 225  3
         return expression;
 226  
     }
 227  
 
 228  
     public Configuration getConfiguration() {
 229  9
         if (configuration == null) {
 230  6
             configuration = new Configuration();
 231  6
             configuration.setHostLanguage(Configuration.XQUERY);
 232  
         }
 233  9
         return configuration;
 234  
     }
 235  
 
 236  
     public void setConfiguration(Configuration configuration) {
 237  0
         this.configuration = configuration;
 238  0
     }
 239  
 
 240  
     public StaticQueryContext getStaticQueryContext() {
 241  6
         if (staticQueryContext == null) {
 242  6
             staticQueryContext = new StaticQueryContext(getConfiguration());
 243  
         }
 244  6
         return staticQueryContext;
 245  
     }
 246  
 
 247  
     public void setStaticQueryContext(StaticQueryContext staticQueryContext) {
 248  0
         this.staticQueryContext = staticQueryContext;
 249  0
     }
 250  
 
 251  
     public Map<String, Object> getParameters() {
 252  3
         return parameters;
 253  
     }
 254  
 
 255  
     public void setParameters(Map<String, Object> parameters) {
 256  0
         this.parameters = parameters;
 257  0
     }
 258  
 
 259  
     public Properties getProperties() {
 260  0
         return properties;
 261  
     }
 262  
 
 263  
     public void setProperties(Properties properties) {
 264  0
         this.properties = properties;
 265  0
     }
 266  
 
 267  
     public ResultFormat getResultsFormat() {
 268  0
         return resultsFormat;
 269  
     }
 270  
 
 271  
     public void setResultsFormat(ResultFormat resultsFormat) {
 272  0
         this.resultsFormat = resultsFormat;
 273  0
     }
 274  
 
 275  
     // Implementation methods
 276  
     // -------------------------------------------------------------------------
 277  
 
 278  
     /**
 279  
      * A factory method to create the XQuery expression
 280  
      */
 281  
     protected abstract XQueryExpression createQueryExpression(StaticQueryContext staticQueryContext) throws XPathException, IOException;
 282  
 
 283  
     /**
 284  
      * Creates a dynamic context for the given exchange
 285  
      */
 286  
     protected DynamicQueryContext createDynamicContext(E exchange) throws Exception {
 287  3
         Configuration config = getConfiguration();
 288  3
         DynamicQueryContext dynamicQueryContext = new DynamicQueryContext(config);
 289  
 
 290  3
         Source source = exchange.getIn().getBody(Source.class);
 291  3
         if (source == null) {
 292  0
             source = converter.toSource(converter.createDocument());
 293  
         }
 294  
 
 295  3
         DocumentInfo doc = getStaticQueryContext().buildDocument(source);
 296  3
         dynamicQueryContext.setContextItem(doc);
 297  3
         configureQuery(dynamicQueryContext, exchange);
 298  3
         return dynamicQueryContext;
 299  
     }
 300  
 
 301  
     /**
 302  
      * Configures the dynamic context with exchange specific parameters
 303  
      *
 304  
      * @param dynamicQueryContext
 305  
      * @param exchange
 306  
      * @throws Exception
 307  
      */
 308  
     protected void configureQuery(DynamicQueryContext dynamicQueryContext, Exchange exchange) throws Exception {
 309  3
         addParameters(dynamicQueryContext, exchange.getProperties());
 310  3
         addParameters(dynamicQueryContext, exchange.getIn().getHeaders());
 311  3
         addParameters(dynamicQueryContext, getParameters());
 312  
 
 313  3
         dynamicQueryContext.setParameter("exchange", exchange);
 314  3
         dynamicQueryContext.setParameter("in", exchange.getIn());
 315  3
         dynamicQueryContext.setParameter("out", exchange.getOut());
 316  3
     }
 317  
 
 318  
     protected void addParameters(DynamicQueryContext dynamicQueryContext, Map<String, Object> map) {
 319  9
         Set<Map.Entry<String, Object>> propertyEntries = map.entrySet();
 320  9
         for (Map.Entry<String, Object> entry : propertyEntries) {
 321  0
             dynamicQueryContext.setParameter(entry.getKey(), entry.getValue());
 322  0
         }
 323  9
     }
 324  
 
 325  
     /**
 326  
      * To avoid keeping around any unnecessary objects after the expresion has been created lets
 327  
      * nullify references here
 328  
      */
 329  
     protected void clearBuilderReferences() {
 330  3
         staticQueryContext = null;
 331  3
         configuration = null;
 332  3
     }
 333  
 
 334  
     protected boolean matches(E exchange, List results) {
 335  2
         return ObjectHelper.matches(results);
 336  
     }
 337  
 }