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.specific.properties;
8   
9   import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
10  import org.mule.config.spring.parsers.assembly.MapEntryCombiner;
11  import org.mule.config.spring.parsers.collection.ChildSingletonMapDefinitionParser;
12  import org.mule.config.spring.parsers.delegate.AbstractSingleParentFamilyDefinitionParser;
13  import org.mule.config.spring.parsers.processors.AddAttribute;
14  
15  /**
16   * This constructs a nested map - keyed by "mapKey" - and then adds an entry in that
17   * named from the attribute "keyAttribute".  Child elements can then set a value (or
18   * values, if it is a collection) on {@link org.mule.config.spring.parsers.assembly.MapEntryCombiner#VALUE}
19   */
20  public class ElementInNestedMapDefinitionParser extends AbstractSingleParentFamilyDefinitionParser
21  {
22  
23      public ElementInNestedMapDefinitionParser(String mapSetter, String mapKey, String keyAttribute)
24      {
25          // children (parameters) want to append to the inner map, not the outer one
26          setReturnFirstResult(false);
27          addDelegate(new ChildSingletonMapDefinitionParser(mapSetter))
28                  .registerPreProcessor(new AddAttribute(MapEntryCombiner.KEY, mapKey))
29                  .addCollection(mapSetter)
30                  .setIgnoredDefault(true)
31                  .removeIgnored(MapEntryCombiner.KEY)
32                  .addIgnored(AbstractMuleBeanDefinitionParser.ATTRIBUTE_NAME);
33          addChildDelegate(new ChildSingletonMapDefinitionParser(MapEntryCombiner.VALUE))
34                  .addAlias(keyAttribute, MapEntryCombiner.KEY)
35                  .addCollection(MapEntryCombiner.VALUE)
36                  .addIgnored(AbstractMuleBeanDefinitionParser.ATTRIBUTE_NAME);
37      }
38  
39  }