View Javadoc

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