1 /* 2 * $Id: ChildDefinitionParser.java 19191 2010-08-25 21:05:23Z tcarlson $ 3 * -------------------------------------------------------------------------------------- 4 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.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 * 50 * @param setterMethod 51 * @param clazz 52 * @param singleton determines is bean should be singleton or not 53 */ 54 public ChildDefinitionParser(String setterMethod, Class clazz, boolean singleton) 55 { 56 this(setterMethod, clazz); 57 this.singleton = singleton; 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 * @param setterMethod The target method (where the child will be injected) 64 * @param clazz The class created by this element/parser (may be null) 65 * @param constraint Superclass of clazz (may be null) 66 */ 67 public ChildDefinitionParser(String setterMethod, Class clazz, Class constraint) 68 { 69 this(setterMethod, clazz, constraint, null == clazz); 70 } 71 72 /** 73 * The class (which is inferred from the class attribute if null here) is checked to be 74 * a subclass of the constraint. 75 * 76 * @param setterMethod The target method (where the child will be injected) 77 * @param clazz The class created by this element/parser (may be null) 78 * @param constraint Superclass of clazz (may be null) 79 * @param allowClassAttribute Is class read from class attribute (if present, takes precedence over clazz) 80 */ 81 public ChildDefinitionParser(String setterMethod, Class clazz, Class constraint, boolean allowClassAttribute) 82 { 83 this.clazz = clazz; 84 this.setterMethod = setterMethod; 85 setClassConstraint(constraint); 86 setAllowClassAttribute(allowClassAttribute); 87 } 88 89 protected void preProcess(Element element) 90 { 91 super.preProcess(element); 92 if (isAllowClassAttribute()) 93 { 94 clazz = null; // reset for this element 95 } 96 } 97 98 protected Class getBeanClass(Element element) 99 { 100 return clazz; 101 } 102 103 public String getPropertyName(Element e) 104 { 105 return setterMethod; 106 } 107 108 }