KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > encoding > ser > xbeans > 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.axis.encoding.ser.xbeans;
23
24 import org.apache.axis.encoding.DeserializationContext;
25 import org.apache.axis.encoding.DeserializerImpl;
26 import org.apache.axis.message.MessageElement;
27 import org.apache.xmlbeans.QNameSet;
28 import org.apache.xmlbeans.SchemaType;
29 import org.apache.xmlbeans.XmlObject;
30 import org.apache.xmlbeans.XmlOptions;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 import javax.xml.namespace.QName JavaDoc;
35
36 /**
37  * Class XmlBeanDeserializer
38  * @author Jonathan Colwell
39  */

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