Coverage Report - org.mule.config.spring.parsers.processors.CheckExclusiveAttributes
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckExclusiveAttributes
0%
0/32
0%
0/16
3.667
CheckExclusiveAttributes$1
N/A
N/A
3.667
CheckExclusiveAttributes$CheckExclusiveAttributesException
0%
0/3
N/A
3.667
 
 1  
 /*
 2  
  * $Id:CheckExclusiveAttributes.java 8321 2007-09-10 19:22:52Z acooke $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.HashMap;
 18  
 import java.util.Iterator;
 19  
 import java.util.LinkedList;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.w3c.dom.Attr;
 24  
 import org.w3c.dom.Element;
 25  
 import org.w3c.dom.NamedNodeMap;
 26  
 
 27  
 /**
 28  
  * Attributes from two different sets cannot appear together
 29  
  */
 30  
 public class CheckExclusiveAttributes implements PreProcessor
 31  
 {
 32  
 
 33  
     public static final int NONE = -1;
 34  0
     private Map knownAttributes = new HashMap();
 35  
 
 36  
     public CheckExclusiveAttributes(String[][] attributeSets)
 37  0
     {
 38  0
         for (int set = 0; set < attributeSets.length; set++)
 39  
         {
 40  0
             String[] attributes = attributeSets[set];
 41  0
             for (int attribute = 0; attribute < attributes.length; attribute++)
 42  
             {
 43  0
                 knownAttributes.put(attributes[attribute], new Integer(set));
 44  
             }
 45  
         }
 46  0
     }
 47  
 
 48  
     public void preProcess(PropertyConfiguration config, Element element)
 49  
     {
 50  0
         List foundAttributes = new LinkedList();
 51  0
         int foundSetIndex = NONE;
 52  
 
 53  0
         NamedNodeMap attributes = element.getAttributes();
 54  0
         for (int i = 0; i < attributes.getLength(); i++)
 55  
         {
 56  0
             String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
 57  
             // don't translate to alias because the error message is in terms of the attributes
 58  
             // the user enters - we don't want to expose the details of translations
 59  
 //            String name = null == config ? alias : config.translateName(alias);
 60  0
             if (knownAttributes.containsKey(alias))
 61  
             {
 62  0
                 int index = ((Integer) knownAttributes.get(alias)).intValue();
 63  0
                 if (foundSetIndex != NONE && foundSetIndex != index)
 64  
                 {
 65  0
                     StringBuffer message = new StringBuffer("The attribute '");
 66  0
                     message.append(alias);
 67  0
                     message.append("' cannot appear with the attribute");
 68  0
                     if (foundAttributes.size() > 1)
 69  
                     {
 70  0
                         message.append("s");
 71  
                     }
 72  0
                     Iterator found = foundAttributes.iterator();
 73  0
                     while (found.hasNext())
 74  
                     {
 75  0
                         message.append(" '");
 76  0
                         message.append(found.next());
 77  0
                         message.append("'");
 78  
                     }
 79  0
                     message.append(" in element ");
 80  0
                     message.append(SpringXMLUtils.elementToString(element));
 81  0
                     message.append(".");
 82  0
                     throw new CheckExclusiveAttributesException(message.toString());
 83  
                 }
 84  
                 else
 85  
                 {
 86  0
                     foundSetIndex = index;
 87  0
                     foundAttributes.add(alias);
 88  
                 }
 89  
             }
 90  
         }
 91  0
     }
 92  
 
 93  0
     public static class CheckExclusiveAttributesException extends IllegalStateException
 94  
     {
 95  
 
 96  
         private CheckExclusiveAttributesException(String message)
 97  
         {
 98  0
             super(message);
 99  0
         }
 100  
 
 101  
     }
 102  
 
 103  
 }