Coverage Report - org.mule.config.spring.parsers.processors.CheckExclusiveAttributesAndChildren
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckExclusiveAttributesAndChildren
0%
0/39
0%
0/22
0
CheckExclusiveAttributesAndChildren$CheckExclusiveAttributesAndChildrenException
0%
0/2
N/A
0
CheckExclusiveAttributesAndChildren$ChildType
0%
0/5
N/A
0
 
 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  0
     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  0
         {
 44  0
             this.ns = ns;
 45  0
             this.name = name;
 46  0
         }
 47  
 
 48  
         @Override
 49  
         public String toString()
 50  
         {
 51  0
             return "{" + ns + "}" + name;
 52  
         }
 53  
 
 54  
     }
 55  
 
 56  
     public CheckExclusiveAttributesAndChildren(String[] attributeNames, String[] childrenNamesOrTypes)
 57  0
     {
 58  0
         this.attributeNames = new HashSet<String>(Arrays.asList(attributeNames));
 59  0
         this.childrenNames = new HashSet<String>();
 60  0
         this.childrenTypes = new HashSet<ChildType>();
 61  0
         parseChildrenNamesOrTypes(childrenNamesOrTypes);
 62  0
     }
 63  
 
 64  
     private void parseChildrenNamesOrTypes(String[] childrenNamesOrTypes)
 65  
     {
 66  0
         for (final String childrenNameOrType : childrenNamesOrTypes)
 67  
         {
 68  0
             final Matcher matcher = TYPE_REGEXP.matcher(childrenNameOrType);
 69  0
             if (matcher.matches())
 70  
             {
 71  0
                 childrenTypes.add(new ChildType(matcher.group(1), matcher.group(2)));
 72  
             }
 73  
             else
 74  
             {
 75  0
                 childrenNames.add(childrenNameOrType);
 76  
             }
 77  
         }
 78  0
     }
 79  
 
 80  
     public void preProcess(PropertyConfiguration config, Element element)
 81  
     {
 82  0
         final NamedNodeMap attributes = element.getAttributes();
 83  0
         final int attributesCount = attributes.getLength();
 84  
 
 85  0
         for (int i = 0; i < attributesCount; i++)
 86  
         {
 87  0
             final String attributeName = SpringXMLUtils.attributeName((Attr) attributes.item(i));
 88  
 
 89  0
             if (attributeNames.contains(attributeName))
 90  
             {
 91  0
                 ensureNoForbiddenChildren(element, attributeName);
 92  
             }
 93  
         }
 94  0
     }
 95  
 
 96  
     private void ensureNoForbiddenChildren(Element element, final String attributeName)
 97  
     {
 98  0
         final NodeList childNodes = element.getChildNodes();
 99  0
         final int childNodesCount = childNodes.getLength();
 100  0
         for (int j = 0; j < childNodesCount; j++)
 101  
         {
 102  0
             checkChildNode(element, attributeName, childNodes.item(j));
 103  
         }
 104  0
     }
 105  
 
 106  
     private void checkChildNode(Element element, final String attributeName, final Node child)
 107  
     {
 108  0
         if (child.getNodeType() == Node.ELEMENT_NODE)
 109  
         {
 110  0
             checkChildElement(element, attributeName, (Element) child);
 111  
         }
 112  0
     }
 113  
 
 114  
     private void checkChildElement(Element element, final String attributeName, final Element child)
 115  
     {
 116  0
         checkAttributeNameMatch(element, attributeName, child);
 117  
 
 118  0
         for (final ChildType childrenType : childrenTypes)
 119  
         {
 120  0
             final TypeInfo typeInfo = (TypeInfo) child;
 121  
 
 122  0
             if (((childrenType.ns.equals(typeInfo.getTypeNamespace()) && childrenType.name.equals(typeInfo.getTypeName())))
 123  
                 || typeInfo.isDerivedFrom(childrenType.ns, childrenType.name, TypeInfo.DERIVATION_EXTENSION))
 124  
             {
 125  
 
 126  0
                 throw new CheckExclusiveAttributesAndChildrenException(
 127  
                     "Element " + SpringXMLUtils.elementToString(element) + " can't contain child of type "
 128  
                                     + childrenType + " because it defines attribute " + attributeName);
 129  
             }
 130  0
         }
 131  
 
 132  0
     }
 133  
 
 134  
     private void checkAttributeNameMatch(Element element, final String attributeName, final Element child)
 135  
     {
 136  0
         final String childElementName = child.getLocalName();
 137  
 
 138  0
         if (childrenNames.contains(childElementName))
 139  
         {
 140  0
             throw new CheckExclusiveAttributesAndChildrenException("Element "
 141  
                                                                    + SpringXMLUtils.elementToString(element)
 142  
                                                                    + " can't contain child "
 143  
                                                                    + childElementName
 144  
                                                                    + " because it defines attribute "
 145  
                                                                    + attributeName);
 146  
         }
 147  0
     }
 148  
 
 149  
     public static class CheckExclusiveAttributesAndChildrenException extends IllegalStateException
 150  
     {
 151  
         private static final long serialVersionUID = 8661524219979354246L;
 152  
 
 153  
         public CheckExclusiveAttributesAndChildrenException(String message)
 154  
         {
 155  0
             super(message);
 156  0
         }
 157  
     }
 158  
 }