KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > beans > override > OverrideNamespaceHandler


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  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.beans.override;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.riotfamily.common.beans.xml.GenericBeanDefinitionParser;
32 import org.riotfamily.common.beans.xml.GenericNamespaceHandlerSupport;
33 import org.springframework.beans.factory.config.BeanDefinition;
34 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
35 import org.springframework.beans.factory.support.RootBeanDefinition;
36 import org.springframework.beans.factory.xml.ParserContext;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * @author Felix Gnass [fgnass at neteye dot de]
41  * @since 6.5
42  */

43 public class OverrideNamespaceHandler extends GenericNamespaceHandlerSupport {
44
45     public void init() {
46         registerBeanDefinitionParser("properties", new PropertyOverrideParser());
47         registerBeanDefinitionParser("put", new MapMergeParser());
48         registerBeanDefinitionParser("add", new ListMergeParser());
49         registerBeanDefinitionParser("bean", new BeanOverrideParser());
50     }
51     
52     private static class PropertyOverrideParser extends GenericBeanDefinitionParser {
53         
54         public PropertyOverrideParser() {
55             super(PropertyOverrideProcessor.class);
56         }
57         
58         protected void postProcess(BeanDefinitionBuilder beanDefinition,
59                 ParserContext parserContext, Element JavaDoc element) {
60             
61             BeanDefinition bd = new RootBeanDefinition();
62             parserContext.getDelegate().parsePropertyElements(element, bd);
63             beanDefinition.addPropertyValue("propertyValues", bd.getPropertyValues());
64         }
65     }
66     
67     private static class MapMergeParser extends GenericBeanDefinitionParser {
68         
69         public MapMergeParser() {
70             super(MapMergeProcessor.class);
71         }
72         
73         protected void postProcess(BeanDefinitionBuilder beanDefinition,
74                 ParserContext parserContext, Element JavaDoc element) {
75             
76             Map JavaDoc entries = parserContext.getDelegate().parseMapElement(
77                     element, beanDefinition.getBeanDefinition());
78             
79             // The parsed Map is a ManagedMap. We put the values into a
80
// HashMap so that the reference resolution is deferred until
81
// the actual target bean is initialized.
82
beanDefinition.addPropertyValue("entries", new HashMap JavaDoc(entries));
83         }
84     }
85     
86     private static class ListMergeParser extends GenericBeanDefinitionParser {
87         
88         public ListMergeParser() {
89             super(ListMergeProcessor.class);
90         }
91         
92         protected void postProcess(BeanDefinitionBuilder beanDefinition,
93                 ParserContext parserContext, Element JavaDoc element) {
94             
95             List JavaDoc values = parserContext.getDelegate().parseListElement(
96                     element, beanDefinition.getBeanDefinition());
97             
98             // The parsed List is a ManagedList. We put the values into an
99
// ArrayList so that the reference resolution is deferred until
100
// the actual target bean is initialized.
101
beanDefinition.addPropertyValue("values", new ArrayList JavaDoc(values));
102         }
103     }
104
105     private static class BeanOverrideParser extends GenericBeanDefinitionParser {
106         
107         public BeanOverrideParser() {
108             super(BeanOverrideProcessor.class);
109         }
110         
111         protected boolean isEligibleAttribute(String JavaDoc attributeName) {
112             return attributeName.equals("ref")
113                     || attributeName.equals("merge")
114                     || attributeName.equals("order");
115         }
116         
117         protected void postProcess(BeanDefinitionBuilder builder,
118                 ParserContext parserContext, Element JavaDoc element) {
119             
120             BeanReplacement replacement = new BeanReplacement(
121                     parserContext.getDelegate().parseBeanDefinitionElement(
122                     element, null, builder.getBeanDefinition()));
123             
124             builder.addPropertyValue("beanReplacement", replacement);
125             builder.getBeanDefinition().getPropertyValues().setConverted();
126         }
127     }
128     
129     static class BeanReplacement {
130         
131         private BeanDefinition beanDefinition;
132
133         public BeanReplacement(BeanDefinition beanDefinition) {
134             this.beanDefinition = beanDefinition;
135         }
136
137         public BeanDefinition getBeanDefinition() {
138             return this.beanDefinition;
139         }
140         
141     }
142     
143 }
144
Popular Tags