KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > wsdl > codegen > extension > XMLBeansExtension


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 JavaDoc;
11
12 import java.io.*;
13 import java.util.List JavaDoc;
14
15 /*
16 * Copyright 2004,2005 The Apache Software Foundation.
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 *
30 *
31 */

32 public class XMLBeansExtension extends AbstractCodeGenerationExtension implements CodeGenExtension {
33     private static final String JavaDoc 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             //there are no types to be code generated
44
return;
45         }
46         List JavaDoc 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 // //add the namespaces
56
XmlOptions options = new XmlOptions();
57                     options.setLoadAdditionalNamespaces(configuration.getWom().getNamespaces());
58                     //options.
59
xmlObjects[i] = XmlObject.Factory.parse(schemaElement,options);
60                 } catch (XmlException e) {
61                     throw new RuntimeException JavaDoc(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 JavaDoc 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 JavaDoc 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             //create the type mapper
92
JavaTypeMapper mapper = new JavaTypeMapper();
93             SchemaType[] types= sts.documentTypes();
94
95             for (int i= 0; i < types.length; i++){
96                 //System.out.println("type name = " + types[i].getFullJavaImplName()+" "+types[i].getDocumentElementName());
97
mapper.addTypeMapping(types[i].getDocumentElementName(), types[i].getFullJavaName());
98             }
99             //set the type mapper to the config
100
configuration.setTypeMapper(mapper);
101
102         } catch (XmlException e) {
103             throw new RuntimeException JavaDoc(e);
104         }
105     }
106
107
108
109 }
110
Popular Tags