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.generic;
8   
9   import org.mule.config.spring.parsers.assembly.BeanAssembler;
10  
11  import org.springframework.beans.factory.support.AbstractBeanDefinition;
12  import org.springframework.beans.factory.xml.ParserContext;
13  import org.w3c.dom.Element;
14  
15  /**
16   * Behaves as {@link org.mule.config.spring.parsers.generic.ParentDefinitionParser},
17   * but allows any named bean to be the parent, rather than using the enclosing element in the DOM tree.
18   */
19  public class NamedDefinitionParser extends ParentDefinitionParser
20  {
21  
22      private String name;
23      private boolean isDynamic = false;
24  
25      public NamedDefinitionParser()
26      {
27          isDynamic = true;
28      }
29  
30      public NamedDefinitionParser(String name)
31      {
32          addIgnored(ATTRIBUTE_NAME);
33          this.name = name;
34      }
35  
36      public String getName()
37      {
38          return name;
39      }
40  
41      public void setName(String name)
42      {
43          this.name = name;
44      }
45  
46      protected String getParentBeanName(Element element)
47      {
48          return name;
49      }
50  
51      protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext)
52      {
53          if (isDynamic)
54          {
55              if (element.hasAttribute(ATTRIBUTE_NAME))
56              {
57                  setName(element.getAttribute(ATTRIBUTE_NAME));
58                  element.removeAttribute(ATTRIBUTE_NAME);
59              }
60              else
61              {
62                  throw new IllegalStateException("Missing name attribute for " + element.getLocalName());
63              }
64          }
65          return super.parseInternal(element, parserContext);
66      }
67  
68      protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
69      {
70          super.postProcess(context, assembler, element);
71          // may be used as top level element, so set ID from name
72          AutoIdUtils.ensureUniqueId(element, "named");
73      }
74  }