KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > setup > config > PageNamespaceHandler


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) 2006
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.pages.setup.config;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import org.riotfamily.common.beans.xml.GenericNamespaceHandlerSupport;
31 import org.riotfamily.common.beans.xml.NestedListDecorator;
32 import org.riotfamily.pages.setup.PageDefinition;
33 import org.riotfamily.pages.setup.PageSetupBean;
34 import org.springframework.beans.MutablePropertyValues;
35 import org.springframework.beans.PropertyValue;
36 import org.springframework.beans.factory.config.BeanDefinition;
37 import org.springframework.beans.factory.config.BeanDefinitionHolder;
38 import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
39 import org.springframework.beans.factory.xml.ParserContext;
40 import org.springframework.util.StringUtils;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43
44 /**
45  * NamespaceHandler that handles the <code>page</code> namspace as
46  * defined in <code>page.xsd</code> which can be found in the same package.
47  */

48 public class PageNamespaceHandler extends GenericNamespaceHandlerSupport {
49
50     public void init() {
51         register("setup", PageSetupBean.class)
52                 .addTranslation("tx", "transactionManager")
53                 .addTranslation("dao", "pageDao")
54                 .addReference("dao")
55                 .addReference("locales")
56                 .addReference("tx");
57
58         register("page", PageDefinition.class,
59                 new NestedListDecorator("definitions"))
60                 .addTranslation("system", "systemNode");
61
62         registerBeanDefinitionDecorator("props", new PropsDecorator());
63     }
64
65     private static class PropsDecorator implements BeanDefinitionDecorator {
66
67         private static final String JavaDoc LOCALE_ATTRIBUTE = "locale";
68         private static final String JavaDoc LOCALIZED_PROPS_NAME = "localizedProps";
69         private static final String JavaDoc GLOBAL_PROPS_NAME = "globalProps";
70
71         public BeanDefinitionHolder decorate(Node JavaDoc node,
72                 BeanDefinitionHolder definition, ParserContext parserContext) {
73
74             BeanDefinition bd = definition.getBeanDefinition();
75             MutablePropertyValues pvs = bd.getPropertyValues();
76
77             Element JavaDoc element = (Element JavaDoc) node;
78             Properties JavaDoc props = parserContext.getDelegate().parsePropsElement(element);
79
80             String JavaDoc locale = element.getAttribute(LOCALE_ATTRIBUTE);
81             if(StringUtils.hasText(locale)) {
82                 Map JavaDoc map = null;
83                 PropertyValue pv = pvs.getPropertyValue(LOCALIZED_PROPS_NAME);
84                 if (pv != null) {
85                     map = (Map JavaDoc) pv.getValue();
86                 }
87                 if (map == null) {
88                     map = new HashMap JavaDoc();
89                     pvs.addPropertyValue(LOCALIZED_PROPS_NAME, map);
90                 }
91                 map.put(locale, props);
92             }
93             else {
94                 pvs.addPropertyValue(GLOBAL_PROPS_NAME, props);
95             }
96
97             return definition;
98         }
99     }
100 }
101
Popular Tags