KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > xml > SimplePropertyNamespaceHandler


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.xml;
18
19 import org.w3c.dom.Attr JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22
23 import org.springframework.beans.MutablePropertyValues;
24 import org.springframework.beans.factory.config.BeanDefinition;
25 import org.springframework.beans.factory.config.BeanDefinitionHolder;
26 import org.springframework.beans.factory.config.RuntimeBeanReference;
27 import org.springframework.core.Conventions;
28
29 /**
30  * Simple <code>NamespaceHandler</code> implementation that maps custom attributes directly through
31  * to bean properties. An important point to note is that this <code>NamespaceHandler</code> does not
32  * have a corresponding schema since there is no way to know in advance all possible attribute names.
33  * <p/>
34  * An example of the usage of this <code>NamespaceHandler</code> is shown below:
35  * <pre class="code">
36  * &lt;bean id=&quot;rob&quot; class=&quot;..TestBean&quot; p:name=&quot;Rob Harrop&quot; p:spouse-ref=&quot;sally&quot;/&gt;
37  * </pre>
38  * Here the '<code>p:name</code>' corresponds directly to the '<code>name</code>' property on class
39  * '<code>TestBean</code>'. The '<code>p:spouse-ref</code>' attributes corresponds to the
40  * '<code>spouse</code>' property and rather than being the concrete value it conatins the name
41  * of the bean that will be injected into that property.
42  *
43  * @author Rob Harrop
44  * @since 2.0
45  */

46 public class SimplePropertyNamespaceHandler implements NamespaceHandler {
47
48     private static final String JavaDoc REF_SUFFIX = "-ref";
49
50
51     public void init() {
52     }
53
54     public BeanDefinition parse(Element JavaDoc element, ParserContext parserContext) {
55         parserContext.getReaderContext().error(
56                 "Class [" + getClass().getName() + "] does not support custom elements.", element);
57         return null;
58     }
59
60     public BeanDefinitionHolder decorate(Node JavaDoc node, BeanDefinitionHolder definition, ParserContext parserContext) {
61         if (node instanceof Attr JavaDoc) {
62             Attr JavaDoc attr = (Attr JavaDoc) node;
63             String JavaDoc propertyName = attr.getLocalName();
64             String JavaDoc propertyValue = attr.getValue();
65             MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues();
66             if (pvs.contains(propertyName)) {
67                 parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " +
68                         "both <property> and inline syntax. Only one approach may be used per property.", attr);
69             }
70             if (propertyName.endsWith(REF_SUFFIX)) {
71                 propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length());
72                 pvs.addPropertyValue(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue));
73             }
74             else {
75                 pvs.addPropertyValue(Conventions.attributeNameToPropertyName(propertyName), propertyValue);
76             }
77         }
78         return definition;
79     }
80
81 }
82
Popular Tags