View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring.parsers.processors;
8   
9   import java.util.Arrays;
10  import java.util.HashSet;
11  import java.util.Set;
12  import java.util.regex.Matcher;
13  import java.util.regex.Pattern;
14  
15  import org.mule.config.spring.parsers.PreProcessor;
16  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
17  import org.mule.config.spring.util.SpringXMLUtils;
18  import org.w3c.dom.Attr;
19  import org.w3c.dom.Element;
20  import org.w3c.dom.NamedNodeMap;
21  import org.w3c.dom.Node;
22  import org.w3c.dom.NodeList;
23  import org.w3c.dom.TypeInfo;
24  
25  /**
26   * Attributes and children elements cannot appear together. Child names are either
27   * node names or types.
28   */
29  public class CheckExclusiveAttributesAndChildren implements PreProcessor
30  {
31      private final static Pattern TYPE_REGEXP = Pattern.compile("\\{(.*)\\}(.*)");
32  
33      private final Set<String> attributeNames;
34      private final Set<String> childrenNames;
35      private final Set<ChildType> childrenTypes;
36  
37      private static class ChildType
38      {
39          String ns;
40          String name;
41  
42          public ChildType(String ns, String name)
43          {
44              this.ns = ns;
45              this.name = name;
46          }
47  
48          @Override
49          public String toString()
50          {
51              return "{" + ns + "}" + name;
52          }
53  
54      }
55  
56      public CheckExclusiveAttributesAndChildren(String[] attributeNames, String[] childrenNamesOrTypes)
57      {
58          this.attributeNames = new HashSet<String>(Arrays.asList(attributeNames));
59          this.childrenNames = new HashSet<String>();
60          this.childrenTypes = new HashSet<ChildType>();
61          parseChildrenNamesOrTypes(childrenNamesOrTypes);
62      }
63  
64      private void parseChildrenNamesOrTypes(String[] childrenNamesOrTypes)
65      {
66          for (final String childrenNameOrType : childrenNamesOrTypes)
67          {
68              final Matcher matcher = TYPE_REGEXP.matcher(childrenNameOrType);
69              if (matcher.matches())
70              {
71                  childrenTypes.add(new ChildType(matcher.group(1), matcher.group(2)));
72              }
73              else
74              {
75                  childrenNames.add(childrenNameOrType);
76              }
77          }
78      }
79  
80      public void preProcess(PropertyConfiguration config, Element element)
81      {
82          final NamedNodeMap attributes = element.getAttributes();
83          final int attributesCount = attributes.getLength();
84  
85          for (int i = 0; i < attributesCount; i++)
86          {
87              final String attributeName = SpringXMLUtils.attributeName((Attr) attributes.item(i));
88  
89              if (attributeNames.contains(attributeName))
90              {
91                  ensureNoForbiddenChildren(element, attributeName);
92              }
93          }
94      }
95  
96      private void ensureNoForbiddenChildren(Element element, final String attributeName)
97      {
98          final NodeList childNodes = element.getChildNodes();
99          final int childNodesCount = childNodes.getLength();
100         for (int j = 0; j < childNodesCount; j++)
101         {
102             checkChildNode(element, attributeName, childNodes.item(j));
103         }
104     }
105 
106     private void checkChildNode(Element element, final String attributeName, final Node child)
107     {
108         if (child.getNodeType() == Node.ELEMENT_NODE)
109         {
110             checkChildElement(element, attributeName, (Element) child);
111         }
112     }
113 
114     private void checkChildElement(Element element, final String attributeName, final Element child)
115     {
116         checkAttributeNameMatch(element, attributeName, child);
117 
118         for (final ChildType childrenType : childrenTypes)
119         {
120             final TypeInfo typeInfo = (TypeInfo) child;
121 
122             if (((childrenType.ns.equals(typeInfo.getTypeNamespace()) && childrenType.name.equals(typeInfo.getTypeName())))
123                 || typeInfo.isDerivedFrom(childrenType.ns, childrenType.name, TypeInfo.DERIVATION_EXTENSION))
124             {
125 
126                 throw new CheckExclusiveAttributesAndChildrenException(
127                     "Element " + SpringXMLUtils.elementToString(element) + " can't contain child of type "
128                                     + childrenType + " because it defines attribute " + attributeName);
129             }
130         }
131 
132     }
133 
134     private void checkAttributeNameMatch(Element element, final String attributeName, final Element child)
135     {
136         final String childElementName = child.getLocalName();
137 
138         if (childrenNames.contains(childElementName))
139         {
140             throw new CheckExclusiveAttributesAndChildrenException("Element "
141                                                                    + SpringXMLUtils.elementToString(element)
142                                                                    + " can't contain child "
143                                                                    + childElementName
144                                                                    + " because it defines attribute "
145                                                                    + attributeName);
146         }
147     }
148 
149     public static class CheckExclusiveAttributesAndChildrenException extends IllegalStateException
150     {
151         private static final long serialVersionUID = 8661524219979354246L;
152 
153         public CheckExclusiveAttributesAndChildrenException(String message)
154         {
155             super(message);
156         }
157     }
158 }