Coverage Report - org.mule.config.spring.parsers.processors.CheckRequiredAttributes
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckRequiredAttributes
0%
0/37
0%
0/24
4.667
CheckRequiredAttributes$1
N/A
N/A
4.667
CheckRequiredAttributes$CheckRequiredAttributesException
0%
0/3
N/A
4.667
 
 1  
 /*
 2  
  * $Id: CheckRequiredAttributes.java 10787 2008-02-12 18:51:50Z dfeist $
 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.Map;
 20  
 
 21  
 import org.w3c.dom.Attr;
 22  
 import org.w3c.dom.Element;
 23  
 import org.w3c.dom.NamedNodeMap;
 24  
 
 25  
 /**
 26  
  * All attributes from at least one set must be provided
 27  
  */
 28  
 public class CheckRequiredAttributes implements PreProcessor
 29  
 {
 30  
 
 31  
     // maps from attribute name to attribute set index (integer)
 32  0
     private Map knownAttributes = new HashMap();
 33  
     // maps from attribute set index to number of attributes in that set
 34  0
     private Map numberOfAttributes = new HashMap();
 35  
     // description of acceptable attributes
 36  
     private String summary;
 37  
 
 38  
     public CheckRequiredAttributes(String[][] attributeSets)
 39  0
     {
 40  0
         StringBuffer buffer = new StringBuffer();
 41  0
         for (int set = 0; set < attributeSets.length; set++)
 42  
         {
 43  0
             String[] attributes = attributeSets[set];
 44  
             // ignore empty sets
 45  0
             if (attributes.length > 0)
 46  
             {
 47  0
                 Integer index = new Integer(set);
 48  0
                 numberOfAttributes.put(index, new Integer(attributes.length));
 49  0
                 if (set > 0)
 50  
                 {
 51  0
                     buffer.append("; ");
 52  
                 }
 53  0
                 for (int attribute = 0; attribute < attributes.length; attribute++)
 54  
                 {
 55  0
                     knownAttributes.put(attributes[attribute], index);
 56  0
                     if (attribute > 0)
 57  
                     {
 58  0
                         buffer.append(", ");
 59  
                     }
 60  
                     // don't translate to alias because the error message is in terms of the attributes
 61  
                     // the user enters - we don't want to expose the details of translations
 62  0
                     buffer.append(attributes[attribute]);
 63  
                 }
 64  
             }
 65  
         }
 66  0
         summary = buffer.toString();
 67  0
     }
 68  
 
 69  
     public void preProcess(PropertyConfiguration config, Element element)
 70  
     {
 71  
         // map from attribute set index to count
 72  0
         Map foundAttributesCount = new HashMap();
 73  
 
 74  0
         NamedNodeMap attributes = element.getAttributes();
 75  0
         for (int i = 0; i < attributes.getLength(); i++)
 76  
         {
 77  0
             String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
 78  0
             if (knownAttributes.containsKey(alias))
 79  
             {
 80  0
                 Integer index = (Integer) knownAttributes.get(alias);
 81  0
                 if (!foundAttributesCount.containsKey(index))
 82  
                 {
 83  0
                     foundAttributesCount.put(index, new Integer(0));
 84  
                 }
 85  0
                 foundAttributesCount.put(index,
 86  
                         new Integer(1 + ((Integer) foundAttributesCount.get(index)).intValue()));
 87  
 
 88  
             }
 89  
         }
 90  
 
 91  
         // if there are no attributes to check for, we are ok
 92  0
         boolean ok = knownAttributes.size() == 0;
 93  0
         Iterator indices = foundAttributesCount.keySet().iterator();
 94  0
         while (indices.hasNext() && !ok)
 95  
         {
 96  0
             Object index = indices.next();
 97  0
             Object count = foundAttributesCount.get(index);
 98  0
             ok = numberOfAttributes.get(index).equals(count);
 99  0
         }
 100  0
         if (!ok)
 101  
         {
 102  0
             throw new CheckRequiredAttributesException(element, summary);
 103  
         }
 104  0
     }
 105  
 
 106  0
     public static class CheckRequiredAttributesException extends IllegalStateException
 107  
     {
 108  
 
 109  
         private CheckRequiredAttributesException(Element element, String summary)
 110  
         {
 111  0
             super("Element " + SpringXMLUtils.elementToString(element) +
 112  
                     " must have all attributes for one of the sets: " + summary + ".");
 113  0
         }
 114  
 
 115  
     }
 116  
 
 117  
 }