View Javadoc

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