Coverage Report - org.mule.config.converters.QNameConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
QNameConverter
0%
0/26
0%
0/5
5.25
 
 1  
 /*
 2  
  * $Id: QNameConverter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.converters;
 12  
 
 13  
 
 14  
 import java.util.ArrayList;
 15  
 import java.util.List;
 16  
 import java.util.StringTokenizer;
 17  
 
 18  
 import javax.xml.namespace.QName;
 19  
 
 20  
 import org.apache.commons.beanutils.ConversionException;
 21  
 import org.apache.commons.beanutils.Converter;
 22  
 
 23  
 /**
 24  
  * <code>QNameConverter</code> TODO document properly; see QNameConverterTestCase
 25  
  * for now
 26  
  */
 27  
 public class QNameConverter implements Converter
 28  
 {
 29  
 
 30  0
     boolean explicit = false;
 31  
 
 32  
     public QNameConverter()
 33  
     {
 34  0
         super();
 35  0
     }
 36  
 
 37  
     public QNameConverter(boolean explicit)
 38  0
     {
 39  0
         this.explicit = explicit;
 40  0
     }
 41  
 
 42  
     // --------------------------------------------------------- Public Methods
 43  
 
 44  
     /**
 45  
      * Convert the specified input object into an output object of the specified
 46  
      * type.
 47  
      * 
 48  
      * @param type Data type to which this value should be converted
 49  
      * @param value The input value to be converted
 50  
      * @throws org.apache.commons.beanutils.ConversionException if conversion cannot
 51  
      *             be performed successfully
 52  
      */
 53  
     public Object convert(Class type, Object value)
 54  
     {
 55  0
         if (value == null)
 56  
         {
 57  0
             throw new ConversionException("No value specified");
 58  
         }
 59  
 
 60  0
         if (value instanceof QName)
 61  
         {
 62  0
             return (value);
 63  
         }
 64  
 
 65  0
         String val = value.toString();
 66  0
         if (val.startsWith("qname{"))
 67  
         {
 68  0
             return parseQName(val.substring(6, val.length() - 1));
 69  
         }
 70  0
         else if (!explicit)
 71  
         {
 72  0
             return parseQName(val);
 73  
         }
 74  
         else
 75  
         {
 76  0
             return new QName(val);
 77  
         }
 78  
     }
 79  
 
 80  
     protected QName parseQName(String val)
 81  
     {
 82  0
         StringTokenizer st = new StringTokenizer(val, ":");
 83  0
         List elements = new ArrayList();
 84  
 
 85  0
         while (st.hasMoreTokens())
 86  
         {
 87  0
             elements.add(st.nextToken());
 88  
         }
 89  
 
 90  0
         switch (elements.size())
 91  
         {
 92  
             case 1 :
 93  0
                 return new QName((String) elements.get(0));
 94  
             case 2 :
 95  0
                 return new QName((String) elements.get(0), (String) elements.get(1));
 96  
             case 3 :
 97  0
                 return new QName((String) elements.get(1) + ":" + (String) elements.get(2),
 98  
                     (String) elements.get(0));
 99  
             case 4 :
 100  0
                 return new QName((String) elements.get(2) + ":" + (String) elements.get(3),
 101  
                     (String) elements.get(1), (String) elements.get(0));
 102  
             default :
 103  0
                 return null;
 104  
         }
 105  
     }
 106  
 
 107  
 }