Coverage Report - org.mule.module.xml.config.XsltTextDefinitionParser
 
Classes in this File Line Coverage Branch Coverage Complexity
XsltTextDefinitionParser
0%
0/28
0%
0/12
2.5
 
 1  
 /*
 2  
  * $Id: XsltTextDefinitionParser.java 11195 2008-03-06 04:13:01Z tcarlson $
 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.config;
 11  
 
 12  
 import org.mule.config.spring.parsers.assembly.BeanAssembler;
 13  
 import org.mule.config.spring.parsers.generic.ChildDefinitionParser;
 14  
 import org.mule.module.xml.util.XMLUtils;
 15  
 
 16  
 import javax.xml.transform.Result;
 17  
 import javax.xml.transform.Source;
 18  
 import javax.xml.transform.dom.DOMSource;
 19  
 import javax.xml.transform.stream.StreamResult;
 20  
 
 21  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 22  
 import org.springframework.beans.factory.xml.ParserContext;
 23  
 import org.w3c.dom.Element;
 24  
 import org.w3c.dom.Node;
 25  
 import org.w3c.dom.NodeList;
 26  
 
 27  
 /**
 28  
  * TODO
 29  
  */
 30  
 public class XsltTextDefinitionParser extends ChildDefinitionParser
 31  
 {
 32  
     public static final String STYLESHEET = "stylesheet";
 33  
     public static final int UNDEFINED = -1;
 34  
 
 35  
     /**
 36  
      * The class will be inferred from the class attribute
 37  
      *
 38  
      * @param setterMethod The target method (where the child will be injected)
 39  
      */
 40  
     public XsltTextDefinitionParser(String setterMethod)
 41  
     {
 42  0
         super(setterMethod);
 43  0
     }
 44  
 
 45  
     /**
 46  
      * @param setterMethod The target method (where the child will be injected)
 47  
      * @param clazz        The class created by this element/parser
 48  
      */
 49  
     public XsltTextDefinitionParser(String setterMethod, Class clazz)
 50  
     {
 51  0
         super(setterMethod, clazz);
 52  0
     }
 53  
 
 54  
     protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
 55  
     {
 56  0
         NodeList children = element.getChildNodes();
 57  0
         if (0 != children.getLength())
 58  
         {
 59  0
             Element stylesheet = null;
 60  0
             for (int i = 0; i < children.getLength(); i++)
 61  
             {
 62  0
                 if (Node.ELEMENT_NODE == children.item(i).getNodeType())
 63  
                 {
 64  0
                     assertArgument(null == stylesheet, "XSLT transformer can have at most one child element");
 65  0
                     stylesheet = (Element) children.item(i);
 66  
                 }
 67  
             }
 68  0
             if (null != stylesheet)
 69  
             {
 70  0
                 assertArgument(STYLESHEET.equals(stylesheet.getLocalName()),
 71  
                         "XSLT transformer child element must be named " + STYLESHEET);
 72  0
                 assembler.extendTarget("xslt", domToString(stylesheet), false);
 73  
                 // block processing by Spring
 74  0
                 element.removeChild(stylesheet);
 75  
             }
 76  
         }
 77  0
         super.postProcess(context, assembler, element);
 78  0
     }
 79  
 
 80  
     //@Override
 81  
     public String getPropertyName(Element e)
 82  
     {
 83  
         //We need to set this to null since we have already set the property on the parent in the postProcess() method
 84  0
         return null;
 85  
     }
 86  
 
 87  
     protected String domToString(Element dom)
 88  
     {
 89  
         try
 90  
         {
 91  
             // maybe change the transformer to avoid this step?
 92  0
             Source source = new DOMSource(dom);
 93  0
             ByteArrayOutputStream output = new ByteArrayOutputStream();
 94  0
             Result result = new StreamResult(output);
 95  0
             XMLUtils.getTransformer().transform(source, result);
 96  0
             return output.toString();
 97  
         }
 98  0
         catch (Exception e)
 99  
         {
 100  0
             throw (IllegalStateException) new IllegalStateException(e.getMessage()).initCause(e);
 101  
         }
 102  
     }
 103  
 
 104  
     protected void assertArgument(boolean condition, String message)
 105  
     {
 106  0
         if (!condition)
 107  
         {
 108  0
             throw new IllegalArgumentException(message);
 109  
         }
 110  0
     }
 111  
 }