Coverage Report - org.mule.config.spring.parsers.delegate.AttributeSelectionDefinitionParser
 
Classes in this File Line Coverage Branch Coverage Complexity
AttributeSelectionDefinitionParser
0%
0/15
0%
0/4
2.333
 
 1  
 /*
 2  
  * $Id: AttributeSelectionDefinitionParser.java 10256 2008-01-08 15:20:25Z 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.delegate;
 12  
 
 13  
 import org.mule.config.spring.parsers.MuleDefinitionParser;
 14  
 import org.mule.config.spring.util.SpringXMLUtils;
 15  
 import org.mule.util.CollectionUtils;
 16  
 
 17  
 import java.util.HashMap;
 18  
 import java.util.Map;
 19  
 
 20  
 import org.springframework.beans.factory.xml.ParserContext;
 21  
 import org.w3c.dom.Attr;
 22  
 import org.w3c.dom.Element;
 23  
 import org.w3c.dom.NamedNodeMap;
 24  
 
 25  
 /**
 26  
  * Select sub parser depending on presence of a particular attribute
 27  
  */
 28  
 public class AttributeSelectionDefinitionParser extends AbstractParallelDelegatingDefinitionParser
 29  
 {
 30  
 
 31  0
     private Map attributeToParserIndex = new HashMap();
 32  
 
 33  
     public AttributeSelectionDefinitionParser(String attribute, MuleDefinitionParser delegate)
 34  
     {
 35  0
         super();
 36  0
         addDelegate(attribute, delegate);
 37  0
     }
 38  
 
 39  
     public void addDelegate(String attribute, MuleDefinitionParser delegate)
 40  
     {
 41  0
         addDelegate(delegate);
 42  0
         attributeToParserIndex.put(attribute, new Integer(size() - 1));
 43  0
         delegate.setIgnoredDefault(true);
 44  0
         delegate.removeIgnored(attribute);
 45  0
     }
 46  
 
 47  
     protected MuleDefinitionParser getDelegate(Element element, ParserContext parserContext)
 48  
     {
 49  0
         NamedNodeMap attributes = element.getAttributes();
 50  0
         for (int i = 0; i < attributes.getLength(); ++i)
 51  
         {
 52  0
             String attribute = SpringXMLUtils.attributeName((Attr) attributes.item(i));
 53  0
             if (attributeToParserIndex.containsKey(attribute))
 54  
             {
 55  0
                 return getDelegate(((Integer) attributeToParserIndex.get(attribute)).intValue());
 56  
             }
 57  
         }
 58  0
         throw new IllegalArgumentException("Element " + SpringXMLUtils.elementToString(element) +
 59  
                 " does not contain any attribute from " +
 60  
                 CollectionUtils.toString(attributeToParserIndex.keySet(), 10, false));
 61  
     }
 62  
 
 63  
 }