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