Coverage Report - org.mule.config.spring.editors.QNamePropertyEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
QNamePropertyEditor
0%
0/28
0%
0/11
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.config.spring.editors;
 8  
 
 9  
 import java.beans.PropertyEditorSupport;
 10  
 import java.util.ArrayList;
 11  
 import java.util.List;
 12  
 import java.util.StringTokenizer;
 13  
 
 14  
 import javax.xml.namespace.QName;
 15  
 
 16  
 /**
 17  
  * This handles qname{....} syntax as used in stockquote-soap-config.xml
 18  
  */
 19  
 public class QNamePropertyEditor extends PropertyEditorSupport
 20  
 {
 21  0
     private boolean explicit = false;
 22  
 
 23  
     public QNamePropertyEditor()
 24  
     {
 25  0
         super();
 26  0
     }
 27  
     
 28  
     public QNamePropertyEditor(boolean explicit)
 29  
     {
 30  0
         this();
 31  0
         this.explicit = explicit;
 32  0
     }
 33  
 
 34  
     @Override
 35  
     public void setAsText(String text) throws IllegalArgumentException
 36  
     {
 37  
 
 38  0
         if (text.startsWith("qname{"))
 39  
         {
 40  0
             setValue(parseQName(text.substring(6, text.length() - 1)));
 41  
         }
 42  0
         else if (!explicit)
 43  
         {
 44  0
             setValue(parseQName(text));
 45  
         }
 46  
         else
 47  
         {
 48  0
             setValue(new QName(text));
 49  
         }
 50  0
     }
 51  
 
 52  
     protected QName parseQName(String val)
 53  
     {
 54  0
         StringTokenizer st = new StringTokenizer(val, ":");
 55  0
         List elements = new ArrayList();
 56  
 
 57  0
         while (st.hasMoreTokens())
 58  
         {
 59  0
             elements.add(st.nextToken());
 60  
         }
 61  
 
 62  0
         switch (elements.size())
 63  
         {
 64  
             case 0 :
 65  
             {
 66  0
                 return null;
 67  
             }
 68  
             case 1 :
 69  
             {
 70  0
                 return new QName((String) elements.get(0));
 71  
             }
 72  
             case 2 :
 73  
             {
 74  0
                 return new QName((String) elements.get(0), (String) elements.get(1));
 75  
             }
 76  
             case 3 :
 77  
             {
 78  0
                 return new QName((String) elements.get(1) + ":" + (String) elements.get(2), (String) elements.get(0));
 79  
             }
 80  
             default :
 81  
             {
 82  0
                 String prefix = (String) elements.get(0);
 83  0
                 String local = (String) elements.get(1);
 84  
                 // namespace can have multiple colons in it, so just assume the rest
 85  
                 // is a namespace
 86  0
                 String ns = val.substring(prefix.length() + local.length() + 2);
 87  0
                 return new QName(ns, local, prefix);
 88  
             }
 89  
         }
 90  
     }
 91  
 
 92  
     public static QName convert(String value)
 93  
     {
 94  0
         QNamePropertyEditor editor = new QNamePropertyEditor();
 95  0
         editor.setAsText(value);
 96  0
         return (QName) editor.getValue();
 97  
     }
 98  
 
 99  
 }