View Javadoc

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