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