1 20 package org.apache.beehive.wsm.axis.util.encoding; 21 22 import java.io.IOException ; 23 import java.lang.reflect.Array ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 27 import javax.xml.namespace.QName ; 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 ; 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.Node ; 47 import org.w3c.dom.NodeList ; 48 import org.xml.sax.Attributes ; 49 50 55 public class XmlBeanSerializer implements Serializer { 56 57 69 public void serialize(QName name, Attributes attributes, Object value, 70 SerializationContext context) throws IOException { 71 if (!(value instanceof XmlObject)) { 72 throw new IOException (((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 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 getMechanismType() { 94 return Constants.AXIS_SAX; 95 } 96 97 110 public Element writeSchema(Class javaType, Types types) throws Exception { 111 try { 112 if (!XmlObject.class.isAssignableFrom(javaType)) { 113 throw new RuntimeException ( 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 return null; 123 } catch (Exception e) { 124 e.printStackTrace(); 125 throw e; 126 } 127 } 128 129 135 private void writeSchemaForDocType(SchemaType docType, Types types) 136 throws Exception { 137 Schema mySchema = Utilities.findtSchemaDocument(docType); 138 139 QName q = docType.getName(); 140 141 XmlObject typeNodeInWSDL = mySchema.getTypeNode(q); 142 143 if (null == typeNodeInWSDL) 144 throw new RuntimeException ( 145 "Type for object not found in the assigned WSDL file. " 146 + docType.getName() + " schema in: " 147 + docType.getSourceName()); 148 Node n = typeNodeInWSDL.getDomNode(); 150 Document 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 } 160 Set <QName > dependentTypes = new HashSet <QName >(); 161 getAllDependentTypes(typeNodeInWSDL, dependentTypes); 162 for (QName nxtType : dependentTypes) { 163 Class nxtJavaType = null; 164 if (null != (nxtJavaType = q2UserClass(nxtType)) 166 && XmlObject.class.isAssignableFrom(nxtJavaType)) { 167 writeSchema(nxtJavaType, types); 168 } 169 } 170 return; 171 } 172 173 178 private Class q2UserClass(QName 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; 192 } 193 194 202 private void getAllDependentTypes(XmlObject nodeInWSDL, 203 Set <QName > dependentTypes) { 204 XmlCursor cursor = nodeInWSDL.newCursor(); 206 if (cursor.toFirstChild()) { 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 private static <T extends XmlObject> T[] selectChildren(XmlObject parent, 229 Class <T> childClass) throws IllegalAccessException , 230 NoSuchFieldException { 231 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 |