KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > deployment > impl > JbiElementProcessor


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.deployment.impl;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.jbi.config.spring.ElementProcessor;
22 import org.apache.servicemix.jbi.config.spring.ElementProcessorSupport;
23 import org.apache.servicemix.jbi.deployment.Descriptor;
24 import org.apache.servicemix.jbi.util.DOMUtil;
25 import org.springframework.beans.factory.support.BeanDefinitionReader;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30 /**
31  * @version $Revision: 429628 $
32  */

33 public class JbiElementProcessor extends ElementProcessorSupport implements ElementProcessor {
34     private static final transient Log log = LogFactory.getLog(JbiElementProcessor.class);
35
36     private static final JbiNamespaceProcessor compositeProcessor = new JbiNamespaceProcessor();
37
38
39     public void processElement(Element JavaDoc element, BeanDefinitionReader beanDefinitionReader) {
40         Document JavaDoc document = element.getOwnerDocument();
41         Element JavaDoc beans = document.createElement("beans");
42         document.removeChild(element);
43         document.appendChild(beans);
44
45         Element JavaDoc bean = addBeanElement(beans, Descriptor.class.getName());
46         DOMUtil.copyAttributes(element, beans);
47         DOMUtil.moveContent(element, bean);
48
49         // lets remove the default namespace just in case
50
beans.removeAttribute("xmlns");
51         
52         String JavaDoc version = beans.getAttribute("version");
53         if (version != null && version.length() != 0) {
54             addPropertyElement(bean, "version", version);
55             beans.removeAttribute("version");
56         }
57
58         String JavaDoc id = bean.getAttribute("id");
59         if (id == null || id.length() == 0) {
60             bean.setAttribute("id", "jbi");
61         }
62         bean.setAttribute("singleton", "true");
63                 
64         moveToProperyListElement(bean, "path-element", "pathElements");
65         moveToProperyListElement(bean, "service-unit", "serviceUnits");
66         moveToProperyListElement(bean, "connection", "connections");
67         moveToProperyListElement(bean, "provides", "provides");
68         moveToProperyListElement(bean, "consumes", "consumes");
69
70         processChildren(compositeProcessor, bean, beanDefinitionReader);
71         
72         logXmlGenerated(log, "Adding new beans", beans);
73     }
74
75     /**
76      * Recursively moves all elements of the given name into a <property><list> element so they can be added
77      * to a collection or array by Spring
78      *
79      * @param bean the root bean
80      * @param elementName the element name to move into a property list
81      * @param propertyName the name of the property to use.
82      */

83     protected void moveToProperyListElement(Element JavaDoc bean, String JavaDoc elementName, String JavaDoc propertyName) {
84         Element JavaDoc property = null;
85         Element JavaDoc to = null;
86         Node JavaDoc node = bean.getFirstChild();
87         while (node != null) {
88             Node JavaDoc current = node;
89             node = node.getNextSibling();
90             if (current.getNodeType() == Node.ELEMENT_NODE) {
91                 if (elementName.equals(current.getNodeName())) {
92                     bean.removeChild(current);
93                     if (to == null) {
94                         property = addPropertyElement(bean, propertyName);
95                         to = addElement(property, "list");
96
97                         // lets detach the to element for now
98
bean.removeChild(property);
99                     }
100                     to.appendChild(current);
101                 }
102                 else {
103                     moveToProperyListElement((Element JavaDoc) current, elementName, propertyName);
104                 }
105             }
106         }
107         if (property != null) {
108             bean.appendChild(property);
109         }
110     }
111 }
112
Popular Tags