1 24 package org.riotfamily.website.template.config; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.riotfamily.common.beans.xml.GenericBeanDefinitionParser; 32 import org.riotfamily.common.xml.XmlUtils; 33 import org.riotfamily.website.template.PushUpTemplateController; 34 import org.springframework.beans.factory.config.BeanDefinition; 35 import org.springframework.beans.factory.config.BeanDefinitionHolder; 36 import org.springframework.beans.factory.support.BeanDefinitionBuilder; 37 import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; 38 import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate; 39 import org.springframework.beans.factory.xml.ParserContext; 40 import org.springframework.util.xml.DomUtils; 41 import org.w3c.dom.Element ; 42 43 public class TemplateDefinitionParser extends GenericBeanDefinitionParser { 44 45 private String prefix = "/inc"; 46 47 private String suffix = ".html"; 48 49 public TemplateDefinitionParser() { 50 super(PushUpTemplateController.class); 51 addReference("parent"); 52 setDecorate(false); 53 } 54 55 public void setPrefix(String prefix) { 56 this.prefix = prefix; 57 } 58 59 public void setSuffix(String suffix) { 60 this.suffix = suffix; 61 } 62 63 protected void postProcess(BeanDefinitionBuilder builder, 64 ParserContext parserContext, Element element) { 65 66 HashMap configuration = new HashMap (); 67 ArrayList pushUpSlots = null; 68 69 Iterator it = DomUtils.getChildElementsByTagName(element, "insert").iterator(); 70 while (it.hasNext()) { 71 Element ele = (Element ) it.next(); 72 String slot = XmlUtils.getAttribute(ele, "slot"); 73 Object url = XmlUtils.getAttribute(ele, "url"); 74 if (url == null) { 75 String templateId = XmlUtils.getAttribute(element, "id"); 76 List handlersElements = XmlUtils.getChildElements(ele); 77 if (handlersElements.size() == 1) { 78 url = getHandlerUrl(templateId, slot, 79 (Element ) handlersElements.get(0), 80 parserContext); 81 } 82 else if (handlersElements.size() > 1) { 83 url = getHandlerUrls(templateId, slot, 84 handlersElements, parserContext); 85 } 86 } 87 Integer pushUp = XmlUtils.getIntegerAttribute(ele, "push-up"); 88 if (pushUp != null) { 89 if (pushUpSlots == null) { 90 pushUpSlots = new ArrayList (); 91 builder.addPropertyValue("pushUpSlots", pushUpSlots); 92 } 93 pushUpSlots.add(slot); 94 } 95 configuration.put(slot, url); 96 } 97 98 it = DomUtils.getChildElementsByTagName(element, "remove").iterator(); 99 while (it.hasNext()) { 100 Element ele = (Element ) it.next(); 101 String slot = ele.getAttribute("slot"); 102 configuration.put(slot, null); 103 } 104 105 if (!configuration.isEmpty()) { 106 builder.addPropertyValue("configuration", configuration); 107 } 108 } 109 110 private List getHandlerUrls(String templateId, String slot, 111 List handlerElements, ParserContext parserContext) { 112 113 ArrayList urls = new ArrayList (); 114 int i = 0; 115 Iterator it = handlerElements.iterator(); 116 while (it.hasNext()) { 117 Element ele = (Element ) it.next(); 118 urls.add(getHandlerUrl(templateId, slot + i++, ele, parserContext)); 119 } 120 return urls; 121 } 122 123 private String getHandlerUrl(String templateId, String slot, 124 Element handlerElement, ParserContext parserContext) { 125 126 BeanDefinition bd; 127 BeanDefinitionParserDelegate delegate = parserContext.getDelegate(); 128 129 if (delegate.isDefaultNamespace(handlerElement.getNamespaceURI())) { 130 bd = delegate.parseBeanDefinitionElement(handlerElement, null, null); 131 } 132 else { 133 bd = delegate.parseCustomElement(handlerElement); 134 } 135 136 String beanName = delegate.getReaderContext() 137 .generateBeanName(bd).replace('#', '-'); 138 139 if (templateId == null) { 140 templateId = beanName; 141 } 142 String url = prefix + "/" + templateId + "/" + slot + suffix; 143 String [] aliases = new String [] {url}; 144 145 BeanDefinitionHolder bdHolder = new BeanDefinitionHolder(bd, beanName, aliases); 146 BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, parserContext.getRegistry()); 147 return url; 148 } 149 150 } 151 | Popular Tags |