KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > beans > xml > GenericBeanDefinitionParser


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  * Carsten Woelk [cwoelk at neteye dot de]
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.riotfamily.common.beans.xml;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29
30 import org.springframework.beans.factory.config.RuntimeBeanReference;
31 import org.springframework.beans.factory.support.AbstractBeanDefinition;
32 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
33 import org.springframework.beans.factory.xml.ParserContext;
34 import org.springframework.core.Conventions;
35 import org.springframework.util.Assert;
36 import org.springframework.util.StringUtils;
37 import org.w3c.dom.Attr JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.NamedNodeMap JavaDoc;
40
41 /**
42  * @author Carsten Woelk [cwoelk at neteye dot de]
43  * @since 6.5
44  */

45 public class GenericBeanDefinitionParser extends AbstractGenericBeanDefinitionParser {
46
47     public static final String JavaDoc NAME_ATTRIBUTE = "name";
48
49     private HashMap JavaDoc translations = new HashMap JavaDoc();
50
51     private HashSet JavaDoc references = new HashSet JavaDoc();
52
53     public GenericBeanDefinitionParser(Class JavaDoc beanClass) {
54         super(beanClass);
55     }
56
57     public GenericBeanDefinitionParser addTranslation(String JavaDoc attributeName,
58             String JavaDoc property) {
59
60         translations.put(attributeName, property);
61         return this;
62     }
63
64     public GenericBeanDefinitionParser addReference(String JavaDoc attributeName) {
65         references.add(extractPropertyName(attributeName));
66         return this;
67     }
68
69     protected String JavaDoc resolveAlias(Element JavaDoc element, AbstractBeanDefinition definition, ParserContext parserContext) {
70         return element.getAttribute(NAME_ATTRIBUTE);
71     }
72
73     /**
74      * Parse the supplied {@link Element} and populate the supplied
75      * {@link BeanDefinitionBuilder} as required.
76      * <p>This implementation maps any attributes present on the
77      * supplied element to {@link org.springframework.beans.PropertyValue}
78      * instances, and
79      * {@link BeanDefinitionBuilder#addPropertyValue(String, Object) adds them}
80      * to the
81      * {@link org.springframework.beans.factory.config.BeanDefinition builder}.
82      * <p>The {@link #extractPropertyName(String)} method is used to
83      * reconcile the name of an attribute with the name of a JavaBean
84      * property.
85      * @param element the XML element being parsed
86      * @param parserContext the object encapsulating the current state of the parsing process
87      * @param builder used to define the <code>BeanDefinition</code>
88      * @see #extractPropertyName(String)
89      */

90     protected final void doParse(Element JavaDoc element,
91             ParserContext parserContext, BeanDefinitionBuilder builder) {
92         
93         NamedNodeMap JavaDoc attributes = element.getAttributes();
94         for (int x = 0; x < attributes.getLength(); x++) {
95             Attr JavaDoc attribute = (Attr JavaDoc) attributes.item(x);
96             String JavaDoc name = attribute.getLocalName();
97             if (isEligibleAttribute(name)) {
98                 String JavaDoc propertyName = extractPropertyName(name);
99                 Assert.state(StringUtils.hasText(propertyName),
100                         "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");
101
102                 Object JavaDoc value;
103                 if (references.contains(propertyName)) {
104                     value = new RuntimeBeanReference(attribute.getValue());
105                 }
106                 else {
107                     value = attribute.getValue();
108                 }
109                 builder.addPropertyValue(propertyName, value);
110             }
111         }
112         postProcess(builder, parserContext, element);
113     }
114
115     /**
116      * Determine whether the given attribute is eligible for being
117      * turned into a corresponding bean property value.
118      * <p>The default implementation considers any attribute as eligible,
119      * except for the "id" and "name" attributes.
120      * @param attributeName the attribute name taken straight from the
121      * XML element being parsed (never <code>null</code>)
122      */

123     protected boolean isEligibleAttribute(String JavaDoc attributeName) {
124         return !ID_ATTRIBUTE.equals(attributeName) && !NAME_ATTRIBUTE.equals(attributeName);
125     }
126
127     /**
128      * Extract a JavaBean property name from the supplied attribute name.
129      * <p>The default implementation first looks for a translation set via
130      * {@link #addTranslation(String, String)}. If no translation is found,
131      * the {@link Conventions#attributeNameToPropertyName(String)}
132      * method to perform the extraction.
133      * <p>The name returned must obey the standard JavaBean property name
134      * conventions. For example for a class with a setter method
135      * '<code>setBingoHallFavourite(String)</code>', the name returned had
136      * better be '<code>bingoHallFavourite</code>' (with that exact casing).
137      * @param attributeName the attribute name taken straight from the
138      * XML element being parsed (never <code>null</code>)
139      * @return the extracted JavaBean property name (must never be <code>null</code>)
140      */

141     protected String JavaDoc extractPropertyName(String JavaDoc attributeName) {
142         String JavaDoc property = (String JavaDoc) translations.get(attributeName);
143         if (property == null) {
144             property = Conventions.attributeNameToPropertyName(attributeName);
145         }
146         return property;
147     }
148
149     /**
150      * Hook method that derived classes can implement to inspect/change a
151      * bean definition after parsing is complete.
152      * <p>The default implementation delegates to the <code>postProcess</code>
153      * version without ParserContext argument.
154      * @param beanDefinition the parsed (and probably totally defined) bean definition being built
155      * @param parserContext the object encapsulating the current state of the parsing process
156      * @param element the XML element that was the source of the bean definition's metadata
157      */

158     protected void postProcess(BeanDefinitionBuilder beanDefinition,
159             ParserContext parserContext, Element JavaDoc element) {
160         
161         postProcess(beanDefinition, element);
162     }
163     
164     /**
165      * Hook method that derived classes can implement to inspect/change a
166      * bean definition after parsing is complete.
167      * <p>The default implementation does nothing.
168      * @param beanDefinition the parsed (and probably totally defined) bean definition being built
169      * @param element the XML element that was the source of the bean definition's metadata
170      */

171     protected void postProcess(BeanDefinitionBuilder beanDefinition, Element JavaDoc element) {
172     }
173
174
175 }
176
Popular Tags