KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * XmlBeanSerializer.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 package org.apache.beehive.wsm.axis.util.encoding;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.Array JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28
29 import org.apache.axis.AxisFault;
30 import org.apache.axis.Constants;
31 import org.apache.axis.encoding.SerializationContext;
32 import org.apache.axis.encoding.Serializer;
33 import org.apache.axis.wsdl.fromJava.Types;
34 import org.apache.beehive.wsm.wsdl.Schema;
35 import org.apache.beehive.wsm.wsdl.Utilities;
36 import org.apache.xmlbeans.SchemaField;
37 import org.apache.xmlbeans.SchemaType;
38 import org.apache.xmlbeans.SchemaTypeLoader;
39 import org.apache.xmlbeans.XmlBeans;
40 import org.apache.xmlbeans.XmlCursor;
41 import org.apache.xmlbeans.XmlObject;
42 import org.apache.xmlbeans.impl.xb.xsdschema.LocalElement;
43
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.Element JavaDoc;
46 import org.w3c.dom.Node JavaDoc;
47 import org.w3c.dom.NodeList JavaDoc;
48 import org.xml.sax.Attributes JavaDoc;
49
50 /**
51  * ****************************************************************************
52  *
53  * @author Jonathan Colwell
54  */

55 public class XmlBeanSerializer implements Serializer {
56
57     /**
58      * Serialize an element named name, with the indicated attributes and value.
59      *
60      * @param name
61      * is the element name
62      * @param attributes
63      * are the attributes...serialize is free to add more.
64      * @param value
65      * is the value
66      * @param context
67      * is the SerializationContext
68      */

69     public void serialize(QName JavaDoc name, Attributes JavaDoc attributes, Object JavaDoc value,
70             SerializationContext context) throws IOException JavaDoc {
71         if (!(value instanceof XmlObject)) {
72             throw new IOException JavaDoc(((value != null)
73                     ? value.getClass().getName()
74                     : "null")
75                     + " is not an " + XmlObject.class.getName());
76         } else {
77             context.setWriteXMLType(null);
78             context.startElement(name, attributes);
79
80             XmlCursor xCur = ((XmlObject) value).newCursor();
81             if (xCur.toFirstContentToken() == XmlCursor.TokenType.START) {
82                 do {
83                     Node JavaDoc n = xCur.getDomNode();
84                     if (n.getNodeType() == Node.ELEMENT_NODE) {
85                         context.writeDOMElement((Element) n);
86                     }
87                 } while (xCur.toNextSibling());
88             }
89             context.endElement();
90         }
91     }
92
93     public String JavaDoc getMechanismType() {
94         return Constants.AXIS_SAX;
95     }
96
97     /**
98      * Return XML schema for the specified type, suitable for insertion into the
99      * <types> element of a WSDL document, or underneath an
100      * <element> or <attribute> declaration.
101      *
102      * @param javaType
103      * the Java Class we're writing out schema for
104      * @param types
105      * the Java2WSDL Types object which holds the context for the
106      * WSDL being generated.
107      * @return a type element containing a schema simpleType/complexType
108      * @see org.apache.axis.wsdl.fromJava.Types
109      */

110     public Element writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
111         try {
112             if (!XmlObject.class.isAssignableFrom(javaType)) {
113                 throw new RuntimeException JavaDoc(
114                         "Invalid Object type is assigned to the XMLBeanSerialization Type: "
115                                 + javaType.getCanonicalName());
116             }
117
118             SchemaType docType = XmlBeans.typeForClass(javaType);
119             writeSchemaForDocType(docType, types);
120             // assume that the writeSchemaForDocType wrote the schema
121
// for the type and all the dependent types.
122
return null;
123         } catch (Exception JavaDoc e) {
124             e.printStackTrace();
125             throw e;
126         }
127     }
128
129     /**
130      * @param types
131      * @param docType
132      * @return
133      * @throws Exception
134      */

