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.delegate;
8   
9   import org.mule.config.spring.parsers.MuleChildDefinitionParser;
10  import org.mule.config.spring.parsers.MuleDefinitionParser;
11  import org.mule.config.spring.parsers.MuleDefinitionParserConfiguration;
12  
13  import org.springframework.beans.factory.support.AbstractBeanDefinition;
14  import org.springframework.beans.factory.xml.ParserContext;
15  import org.w3c.dom.Element;
16  
17  /**
18   * This allows several parsers to be used on a single element, creating a parent bean with
19   * the first parser and then extending that with child parsers.
20   *
21   * <p>Note that this make a lot of assumptions about what type of parser is used.
22   */
23  public abstract class AbstractSingleParentFamilyDefinitionParser
24          extends AbstractFirstResultSerialDefinitionParser
25  {
26  
27      public AbstractSingleParentFamilyDefinitionParser()
28      {
29          super();
30      }
31  
32      public AbstractSingleParentFamilyDefinitionParser(boolean doReset)
33      {
34          super(doReset);
35      }
36  
37      protected MuleChildDefinitionParser addChildDelegate(MuleChildDefinitionParser delegate)
38      {
39          return (MuleChildDefinitionParser) super.addDelegate(delegate);
40      }
41  
42      protected MuleDefinitionParserConfiguration addDelegate(MuleDefinitionParser delegate)
43      {
44          if (size() > 0)
45          {
46              return addDelegateAsChild(delegate);
47          }
48          else
49          {
50              return super.addDelegate(delegate);
51          }
52      }
53  
54      protected MuleDefinitionParserConfiguration addDelegateAsChild(MuleDefinitionParser delegate)
55      {
56          if (delegate instanceof MuleChildDefinitionParser)
57          {
58              return addChildDelegate((MuleChildDefinitionParser) delegate);
59          }
60          else
61          {
62              throw new IllegalStateException("Children must implement child interface");
63          }
64      }
65  
66      protected AbstractBeanDefinition doSingleBean(int index, MuleDefinitionParser parser,
67                                                    Element element, ParserContext parserContext)
68      {
69          if (0 != index)
70          {
71              ((MuleChildDefinitionParser) parser).forceParent(firstDefinition);
72              // we need this because we often block "everything but" which would mean
73              // being unable to set ourselves on the parent
74              ((MuleChildDefinitionParser) parser).getTargetPropertyConfiguration().setIgnoredDefault(false);
75          }
76          return super.doSingleBean(index, parser, element, parserContext);
77      }
78  
79  }