KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > template > config > TemplateDefinitionParser


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.website.template.config;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
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 JavaDoc;
42
43 public class TemplateDefinitionParser extends GenericBeanDefinitionParser {
44
45     private String JavaDoc prefix = "/inc";
46     
47     private String JavaDoc suffix = ".html";
48     
49     public TemplateDefinitionParser() {
50         super(PushUpTemplateController.class);
51         addReference("parent");
52         setDecorate(false);
53     }
54
55     public void setPrefix(String JavaDoc prefix) {
56         this.prefix = prefix;
57     }
58
59     public void setSuffix(String JavaDoc suffix) {
60         this.suffix = suffix;
61     }
62
63     protected void postProcess(BeanDefinitionBuilder builder,
64             ParserContext parserContext, Element JavaDoc element) {
65
66         HashMap JavaDoc configuration = new HashMap JavaDoc();
67         ArrayList JavaDoc pushUpSlots = null;
68         
69         Iterator JavaDoc it = DomUtils.getChildElementsByTagName(element, "insert").iterator();
70         while (it.hasNext()) {
71             Element JavaDoc ele = (Element JavaDoc) it.next();
72             String JavaDoc slot = XmlUtils.getAttribute(ele, "slot");
73             Object JavaDoc url = XmlUtils.getAttribute(ele, "url");
74             if (url == null) {
75                 String JavaDoc templateId = XmlUtils.getAttribute(element, "id");
76                 List JavaDoc handlersElements = XmlUtils.getChildElements(ele);
77                 if (handlersElements.size() == 1) {
78                     url = getHandlerUrl(templateId, slot,
79                             (Element JavaDoc) handlersElements.get(0),
80                             parserContext);
81                 }
82                 else if (handlersElements.size() > 1) {
83                     url = getHandlerUrls(templateId, slot,
84                             handlersElements, parserContext);
85                 }
86             }
87             Integer JavaDoc pushUp = XmlUtils.getIntegerAttribute(ele, "push-up");
88             if (pushUp != null) {
89                 if (pushUpSlots == null) {
90                     pushUpSlots = new ArrayList JavaDoc();
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 JavaDoc ele = (Element JavaDoc) it.next();
101             String JavaDoc 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 JavaDoc getHandlerUrls(String JavaDoc templateId, String JavaDoc slot,
111             List JavaDoc handlerElements, ParserContext parserContext) {
112         
113         ArrayList JavaDoc urls = new ArrayList JavaDoc();
114         int i = 0;
115         Iterator JavaDoc it = handlerElements.iterator();
116         while (it.hasNext()) {
117             Element JavaDoc ele = (Element JavaDoc) it.next();
118             urls.add(getHandlerUrl(templateId, slot + i++, ele, parserContext));
119         }
120         return urls;
121     }
122     
123     private String JavaDoc getHandlerUrl(String JavaDoc templateId, String JavaDoc slot,
124             Element JavaDoc 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 JavaDoc beanName = delegate.getReaderContext()
137                 .generateBeanName(bd).replace('#', '-');
138         
139         if (templateId == null) {
140             templateId = beanName;
141         }
142         String JavaDoc url = prefix + "/" + templateId + "/" + slot + suffix;
143         String JavaDoc[] aliases = new String JavaDoc[] {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