View Javadoc

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