View Javadoc

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