View Javadoc

1   /*
2    * $Id: AttributeListEntryDefinitionParser.java 12259 2008-07-09 14:18:28Z dirk.olmes $
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.collection;
12  
13  import org.mule.config.spring.parsers.AbstractChildDefinitionParser;
14  import org.mule.config.spring.util.SpringXMLUtils;
15  import org.mule.util.StringUtils;
16  
17  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
18  import org.springframework.beans.factory.xml.ParserContext;
19  import org.w3c.dom.Attr;
20  import org.w3c.dom.Element;
21  
22  public class AttributeListEntryDefinitionParser
23          extends AbstractChildDefinitionParser implements DynamicAttributeDefinitionParser
24  {
25  
26      private String setterMethod;
27      private String attributeName;
28  
29      /**
30       * Only for use with dynamic naming
31       */
32      public AttributeListEntryDefinitionParser(String setterMethod)
33      {
34          this(setterMethod, null);
35      }
36  
37      public AttributeListEntryDefinitionParser(String setterMethod, String attributeName)
38      {
39          this.setterMethod = setterMethod;
40          setAttributeName(attributeName);
41      }
42  
43      public String getPropertyName(Element element)
44      {
45          return setterMethod;
46      }
47  
48      protected Class getBeanClass(Element element)
49      {
50          return ChildListEntryDefinitionParser.ListEntry.class;
51      }
52  
53      public void setAttributeName(String attributeName)
54      {
55          this.attributeName = attributeName;
56      }
57  
58      protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
59      {
60          Attr attribute = element.getAttributeNode(attributeName);
61          if (null == attribute || StringUtils.isEmpty(attribute.getNodeValue()))
62          {
63              throw new IllegalStateException(
64                      "No value for " + attributeName + " in " + SpringXMLUtils.elementToString(element));
65          }
66          String value = attribute.getNodeValue();
67          builder.getRawBeanDefinition().setSource(new ChildListEntryDefinitionParser.ListEntry(value));
68          this.postProcess(parserContext, getBeanAssembler(element, builder), element);
69      }
70  
71  }