1
2
3
4
5
6
7 package org.mule.config.spring.parsers.specific;
8
9 import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
10 import org.mule.config.spring.parsers.MuleDefinitionParser;
11 import org.mule.config.spring.parsers.PreProcessor;
12 import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
13 import org.mule.config.spring.parsers.delegate.AbstractParallelDelegatingDefinitionParser;
14 import org.mule.config.spring.util.SpringXMLUtils;
15 import org.mule.util.StringUtils;
16
17 import org.springframework.beans.factory.xml.ParserContext;
18 import org.w3c.dom.Attr;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.NamedNodeMap;
21 import org.w3c.dom.Node;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class ComponentDelegatingDefinitionParser extends AbstractParallelDelegatingDefinitionParser
42 {
43
44 private MuleDefinitionParser normalConfig;
45 private MuleDefinitionParser shortcutConfig;
46
47 public ComponentDelegatingDefinitionParser(Class clazz)
48 {
49 normalConfig = new ComponentDefinitionParser(clazz);
50 shortcutConfig = new ShortcutComponentDefinitionParser(clazz);
51 addDelegate(normalConfig);
52 addDelegate(shortcutConfig);
53 registerPreProcessor(new CheckExclusiveClassAttributeObjectFactory());
54 }
55
56 @Override
57 protected MuleDefinitionParser getDelegate(Element element, ParserContext parserContext)
58 {
59 if (StringUtils.isEmpty(element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS)))
60 {
61 return normalConfig;
62 }
63 else
64 {
65 return shortcutConfig;
66 }
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80 class CheckExclusiveClassAttributeObjectFactory implements PreProcessor
81 {
82
83 private static final String OBJECT_FACTORY_ELEMENT_CONVENTION_SUFFIX = "object";
84
85 public void preProcess(PropertyConfiguration config, Element element)
86 {
87 NamedNodeMap attributes = element.getAttributes();
88 for (int i = 0; i < attributes.getLength(); i++)
89 {
90 String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
91 if (alias.equals(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS))
92 {
93 for (int j = 0; j < element.getChildNodes().getLength(); j++)
94 {
95 Node child = element.getChildNodes().item(j);
96 if (child instanceof Element
97 && child.getLocalName().endsWith(OBJECT_FACTORY_ELEMENT_CONVENTION_SUFFIX))
98 {
99 StringBuffer message = new StringBuffer("The child element '");
100 message.append(child.getLocalName());
101 message.append("' cannot appear with the 'class' attribute");
102 message.append(" in element ");
103 message.append(SpringXMLUtils.elementToString(element));
104 message.append(".");
105 throw new CheckExclusiveClassAttributeObjectFactoryException(message.toString());
106 }
107 }
108 }
109 }
110 }
111 }
112
113 class CheckExclusiveClassAttributeObjectFactoryException extends IllegalStateException
114 {
115 private static final long serialVersionUID = 4625276914151932111L;
116
117 CheckExclusiveClassAttributeObjectFactoryException(String message)
118 {
119 super(message);
120 }
121 }
122
123 }