1 17 package org.apache.servicemix.jbi.config.spring; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.jbi.util.DOMUtil; 22 import org.springframework.beans.factory.support.BeanDefinitionReader; 23 import org.w3c.dom.Attr ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.NamedNodeMap ; 26 import org.w3c.dom.Node ; 27 28 33 public class BeanElementProcessor extends ElementProcessorSupport implements ElementProcessor { 34 35 private static final transient Log log = LogFactory.getLog(BeanElementProcessor.class); 36 37 private String className; 38 39 private String textProperty; 40 41 private ElementProcessor childProcessor; 42 43 public BeanElementProcessor(String className) { 44 this.className = className; 45 } 46 47 public BeanElementProcessor(String className, 48 ElementProcessor childProcessor) { 49 this.className = className; 50 this.childProcessor = childProcessor; 51 } 52 53 public BeanElementProcessor(String className, String textProperty) { 54 this.className = className; 55 this.textProperty = textProperty; 56 } 57 58 public BeanElementProcessor(String className, String textProperty, 59 ElementProcessor childProcessor) { 60 this.className = className; 61 this.textProperty = textProperty; 62 this.childProcessor = childProcessor; 63 } 64 65 public BeanElementProcessor(Class type) { 66 this(type.getName()); 67 } 68 69 public void processElement(Element element, BeanDefinitionReader beanDefinitionReader) { 70 logXmlGenerated(log, "processing Element", element); 71 Element root = (Element ) element.getParentNode(); 72 String tmp = element.getAttribute("class"); 73 if (tmp != null && tmp.length() > 0) { 74 element.removeAttribute("class"); 75 } else { 76 tmp = className; 77 } 78 Element bean = createBeanElement(root, element, tmp); 79 80 turnAttributesIntoProperties(element, bean); 81 DOMUtil.moveContent(element, bean); 82 if (textProperty != null) { 83 turnTextIntoProperty(bean); 84 } 85 root.removeChild(element); 86 87 processBean(bean, beanDefinitionReader); 88 89 logXmlGenerated(log, "component generated", bean); 90 } 91 92 protected void turnTextIntoProperty(Element bean) { 93 String text = DOMUtil.getElementText(bean).trim(); 94 addPropertyElement(bean, textProperty, text); 95 96 Node node = bean.getFirstChild(); 98 while (node != null) { 99 if (node.getNodeType() == Node.TEXT_NODE) { 100 Node textNode = node; 101 node = node.getNextSibling(); 102 bean.removeChild(textNode); 103 } else { 104 node = node.getNextSibling(); 105 } 106 } 107 } 108 109 protected Element createBeanElement(Element root, Element element, 110 String className) { 111 Element bean = addBeanElement(root, className); 112 return bean; 113 } 114 115 118 protected void processBean(Element bean, BeanDefinitionReader beanDefinitionReader) { 119 if (childProcessor != null) { 120 processChildren(childProcessor, bean, beanDefinitionReader); 121 } 122 } 123 124 protected void turnAttributesIntoProperties(Element from, Element to) { 125 NamedNodeMap attributes = from.getAttributes(); 126 for (int i = 0, size = attributes.getLength(); i < size; i++) { 127 Attr node = (Attr ) attributes.item(i); 128 if (node.getNodeName().startsWith("xmlns:")) { 131 to.setAttribute(node.getNodeName(), node.getNodeValue()); 132 } else { 133 if (node.getNodeName().equals("service-name") 137 || node.getNodeName().equals("interface-name")) { 138 Element propertyElement = addPropertyElement(to, node 139 .getName(), null); 140 QNameElementProcessor qnameElementProcessor = new QNameElementProcessor(); 141 qnameElementProcessor.addQNameBeanElement(propertyElement, 142 DOMUtil.createQName(from, node.getNodeValue())); 143 } else 144 addPropertyElement(to, node.getName(), node.getValue()); 145 } 146 } 147 } 148 } 149 | Popular Tags |