View Javadoc
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      private boolean explicit = false;
22  
23      public QNamePropertyEditor()
24      {
25          super();
26      }
27      
28      public QNamePropertyEditor(boolean explicit)
29      {
30          this();
31          this.explicit = explicit;
32      }
33  
34      @Override
35      public void setAsText(String text) throws IllegalArgumentException
36      {
37  
38          if (text.startsWith("qname{"))
39          {
40              setValue(parseQName(text.substring(6, text.length() - 1)));
41          }
42          else if (!explicit)
43          {
44              setValue(parseQName(text));
45          }
46          else
47          {
48              setValue(new QName(text));
49          }
50      }
51  
52      protected QName parseQName(String val)
53      {
54          StringTokenizer st = new StringTokenizer(val, ":");
55          List elements = new ArrayList();
56  
57          while (st.hasMoreTokens())
58          {
59              elements.add(st.nextToken());
60          }
61  
62          switch (elements.size())
63          {
64              case 0 :
65              {
66                  return null;
67              }
68              case 1 :
69              {
70                  return new QName((String) elements.get(0));
71              }
72              case 2 :
73              {
74                  return new QName((String) elements.get(0), (String) elements.get(1));
75              }
76              case 3 :
77              {
78                  return new QName((String) elements.get(1) + ":" + (String) elements.get(2), (String) elements.get(0));
79              }
80              default :
81              {
82                  String prefix = (String) elements.get(0);
83                  String local = (String) elements.get(1);
84                  // namespace can have multiple colons in it, so just assume the rest
85                  // is a namespace
86                  String ns = val.substring(prefix.length() + local.length() + 2);
87                  return new QName(ns, local, prefix);
88              }
89          }
90      }
91  
92      public static QName convert(String value)
93      {
94          QNamePropertyEditor editor = new QNamePropertyEditor();
95          editor.setAsText(value);
96          return (QName) editor.getValue();
97      }
98  
99  }