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.generic.ChildDefinitionParser;
10  import org.mule.config.spring.util.SpringXMLUtils;
11  
12  import org.springframework.beans.factory.config.MapFactoryBean;
13  import org.springframework.beans.factory.config.RuntimeBeanReference;
14  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
15  import org.springframework.beans.factory.support.ManagedMap;
16  import org.springframework.beans.factory.xml.ParserContext;
17  import org.w3c.dom.Attr;
18  import org.w3c.dom.Element;
19  import org.w3c.dom.NamedNodeMap;
20  
21  /**
22   * Creates a single, stand-alone map object and processes all attributes to this map
23   */
24  public class AttributeMapDefinitionParser extends ChildDefinitionParser
25  {
26      public AttributeMapDefinitionParser(String setter)
27      {
28          super(setter, ManagedMap.class);
29      }
30  
31      protected Class getBeanClass(Element element)
32      {
33          return MapFactoryBean.class;
34      }
35  
36      protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
37      {
38          ManagedMap values = new ManagedMap();
39          NamedNodeMap attributes = element.getAttributes();
40          for (int x = 0; x < attributes.getLength(); x++)
41          {
42              Attr attribute = (Attr) attributes.item(x);
43              String oldName = SpringXMLUtils.attributeName(attribute);
44              //TODO How can I use bestGuessName
45              String name = beanPropertyConfiguration.translateName(oldName);
46              Object value = beanPropertyConfiguration.translateValue(oldName, attribute.getNodeValue());
47              if (beanPropertyConfiguration.isReference(oldName))
48              {
49                  values.put(name, new RuntimeBeanReference(attribute.getNodeValue()));
50              }
51              else
52              {
53                  values.put(name, value);
54              }
55          }
56          builder.addPropertyValue("sourceMap", values);
57          builder.addPropertyValue("targetMapClass", super.getBeanClass(element));
58          postProcess(parserContext, getBeanAssembler(element, builder), element);
59      }
60  
61  }