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.MuleHierarchicalBeanDefinitionParserDelegate;
10  import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
11  
12  import java.util.Map;
13  
14  import org.springframework.beans.factory.BeanDefinitionStoreException;
15  import org.springframework.beans.factory.config.MapFactoryBean;
16  import org.springframework.beans.factory.support.AbstractBeanDefinition;
17  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
18  import org.springframework.beans.factory.xml.ParserContext;
19  import org.w3c.dom.Element;
20  
21  /**
22   * Creates a single, stand-alone map object and processes standard Spring sub elements
23   */
24  public class OrphanMapDefinitionParser extends AbstractMuleBeanDefinitionParser
25  {
26      protected Class mapClass;
27      protected String name;
28      protected boolean attributeName;
29      protected boolean dynamicName = false;
30  
31      /**
32       * Creates a Map parser that will add the map directly to the registry
33       *
34       * @param mapClass the type of map to create
35       */
36      public OrphanMapDefinitionParser(Class mapClass)
37      {
38          this.mapClass = mapClass;
39          dynamicName = true;
40      }
41  
42      /**
43       * Creates a Map parser that will add the map directly to the registry
44       *
45       * @param mapClass the type of map to create
46       * @param name the name of the map property
47       */
48      public OrphanMapDefinitionParser(Class mapClass, String name)
49      {
50          this.mapClass = mapClass;
51          this.name = name;
52      }
53  
54      /**
55       * Creates a Map parser that will add the map directly to the registry
56       *
57       * @param mapClass the type of map to create
58       * @param name the name of the map property
59       * @param attributeName whether the name specified is actually an attribute name on the element.  The map name will
60       * be retrieved from the element attribute.
61       */
62      public OrphanMapDefinitionParser(Class mapClass, String name, boolean attributeName)
63      {
64          this.mapClass = mapClass;
65          this.name = name;
66          this.attributeName = attributeName;
67      }
68  
69      protected Class getBeanClass(Element element)
70      {
71          return MapFactoryBean.class;
72      }
73  
74      protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
75      {
76          Map parsedMap = parserContext.getDelegate().parseMapElement(element, builder.getRawBeanDefinition());
77          builder.addPropertyValue("sourceMap", parsedMap);
78          builder.addPropertyValue("targetMapClass", mapClass.getName());
79          getBeanAssembler(element, builder).setBeanFlag(MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_RECURSE);
80      }
81  
82      @Override
83  
84      protected void preProcess(Element element)
85      {
86          super.preProcess(element);
87          if (dynamicName)
88          {
89              name = null;
90          }
91      }
92      
93      @java.lang.Override
94      protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException
95      {
96          if(attributeName)
97          {
98              return element.getAttribute(name);
99          }
100         return name;
101     }
102 }