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.generic;
8   
9   import org.w3c.dom.Element;
10  import org.w3c.dom.Node;
11  
12  /**
13   * Same as ChildDefinitionParser but injects the child element into the grandparent object 
14   * (2 levels up in the XML tree).
15   */
16  public class GrandchildDefinitionParser extends ChildDefinitionParser
17  {
18      public GrandchildDefinitionParser(String setterMethod, Class clazz)
19      {
20          super(setterMethod, clazz);
21      }
22  
23      public GrandchildDefinitionParser(String setterMethod, Class clazz, Class constraint, boolean allowClassAttribute)
24      {
25          super(setterMethod, clazz, constraint, allowClassAttribute);
26      }
27  
28      @Override
29      protected String getParentBeanName(Element element)
30      {
31          return getGrandparentBeanName(element);
32      }
33  
34      protected String getGrandparentBeanName(Element element)
35      {
36          Node parent = element.getParentNode();
37          if (parent == null)
38          {
39              logger.error("No parent node found for element " + element);
40              return null;
41          }
42          Node grandparent = parent.getParentNode();
43          if (grandparent == null)
44          {
45              logger.error("No parent node found for element " + parent);
46              return null;
47          }
48          Node grandparentNameAttribute = grandparent.getAttributes().getNamedItem("name");
49          if (grandparentNameAttribute == null)
50          {
51              logger.error("Grandparent node has no 'name' attribute: " + grandparent);
52              return null;
53          }
54          return grandparentNameAttribute.getNodeValue();
55      }
56  }