Coverage Report - org.mule.util.properties.JXPathPropertyExtractor
 
Classes in this File Line Coverage Branch Coverage Complexity
JXPathPropertyExtractor
0%
0/19
0%
0/4
6
 
 1  
 /*
 2  
  * $Id: JXPathPropertyExtractor.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 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  
 
 11  
 package org.mule.util.properties;
 12  
 
 13  
 import org.mule.umo.UMOMessage;
 14  
 
 15  
 import org.apache.commons.jxpath.JXPathContext;
 16  
 import org.apache.commons.logging.Log;
 17  
 import org.apache.commons.logging.LogFactory;
 18  
 import org.dom4j.Document;
 19  
 import org.dom4j.DocumentException;
 20  
 import org.dom4j.DocumentHelper;
 21  
 
 22  
 /**
 23  
  * Will extract properties based on Xpath expressions. Will work on Xml/Dom and beans
 24  
  */
 25  0
 public class JXPathPropertyExtractor implements PropertyExtractor
 26  
 {
 27  
 
 28  
     /**
 29  
      * logger used by this class
 30  
      */
 31  0
     protected transient Log logger = LogFactory.getLog(getClass());
 32  
 
 33  
     public Object getProperty(String name, Object message)
 34  
     {
 35  
 
 36  0
         Object result = null;
 37  0
         Object payload = message;
 38  0
         if (message instanceof UMOMessage)
 39  
         {
 40  0
             payload = ((UMOMessage)message).getPayload();
 41  
         }
 42  
 
 43  0
         if (payload instanceof String)
 44  
         {
 45  
             Document doc;
 46  
             try
 47  
             {
 48  0
                 doc = DocumentHelper.parseText((String)payload);
 49  
             }
 50  0
             catch (DocumentException e)
 51  
             {
 52  0
                 logger.error(e);
 53  0
                 return null;
 54  0
             }
 55  0
             result = doc.valueOf(name);
 56  0
         }
 57  
         else
 58  
         {
 59  0
             JXPathContext context = JXPathContext.newContext(payload);
 60  
             try
 61  
             {
 62  0
                 result = context.getValue(name);
 63  
             }
 64  0
             catch (Exception e)
 65  
             {
 66  
                 // ignore
 67  0
             }
 68  
         }
 69  0
         return result;
 70  
     }
 71  
 }