KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > axis > util > encoding > XmlBeanDeserializer


1 /*
2  * XmlBeanDeserializer.java
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *
20  * Original author: Jonathan Colwell
21  */

22 package org.apache.beehive.wsm.axis.util.encoding;
23
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.apache.axis.encoding.DeserializationContext;
27 import org.apache.axis.encoding.DeserializerImpl;
28 import org.apache.axis.message.MessageElement;
29 import org.apache.xmlbeans.QNameSet;
30 import org.apache.xmlbeans.SchemaType;
31 import org.apache.xmlbeans.XmlObject;
32 import org.apache.xmlbeans.XmlOptions;
33 import org.xml.sax.Attributes JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * ****************************************************************************
38  *
39  * @author Jonathan Colwell
40  */

41 public class XmlBeanDeserializer extends DeserializerImpl {
42
43     private Class JavaDoc mJavaType;
44     private QName JavaDoc mXmlType;
45
46     public XmlBeanDeserializer(Class JavaDoc javaType, QName JavaDoc xmlType) {
47         mJavaType = javaType;
48         mXmlType = xmlType;
49     }
50
51     public void onStartElement(String JavaDoc namespace, String JavaDoc localName,
52                                String JavaDoc prefix, Attributes JavaDoc attributes,
53                                DeserializationContext context)
54             throws SAXException JavaDoc {
55         try {
56             MessageElement me = context.getCurElement();
57             XmlOptions opts = new XmlOptions()
58                     .setLoadReplaceDocumentElement(null);
59             XmlObject xObj = XmlObject.Factory.parse(me, opts);
60             SchemaType st = xObj.schemaType();
61             SchemaType jt = (SchemaType) mJavaType.getField("type").get(null);
62             XmlObject converted = xObj.changeType(jt);
63             if (converted != null) {
64                 setValue(converted);
65             } else {
66                 XmlObject[] children = xObj.selectChildren(QNameSet.ALL);
67                 for (XmlObject kid : children) {
68                     st = kid.schemaType();
69                     converted = xObj.changeType(jt);
70                     if (converted != null) {
71                         setValue(converted);
72                         break;
73                     }
74                 }
75             }
76         } catch (Exception JavaDoc xe) {
77             throw new SAXException JavaDoc(xe);
78         }
79     }
80 }
81
Popular Tags