KickJava   Java API By Example, From Geeks To Geeks.

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


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.springframework.beans.factory.support.BeanDefinitionReader;
20 import org.w3c.dom.Element JavaDoc;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * A useful base class for mapping a number of processors to names in a given namespace.
27  *
28  * @version $Revision: 426415 $
29  */

30 public abstract class CompositeElementProcessor implements ElementProcessor {
31     private String JavaDoc namespaceURI;
32     private Map JavaDoc localNameToProcessor;
33     private ElementProcessor defaultProcessor;
34
35     public CompositeElementProcessor() {
36         this("");
37     }
38
39     public CompositeElementProcessor(String JavaDoc namespaceURI) {
40         this(namespaceURI, new ElementToPropertyProcessor());
41     }
42
43     protected CompositeElementProcessor(String JavaDoc namespaceURI, ElementProcessor defaultProcessor) {
44         this.namespaceURI = namespaceURI;
45         this.defaultProcessor = defaultProcessor;
46         /*
47         if (defaultProcessor == null && namespaceURI != null && namespaceURI.length() > 0) {
48             defaultProcessor = new ElementToPropertyProcessor();
49         }
50         */

51         localNameToProcessor = new HashMap JavaDoc();
52         loadLocalNameToProcessorMap();
53     }
54
55     public String JavaDoc getNamespaceURI() {
56         return namespaceURI;
57     }
58
59     public void processElement(Element JavaDoc element, BeanDefinitionReader beanDefinitionReader) {
60         String JavaDoc uri = element.getNamespaceURI();
61         if (sameNamespace(uri, namespaceURI)) {
62             String JavaDoc localName = element.getLocalName();
63             ElementProcessor processor = (ElementProcessor) localNameToProcessor.get(localName);
64             if (processor == null) {
65                 processor = defaultProcessor;
66             }
67             if (processor != null) {
68                 processor.processElement(element, beanDefinitionReader);
69             }
70         }
71     }
72
73     protected abstract void loadLocalNameToProcessorMap();
74
75     protected boolean sameNamespace(String JavaDoc uri, String JavaDoc uri2) {
76         return uri == uri2 || (uri != null && uri.equals(uri2)) || uri == null && uri2.length() == 0;
77     }
78
79     protected void registerProcessor(String JavaDoc localName, ElementProcessor processor) {
80         localNameToProcessor.put(localName, processor);
81     }
82
83     protected ElementToPropertyProcessor registerPropertyAlias(String JavaDoc elementName, String JavaDoc propertyName) {
84         ElementToPropertyProcessor processor = new ElementToPropertyProcessor(propertyName);
85         registerProcessor(elementName, processor);
86         return processor;
87     }
88
89     protected ElementToValueProcessor registerValueAlias(String JavaDoc elementName) {
90         ElementToValueProcessor processor = new ElementToValueProcessor();
91         registerProcessor(elementName, processor);
92         return processor;
93     }
94
95     protected BeanElementProcessor registerBeanProcessor(String JavaDoc elementName, Class JavaDoc beanType) {
96         return registerBeanProcessor(elementName, beanType, null);
97     }
98
99     protected BeanElementProcessor registerBeanProcessor(String JavaDoc elementName, Class JavaDoc beanType, String JavaDoc textPropertyName) {
100         BeanElementProcessor processor = new BeanElementProcessor(beanType.getName(), textPropertyName, this);
101         registerProcessor(elementName, processor);
102         return processor;
103     }
104
105     protected BeanPropertyElementProcessor registerBeanPropertyProcessor(String JavaDoc elementName, Class JavaDoc type, String JavaDoc propertyName) {
106         BeanPropertyElementProcessor processor = new BeanPropertyElementProcessor(type, propertyName, this);
107         registerProcessor(elementName, processor);
108         return processor;
109     }
110
111     protected BeanPropertyElementProcessor registerBeanPropertyProcessor(String JavaDoc elementName, Class JavaDoc type) {
112         return registerBeanPropertyProcessor(elementName, type, null);
113     }
114
115
116 }
117
Popular Tags