View Javadoc

1   /*
2    * $Id: ChildDefinitionParser.java 10285 2008-01-11 13:58:15Z acooke $
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.config.spring.parsers.AbstractChildDefinitionParser;
13  
14  import org.w3c.dom.Element;
15  
16  /**
17   * Creates a definition parser that will construct a single child element and inject it into
18   * the parent object (the enclosing XML element).
19   *
20   * The parser will set all attributes defined in the Xml as bean properties and will
21   * process any nested elements as bean properties too, except the correct Definition parser
22   * for the element will be looked up automatically.
23   */
24  public class ChildDefinitionParser extends AbstractChildDefinitionParser
25  {
26  
27      protected Class clazz;
28      protected String setterMethod;
29  
30      /**
31       * The class will be inferred from the class attribute
32       * @param setterMethod The target method (where the child will be injected)
33       */
34      public ChildDefinitionParser(String setterMethod)
35      {
36          this(setterMethod, null, null, true);
37      }
38  
39      /**
40       * @param setterMethod The target method (where the child will be injected)
41       * @param clazz The class created by this element/parser
42       */
43      public ChildDefinitionParser(String setterMethod, Class clazz)
44      {
45          this(setterMethod, clazz, null, null == clazz);
46      }
47  
48      /**
49       * The class (which is inferred from the class attribute if null here) is checked to be
50       * a subclass of the constraint
51       * @param setterMethod The target method (where the child will be injected)
52       * @param clazz The class created by this element/parser (may be null)
53       * @param constraint Superclass of clazz (may be null)
54       */
55      public ChildDefinitionParser(String setterMethod, Class clazz, Class constraint)
56      {
57          this(setterMethod, clazz, constraint, null == clazz);
58      }
59  
60      /**
61       * The class (which is inferred from the class attribute if null here) is checked to be
62       * a subclass of the constraint.
63       *
64       * @param setterMethod The target method (where the child will be injected)
65       * @param clazz The class created by this element/parser (may be null)
66       * @param constraint Superclass of clazz (may be null)
67       * @param allowClassAttribute Is class read from class attribute (if present, takes precedence over clazz)
68       */
69      public ChildDefinitionParser(String setterMethod, Class clazz, Class constraint, boolean allowClassAttribute)
70      {
71          this.clazz = clazz;
72          this.setterMethod = setterMethod;
73          setClassConstraint(constraint);
74          setAllowClassAttribute(allowClassAttribute);
75      }
76  
77      protected void preProcess(Element element)
78      {
79          super.preProcess(element);
80          if (isAllowClassAttribute())
81          {
82             clazz = null; // reset for this element
83          }
84      }
85  
86      protected Class getBeanClass(Element element)
87      {
88          return clazz;
89      }
90  
91      public String getPropertyName(Element e)
92      {
93          return setterMethod;
94      }
95  
96  }