Coverage Report - org.mule.module.xml.expression.AbstractXPathExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractXPathExpressionEvaluator
13%
4/30
0%
0/12
2.857
 
 1  
 /*
 2  
  * $Id: AbstractXPathExpressionEvaluator.java 11231 2008-03-06 21:17:37Z rossmason $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 package org.mule.module.xml.expression;
 11  
 
 12  
 import org.mule.api.MuleMessage;
 13  
 import org.mule.api.MuleRuntimeException;
 14  
 import org.mule.api.lifecycle.Disposable;
 15  
 import org.mule.module.xml.i18n.XmlMessages;
 16  
 import org.mule.util.expression.ExpressionEvaluator;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import java.util.WeakHashMap;
 23  
 
 24  
 import org.jaxen.JaxenException;
 25  
 import org.jaxen.XPath;
 26  
 
 27  
 /**
 28  
  * Provides a base class for XPath property extractors. The XPath engine used is jaxen (http://jaxen.org) which supports
 29  
  * XPath queries on other object models such as JavaBeans as well as Xml
 30  
  */
 31  464
 public abstract class AbstractXPathExpressionEvaluator implements ExpressionEvaluator, Disposable
 32  
 {
 33  464
     private Map cache = new WeakHashMap(8);
 34  
 
 35  
     /** {@inheritDoc} */
 36  
     public Object evaluate(String expression, Object message)
 37  
     {
 38  
         try
 39  
         {
 40  0
             if(message instanceof MuleMessage)
 41  
             {
 42  0
                 message = ((MuleMessage)message).getPayload();
 43  
             }
 44  0
             XPath xpath = getXPath(expression, message);
 45  
 
 46  0
             List result = xpath.selectNodes(message);
 47  0
             result = extractResultsFromNodes(result);
 48  0
             if(result.size()==1)
 49  
             {
 50  0
                 return result.get(0);
 51  
             }
 52  0
             else if(result.size()==0)
 53  
             {
 54  0
                 return null;
 55  
             }
 56  
             else
 57  
             {
 58  0
                 return result;
 59  
             }
 60  
         }
 61  0
         catch (JaxenException e)
 62  
         {
 63  0
             throw new MuleRuntimeException(XmlMessages.failedToProcessXPath(expression), e);
 64  
         }
 65  
     }
 66  
 
 67  
     /** {@inheritDoc} */
 68  
     public final void setName(String name)
 69  
     {
 70  0
         throw new UnsupportedOperationException("setName");
 71  
     }
 72  
 
 73  
     protected XPath getXPath(String expression, Object object) throws JaxenException
 74  
     {
 75  0
         XPath xpath = (XPath)cache.get(expression + getClass().getName());
 76  0
         if(xpath==null)
 77  
         {
 78  0
             xpath = createXPath(expression, object);
 79  0
             cache.put(expression + getClass().getName(), xpath);
 80  
         }
 81  0
         return xpath;
 82  
     }
 83  
 
 84  
     protected abstract XPath createXPath(String expression, Object object) throws JaxenException;
 85  
 
 86  
     protected List extractResultsFromNodes(List results)
 87  
     {
 88  0
         if(results==null)
 89  
         {
 90  0
             return null;
 91  
         }
 92  0
         List newResults = new ArrayList(results.size());
 93  0
         for (Iterator iterator = results.iterator(); iterator.hasNext();)
 94  
         {
 95  0
             Object o = iterator.next();
 96  0
             newResults.add(extractResultFromNode(o));
 97  0
         }
 98  0
         return newResults;
 99  
     }
 100  
 
 101  
     /**
 102  
      * A lifecycle method where implementor should free up any resources. If an
 103  
      * exception is thrown it should just be logged and processing should continue.
 104  
      * This method should not throw Runtime exceptions.
 105  
      */
 106  
     public void dispose()
 107  
     {
 108  464
         cache.clear();
 109  464
     }
 110  
 
 111  
     protected abstract Object extractResultFromNode(Object result);
 112  
 }