1 17 package org.apache.servicemix.jbi.config.spring; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.servicemix.jbi.util.DOMUtil; 21 import org.springframework.beans.factory.support.BeanDefinitionReader; 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.Node ; 24 25 import javax.xml.transform.TransformerException ; 26 27 33 public class ElementProcessorSupport { 34 public static final String NAMESPACE = ""; 35 36 39 protected void processChildren(ElementProcessor processor, Element element, BeanDefinitionReader beanDefinitionReader) { 40 Node current = element.getFirstChild(); 41 while (current != null) { 42 Node node = current; 43 current = current.getNextSibling(); 44 if (node instanceof Element ) { 45 Element child = (Element ) node; 46 processor.processElement(child, beanDefinitionReader); 47 processChildren(processor, child, beanDefinitionReader); 48 } 49 } 50 } 51 52 54 57 protected Element addElement(Node owner, String name) { 58 Element property = owner.getOwnerDocument().createElementNS(NAMESPACE, 59 name); 60 owner.appendChild(property); 61 return property; 62 } 63 64 67 protected Element addBeanElement(Node owner, String className) { 68 Element bean = addElement(owner, "bean"); 69 bean.setAttribute("class", className); 70 return bean; 71 } 72 73 76 protected Element addPropertyElement(Node bean, String propertyName) { 77 Element property = addElement(bean, "property"); 78 property.setAttribute("name", convertToCamelCase(propertyName)); 79 return property; 80 } 81 82 86 protected Element addPropertyElement(Node bean, String propertyName, 87 String value) { 88 Element property = addPropertyElement(bean, propertyName); 89 if (value != null) 90 property.setAttribute("value", value); 91 return property; 92 } 93 94 97 protected void addConstructorValueNode(Node bean, String value) { 98 Element constructorArg = addElement(bean, "constructor-arg"); 99 constructorArg.setAttribute("value", value); 100 bean.appendChild(constructorArg); 101 } 102 103 105 protected void logXmlGenerated(Log log, String message, Node node) { 106 if (log.isDebugEnabled()) { 107 try { 108 String xml = DOMUtil.asXML(node); 109 log.debug(message + ": " + xml); 110 } catch (TransformerException e) { 111 log.warn("Could not transform generated XML into text: " + e, e); 112 } 113 } 114 } 115 116 protected String getElementNameToPropertyName(Element element) { 117 String name = element.getNodeName(); 118 return convertToCamelCase(name); 120 } 121 122 protected String convertToCamelCase(String name) { 123 while (true) { 124 int idx = name.indexOf('-'); 125 if (idx >= 0) { 126 String prefix = name.substring(0, idx); 127 String cap = name.substring(idx + 1, idx + 2); 128 String rest = name.substring(idx + 2); 129 name = prefix + cap.toUpperCase() + rest; 130 } else { 131 break; 132 } 133 } 134 return name; 135 } 136 } 137 | Popular Tags |