Coverage Report - org.mule.module.xml.transformer.XPathExtractor
 
Classes in this File Line Coverage Branch Coverage Complexity
XPathExtractor
0%
0/41
0%
0/11
0
XPathExtractor$1
0%
0/10
0%
0/6
0
XPathExtractor$2
0%
0/1
N/A
0
XPathExtractor$ResultType
0%
0/6
N/A
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.xml.transformer;
 8  
 
 9  
 import org.mule.api.lifecycle.InitialisationException;
 10  
 import org.mule.api.transformer.TransformerException;
 11  
 import org.mule.config.i18n.MessageFactory;
 12  
 import org.mule.transformer.AbstractTransformer;
 13  
 import org.mule.transformer.types.DataTypeFactory;
 14  
 
 15  
 import java.util.Arrays;
 16  
 import java.util.Collections;
 17  
 import java.util.Iterator;
 18  
 import java.util.Map;
 19  
 
 20  
 import javax.xml.namespace.NamespaceContext;
 21  
 import javax.xml.namespace.QName;
 22  
 import javax.xml.xpath.XPath;
 23  
 import javax.xml.xpath.XPathConstants;
 24  
 import javax.xml.xpath.XPathExpressionException;
 25  
 import javax.xml.xpath.XPathFactory;
 26  
 
 27  
 import org.xml.sax.InputSource;
 28  
 
 29  
 /**
 30  
  * Simple transformer for using the JAXP XPath library to extract an XPath value from
 31  
  * an XPath expression.
 32  
  * 
 33  
  * @author Ryan Heaton
 34  
  */
 35  
 public class XPathExtractor extends AbstractTransformer
 36  
 {
 37  
 
 38  
     /**
 39  
      * Result type.
 40  
      */
 41  0
     public enum ResultType
 42  
     {
 43  0
         NODESET,
 44  0
         NODE,
 45  0
         STRING,
 46  0
         BOOLEAN,
 47  0
         NUMBER
 48  
     }
 49  
 
 50  0
     private volatile XPath xpath = XPathFactory.newInstance().newXPath();
 51  0
     private volatile Map<String, String> prefixToNamespaceMap = null;
 52  
     private volatile String expression;
 53  0
     private volatile ResultType resultType = ResultType.STRING;
 54  
 
 55  
     public XPathExtractor()
 56  0
     {
 57  0
         registerSourceType(DataTypeFactory.create(org.w3c.dom.Node.class));
 58  0
         registerSourceType(DataTypeFactory.create(InputSource.class));
 59  0
     }
 60  
 
 61  
     @Override
 62  
     public void initialise() throws InitialisationException
 63  
     {
 64  0
         super.initialise();
 65  
 
 66  0
         if (expression == null)
 67  
         {
 68  0
             throw new InitialisationException(
 69  
                 MessageFactory.createStaticMessage("An expression must be supplied to the StandardXPathExtractor"),
 70  
                 this);
 71  
         }
 72  
 
 73  0
         final Map<String, String> prefixToNamespaceMap = this.prefixToNamespaceMap;
 74  0
         if (prefixToNamespaceMap != null)
 75  
         {
 76  0
             getXpath().setNamespaceContext(new NamespaceContext()
 77  0
             {
 78  
                 public String getNamespaceURI(String prefix)
 79  
                 {
 80  0
                     return prefixToNamespaceMap.get(prefix);
 81  
                 }
 82  
 
 83  
                 public String getPrefix(String namespaceURI)
 84  
                 {
 85  
 
 86  0
                     for (Map.Entry<String, String> entry : prefixToNamespaceMap.entrySet())
 87  
                     {
 88  0
                         if (namespaceURI.equals(entry.getValue()))
 89  
                         {
 90  0
                             return entry.getKey();
 91  
                         }
 92  
                     }
 93  
 
 94  0
                     return null;
 95  
                 }
 96  
 
 97  
                 public Iterator getPrefixes(String namespaceURI)
 98  
                 {
 99  0
                     String prefix = getPrefix(namespaceURI);
 100  0
                     if (prefix == null)
 101  
                     {
 102  0
                         return Collections.emptyList().iterator();
 103  
                     }
 104  
                     else
 105  
                     {
 106  0
                         return Arrays.asList(prefix).iterator();
 107  
                     }
 108  
                 }
 109  
             });
 110  
         }
 111  0
     }
 112  
 
 113  
     @Override
 114  
     public Object doTransform(Object src, String encoding) throws TransformerException
 115  
     {
 116  
         QName resultType;
 117  0
         switch (getResultType())
 118  
         {
 119  
             case BOOLEAN :
 120  0
                 resultType = XPathConstants.BOOLEAN;
 121  0
                 break;
 122  
             case NODE :
 123  0
                 resultType = XPathConstants.NODE;
 124  0
                 break;
 125  
             case NODESET :
 126  0
                 resultType = XPathConstants.NODESET;
 127  0
                 break;
 128  
             case NUMBER :
 129  0
                 resultType = XPathConstants.NUMBER;
 130  0
                 break;
 131  
             default :
 132  0
                 resultType = XPathConstants.STRING;
 133  
                 break;
 134  
         }
 135  
 
 136  
         try
 137  
         {
 138  0
             if (src instanceof InputSource)
 139  
             {
 140  0
                 return xpath.evaluate(expression, (InputSource) src, resultType);
 141  
             }
 142  
             else
 143  
             {
 144  0
                 return xpath.evaluate(expression, src, resultType);
 145  
             }
 146  
         }
 147  0
         catch (XPathExpressionException e)
 148  
         {
 149  0
             throw new TransformerException(this, e);
 150  
         }
 151  
     }
 152  
 
 153  
     /**
 154  
      * @return Returns the expression.
 155  
      */
 156  
     public String getExpression()
 157  
     {
 158  0
         return expression;
 159  
     }
 160  
 
 161  
     /**
 162  
      * @param expression The expression to set.
 163  
      */
 164  
     public void setExpression(String expression)
 165  
     {
 166  0
         this.expression = expression;
 167  0
     }
 168  
 
 169  
     /**
 170  
      * Result type from this transformer.
 171  
      * 
 172  
      * @return Result type from this transformer.
 173  
      */
 174  
     public ResultType getResultType()
 175  
     {
 176  0
         return resultType;
 177  
     }
 178  
 
 179  
     /**
 180  
      * Result type from this transformer.
 181  
      * 
 182  
      * @param resultType Result type from this transformer.
 183  
      */
 184  
     public void setResultType(ResultType resultType)
 185  
     {
 186  0
         this.resultType = resultType;
 187  0
     }
 188  
 
 189  
     /**
 190  
      * The XPath evaluator.
 191  
      * 
 192  
      * @return The XPath evaluator.
 193  
      */
 194  
     public XPath getXpath()
 195  
     {
 196  0
         return xpath;
 197  
     }
 198  
 
 199  
     /**
 200  
      * The XPath evaluator.
 201  
      * 
 202  
      * @param xPath The XPath evaluator.
 203  
      */
 204  
     public void setXpath(XPath xPath)
 205  
     {
 206  0
         this.xpath = xPath;
 207  0
     }
 208  
 
 209  
     /**
 210  
      * The prefix-to-namespace map.
 211  
      * 
 212  
      * @return The prefix-to-namespace map.
 213  
      */
 214  
     public Map<String, String> getNamespaces()
 215  
     {
 216  0
         return prefixToNamespaceMap;
 217  
     }
 218  
 
 219  
     /**
 220  
      * The prefix-to-namespace map.
 221  
      * 
 222  
      * @param prefixToNamespaceMap The prefix-to-namespace map.
 223  
      */
 224  
     public void setNamespaces(Map<String, String> prefixToNamespaceMap)
 225  
     {
 226  0
         this.prefixToNamespaceMap = prefixToNamespaceMap;
 227  0
     }
 228  
 }