KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.NamedNodeMap JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27
28 /**
29  * Maps an element to a top level <bean> element.
30  *
31  * @version $Revision: 426415 $
32  */

33 public class BeanElementProcessor extends ElementProcessorSupport implements ElementProcessor {
34
35     private static final transient Log log = LogFactory.getLog(BeanElementProcessor.class);
36
37     private String JavaDoc className;
38
39     private String JavaDoc textProperty;
40
41     private ElementProcessor childProcessor;
42
43     public BeanElementProcessor(String JavaDoc className) {
44         this.className = className;
45     }
46
47     public BeanElementProcessor(String JavaDoc className,
48             ElementProcessor childProcessor) {
49         this.className = className;
50         this.childProcessor = childProcessor;
51     }
52
53     public BeanElementProcessor(String JavaDoc className, String JavaDoc textProperty) {
54         this.className = className;
55         this.textProperty = textProperty;
56     }
57
58     public BeanElementProcessor(String JavaDoc className, String JavaDoc textProperty,
59             ElementProcessor childProcessor) {
60         this.className = className;
61         this.textProperty = textProperty;
62         this.childProcessor = childProcessor;
63     }
64
65     public BeanElementProcessor(Class JavaDoc type) {
66         this(type.getName());
67     }
68
69     public void processElement(Element JavaDoc element, BeanDefinitionReader beanDefinitionReader) {
70         logXmlGenerated(log, "processing Element", element);
71         Element JavaDoc root = (Element JavaDoc) element.getParentNode();
72         String JavaDoc tmp = element.getAttribute("class");
73         if (tmp != null && tmp.length() > 0) {
74             element.removeAttribute("class");
75         } else {
76             tmp = className;
77         }
78         Element JavaDoc 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 JavaDoc bean) {
93         String JavaDoc text = DOMUtil.getElementText(bean).trim();
94         addPropertyElement(bean, textProperty, text);
95
96         // lets remove all the text nodes
97
Node JavaDoc node = bean.getFirstChild();
98         while (node != null) {
99             if (node.getNodeType() == Node.TEXT_NODE) {
100                 Node JavaDoc textNode = node;
101                 node = node.getNextSibling();
102                 bean.removeChild(textNode);
103             } else {
104                 node = node.getNextSibling();
105             }
106         }
107     }
108
109     protected Element JavaDoc createBeanElement(Element JavaDoc root, Element JavaDoc element,
110             String JavaDoc className) {
111         Element JavaDoc bean = addBeanElement(root, className);
112         return bean;
113     }
114
115     /**
116      * Provides a post processing hook
117      */

118     protected void processBean(Element JavaDoc bean, BeanDefinitionReader beanDefinitionReader) {
119         if (childProcessor != null) {
120             processChildren(childProcessor, bean, beanDefinitionReader);
121         }
122     }
123
124     protected void turnAttributesIntoProperties(Element JavaDoc from, Element JavaDoc to) {
125         NamedNodeMap JavaDoc attributes = from.getAttributes();
126         for (int i = 0, size = attributes.getLength(); i < size; i++) {
127             Attr JavaDoc node = (Attr JavaDoc) attributes.item(i);
128             // If the attribute is prefixed xmlns: then we we want to ignore
129
// it
130
if (node.getNodeName().startsWith("xmlns:")) {
131                 to.setAttribute(node.getNodeName(), node.getNodeValue());
132             } else {
133                 // If the attribute is service-name or interface-name then we
134
// need
135
// to turn the property into a QName
136
if (node.getNodeName().equals("service-name")
137                         || node.getNodeName().equals("interface-name")) {
138                     Element JavaDoc 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