View Javadoc

1   /*
2    * $Id: XsltTextDefinitionParser.java 19191 2010-08-25 21:05:23Z tcarlson $
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  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          super(setterMethod);
43      }
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          super(setterMethod, clazz);
52      }
53  
54      protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
55      {
56          NodeList children = element.getChildNodes();
57          if (0 != children.getLength())
58          {
59              Element stylesheet = null;
60              for (int i = 0; i < children.getLength(); i++)
61              {
62                  if (Node.ELEMENT_NODE == children.item(i).getNodeType())
63                  {
64                      assertArgument(null == stylesheet, "XSLT transformer can have at most one child element");
65                      stylesheet = (Element) children.item(i);
66                  }
67              }
68              if (null != stylesheet)
69              {
70                  assertArgument(STYLESHEET.equals(stylesheet.getLocalName()),
71                          "XSLT transformer child element must be named " + STYLESHEET);
72                  assembler.extendTarget("xslt", domToString(stylesheet), false);
73                  // block processing by Spring
74                  element.removeChild(stylesheet);
75              }
76          }
77          super.postProcess(context, assembler, element);
78      }
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          return null;
85      }
86  
87      protected String domToString(Element dom)
88      {
89          try
90          {
91              // maybe change the transformer to avoid this step?
92              Source source = new DOMSource(dom);
93              ByteArrayOutputStream output = new ByteArrayOutputStream();
94              Result result = new StreamResult(output);
95              XMLUtils.getTransformer().transform(source, result);
96              return output.toString();
97          }
98          catch (Exception e)
99          {
100             throw (IllegalStateException) new IllegalStateException(e.getMessage()).initCause(e);
101         }
102     }
103 
104     protected void assertArgument(boolean condition, String message)
105     {
106         if (!condition)
107         {
108             throw new IllegalArgumentException(message);
109         }
110     }
111 }