1 17 package org.apache.servicemix.jbi.config.spring; 18 19 import org.springframework.beans.factory.support.BeanDefinitionReader; 20 import org.w3c.dom.Element ; 21 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 30 public abstract class CompositeElementProcessor implements ElementProcessor { 31 private String namespaceURI; 32 private Map localNameToProcessor; 33 private ElementProcessor defaultProcessor; 34 35 public CompositeElementProcessor() { 36 this(""); 37 } 38 39 public CompositeElementProcessor(String namespaceURI) { 40 this(namespaceURI, new ElementToPropertyProcessor()); 41 } 42 43 protected CompositeElementProcessor(String namespaceURI, ElementProcessor defaultProcessor) { 44 this.namespaceURI = namespaceURI; 45 this.defaultProcessor = defaultProcessor; 46 51 localNameToProcessor = new HashMap (); 52 loadLocalNameToProcessorMap(); 53 } 54 55 public String getNamespaceURI() { 56 return namespaceURI; 57 } 58 59 public void processElement(Element element, BeanDefinitionReader beanDefinitionReader) { 60 String uri = element.getNamespaceURI(); 61 if (sameNamespace(uri, namespaceURI)) { 62 String localName = element.getLocalName(); 63 ElementProcessor processor = (ElementProcessor) localNameToProcessor.get(localName); 64 if (processor == null) { 65 processor = defaultProcessor; 66 } 67 if (processor != null) { 68 processor.processElement(element, beanDefinitionReader); 69 } 70 } 71 } 72 73 protected abstract void loadLocalNameToProcessorMap(); 74 75 protected boolean sameNamespace(String uri, String uri2) { 76 return uri == uri2 || (uri != null && uri.equals(uri2)) || uri == null && uri2.length() == 0; 77 } 78 79 protected void registerProcessor(String localName, ElementProcessor processor) { 80 localNameToProcessor.put(localName, processor); 81 } 82 83 protected ElementToPropertyProcessor registerPropertyAlias(String elementName, String propertyName) { 84 ElementToPropertyProcessor processor = new ElementToPropertyProcessor(propertyName); 85 registerProcessor(elementName, processor); 86 return processor; 87 } 88 89 protected ElementToValueProcessor registerValueAlias(String elementName) { 90 ElementToValueProcessor processor = new ElementToValueProcessor(); 91 registerProcessor(elementName, processor); 92 return processor; 93 } 94 95 protected BeanElementProcessor registerBeanProcessor(String elementName, Class beanType) { 96 return registerBeanProcessor(elementName, beanType, null); 97 } 98 99 protected BeanElementProcessor registerBeanProcessor(String elementName, Class beanType, String textPropertyName) { 100 BeanElementProcessor processor = new BeanElementProcessor(beanType.getName(), textPropertyName, this); 101 registerProcessor(elementName, processor); 102 return processor; 103 } 104 105 protected BeanPropertyElementProcessor registerBeanPropertyProcessor(String elementName, Class type, String propertyName) { 106 BeanPropertyElementProcessor processor = new BeanPropertyElementProcessor(type, propertyName, this); 107 registerProcessor(elementName, processor); 108 return processor; 109 } 110 111 protected BeanPropertyElementProcessor registerBeanPropertyProcessor(String elementName, Class type) { 112 return registerBeanPropertyProcessor(elementName, type, null); 113 } 114 115 116 } 117 | Popular Tags |