1 21 22 package org.apache.axis.encoding.ser.xbeans; 23 24 import org.apache.axis.Constants; 25 import org.apache.axis.encoding.SerializationContext; 26 import org.apache.axis.encoding.Serializer; 27 import org.apache.axis.wsdl.fromJava.Types; 28 import org.apache.xmlbeans.SchemaType; 29 import org.apache.xmlbeans.XmlBeans; 30 import org.apache.xmlbeans.XmlCursor; 31 import org.apache.xmlbeans.XmlObject; 32 import org.apache.xmlbeans.XmlOptions; 33 import org.w3.x2001.xmlSchema.SchemaDocument; 34 import org.w3.x2001.xmlSchema.TopLevelComplexType; 35 import org.w3.x2001.xmlSchema.TopLevelElement; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 import org.xml.sax.Attributes ; 40 import org.xmlsoap.schemas.wsdl.DefinitionsDocument; 41 import org.xmlsoap.schemas.wsdl.TDefinitions; 42 import org.xmlsoap.schemas.wsdl.TTypes; 43 44 import javax.xml.namespace.QName ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.util.HashSet ; 48 import java.util.Set ; 49 50 54 public class XmlBeanSerializer implements Serializer { 55 56 65 public void serialize(QName name, Attributes attributes, 66 Object value, SerializationContext context) 67 throws IOException { 68 if (!(value instanceof XmlObject)) { 69 throw new IOException (((value != null) ? 70 value.getClass().getName() 71 : "null") 72 + " is not an " 73 + XmlObject.class.getName()); 74 } else { 75 context.setWriteXMLType(null); 76 context.startElement(name, attributes); 77 XmlCursor xCur = ((XmlObject) value).newCursor(); 78 if (xCur.toFirstContentToken() == XmlCursor.TokenType.START) { 79 do { 80 Node n = xCur.getDomNode(); 81 if (n.getNodeType() == Node.ELEMENT_NODE) { 82 context.writeDOMElement((Element) n); 83 } 84 } while (xCur.toNextSibling()); 85 } 86 context.endElement(); 87 } 88 } 89 90 public String getMechanismType() { 91 return Constants.AXIS_SAX; 92 } 93 94 105 public Element writeSchema(Class javaType, Types types) throws Exception { 106 if (XmlObject.class.isAssignableFrom(javaType)) { 107 SchemaType docType = XmlBeans.typeForClass(javaType); 108 109 118 119 Document doc = types.createElement("deleteme") 120 .getOwnerDocument(); 121 XmlOptions opts = new XmlOptions() 122 .setLoadReplaceDocumentElement(null); 123 Element root = doc.getDocumentElement(); 124 String schemaSrc = docType.getSourceName(); 125 InputStream stream = docType.getTypeSystem() 126 .getSourceAsStream(schemaSrc); 127 SchemaDocument.Schema schema = null; 128 if (schemaSrc.endsWith(".wsdl") || schemaSrc.endsWith(".WSDL")) { 129 DefinitionsDocument defDoc = 130 DefinitionsDocument.Factory.parse(stream); 131 TTypes tt = defDoc.getDefinitions().getTypesArray(0); 132 XmlObject[] kids = selectChildren 133 (tt, SchemaDocument.Schema.class); 134 SchemaDocument.Schema[] schemas = 135 new SchemaDocument.Schema[kids.length]; 136 137 141 for (int j = 0; j < kids.length; j++) { 142 schemas[j] = (SchemaDocument.Schema) kids[j]; 143 } 144 if (schemas.length == 1) { 145 schema = schemas[0]; 146 } else { 147 String stNS = docType.getName().getNamespaceURI(); 148 for (int j = 0; j < schemas.length; j++) { 149 if (stNS.equals(schemas[j].getTargetNamespace())) { 150 schema = schemas[j]; 151 break; 152 } 153 } 154 } 155 } else { 156 SchemaDocument schemaDoc = SchemaDocument.Factory.parse(stream); 157 schema = schemaDoc.getSchema(); 158 } 159 160 166 DefinitionsDocument defDoc = DefinitionsDocument.Factory 167 .newInstance(); 168 TDefinitions definitions = defDoc.addNewDefinitions(); 169 definitions.addNewService(); 170 Node defEl = definitions.newDomNode(new XmlOptions() 171 .setSaveOuter()); 172 Document dDoc = defEl.getOwnerDocument(); 173 if (null == dDoc.getDocumentElement()) { 174 dDoc.appendChild(defEl); 175 } 176 Set existingNameSpaces = new HashSet (); 177 if (dDoc != null) { 178 types.insertTypesFragment(dDoc); 179 Element e = (Element) dDoc.getFirstChild().getFirstChild() 180 .getFirstChild(); 181 if (e != null) { 182 String tn = e.getAttribute("targetNamespace"); 183 existingNameSpaces.add(tn); 184 while (null != (e = (Element) e.getNextSibling())) { 185 tn = e.getAttribute("targetNamespace"); 186 existingNameSpaces.add(tn); 187 } 188 } 189 } else { 190 throw new Exception ("null document"); 191 } 192 if (schema != null) { 193 String targetNamespace = schema.getTargetNamespace(); 194 if (targetNamespace != null) { 195 if (!existingNameSpaces.contains(targetNamespace)) { 196 TopLevelComplexType[] schemaTypes = schema 197 .getComplexTypeArray(); 198 for (int j = 0; j < schemaTypes.length; j++) { 199 types.writeSchemaElement(targetNamespace, 200 (Element) doc 201 .importNode(schemaTypes[j].newDomNode() 202 .getFirstChild(), 203 true)); 204 } 205 TopLevelElement[] elements = schema 206 .getElementArray(); 207 for (int j = 0; j < elements.length; j++) { 208 types.writeSchemaElement(targetNamespace, 209 (Element) doc 210 .importNode(elements[j].newDomNode() 211 .getFirstChild(), 212 true)); 213 } 214 } 215 return null; 216 } 217 } 218 throw new Exception (javaType.getName() 219 + "did not specify a target namespace"); 220 } else { 221 throw new Exception (javaType.getName() 222 + " must be a subclass of XmlObject"); 223 } 224 } 225 226 private static XmlObject[] selectChildren(XmlObject parent, 230 Class childClass) 231 throws IllegalAccessException , NoSuchFieldException { 232 SchemaType st = (SchemaType) childClass.getField("type").get(null); 234 return parent.selectChildren(st.getDocumentElementName()); 235 } 236 } 237 | Popular Tags |