Coverage Report - org.mule.config.spring.parsers.processors.CheckExclusiveAttributes
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckExclusiveAttributes
0%
0/34
0%
0/24
0
CheckExclusiveAttributes$AttributeSet
0%
0/9
0%
0/4
0
CheckExclusiveAttributes$CheckExclusiveAttributesException
0%
0/19
0%
0/4
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 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.ArrayList;
 14  
 import java.util.Arrays;
 15  
 import java.util.Collection;
 16  
 import java.util.List;
 17  
 
 18  
 import org.w3c.dom.Attr;
 19  
 import org.w3c.dom.Element;
 20  
 import org.w3c.dom.NamedNodeMap;
 21  
 
 22  
 /**
 23  
  * Attributes from two different sets cannot appear together
 24  
  */
 25  
 public class CheckExclusiveAttributes implements PreProcessor
 26  
 {
 27  
     private Collection<AttributeSet> attributeSets;
 28  
 
 29  
     public CheckExclusiveAttributes(String[][] attributeNames)
 30  
     {
 31  0
         super();
 32  
         
 33  0
         attributeSets = new ArrayList<AttributeSet>();
 34  0
         for (int i = 0; i < attributeNames.length; i++)
 35  
         {
 36  0
             String[] attributes = attributeNames[i];
 37  0
             attributeSets.add(new AttributeSet(attributes));
 38  
         }            
 39  0
     }
 40  
 
 41  
     public void preProcess(PropertyConfiguration config, Element element)
 42  
     {
 43  0
         Collection<AttributeSet> allMatchingSets = new ArrayList<AttributeSet>(attributeSets);
 44  0
         boolean atLeastOneAttributeDidMatch = false;
 45  
 
 46  
         // itereate over all attribute names in 'element'
 47  0
         NamedNodeMap attributes = element.getAttributes();
 48  0
         int length = attributes.getLength();
 49  0
         for (int i = 0; i < length; i++)
 50  
         {
 51  0
             String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
 52  0
             if (isOptionalAttribute(alias))
 53  
             {
 54  0
                 continue;
 55  
             }
 56  
 
 57  0
             allMatchingSets = filterMatchingSets(allMatchingSets, alias);
 58  0
             atLeastOneAttributeDidMatch = true;
 59  
         }
 60  
         
 61  0
         if (atLeastOneAttributeDidMatch && allMatchingSets.size() == 0)
 62  
         {
 63  0
             CheckExclusiveAttributesException ex = 
 64  
                 CheckExclusiveAttributesException.createForDisjunctGroups(element, attributeSets);
 65  0
           throw ex;
 66  
 
 67  
         }
 68  0
         else if (atLeastOneAttributeDidMatch && allMatchingSets.size() > 1)
 69  
         {
 70  0
             CheckExclusiveAttributesException ex = 
 71  
                 CheckExclusiveAttributesException.createForInsufficientAttributes(element, allMatchingSets);
 72  0
             throw ex;
 73  
         }
 74  0
     }
 75  
 
 76  
     private Collection<AttributeSet> filterMatchingSets(Collection<AttributeSet> allMatchingSets, 
 77  
         String attributeName)
 78  
     {
 79  0
         Collection<AttributeSet> newMatchingSets = new ArrayList<AttributeSet>();
 80  0
         for (AttributeSet currentSet : allMatchingSets)
 81  
         {
 82  0
             if (currentSet.containsAttribute(attributeName))
 83  
             {
 84  0
                 newMatchingSets.add(currentSet);
 85  
             }
 86  
         }
 87  0
         return newMatchingSets;
 88  
     }
 89  
 
 90  
     private boolean isOptionalAttribute(String name)
 91  
     {
 92  0
         return findMatchingAttributeSets(name).size() == 0;
 93  
     }
 94  
 
 95  
     private Collection<AttributeSet> findMatchingAttributeSets(String alias)
 96  
     {
 97  0
         List<AttributeSet> matchingSets = new ArrayList<AttributeSet>();
 98  0
         for (AttributeSet currentSet : attributeSets)
 99  
         {
 100  0
             if (currentSet.containsAttribute(alias))
 101  
             {
 102  0
                 matchingSets.add(currentSet);
 103  
             }
 104  
         }
 105  0
         return matchingSets;
 106  
     }
 107  
 
 108  
     private static class AttributeSet
 109  
     {
 110  
         private String[] attributeNames;
 111  
 
 112  
         public AttributeSet(String[] attributeNames)
 113  
         {
 114  0
             super();
 115  0
             this.attributeNames = attributeNames;
 116  0
         }
 117  
 
 118  
         public boolean containsAttribute(String name)
 119  
         {
 120  0
             for (int i = 0; i < attributeNames.length; i++)
 121  
             {
 122  0
                 String element = attributeNames[i];
 123  0
                 if (element.equals(name))
 124  
                 {
 125  0
                     return true;
 126  
                 }
 127  
             }
 128  0
             return false;
 129  
         }
 130  
 
 131  
         @Override
 132  
         public String toString()
 133  
         {
 134  0
             return Arrays.toString(attributeNames);
 135  
         }
 136  
     }
 137  
     
 138  
     public static class CheckExclusiveAttributesException extends IllegalStateException
 139  
     {
 140  
         public static CheckExclusiveAttributesException createForDisjunctGroups(Element element,
 141  
             Collection<AttributeSet> allMatchingSets)
 142  
         {
 143  0
             String message = createMessage(element, allMatchingSets);
 144  0
             return new CheckExclusiveAttributesException(message);
 145  
         }
 146  
 
 147  
         private static String createMessage(Element element, Collection<AttributeSet> allMatchingSets)
 148  
         {
 149  0
             StringBuilder buf = new StringBuilder("The attributes of Element ");
 150  0
             buf.append(SpringXMLUtils.elementToString(element));
 151  0
             buf.append(" do not match the exclusive groups");
 152  
             
 153  0
             for (AttributeSet match : allMatchingSets)
 154  
             {
 155  0
                 buf.append(" ");
 156  0
                 buf.append(match.toString());
 157  
             }
 158  
             
 159  0
             return buf.toString();
 160  
         }
 161  
         
 162  
         public static CheckExclusiveAttributesException createForInsufficientAttributes(Element element,
 163  
             Collection<AttributeSet> attributeSets)
 164  
         {
 165  0
             StringBuilder buf = new StringBuilder("Attributes of Element ");
 166  0
             buf.append(SpringXMLUtils.elementToString(element));
 167  0
             buf.append(" do not satisfy the exclusive groups");
 168  
             
 169  0
             for (AttributeSet attributeSet : attributeSets)
 170  
             {
 171  0
                 buf.append(" ");
 172  0
                 buf.append(attributeSet);
 173  
             }
 174  0
             buf.append(".");
 175  
             
 176  0
             return new CheckExclusiveAttributesException(buf.toString());
 177  
         }
 178  
         
 179  
         private CheckExclusiveAttributesException(String message)
 180  
         {
 181  0
             super(message);
 182  0
         }
 183  
     }
 184  
 }