Coverage Report - org.mule.config.spring.parsers.processors.CheckExclusiveAttribute
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckExclusiveAttribute
0%
0/28
0%
0/14
3.333
CheckExclusiveAttribute$1
N/A
N/A
3.333
CheckExclusiveAttribute$CheckExclusiveAttributeException
0%
0/3
N/A
3.333
 
 1  
 /*
 2  
  * $Id:CheckExclusiveAttributes.java 8321 2007-09-10 19:22:52Z acooke $
 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.Iterator;
 18  
 import java.util.LinkedList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.w3c.dom.Attr;
 22  
 import org.w3c.dom.Element;
 23  
 import org.w3c.dom.NamedNodeMap;
 24  
 
 25  
 /**
 26  
  * If this attribute is present, no other can be
 27  
  */
 28  
 public class CheckExclusiveAttribute implements PreProcessor
 29  
 {
 30  
 
 31  
     public static final int NONE = -1;
 32  
     private String attribute;
 33  
 
 34  
     public CheckExclusiveAttribute(String attribute)
 35  0
     {
 36  0
         this.attribute = attribute;
 37  0
     }
 38  
 
 39  
     public void preProcess(PropertyConfiguration config, Element element)
 40  
     {
 41  0
         List foundAttributes = new LinkedList();
 42  0
         boolean found = false;
 43  
 
 44  0
         NamedNodeMap attributes = element.getAttributes();
 45  0
         for (int i = 0; i < attributes.getLength(); i++)
 46  
         {
 47  0
             String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
 48  0
             if (! config.isIgnored(alias))
 49  
             {
 50  0
                 if (attribute.equals(alias))
 51  
                 {
 52  0
                     found = true;
 53  
                 }
 54  
                 else
 55  
                 {
 56  0
                     foundAttributes.add(alias);
 57  
                 }
 58  
             }
 59  
         }
 60  
 
 61  0
         if (found && foundAttributes.size() > 0)
 62  
         {
 63  0
             StringBuffer message = new StringBuffer("The attribute '");
 64  0
             message.append(attribute);
 65  0
             message.append("' cannot appear with the attribute");
 66  0
             if (foundAttributes.size() > 1)
 67  
             {
 68  0
                 message.append("s");
 69  
             }
 70  0
             Iterator others = foundAttributes.iterator();
 71  0
             while (others.hasNext())
 72  
             {
 73  0
                 message.append(" '");
 74  0
                 message.append(others.next());
 75  0
                 message.append("'");
 76  
             }
 77  0
             message.append(" in element ");
 78  0
             message.append(SpringXMLUtils.elementToString(element));
 79  0
             message.append(".");
 80  0
             throw new CheckExclusiveAttributeException(message.toString());
 81  
         }
 82  0
     }
 83  
 
 84  0
     public static class CheckExclusiveAttributeException extends IllegalStateException
 85  
     {
 86  
 
 87  
         private CheckExclusiveAttributeException(String message)
 88  
         {
 89  0
             super(message);
 90  0
         }
 91  
 
 92  
     }
 93  
 
 94  
 }