View Javadoc

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