View Javadoc

1   /*
2    * $Id: QNameConverter.java 9484 2007-10-30 20:13:02Z dandiep $
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  
11  package org.mule.config.converters;
12  
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  import java.util.StringTokenizer;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.apache.commons.beanutils.ConversionException;
21  import org.apache.commons.beanutils.Converter;
22  
23  /**
24   * <code>QNameConverter</code> TODO document properly; see QNameConverterTestCase
25   * for now
26   */
27  public class QNameConverter implements Converter
28  {
29  
30      protected final boolean explicit;
31  
32      public QNameConverter()
33      {
34          this(false);
35      }
36  
37      public QNameConverter(boolean explicit)
38      {
39          this.explicit = explicit;
40      }
41  
42      // --------------------------------------------------------- Public Methods
43  
44      /**
45       * Convert the specified input object into an output object of the specified
46       * type.
47       * 
48       * @param type Data type to which this value should be converted
49       * @param value The input value to be converted
50       * @throws org.apache.commons.beanutils.ConversionException if conversion cannot
51       *             be performed successfully
52       */
53      public Object convert(Class type, Object value)
54      {
55          if (value == null)
56          {
57              throw new ConversionException("No value specified");
58          }
59  
60          if (value instanceof QName)
61          {
62              return (value);
63          }
64  
65          String val = value.toString();
66          if (val.startsWith("qname{"))
67          {
68              return parseQName(val.substring(6, val.length() - 1));
69          }
70          else if (!explicit)
71          {
72              return parseQName(val);
73          }
74          else
75          {
76              return new QName(val);
77          }
78      }
79  
80      protected QName parseQName(String val)
81      {
82          StringTokenizer st = new StringTokenizer(val, ":");
83          List elements = new ArrayList();
84  
85          while (st.hasMoreTokens())
86          {
87              elements.add(st.nextToken());
88          }
89  
90          switch (elements.size())
91          {
92              case 0 :
93          	    return null;
94              case 1 :
95                  return new QName((String) elements.get(0));
96              case 2 :
97                  return new QName((String) elements.get(0), (String) elements.get(1));
98              case 3 :
99                  return new QName((String) elements.get(1) + ":" + (String) elements.get(2),
100                     (String) elements.get(0));
101             default :
102                 String prefix = (String) elements.get(0);
103                 String local = (String) elements.get(1);
104                 // namespace can have multiple colons in it, so just assume the rest is a namespace
105                 String ns = val.substring(prefix.length() + local.length() + 2);
106                 
107                 return new QName(ns, local, prefix);
108         }
109     }
110 
111 }