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 | 0 | { |
49 | 0 | normalConfig = new ComponentDefinitionParser(clazz); |
50 | 0 | shortcutConfig = new ShortcutComponentDefinitionParser(clazz); |
51 | 0 | addDelegate(normalConfig); |
52 | 0 | addDelegate(shortcutConfig); |
53 | 0 | registerPreProcessor(new CheckExclusiveClassAttributeObjectFactory()); |
54 | 0 | } |
55 | |
|
56 | |
@Override |
57 | |
protected MuleDefinitionParser getDelegate(Element element, ParserContext parserContext) |
58 | |
{ |
59 | 0 | if (StringUtils.isEmpty(element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS))) |
60 | |
{ |
61 | 0 | return normalConfig; |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | 0 | return shortcutConfig; |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 0 | 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 | 0 | NamedNodeMap attributes = element.getAttributes(); |
88 | 0 | for (int i = 0; i < attributes.getLength(); i++) |
89 | |
{ |
90 | 0 | String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i)); |
91 | 0 | if (alias.equals(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS)) |
92 | |
{ |
93 | 0 | for (int j = 0; j < element.getChildNodes().getLength(); j++) |
94 | |
{ |
95 | 0 | Node child = element.getChildNodes().item(j); |
96 | 0 | if (child instanceof Element |
97 | |
&& child.getLocalName().endsWith(OBJECT_FACTORY_ELEMENT_CONVENTION_SUFFIX)) |
98 | |
{ |
99 | 0 | StringBuffer message = new StringBuffer("The child element '"); |
100 | 0 | message.append(child.getLocalName()); |
101 | 0 | message.append("' cannot appear with the 'class' attribute"); |
102 | 0 | message.append(" in element "); |
103 | 0 | message.append(SpringXMLUtils.elementToString(element)); |
104 | 0 | message.append("."); |
105 | 0 | throw new CheckExclusiveClassAttributeObjectFactoryException(message.toString()); |
106 | |
} |
107 | |
} |
108 | |
} |
109 | |
} |
110 | 0 | } |
111 | |
} |
112 | |
|
113 | |
class CheckExclusiveClassAttributeObjectFactoryException extends IllegalStateException |
114 | |
{ |
115 | |
private static final long serialVersionUID = 4625276914151932111L; |
116 | |
|
117 | |
CheckExclusiveClassAttributeObjectFactoryException(String message) |
118 | 0 | { |
119 | 0 | super(message); |
120 | 0 | } |
121 | |
} |
122 | |
|
123 | |
} |