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.HashMap; |
18 | |
import java.util.Iterator; |
19 | |
import java.util.Map; |
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 CheckRequiredAttributes implements PreProcessor |
29 | |
{ |
30 | |
|
31 | |
|
32 | 0 | private Map knownAttributes = new HashMap(); |
33 | |
|
34 | 0 | private Map numberOfAttributes = new HashMap(); |
35 | |
|
36 | |
private String summary; |
37 | |
|
38 | |
public CheckRequiredAttributes(String[][] attributeSets) |
39 | 0 | { |
40 | 0 | StringBuffer buffer = new StringBuffer(); |
41 | 0 | for (int set = 0; set < attributeSets.length; set++) |
42 | |
{ |
43 | 0 | String[] attributes = attributeSets[set]; |
44 | |
|
45 | 0 | if (attributes.length > 0) |
46 | |
{ |
47 | 0 | Integer index = new Integer(set); |
48 | 0 | numberOfAttributes.put(index, new Integer(attributes.length)); |
49 | 0 | if (set > 0) |
50 | |
{ |
51 | 0 | buffer.append("; "); |
52 | |
} |
53 | 0 | for (int attribute = 0; attribute < attributes.length; attribute++) |
54 | |
{ |
55 | 0 | knownAttributes.put(attributes[attribute], index); |
56 | 0 | if (attribute > 0) |
57 | |
{ |
58 | 0 | buffer.append(", "); |
59 | |
} |
60 | |
|
61 | |
|
62 | 0 | buffer.append(attributes[attribute]); |
63 | |
} |
64 | |
} |
65 | |
} |
66 | 0 | summary = buffer.toString(); |
67 | 0 | } |
68 | |
|
69 | |
public void preProcess(PropertyConfiguration config, Element element) |
70 | |
{ |
71 | |
|
72 | 0 | Map foundAttributesCount = new HashMap(); |
73 | |
|
74 | 0 | NamedNodeMap attributes = element.getAttributes(); |
75 | 0 | for (int i = 0; i < attributes.getLength(); i++) |
76 | |
{ |
77 | 0 | String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i)); |
78 | 0 | if (knownAttributes.containsKey(alias)) |
79 | |
{ |
80 | 0 | Integer index = (Integer) knownAttributes.get(alias); |
81 | 0 | if (!foundAttributesCount.containsKey(index)) |
82 | |
{ |
83 | 0 | foundAttributesCount.put(index, new Integer(0)); |
84 | |
} |
85 | 0 | foundAttributesCount.put(index, |
86 | |
new Integer(1 + ((Integer) foundAttributesCount.get(index)).intValue())); |
87 | |
|
88 | |
} |
89 | |
} |
90 | |
|
91 | |
|
92 | 0 | boolean ok = knownAttributes.size() == 0; |
93 | 0 | Iterator indices = foundAttributesCount.keySet().iterator(); |
94 | 0 | while (indices.hasNext() && !ok) |
95 | |
{ |
96 | 0 | Object index = indices.next(); |
97 | 0 | Object count = foundAttributesCount.get(index); |
98 | 0 | ok = numberOfAttributes.get(index).equals(count); |
99 | 0 | } |
100 | 0 | if (!ok) |
101 | |
{ |
102 | 0 | throw new CheckRequiredAttributesException(element, summary); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | 0 | public static class CheckRequiredAttributesException extends IllegalStateException |
107 | |
{ |
108 | |
|
109 | |
private CheckRequiredAttributesException(Element element, String summary) |
110 | |
{ |
111 | 0 | super("Element " + SpringXMLUtils.elementToString(element) + |
112 | |
" must have all attributes for one of the sets: " + summary + "."); |
113 | 0 | } |
114 | |
|
115 | |
} |
116 | |
|
117 | |
} |