KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > config > spring > ElementProcessorSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24
25 import javax.xml.transform.TransformerException JavaDoc;
26
27 /**
28  * A useful base class for transforming the configuration elements into regular
29  * Spring XML elements
30  *
31  * @version $Revision: 426415 $
32  */

33 public class ElementProcessorSupport {
34     public static final String JavaDoc NAMESPACE = "";
35
36     /**
37      * Recursively process all the child elements with the given processor
38      */

39     protected void processChildren(ElementProcessor processor, Element JavaDoc element, BeanDefinitionReader beanDefinitionReader) {
40         Node JavaDoc current = element.getFirstChild();
41         while (current != null) {
42             Node JavaDoc node = current;
43             current = current.getNextSibling();
44             if (node instanceof Element JavaDoc) {
45                 Element JavaDoc child = (Element JavaDoc) node;
46                 processor.processElement(child, beanDefinitionReader);
47                 processChildren(processor, child, beanDefinitionReader);
48             }
49         }
50     }
51
52     // Helper methods to add new Spring XML elements
53

54     /**
55      * Adds a new Spring element of the given name to the owner
56      */

57     protected Element JavaDoc addElement(Node JavaDoc owner, String JavaDoc name) {
58         Element JavaDoc property = owner.getOwnerDocument().createElementNS(NAMESPACE,
59                 name);
60         owner.appendChild(property);
61         return property;
62     }
63
64     /**
65      * Creates and adds a new bean node on the given owner element
66      */

67     protected Element JavaDoc addBeanElement(Node JavaDoc owner, String JavaDoc className) {
68         Element JavaDoc bean = addElement(owner, "bean");
69         bean.setAttribute("class", className);
70         return bean;
71     }
72
73     /**
74      * Creates and adds a new property node on the given bean element
75      */

76     protected Element JavaDoc addPropertyElement(Node JavaDoc bean, String JavaDoc propertyName) {
77         Element JavaDoc property = addElement(bean, "property");
78         property.setAttribute("name", convertToCamelCase(propertyName));
79         return property;
80     }
81
82     /**
83      * Adds a new property element to the given bean element with the name and
84      * value
85      */

86     protected Element JavaDoc addPropertyElement(Node JavaDoc bean, String JavaDoc propertyName,
87             String JavaDoc value) {
88         Element JavaDoc property = addPropertyElement(bean, propertyName);
89         if (value != null)
90             property.setAttribute("value", value);
91         return property;
92     }
93
94     /**
95      * Adds a new constructor argument element to the given bean
96      */

97     protected void addConstructorValueNode(Node JavaDoc bean, String JavaDoc value) {
98         Element JavaDoc constructorArg = addElement(bean, "constructor-arg");
99         constructorArg.setAttribute("value", value);
100         bean.appendChild(constructorArg);
101     }
102
103     // DOM utilities
104

105     protected void logXmlGenerated(Log log, String JavaDoc message, Node JavaDoc node) {
106         if (log.isDebugEnabled()) {
107             try {
108                 String JavaDoc xml = DOMUtil.asXML(node);
109                 log.debug(message + ": " + xml);
110             } catch (TransformerException JavaDoc e) {
111                 log.warn("Could not transform generated XML into text: " + e, e);
112             }
113         }
114     }
115
116     protected String JavaDoc getElementNameToPropertyName(Element JavaDoc element) {
117         String JavaDoc name = element.getNodeName();
118         // lets turn dashes into camel case
119
return convertToCamelCase(name);
120     }
121
122     protected String JavaDoc convertToCamelCase(String JavaDoc name) {
123         while (true) {
124             int idx = name.indexOf('-');
125             if (idx >= 0) {
126                 String JavaDoc prefix = name.substring(0, idx);
127                 String JavaDoc cap = name.substring(idx + 1, idx + 2);
128                 String JavaDoc 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