1 package org.apache.axis2.wsdl.codegen.extension; 2 3 import org.apache.axis2.wsdl.codegen.CodeGenConfiguration; 4 import org.apache.axis2.wsdl.databinding.JavaTypeMapper; 5 import org.apache.wsdl.WSDLExtensibilityElement; 6 import org.apache.wsdl.WSDLTypes; 7 import org.apache.wsdl.extensions.ExtensionConstants; 8 import org.apache.wsdl.extensions.Schema; 9 import org.apache.xmlbeans.*; 10 import org.w3c.dom.Element ; 11 12 import java.io.*; 13 import java.util.List ; 14 15 32 public class XMLBeansExtension extends AbstractCodeGenerationExtension implements CodeGenExtension { 33 private static final String DEFUALT_STS_NAME = "foo"; 34 35 36 public void init(CodeGenConfiguration configuration) { 37 this.configuration = configuration; 38 } 39 40 public void engage() { 41 WSDLTypes typesList = configuration.getWom().getTypes(); 42 if (typesList==null){ 43 return; 45 } 46 List typesArray = typesList.getExtensibilityElements(); 47 WSDLExtensibilityElement extensiblityElt = null; 48 XmlObject[] xmlObjects=new XmlObject[typesArray.size()]; 49 50 for (int i = 0; i < typesArray.size(); i++) { 51 extensiblityElt = (WSDLExtensibilityElement)typesArray.get(i); 52 if (ExtensionConstants.SCHEMA.equals(extensiblityElt.getType())) { 53 try { 54 Element schemaElement = ((Schema)extensiblityElt).getElelment(); 55 XmlOptions options = new XmlOptions(); 57 options.setLoadAdditionalNamespaces(configuration.getWom().getNamespaces()); 58 xmlObjects[i] = XmlObject.Factory.parse(schemaElement,options); 60 } catch (XmlException e) { 61 throw new RuntimeException (e); 62 } 63 } 64 } 65 66 final File outputFolder = configuration.getOutputLocation(); 67 68 try { 69 SchemaTypeSystem sts = XmlBeans.compileXmlBeans(DEFUALT_STS_NAME, null, 70 xmlObjects, 71 new BindingConfig(), XmlBeans.getContextTypeLoader(), 72 new Filer() { 73 public OutputStream createBinaryFile(String typename) 74 throws IOException { 75 File file = new File(outputFolder,typename); 76 file.getParentFile().mkdirs(); 77 file.createNewFile(); 78 return new FileOutputStream(file); 79 } 80 81 public Writer createSourceFile(String typename) 82 throws IOException { 83 typename = typename.replace('.',File.separatorChar); 84 File file = new File(outputFolder,typename+".java"); 85 file.getParentFile().mkdirs(); 86 file.createNewFile(); 87 return new FileWriter(file); 88 } 89 }, null); 90 91 JavaTypeMapper mapper = new JavaTypeMapper(); 93 SchemaType[] types= sts.documentTypes(); 94 95 for (int i= 0; i < types.length; i++){ 96 mapper.addTypeMapping(types[i].getDocumentElementName(), types[i].getFullJavaName()); 98 } 99 configuration.setTypeMapper(mapper); 101 102 } catch (XmlException e) { 103 throw new RuntimeException (e); 104 } 105 } 106 107 108 109 } 110 | Popular Tags |