Coverage Report - org.mule.config.spring.editors.QNamePropertyEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
QNamePropertyEditor
0%
0/28
0%
0/11
3.2
 
 1  
 /*
 2  
  * $Id: QNamePropertyEditor.java 11903 2008-06-02 21:08:11Z 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  
 package org.mule.config.spring.editors;
 11  
 
 12  
 import java.beans.PropertyEditorSupport;
 13  
 import java.util.ArrayList;
 14  
 import java.util.List;
 15  
 import java.util.StringTokenizer;
 16  
 
 17  
 import javax.xml.namespace.QName;
 18  
 
 19  
 /**
 20  
  * This handles qname{....} syntax as used in stockquote-soap-config.xml
 21  
  */
 22  
 public class QNamePropertyEditor extends PropertyEditorSupport
 23  
 {
 24  0
     private boolean explicit = false;
 25  
 
 26  
     public QNamePropertyEditor()
 27  
     {
 28  0
         super();
 29  0
     }
 30  
     
 31  
     public QNamePropertyEditor(boolean explicit)
 32  
     {
 33  0
         this();
 34  0
         this.explicit = explicit;
 35  0
     }
 36  
 
 37  
     //@Override
 38  
     public void setAsText(String text) throws IllegalArgumentException
 39  
     {
 40  
 
 41  0
         if (text.startsWith("qname{"))
 42  
         {
 43  0
             setValue(parseQName(text.substring(6, text.length() - 1)));
 44  
         }
 45  0
         else if (!explicit)
 46  
         {
 47  0
             setValue(parseQName(text));
 48  
         }
 49  
         else
 50  
         {
 51  0
             setValue(new QName(text));
 52  
         }
 53  0
     }
 54  
 
 55  
     protected QName parseQName(String val)
 56  
     {
 57  0
         StringTokenizer st = new StringTokenizer(val, ":");
 58  0
         List elements = new ArrayList();
 59  
 
 60  0
         while (st.hasMoreTokens())
 61  
         {
 62  0
             elements.add(st.nextToken());
 63  
         }
 64  
 
 65  0
         switch (elements.size())
 66  
         {
 67  
             case 0 :
 68  
             {
 69  0
                 return null;
 70  
             }
 71  
             case 1 :
 72  
             {
 73  0
                 return new QName((String) elements.get(0));
 74  
             }
 75  
             case 2 :
 76  
             {
 77  0
                 return new QName((String) elements.get(0), (String) elements.get(1));
 78  
             }
 79  
             case 3 :
 80  
             {
 81  0
                 return new QName((String) elements.get(1) + ":" + (String) elements.get(2), (String) elements.get(0));
 82  
             }
 83  
             default :
 84  
             {
 85  0
                 String prefix = (String) elements.get(0);
 86  0
                 String local = (String) elements.get(1);
 87  
                 // namespace can have multiple colons in it, so just assume the rest
 88  
                 // is a namespace
 89  0
                 String ns = val.substring(prefix.length() + local.length() + 2);
 90  0
                 return new QName(ns, local, prefix);
 91  
             }
 92  
         }
 93  
     }
 94  
 
 95  
     public static QName convert(String value)
 96  
     {
 97  0
         QNamePropertyEditor editor = new QNamePropertyEditor();
 98  0
         editor.setAsText(value);
 99  0
         return (QName) editor.getValue();
 100  
     }
 101  
 
 102  
 }