1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.spring.parsers.processors; |
8 | |
|
9 | |
import org.mule.config.spring.parsers.PreProcessor; |
10 | |
import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration; |
11 | |
import org.mule.config.spring.util.SpringXMLUtils; |
12 | |
|
13 | |
import java.util.Iterator; |
14 | |
import java.util.LinkedList; |
15 | |
import java.util.List; |
16 | |
|
17 | |
import org.w3c.dom.Attr; |
18 | |
import org.w3c.dom.Element; |
19 | |
import org.w3c.dom.NamedNodeMap; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class CheckExclusiveAttribute implements PreProcessor |
25 | |
{ |
26 | |
|
27 | |
public static final int NONE = -1; |
28 | |
private String attribute; |
29 | |
|
30 | |
public CheckExclusiveAttribute(String attribute) |
31 | 0 | { |
32 | 0 | this.attribute = attribute; |
33 | 0 | } |
34 | |
|
35 | |
public void preProcess(PropertyConfiguration config, Element element) |
36 | |
{ |
37 | 0 | List foundAttributes = new LinkedList(); |
38 | 0 | boolean found = false; |
39 | |
|
40 | 0 | NamedNodeMap attributes = element.getAttributes(); |
41 | 0 | for (int i = 0; i < attributes.getLength(); i++) |
42 | |
{ |
43 | 0 | String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i)); |
44 | 0 | if (! config.isIgnored(alias)) |
45 | |
{ |
46 | 0 | if (attribute.equals(alias)) |
47 | |
{ |
48 | 0 | found = true; |
49 | |
} |
50 | |
else |
51 | |
{ |
52 | 0 | foundAttributes.add(alias); |
53 | |
} |
54 | |
} |
55 | |
} |
56 | |
|
57 | 0 | if (found && foundAttributes.size() > 0) |
58 | |
{ |
59 | 0 | StringBuffer message = new StringBuffer("The attribute '"); |
60 | 0 | message.append(attribute); |
61 | 0 | message.append("' cannot appear with the attribute"); |
62 | 0 | if (foundAttributes.size() > 1) |
63 | |
{ |
64 | 0 | message.append("s"); |
65 | |
} |
66 | 0 | Iterator others = foundAttributes.iterator(); |
67 | 0 | while (others.hasNext()) |
68 | |
{ |
69 | 0 | message.append(" '"); |
70 | 0 | message.append(others.next()); |
71 | 0 | message.append("'"); |
72 | |
} |
73 | 0 | message.append(" in element "); |
74 | 0 | message.append(SpringXMLUtils.elementToString(element)); |
75 | 0 | message.append("."); |
76 | 0 | throw new CheckExclusiveAttributeException(message.toString()); |
77 | |
} |
78 | 0 | } |
79 | |
|
80 | 0 | public static class CheckExclusiveAttributeException extends IllegalStateException |
81 | |
{ |
82 | |
|
83 | |
private CheckExclusiveAttributeException(String message) |
84 | |
{ |
85 | 0 | super(message); |
86 | 0 | } |
87 | |
|
88 | |
} |
89 | |
|
90 | |
} |