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