1 24 package org.riotfamily.pages.setup.config; 25 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import java.util.Properties ; 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 ; 42 import org.w3c.dom.Node ; 43 44 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 LOCALE_ATTRIBUTE = "locale"; 68 private static final String LOCALIZED_PROPS_NAME = "localizedProps"; 69 private static final String GLOBAL_PROPS_NAME = "globalProps"; 70 71 public BeanDefinitionHolder decorate(Node node, 72 BeanDefinitionHolder definition, ParserContext parserContext) { 73 74 BeanDefinition bd = definition.getBeanDefinition(); 75 MutablePropertyValues pvs = bd.getPropertyValues(); 76 77 Element element = (Element ) node; 78 Properties props = parserContext.getDelegate().parsePropsElement(element); 79 80 String locale = element.getAttribute(LOCALE_ATTRIBUTE); 81 if(StringUtils.hasText(locale)) { 82 Map map = null; 83 PropertyValue pv = pvs.getPropertyValue(LOCALIZED_PROPS_NAME); 84 if (pv != null) { 85 map = (Map ) pv.getValue(); 86 } 87 if (map == null) { 88 map = new HashMap (); 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 |