001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.factory.xml; 018 019import org.w3c.dom.Attr; 020import org.w3c.dom.Element; 021import org.w3c.dom.Node; 022 023import org.springframework.beans.MutablePropertyValues; 024import org.springframework.beans.factory.config.BeanDefinition; 025import org.springframework.beans.factory.config.BeanDefinitionHolder; 026import org.springframework.beans.factory.config.RuntimeBeanReference; 027import org.springframework.core.Conventions; 028import org.springframework.lang.Nullable; 029 030/** 031 * Simple {@code NamespaceHandler} implementation that maps custom attributes 032 * directly through to bean properties. An important point to note is that this 033 * {@code NamespaceHandler} does not have a corresponding schema since there 034 * is no way to know in advance all possible attribute names. 035 * 036 * <p>An example of the usage of this {@code NamespaceHandler} is shown below: 037 * 038 * <pre class="code"> 039 * <bean id="rob" class="..TestBean" p:name="Rob Harrop" p:spouse-ref="sally"/></pre> 040 * 041 * Here the '{@code p:name}' corresponds directly to the '{@code name}' 042 * property on class '{@code TestBean}'. The '{@code p:spouse-ref}' 043 * attributes corresponds to the '{@code spouse}' property and, rather 044 * than being the concrete value, it contains the name of the bean that will 045 * be injected into that property. 046 * 047 * @author Rob Harrop 048 * @author Juergen Hoeller 049 * @since 2.0 050 */ 051public class SimplePropertyNamespaceHandler implements NamespaceHandler { 052 053 private static final String REF_SUFFIX = "-ref"; 054 055 056 @Override 057 public void init() { 058 } 059 060 @Override 061 @Nullable 062 public BeanDefinition parse(Element element, ParserContext parserContext) { 063 parserContext.getReaderContext().error( 064 "Class [" + getClass().getName() + "] does not support custom elements.", element); 065 return null; 066 } 067 068 @Override 069 public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) { 070 if (node instanceof Attr) { 071 Attr attr = (Attr) node; 072 String propertyName = parserContext.getDelegate().getLocalName(attr); 073 String propertyValue = attr.getValue(); 074 MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues(); 075 if (pvs.contains(propertyName)) { 076 parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " + 077 "both <property> and inline syntax. Only one approach may be used per property.", attr); 078 } 079 if (propertyName.endsWith(REF_SUFFIX)) { 080 propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length()); 081 pvs.add(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue)); 082 } 083 else { 084 pvs.add(Conventions.attributeNameToPropertyName(propertyName), propertyValue); 085 } 086 } 087 return definition; 088 } 089 090}