View Javadoc

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