Coverage Report - org.mule.module.xml.expression.JXPathExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
JXPathExpressionEvaluator
0%
0/43
0%
0/12
2.364
JXPathExpressionEvaluator$1
0%
0/3
N/A
2.364
 
 1  
 /*
 2  
  * $Id: JXPathExpressionEvaluator.java 20321 2010-11-24 15:21:24Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  
 
 11  
 package org.mule.module.xml.expression;
 12  
 
 13  
 import org.apache.commons.jxpath.Container;
 14  
 import org.mule.api.MuleContext;
 15  
 import org.mule.api.MuleMessage;
 16  
 import org.mule.api.context.MuleContextAware;
 17  
 import org.mule.api.expression.ExpressionEvaluator;
 18  
 import org.mule.api.expression.ExpressionRuntimeException;
 19  
 import org.mule.api.registry.RegistrationException;
 20  
 import org.mule.config.i18n.CoreMessages;
 21  
 import org.mule.module.xml.i18n.XmlMessages;
 22  
 import org.mule.module.xml.util.NamespaceManager;
 23  
 import org.mule.module.xml.util.XMLUtils;
 24  
 
 25  
 import java.util.Iterator;
 26  
 import java.util.Map;
 27  
 
 28  
 import org.apache.commons.jxpath.JXPathContext;
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.w3c.dom.Document;
 32  
 
 33  
 /**
 34  
  * Will extract properties based on Xpath expressions. Will work on Xml/Dom and beans
 35  
  *
 36  
  * @deprecated Developers should use xpath, bean or groovy instead of this expression evaluator since there are some
 37  
  * quirks with JXPath and the performance is not good.
 38  
  */
 39  0
 public class JXPathExpressionEvaluator implements ExpressionEvaluator, MuleContextAware
 40  
 {
 41  
     public static final String NAME = "jxpath";
 42  
     /**
 43  
      * logger used by this class
 44  
      */
 45  0
     protected transient Log logger = LogFactory.getLog(getClass());
 46  
     protected transient MuleContext muleContext;
 47  
     private NamespaceManager namespaceManager;
 48  
 
 49  
     public void setMuleContext(MuleContext context)
 50  
     {
 51  0
         muleContext = context;
 52  0
     }
 53  
 
 54  
     public Object evaluate(String expression, MuleMessage message)
 55  
     {
 56  
         Document document;
 57  
         try
 58  
         {
 59  0
             document = XMLUtils.toW3cDocument(message.getPayload());
 60  
         }
 61  0
         catch (Exception e)
 62  
         {
 63  0
             logger.error(e);
 64  0
             return null;
 65  0
         }
 66  
 
 67  
         JXPathContext context;
 68  
 
 69  0
         if (document != null)
 70  
         {
 71  0
             context = createContextForXml(document);
 72  
         }
 73  
         else
 74  
         {
 75  0
             context = createContextForBean(message.getPayload());
 76  
         }
 77  
 
 78  0
         return getExpressionValue(context, expression);
 79  
     }
 80  
 
 81  
     private JXPathContext createContextForXml(final Document document)
 82  
     {
 83  0
         Container container = new Container()
 84  0
         {
 85  
 
 86  
             public Object getValue()
 87  
             {
 88  0
                 return document;
 89  
             }
 90  
 
 91  
             public void setValue(Object value)
 92  
             {
 93  0
                 throw new UnsupportedOperationException();
 94  
             }
 95  
         };
 96  
 
 97  0
         return JXPathContext.newContext(container);
 98  
     }
 99  
 
 100  
     private JXPathContext createContextForBean(Object payload)
 101  
     {
 102  0
         return JXPathContext.newContext(payload);
 103  
     }
 104  
 
 105  
     private Object getExpressionValue(JXPathContext context, String expression)
 106  
     {
 107  0
         NamespaceManager theNamespaceManager = getNamespaceManager();
 108  0
         if (theNamespaceManager != null)
 109  
         {
 110  0
             addNamespacesToContext(theNamespaceManager, context);
 111  
         }
 112  
 
 113  0
         Object result = null;
 114  
 
 115  
         try
 116  
         {
 117  0
             result = context.getValue(expression);
 118  
         }
 119  0
         catch (Exception e)
 120  
         {
 121  0
             if (logger.isDebugEnabled())
 122  
             {
 123  0
                 logger.debug("failed to process JXPath expression: " + expression, e);
 124  
             }
 125  0
         }
 126  
 
 127  0
         return result;
 128  
     }
 129  
 
 130  
     protected void addNamespacesToContext(NamespaceManager manager, JXPathContext context)
 131  
     {
 132  0
         for (Iterator iterator = manager.getNamespaces().entrySet().iterator(); iterator.hasNext();)
 133  
         {
 134  0
             Map.Entry entry = (Map.Entry) iterator.next();
 135  
             try
 136  
             {
 137  0
                 context.registerNamespace(entry.getKey().toString(), entry.getValue().toString());
 138  
             }
 139  0
             catch (Exception e)
 140  
             {
 141  0
                 throw new ExpressionRuntimeException(XmlMessages.failedToRegisterNamespace(entry.getKey().toString(), entry.getValue().toString()));
 142  0
             }
 143  0
         }
 144  0
     }
 145  
 
 146  
     /**
 147  
      * {@inheritDoc}
 148  
      */
 149  
     public String getName()
 150  
     {
 151  0
         return NAME;
 152  
     }
 153  
 
 154  
     /**
 155  
      * {@inheritDoc}
 156  
      */
 157  
     public void setName(String name)
 158  
     {
 159  0
         throw new UnsupportedOperationException("setName");
 160  
     }
 161  
 
 162  
     /**
 163  
      *
 164  
      * @return the nsmespace manager from the registry
 165  
      */
 166  
     protected synchronized NamespaceManager getNamespaceManager()
 167  
     {
 168  0
         if (namespaceManager == null)
 169  
         {
 170  
 
 171  
             try
 172  
             {
 173  
                 // We defer looking this up until registry is completely built
 174  0
                 if (muleContext != null)
 175  
                 {
 176  0
                     namespaceManager = muleContext.getRegistry().lookupObject(NamespaceManager.class);
 177  
                 }
 178  
             }
 179  0
             catch (RegistrationException e)
 180  
             {
 181  0
                 throw new ExpressionRuntimeException(CoreMessages.failedToLoad("NamespaceManager"), e);
 182  0
             }
 183  
         }
 184  0
         return namespaceManager;
 185  
     }
 186  
 }