View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package org.springframework.osgi.config;
18  
19  import java.util.Locale;
20  
21  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22  import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
23  import org.springframework.beans.factory.xml.ParserContext;
24  import org.springframework.osgi.bundle.BundleFactoryBean;
25  import org.springframework.osgi.config.ParserUtils.AttributeCallback;
26  import org.w3c.dom.Attr;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.Node;
29  import org.w3c.dom.NodeList;
30  
31  /**
32   * BundleFactoryBean definition.
33   * 
34   * @author Andy Piper
35   * @author Costin Leau
36   */
37  class BundleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
38  
39  	static class BundleActionCallback implements AttributeCallback {
40  		public boolean process(Element parent, Attr attribute, BeanDefinitionBuilder builder) {
41  			String name = attribute.getLocalName();
42  			if (ACTION.equals(name)) {
43  				builder.addPropertyValue(ACTION_PROP, parseAction(parent, attribute));
44  				return false;
45  			}
46  
47  			if (DESTROY_ACTION.equals(name)) {
48  				builder.addPropertyValue(DESTROY_ACTION_PROP, parseAction(parent, attribute));
49  				return false;
50  			}
51  
52  			return true;
53  		}
54  
55  		// do upper case to make sure the constants match
56  		private Object parseAction(Element parent, Attr attribute) {
57  			return attribute.getValue().toUpperCase(Locale.ENGLISH);
58  		}
59  	};
60  
61  	private static final String ACTION = "action";
62  
63  	private static final String DESTROY_ACTION = "destroy-action";
64  
65  	// class properties
66  
67  	private static final String ACTION_PROP = "action";
68  
69  	private static final String DESTROY_ACTION_PROP = "destroyAction";
70  
71  	private static final String BUNDLE_PROP = "bundle";
72  
73  	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
74  		BundleActionCallback callback = new BundleActionCallback();
75  
76  		ParserUtils.parseCustomAttributes(element, builder, new AttributeCallback[] { callback });
77  
78  		// parse nested definition (in case there is any)
79  
80  		if (element.hasChildNodes()) {
81  			NodeList nodes = element.getChildNodes();
82  			boolean foundElement = false;
83  			for (int i = 0; i < nodes.getLength() && !foundElement; i++) {
84  				Node nd = nodes.item(i);
85  				if (nd instanceof Element) {
86  					foundElement = true;
87  					Object obj = parserContext.getDelegate().parsePropertySubElement((Element) nd,
88  						builder.getBeanDefinition());
89  					builder.addPropertyValue(BUNDLE_PROP, obj);
90  				}
91  			}
92  		}
93  	}
94  
95  	protected Class getBeanClass(Element element) {
96  		return BundleFactoryBean.class;
97  	}
98  
99  }