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