001: package org.caramba.spring;
002:
003: import org.springframework.beans.BeansException;
004: import org.springframework.beans.MutablePropertyValues;
005: import org.springframework.beans.factory.config.BeanDefinition;
006: import org.springframework.beans.factory.config.BeanDefinitionHolder;
007: import org.springframework.beans.factory.support.RootBeanDefinition;
008: import org.springframework.beans.factory.support.BeanDefinitionRegistry;
009: import org.springframework.beans.factory.support.AbstractBeanDefinition;
010: import org.springframework.beans.factory.xml.BeanDefinitionParser;
011: import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
012: import org.springframework.beans.factory.xml.ParserContext;
013: import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
014: import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
015: import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
016:
017: import org.w3c.dom.Element;
018: import org.w3c.dom.NamedNodeMap;
019: import org.w3c.dom.Attr;
020: import org.w3c.dom.NodeList;
021: import org.w3c.dom.Node;
022: import org.caramba.DestroyMode;
023: import org.caramba.CarambaException;
024:
025: /**
026: * @author Pieter Degraeuwe
027: */
028: public class CarambaNamespaceHandler extends NamespaceHandlerSupport {
029: public static final String PAGE_ATTRIBUTE_RESOURCE = "resource";
030: public static final String PAGE_ATTRIBUTE_DESTROY_MODE = "destroy-mode";
031: public static final String PANEL_ATTRIBUTE_RESOURCE = "resource";
032:
033: public CarambaNamespaceHandler() {
034: // registerBeanDefinitionDecorator("nop", new NopInterceptorBeanDefinitionDecorator());
035: }
036:
037: public void init() {
038: registerBeanDefinitionDecorator("pageConfig",
039: new PageConfigBeanDefinitionDecorator());
040: registerBeanDefinitionDecorator("panelConfig",
041: new PanelConfigBeanDefinitionDecorator());
042: // registerBeanDefinitionParser("page", new PageBeanDefinitionParser());
043: // registerBeanDefinitionDecorator("page", new BeanDefinitionDecorator() {
044: // public BeanDefinitionHolder decorate(Element pElement, BeanDefinitionHolder pBeanDefinitionHolder, ParserContext pParserContext) {
045: // pBeanDefinitionHolder.getBeanDefinition().setAttribute(PAGE_ATTRIBUTE_RESOURCE, pElement.getAttribute(PAGE_ATTRIBUTE_RESOURCE));
046: // pBeanDefinitionHolder.getBeanDefinition().setAttribute(PAGE_ATTRIBUTE_DESTROY_MODE, DestroyMode.getByName(pElement.getAttribute(PAGE_ATTRIBUTE_DESTROY_MODE)));
047: // return pBeanDefinitionHolder;
048: // }
049: // });
050: // registerBeanDefinitionParser("panel", new PanelBeanDefinitionParser());
051:
052: // registerBeanDefinitionDecorator("set", new PropertyModifyingBeanDefinitionDecorator());
053: // registerBeanDefinitionDecorator("debug", new DebugBeanDefinitionDecorator());
054: }
055:
056: private static class PageConfigBeanDefinitionDecorator implements
057: BeanDefinitionDecorator {
058:
059: public BeanDefinitionHolder decorate(Element pElement,
060: BeanDefinitionHolder pBeanDefinitionHolder,
061: ParserContext pParserContext) {
062: BeanDefinition def = pBeanDefinitionHolder
063: .getBeanDefinition();
064: String beanName = pBeanDefinitionHolder.getBeanName();
065: if (pBeanDefinitionHolder.getBeanDefinition().isSingleton()) {
066: throw new CarambaException(
067: "The bean "
068: + beanName
069: + " cannot be used as a page, since it is a singleton. Set singleton=\"false\".");
070: }
071: String resource = pElement.getAttribute("resource");
072: def.setAttribute(PAGE_ATTRIBUTE_RESOURCE, resource);
073: def
074: .setAttribute(
075: PAGE_ATTRIBUTE_DESTROY_MODE,
076: DestroyMode
077: .getByName(pElement
078: .getAttribute(PAGE_ATTRIBUTE_DESTROY_MODE)));
079: return pBeanDefinitionHolder;
080: }
081: }
082:
083: private static class PanelConfigBeanDefinitionDecorator implements
084: BeanDefinitionDecorator {
085:
086: public BeanDefinitionHolder decorate(Element pElement,
087: BeanDefinitionHolder pBeanDefinitionHolder,
088: ParserContext pParserContext) {
089: BeanDefinition def = pBeanDefinitionHolder
090: .getBeanDefinition();
091: String beanName = pBeanDefinitionHolder.getBeanName();
092: if (pBeanDefinitionHolder.getBeanDefinition().isSingleton()) {
093: throw new CarambaException(
094: "The bean "
095: + beanName
096: + " cannot be used as a panel, since it is a singleton. Set singleton=\"false\".");
097: }
098: String resource = pElement.getAttribute("resource");
099: def.setAttribute(PANEL_ATTRIBUTE_RESOURCE, resource);
100: return pBeanDefinitionHolder;
101: }
102: }
103:
104: private static class PageBeanDefinitionParser implements
105: BeanDefinitionParser {
106: public BeanDefinition parse(Element pElement,
107: ParserContext pParserContext) {
108:
109: RootBeanDefinition definition = new RootBeanDefinition();
110: definition.setSingleton(false);
111: try {
112: String beanName = pElement.getAttribute("name");
113: String className = pElement.getAttribute("class");
114: definition.setBeanClass(Class.forName(className));
115: definition.setAttribute(PAGE_ATTRIBUTE_RESOURCE,
116: pElement.getAttribute(PAGE_ATTRIBUTE_RESOURCE));
117: definition
118: .setAttribute(
119: PAGE_ATTRIBUTE_DESTROY_MODE,
120: DestroyMode
121: .getByName(pElement
122: .getAttribute(PAGE_ATTRIBUTE_DESTROY_MODE)));
123:
124: // MutablePropertyValues mpvs = new MutablePropertyValues();
125: // NamedNodeMap attributes = pElement.getAttributes();
126: // for(int x = 0; x < attributes.getLength(); x++) {
127: // Attr attribute = (Attr) attributes.item(x);
128: // String name = attribute.getLocalName();
129: //
130: // if(ID_ATTRIBUTE.equals(name) || "name".equalsIgnoreCase(name) || "class".equalsIgnoreCase(name) || "destroy-mode".equalsIgnoreCase(name) || "resource".equalsIgnoreCase(name)) {
131: // continue;
132: // }
133: //
134: // mpvs.addPropertyValue(attribute.getLocalName(), attribute.getValue());
135: // }
136: SpringNamespaceHelper nsHelper = new SpringNamespaceHelper();
137: MutablePropertyValues mpvs = nsHelper
138: .parsePropertyElements(pElement, beanName);
139: definition.setPropertyValues(mpvs);
140:
141: pParserContext.getRegistry().registerBeanDefinition(
142: pElement.getAttribute("name"), definition);
143: } catch (BeansException e) {
144: e.printStackTrace();
145: } catch (ClassNotFoundException e) {
146: e.printStackTrace();
147: }
148: return definition;
149: }
150: }
151:
152: private static class PanelBeanDefinitionParser implements
153: BeanDefinitionParser {
154: public BeanDefinition parse(Element pElement,
155: ParserContext pParserContext) {
156: RootBeanDefinition definition = new RootBeanDefinition();
157: definition.setSingleton(false);
158: try {
159: String className = pElement.getAttribute("class");
160: definition.setBeanClass(Class.forName(className));
161: definition
162: .setAttribute(
163: PANEL_ATTRIBUTE_RESOURCE,
164: pElement
165: .getAttribute(PANEL_ATTRIBUTE_RESOURCE));
166: pParserContext.getRegistry().registerBeanDefinition(
167: pElement.getAttribute("name"), definition);
168: } catch (BeansException e) {
169: e.printStackTrace();
170: } catch (ClassNotFoundException e) {
171: e.printStackTrace();
172: }
173: return definition;
174: }
175: }
176:
177: }
|