View Javadoc

1   /*
2    * $Id: DescendentDefinitionParser.java 10256 2008-01-08 15:20:25Z dfeist $
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.mule.util.StringUtils;
13  
14  import org.w3c.dom.Element;
15  import org.w3c.dom.Node;
16  
17  /**
18   * An extension to {@line ChildDefinitionParser} which recurses up the DOM
19   * tree until it finds a named parent.
20   */
21  public class DescendentDefinitionParser extends ChildDefinitionParser
22  {
23      public DescendentDefinitionParser(String setterMethod, Class clazz)
24      {
25          super(setterMethod, clazz);
26      }
27  
28      protected String getParentBeanName(Element element)
29      {
30          Node node = element;
31          while (null != node && node instanceof Element)
32          {
33              String name = super.getParentBeanName((Element) node);
34              if (!StringUtils.isBlank(name))
35              {
36                  return name;
37              }
38              node = element.getParentNode();
39          }
40          throw new IllegalStateException("Bean: " + element.getNodeName() + " has no grandparent");
41      }
42  }