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