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