1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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 }