Coverage Report - org.mule.config.spring.parsers.processors.BlockAttribute
 
Classes in this File Line Coverage Branch Coverage Complexity
BlockAttribute
0%
0/12
0%
0/4
0
BlockAttribute$BlockAttributeException
0%
0/2
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 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.Arrays;
 14  
 import java.util.HashSet;
 15  
 import java.util.Set;
 16  
 
 17  
 import org.w3c.dom.Attr;
 18  
 import org.w3c.dom.Element;
 19  
 import org.w3c.dom.NamedNodeMap;
 20  
 
 21  
 /**
 22  
  * Throws an exception if any of the disallowed attributes (after translation) is present.
 23  
  * Designed to cooperates with
 24  
  * {@link org.mule.config.spring.parsers.delegate.AbstractSerialDelegatingDefinitionParser#addHandledException(Class)}
 25  
  */
 26  
 public class BlockAttribute implements PreProcessor
 27  
 {
 28  
     private Set<String> disallowed;
 29  
 
 30  
     public BlockAttribute(String disallowed)
 31  
     {
 32  0
         this(new String[]{ disallowed });
 33  0
     }
 34  
 
 35  
     public BlockAttribute(String[] disallowed)
 36  0
     {
 37  0
         this.disallowed = new HashSet<String>(Arrays.asList(disallowed));
 38  0
     }
 39  
 
 40  
     public void preProcess(PropertyConfiguration config, Element element)
 41  
     {
 42  0
         NamedNodeMap attributes = element.getAttributes();
 43  0
         for (int i = 0; i < attributes.getLength(); i++)
 44  
         {
 45  0
             String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
 46  0
             String name = config.translateName(alias);
 47  0
             if (disallowed.contains(name))
 48  
             {
 49  0
                 throw new BlockAttributeException("Attribute " + alias + " is not allowed here.");
 50  
             }
 51  
         }
 52  0
     }
 53  
 
 54  
     public static class BlockAttributeException extends IllegalStateException
 55  
     {
 56  
         BlockAttributeException(String message)
 57  
         {
 58  0
             super(message);
 59  0
         }
 60  
     }
 61  
 }