1 10 11 package org.mule.config.converters; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.util.StringTokenizer ; 16 17 import javax.xml.namespace.QName ; 18 19 import org.apache.commons.beanutils.ConversionException; 20 import org.apache.commons.beanutils.Converter; 21 22 26 public class QNameConverter implements Converter 27 { 28 29 boolean explicit = false; 30 31 public QNameConverter() 32 { 33 super(); 34 } 35 36 public QNameConverter(boolean explicit) 37 { 38 this.explicit = explicit; 39 } 40 41 43 52 public Object convert(Class type, Object value) 53 { 54 if (value == null) 55 { 56 throw new ConversionException("No value specified"); 57 } 58 59 if (value instanceof QName ) 60 { 61 return (value); 62 } 63 64 String val = value.toString(); 65 if (val.startsWith("qname{")) 66 { 67 return parseQName(val.substring(6, val.length() - 1)); 68 } 69 else if (!explicit) 70 { 71 return parseQName(val); 72 } 73 else 74 { 75 return new QName (val); 76 } 77 } 78 79 protected QName parseQName(String val) 80 { 81 StringTokenizer st = new StringTokenizer (val, ":"); 82 List elements = new ArrayList (); 83 84 while (st.hasMoreTokens()) 85 { 86 elements.add(st.nextToken()); 87 } 88 89 switch (elements.size()) 90 { 91 case 1 : 92 { 93 return new QName ((String )elements.get(0)); 94 } 95 case 2 : 96 { 97 return new QName ((String )elements.get(0), (String )elements.get(1)); 98 } 99 case 3 : 100 { 101 return new QName ((String )elements.get(1) + ":" + (String )elements.get(2), 102 (String )elements.get(0)); 103 } 104 case 4 : 105 { 106 return new QName ((String )elements.get(2) + ":" + (String )elements.get(3), 107 (String )elements.get(1), (String )elements.get(0)); 108 } 109 default : 110 { 111 return null; 112 } 113 } 114 } 115 116 } 117 | Popular Tags |