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; 028 029/** 030 * Simple {@code NamespaceHandler} implementation that maps custom attributes 031 * directly through to bean properties. An important point to note is that this 032 * {@code NamespaceHandler} does not have a corresponding schema since there 033 * is no way to know in advance all possible attribute names. 034 * 035 * <p>An example of the usage of this {@code NamespaceHandler} is shown below: 036 * 037 * <pre class="code"> 038 * <bean id="rob" class="..TestBean" p:name="Rob Harrop" p:spouse-ref="sally"/></pre> 039 * 040 * Here the '{@code p:name}' corresponds directly to the '{@code name}' 041 * property on class '{@code TestBean}'. The '{@code p:spouse-ref}' 042 * attributes corresponds to the '{@code spouse}' property and, rather 043 * than being the concrete value, it contains the name of the bean that will 044 * be injected into that property. 045 * 046 * @author Rob Harrop 047 * @author Juergen Hoeller 048 * @since 2.0 049 */ 050public class SimplePropertyNamespaceHandler implements NamespaceHandler { 051 052 private static final String REF_SUFFIX = "-ref"; 053 054 055 @Override 056 public void init() { 057 } 058 059 @Override 060 public BeanDefinition parse(Element element, ParserContext parserContext) { 061 parserContext.getReaderContext().error( 062 "Class [" + getClass().getName() + "] does not support custom elements.", element); 063 return null; 064 } 065 066 @Override 067 public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) { 068 if (node instanceof Attr) { 069 Attr attr = (Attr) node; 070 String propertyName = parserContext.getDelegate().getLocalName(attr); 071 String propertyValue = attr.getValue(); 072 MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues(); 073 if (pvs.contains(propertyName)) { 074 parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " + 075 "both <property> and inline syntax. Only one approach may be used per property.", attr); 076 } 077 if (propertyName.endsWith(REF_SUFFIX)) { 078 propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length()); 079 pvs.add(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue)); 080 } 081 else { 082 pvs.add(Conventions.attributeNameToPropertyName(propertyName), propertyValue); 083 } 084 } 085 return definition; 086 } 087 088}