Coverage Report - org.mule.config.spring.parsers.generic.AutoIdUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
AutoIdUtils
0%
0/19
0%
0/12
2.4
 
 1  
 /*
 2  
  * $Id: AutoIdUtils.java 10489 2008-01-23 17:53:38Z 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.generic;
 12  
 
 13  
 import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
 14  
 import org.mule.util.StringUtils;
 15  
 
 16  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
 17  
 
 18  
 import org.w3c.dom.Element;
 19  
 
 20  0
 public class AutoIdUtils
 21  
 {
 22  
 
 23  
     public static final String ATTRIBUTE_ID = AbstractMuleBeanDefinitionParser.ATTRIBUTE_ID;
 24  
     public static final String ATTRIBUTE_NAME = AbstractMuleBeanDefinitionParser.ATTRIBUTE_NAME;
 25  0
     private static final AtomicInteger counter = new AtomicInteger(0);
 26  
     public static final String PREFIX = "org.mule.autogen.";
 27  
 
 28  
     public static boolean blankAttribute(Element element, String attribute)
 29  
     {
 30  0
         return StringUtils.isBlank(element.getAttribute(attribute));
 31  
     }
 32  
 
 33  
     public static void ensureUniqueId(Element element, String type)
 34  
     {
 35  0
         if (null != element && blankAttribute(element, ATTRIBUTE_ID))
 36  
         {
 37  0
             if (blankAttribute(element, ATTRIBUTE_NAME))
 38  
             {
 39  0
                 element.setAttribute(ATTRIBUTE_ID, uniqueValue(PREFIX + type));
 40  
             }
 41  
             else
 42  
             {
 43  0
                 element.setAttribute(ATTRIBUTE_ID, element.getAttribute(ATTRIBUTE_NAME));
 44  
             }
 45  
         }
 46  0
     }
 47  
 
 48  
     public static String getUniqueName(Element element, String type)
 49  
     {
 50  0
         if (!blankAttribute(element, ATTRIBUTE_NAME))
 51  
         {
 52  0
             return element.getAttribute(ATTRIBUTE_NAME);
 53  
         }
 54  0
         else if (!blankAttribute(element, ATTRIBUTE_ID))
 55  
         {
 56  0
             return element.getAttribute(ATTRIBUTE_ID);
 57  
         }
 58  
         else
 59  
         {
 60  0
             return uniqueValue(PREFIX + type);
 61  
         }
 62  
     }
 63  
 
 64  
     public static String uniqueValue(String value)
 65  
     {
 66  0
         return value + "." + counter.incrementAndGet();
 67  
     }
 68  
 
 69  
     public static void forceUniqueId(Element element, String type)
 70  
     {
 71  0
         if (null != element)
 72  
         {
 73  0
             String id = uniqueValue(PREFIX + type);
 74  0
             element.setAttribute(ATTRIBUTE_ID, id);
 75  0
             element.setAttribute(ATTRIBUTE_NAME, id);
 76  
         }
 77  0
     }
 78  
 
 79  
 }