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.Arrays;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.Set;
17
18 import org.w3c.dom.Attr;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.NamedNodeMap;
21
22
23
24
25
26
27 public class RequireAttribute implements PreProcessor
28 {
29
30 private Set required;
31
32 public RequireAttribute(String required)
33 {
34 this(new String[]{required});
35 }
36
37 public RequireAttribute(String[] required)
38 {
39 this.required = new HashSet(Arrays.asList(required));
40 }
41
42 public void preProcess(PropertyConfiguration config, Element element)
43 {
44 NamedNodeMap attributes = element.getAttributes();
45 Iterator names = required.iterator();
46 while (names.hasNext())
47 {
48 String name = (String) names.next();
49 boolean found = false;
50 for (int i = 0; i < attributes.getLength() && !found; i++)
51 {
52 String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
53
54
55
56 found = name.equals(alias);
57 }
58 if (!found)
59 {
60 throw new RequireAttributeException("Attribute " + name + " is required here.");
61 }
62 }
63 }
64
65 public static class RequireAttributeException extends IllegalStateException
66 {
67
68 public RequireAttributeException(String message)
69 {
70 super(message);
71 }
72
73 }
74
75 }