Coverage Report - org.mule.config.spring.parsers.processors.CheckRequiredAttributes
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckRequiredAttributes
0%
0/27
0%
0/16
0
CheckRequiredAttributes$1
N/A
N/A
0
CheckRequiredAttributes$CheckRequiredAttributesException
0%
0/11
0%
0/6
0
 
 1  
 /*
 2  
  * $Id: CheckRequiredAttributes.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 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.ArrayList;
 18  
 import java.util.Arrays;
 19  
 import java.util.Collection;
 20  
 import java.util.HashSet;
 21  
 import java.util.List;
 22  
 import java.util.Set;
 23  
 
 24  
 import org.w3c.dom.Attr;
 25  
 import org.w3c.dom.Element;
 26  
 import org.w3c.dom.NamedNodeMap;
 27  
 
 28  
 /**
 29  
  * All attributes from at least one set must be provided
 30  
  */
 31  
 public class CheckRequiredAttributes implements PreProcessor
 32  
 {
 33  
     Collection<List<String>> attributeSets;
 34  
     
 35  
     public CheckRequiredAttributes(String[][] attributeNames)
 36  
     {
 37  0
         super();
 38  
 
 39  0
         attributeSets = new ArrayList<List<String>>();
 40  0
         for (int i = 0; i < attributeNames.length; i++)
 41  
         {
 42  0
             String[] currentSet = attributeNames[i];
 43  0
             if (currentSet.length > 0)
 44  
             {
 45  0
                 List<String> list = Arrays.asList(currentSet);
 46  0
                 attributeSets.add(list);
 47  
             }
 48  
         }
 49  0
     }
 50  
 
 51  
     public void preProcess(PropertyConfiguration config, Element element)
 52  
     {
 53  0
         Collection<List<String>> matchingSets = new ArrayList<List<String>>();
 54  
         
 55  0
         for (List<String> currentSet : attributeSets)
 56  
         {
 57  0
             if (containsAllRequiredAttributes(currentSet, element))
 58  
             {
 59  0
                 matchingSets.add(currentSet);
 60  
             }
 61  
         }
 62  
         
 63  0
         if (matchingSets.size() == 0)
 64  
         {
 65  0
             throw new CheckRequiredAttributesException(element, attributeSets);
 66  
         }
 67  0
     }
 68  
 
 69  
     private boolean containsAllRequiredAttributes(List<String> currentSet, Element element)
 70  
     {
 71  0
         Set<String> attributes = collectAttributes(element);
 72  0
         if (attributes.size() == 0)
 73  
         {
 74  0
             return false;
 75  
         }
 76  
         
 77  
         // Clone the set of attribute names and subtract all the element's attribute names from it.
 78  
         // If the remaining set is empty, all required attributes of this set were present.
 79  0
         Set<String> remainingElementNames = new HashSet<String>(currentSet);
 80  0
         remainingElementNames.removeAll(attributes);
 81  0
         return (remainingElementNames.size() == 0);
 82  
     }
 83  
     
 84  
     private Set<String> collectAttributes(Element element)
 85  
     {
 86  0
         Set<String> attributeNames = new HashSet<String>();
 87  
         
 88  0
         NamedNodeMap attributes = element.getAttributes();
 89  0
         for (int i = 0; i < attributes.getLength(); i++)
 90  
         {
 91  0
             String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
 92  0
             attributeNames.add(alias);
 93  
         }
 94  
         
 95  0
         return attributeNames;
 96  
     }
 97  
 
 98  0
     public static class CheckRequiredAttributesException extends IllegalStateException
 99  
     {
 100  
         private static String summary(Collection<List<String>> attributeSets)
 101  
         {
 102  0
             StringBuilder buf = new StringBuilder();
 103  0
             for (List<String> set : attributeSets)
 104  
             {
 105  0
                 if (buf.length() > 0)
 106  
                 {
 107  0
                     buf.append(" ");
 108  
                 }
 109  
                 
 110  0
                 if (set.isEmpty())
 111  
                 {
 112  0
                     continue;
 113  
                 }
 114  
                 
 115  0
                 buf.append(set.toString());
 116  
             }
 117  0
             return buf.toString();
 118  
         }
 119  
 
 120  
         private CheckRequiredAttributesException(Element element, Collection<List<String>> attributeSets)
 121  
         {
 122  0
             super("Element " + SpringXMLUtils.elementToString(element) +
 123  
                     " must have all attributes for one of the sets: " + summary(attributeSets) + ".");
 124  0
         }
 125  
     }
 126  
 }