View Javadoc
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.config.spring.parsers.processors;
8   
9   import org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate;
10  import org.mule.config.spring.parsers.PostProcessor;
11  import org.mule.config.spring.parsers.assembly.BeanAssembler;
12  import org.mule.config.spring.parsers.assembly.BeanAssemblerFactory;
13  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
14  import org.mule.config.spring.util.SpringXMLUtils;
15  
16  import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
17  import org.springframework.beans.factory.xml.ParserContext;
18  import org.w3c.dom.Element;
19  import org.w3c.dom.Node;
20  import org.w3c.dom.NodeList;
21  
22  /**
23   * <p>
24   * This iterates over child elements, parsing them and calling
25   * {@link #insertBean(BeanAssembler, Object, Element, Element)}.
26   * </p>
27   * <p>
28   * There are two ways we can parse a tree of elements - have an external loop or let
29   * each parser iterate over its own children. Mule uses the first strategy, but some
30   * (most? all?) third party BDPs use the second. This processor lets us use third
31   * party beans inside the Mule framework.
32   * </p>
33   * <p>
34   * So this is a very specialised parser that should only be used when trying to
35   * inter-operate with beans from third party packages which themselves control how
36   * their children are parsed.
37   * </p>
38   * <p>
39   * Since for Mule beans the iteration over child elements (at least currently) is
40   * done via {@link MuleHierarchicalBeanDefinitionParserDelegate} the calling parser
41   * needs to set the flag
42   * {@link MuleHierarchicalBeanDefinitionParserDelegate#MULE_NO_RECURSE} - this stops
43   * the Mule recursion from working.
44   * </p>
45   * <p>
46   * NOTE - IMHO (ac) the Mule approach was probably a mistake; this processor could be
47   * used as a way to slowly migrate the Mule code to the more common approach.
48   * </p>
49   */
50  public abstract class AbstractChildElementIterator implements PostProcessor
51  {
52  
53      private BeanAssemblerFactory beanAssemblerFactory;
54      private PropertyConfiguration configuration;
55  
56      public AbstractChildElementIterator(BeanAssemblerFactory beanAssemblerFactory, PropertyConfiguration configuration)
57      {
58          this.beanAssemblerFactory = beanAssemblerFactory;
59          this.configuration = configuration;
60      }
61  
62      public void postProcess(ParserContext context, BeanAssembler assembler, Element element)
63      {
64          NodeList children = element.getChildNodes();
65          for (int i = 0; i < children.getLength(); ++i)
66          {
67              Node child = children.item(i);
68              if (child.getNodeType() == Node.ELEMENT_NODE)
69              {
70                  processChildElement(context, assembler, element, (Element) child);
71              }
72          }
73      }
74  
75      protected void processChildElement(ParserContext context, BeanAssembler assembler, Element parent, Element child)
76      {
77          Object childBean = null;
78          if (SpringXMLUtils.isBeansNamespace(child)
79              || SpringXMLUtils.isLocalName(child, BeanDefinitionParserDelegate.REF_ELEMENT))
80          {
81              childBean = context.getDelegate().parsePropertySubElement(child, null);
82          }
83          else
84          {
85              childBean = context.getDelegate().parseCustomElement(child,
86                  assembler.getBean().getBeanDefinition());
87          }
88          BeanAssembler targetAssembler = beanAssemblerFactory.newBeanAssembler(null, null, configuration,
89              assembler.getBean().getRawBeanDefinition());
90          insertBean(targetAssembler, childBean, parent, child);
91      }
92  
93      protected abstract void insertBean(BeanAssembler targetAssembler, Object childBean, Element parent, Element child);
94  
95  }