135     private void writeSchemaForDocType(SchemaType docType, Types types)
136             throws Exception JavaDoc {
137         Schema mySchema = Utilities.findtSchemaDocument(docType);
138
139         QName JavaDoc q = docType.getName();
140
141         XmlObject typeNodeInWSDL = mySchema.getTypeNode(q);
142
143         if (null == typeNodeInWSDL)
144             throw new RuntimeException JavaDoc(
145                     "Type for object not found in the assigned WSDL file. "
146                             + docType.getName() + " schema in: "
147                             + docType.getSourceName());
148         // insertDependentTypes(typeNodeInWSDL, types);
149
Node JavaDoc n = typeNodeInWSDL.getDomNode();
150         Document JavaDoc doc = types.createElement(
151                 "element_to_get_document_useless_otherwise").getOwnerDocument();
152         Element e = (Element) doc.importNode(n, true);
153         try {
154             types.writeSchemaElementDecl(q, e);
155         } catch (AxisFault e1) {
156             // this means the types was already in... fine!
157
// TBD: make sure there are other types of exceptions are at least
158
// reported
159
}
160         Set JavaDoc<QName JavaDoc> dependentTypes = new HashSet JavaDoc<QName JavaDoc>();
161         getAllDependentTypes(typeNodeInWSDL, dependentTypes);
162         for (QName JavaDoc nxtType : dependentTypes) {
163             Class JavaDoc nxtJavaType = null;
164             // add the class if it is an xml bean
165
if (null != (nxtJavaType = q2UserClass(nxtType))
166                     && XmlObject.class.isAssignableFrom(nxtJavaType)) {
167                 writeSchema(nxtJavaType, types);
168             }
169         }
170         return;
171     }
172
173     /**
174      * @param nxtType
175      * @return null for classes that are not found, or if they are primitive types
176      * *
177      */

178     private Class JavaDoc q2UserClass(QName JavaDoc qname) {
179         SchemaTypeLoader stl = XmlBeans.getContextTypeLoader();
180         SchemaType st = stl.findType(qname);
181         if (st == null) {
182             SchemaField sf = stl.findElement(qname);
183             if (sf != null)
184                 st = sf.getType();
185         }
186
187         if (st != null && !st.isBuiltinType())
188             return st.getJavaClass();
189         else
190             return null; // for classes that are not found, or are build in
191

192     }
193
194     /**
195      * @param nodeInWSDL
196      * @param dependentTypes
197      * @return
198      *
199      * Walk all the nodes under the nodeInWSDL if there is an 'element' type the
200      * add its types or references to the dependent type.
201      */

202     private void getAllDependentTypes(XmlObject nodeInWSDL,
203             Set JavaDoc<QName JavaDoc> dependentTypes) {
204         // scan for any node under the type that has "type" or "ref" attribute
205
XmlCursor cursor = nodeInWSDL.newCursor();
206         if (cursor.toFirstChild()) { // has child
207
while (true) {
208                 getAllDependentTypes(cursor.getObject(), dependentTypes);
209                 if (!cursor.toNextSibling())
210                     break;
211             }
212         }
213         if (nodeInWSDL.schemaType().getName().getLocalPart().equals(
214                 "localElement")) {
215             LocalElement e = (LocalElement) nodeInWSDL;
216
217             if (e.isSetType())
218                 dependentTypes.add(e.getType());
219             else if (e.isSetRef())
220                 dependentTypes.add(e.getRef());
221         }
222         return;
223     }
224
225     // NOTE jcolwell@bea.com 2004-Nov-15 --
226
// once the WSDLProcessor is changed to an interface, remove this function
227
// and use the one in the upcoming XmlBeanWSDLProcessor.
228
private static <T extends XmlObject> T[] selectChildren(XmlObject parent,
229             Class JavaDoc<T> childClass) throws IllegalAccessException JavaDoc,
230             NoSuchFieldException JavaDoc {
231         // retrieve the SchemaType from the static type field
232
SchemaType st = (SchemaType) childClass.getField("type").get(null);
233         XmlObject[] kids = parent.selectChildren(st.getDocumentElementName());
234         T[] castKids = (T[]) Array.newInstance(childClass, kids.length);
235         for (int j = 0; j < castKids.length; j++) {
236             castKids[j] = childClass.cast(kids[j]);
237         }
238         return castKids;
239     }
240 }
241
Popular Tags