1 16 17 package org.springframework.beans.factory.xml; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.w3c.dom.Attr ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 25 26 import org.springframework.beans.factory.config.BeanDefinition; 27 import org.springframework.beans.factory.config.BeanDefinitionHolder; 28 29 43 public abstract class NamespaceHandlerSupport implements NamespaceHandler { 44 45 49 private final Map parsers = new HashMap (); 50 51 55 private final Map decorators = new HashMap (); 56 57 61 private final Map attributeDecorators = new HashMap (); 62 63 64 68 public final BeanDefinition parse(Element element, ParserContext parserContext) { 69 return findParserForElement(element, parserContext).parse(element, parserContext); 70 } 71 72 76 private BeanDefinitionParser findParserForElement(Element element, ParserContext parserContext) { 77 BeanDefinitionParser parser = (BeanDefinitionParser) this.parsers.get(element.getLocalName()); 78 if (parser == null) { 79 parserContext.getReaderContext().fatal( 80 "Cannot locate BeanDefinitionParser for element [" + element.getLocalName() + "]", element); 81 } 82 return parser; 83 } 84 85 90 protected final BeanDefinitionParser findParserForElement(Element element) { 91 BeanDefinitionParser parser = (BeanDefinitionParser) this.parsers.get(element.getLocalName()); 92 if (parser == null) { 93 throw new IllegalStateException ( 94 "Cannot locate BeanDefinitionParser for element [" + element.getLocalName() + "]"); 95 } 96 return parser; 97 } 98 99 103 public final BeanDefinitionHolder decorate( 104 Node node, BeanDefinitionHolder definition, ParserContext parserContext) { 105 106 return findDecoratorForNode(node, parserContext).decorate(node, definition, parserContext); 107 } 108 109 114 private BeanDefinitionDecorator findDecoratorForNode(Node node, ParserContext parserContext) { 115 BeanDefinitionDecorator decorator = null; 116 if (node instanceof Element ) { 117 decorator = (BeanDefinitionDecorator) this.decorators.get(node.getLocalName()); 118 } 119 else if (node instanceof Attr ) { 120 decorator = (BeanDefinitionDecorator) this.attributeDecorators.get(node.getLocalName()); 121 } 122 else { 123 parserContext.getReaderContext().fatal( 124 "Cannot decorate based on Nodes of type [" + node.getClass().getName() + "]", node); 125 } 126 if (decorator == null) { 127 parserContext.getReaderContext().fatal("Cannot locate BeanDefinitionDecorator for " + 128 (node instanceof Element ? "element" : "attribute") + " [" + node.getLocalName() + "]", node); 129 } 130 return decorator; 131 } 132 133 139 protected final BeanDefinitionDecorator findDecoratorForNode(Node node) { 140 BeanDefinitionDecorator decorator = null; 141 if (node instanceof Element ) { 142 decorator = (BeanDefinitionDecorator) this.decorators.get(node.getLocalName()); 143 } 144 else if (node instanceof Attr ) { 145 decorator = (BeanDefinitionDecorator) this.attributeDecorators.get(node.getLocalName()); 146 } 147 else { 148 throw new IllegalStateException ( 149 "Cannot decorate based on Nodes of type [" + node.getClass().getName() + "]"); 150 } 151 if (decorator == null) { 152 throw new IllegalStateException ("Cannot locate BeanDefinitionDecorator for " + 153 (node instanceof Element ? "element" : "attribute") + " [" + node.getLocalName() + "]"); 154 } 155 return decorator; 156 } 157 158 159 164 protected final void registerBeanDefinitionParser(String elementName, BeanDefinitionParser parser) { 165 this.parsers.put(elementName, parser); 166 } 167 168 173 protected final void registerBeanDefinitionDecorator(String elementName, BeanDefinitionDecorator decorator) { 174 this.decorators.put(elementName, decorator); 175 } 176 177 182 protected final void registerBeanDefinitionDecoratorForAttribute( 183 String attributeName, BeanDefinitionDecorator decorator) { 184 185 this.attributeDecorators.put(attributeName, decorator); 186 } 187 188 } 189 | Popular Tags |