KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > converters > QNameConverter


1 /*
2  * $Id: QNameConverter.java 3831 2006-11-07 22:29:59Z aperepel $
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 MuleSource MPL
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 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import javax.xml.namespace.QName JavaDoc;
18
19 import org.apache.commons.beanutils.ConversionException;
20 import org.apache.commons.beanutils.Converter;
21
22 /**
23  * <code>QNameConverter</code> TODO document properly; see QNameConverterTestCase
24  * for now
25  */

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     // --------------------------------------------------------- Public Methods
42

43     /**
44      * Convert the specified input object into an output object of the specified
45      * type.
46      *
47      * @param type Data type to which this value should be converted
48      * @param value The input value to be converted
49      * @throws org.apache.commons.beanutils.ConversionException if conversion cannot
50      * be performed successfully
51      */

52     public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value)
53     {
54         if (value == null)
55         {
56             throw new ConversionException("No value specified");
57         }
58
59         if (value instanceof QName JavaDoc)
60         {
61             return (value);
62         }
63
64         String JavaDoc 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 JavaDoc(val);
76         }
77     }
78
79     protected QName JavaDoc parseQName(String JavaDoc val)
80     {
81         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(val, ":");
82         List JavaDoc elements = new ArrayList JavaDoc();
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 JavaDoc((String JavaDoc)elements.get(0));
94             }
95             case 2 :
96             {
97                 return new QName JavaDoc((String JavaDoc)elements.get(0), (String JavaDoc)elements.get(1));
98             }
99             case 3 :
100             {
101                 return new QName JavaDoc((String JavaDoc)elements.get(1) + ":" + (String JavaDoc)elements.get(2),
102                     (String JavaDoc)elements.get(0));
103             }
104             case 4 :
105             {
106                 return new QName JavaDoc((String JavaDoc)elements.get(2) + ":" + (String JavaDoc)elements.get(3),
107                     (String JavaDoc)elements.get(1), (String JavaDoc)elements.get(0));
108             }
109             default :
110             {
111                 return null;
112             }
113         }
114     }
115
116 }
117
Popular Tags