KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > processors > xsd2 > XSDToWSDLProcessor


1 package org.objectweb.celtix.tools.processors.xsd2;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.Writer JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import javax.wsdl.Definition;
10 import javax.wsdl.Types;
11 import javax.wsdl.WSDLException;
12 import javax.wsdl.extensions.ExtensibilityElement;
13 import javax.wsdl.extensions.ExtensionRegistry;
14 import javax.wsdl.extensions.schema.Schema;
15 import javax.wsdl.factory.WSDLFactory;
16 import javax.wsdl.xml.WSDLWriter;
17 import javax.xml.namespace.QName JavaDoc;
18
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21
22 import org.objectweb.celtix.common.i18n.Message;
23 import org.objectweb.celtix.common.logging.LogUtils;
24 import org.objectweb.celtix.tools.common.Processor;
25 import org.objectweb.celtix.tools.common.ProcessorEnvironment;
26 import org.objectweb.celtix.tools.common.ToolConstants;
27 import org.objectweb.celtix.tools.common.ToolException;
28 import org.objectweb.celtix.tools.common.WSDLConstants;
29 import org.objectweb.celtix.tools.common.dom.ExtendedDocumentBuilder;
30 import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBinding;
31 import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBindingDeserializer;
32 import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBindingSerializer;
33 import org.objectweb.celtix.tools.utils.FileWriterUtil;
34
35 public class XSDToWSDLProcessor implements Processor {
36     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(XSDToWSDLProcessor.class);
37     private static final String JavaDoc XSD_FILE_NAME_EXT = ".xsd";
38     private static final String JavaDoc WSDL_FILE_NAME_EXT = ".wsdl";
39     
40     private Definition wsdlDefinition;
41     private ExtensionRegistry registry;
42     private WSDLFactory wsdlFactory;
43
44     private String JavaDoc xsdUrl;
45     private final ExtendedDocumentBuilder xsdBuilder = new ExtendedDocumentBuilder();
46     private Document JavaDoc xsdDoc;
47     private ProcessorEnvironment env;
48
49     public void process() throws ToolException {
50         envParamSetting();
51         initXSD();
52         initWSDL();
53         addWSDLTypes();
54     }
55
56     public void setEnvironment(ProcessorEnvironment newEnv) {
57         this.env = newEnv;
58     }
59
60     private void envParamSetting() {
61         xsdUrl = (String JavaDoc)env.get(ToolConstants.CFG_XSDURL);
62
63         if (!env.optionSet(ToolConstants.CFG_NAME)) {
64             env.put(ToolConstants.CFG_NAME, xsdUrl.substring(0, xsdUrl.length() - 4));
65         }
66     }
67
68     private void initWSDL() throws ToolException {
69         try {
70             wsdlFactory = WSDLFactory.newInstance();
71             wsdlDefinition = wsdlFactory.newDefinition();
72         } catch (WSDLException we) {
73             Message msg = new Message("FAIL_TO_CREATE_WSDL_DEFINITION", LOG);
74             throw new ToolException(msg, we);
75         }
76     }
77
78     private void initXSD() throws ToolException {
79         InputStream JavaDoc in;
80         try {
81             in = new FileInputStream JavaDoc(xsdUrl);
82         } catch (IOException JavaDoc ioe) {
83             Message msg = new Message("FAIL_TO_OPEN_XSD_FILE", LOG);
84             throw new ToolException(msg, ioe);
85         }
86         if (in == null) {
87             throw new NullPointerException JavaDoc("Cannot create a ToolSpec object from a null stream");
88         }
89         try {
90             xsdBuilder.setValidating(false);
91             this.xsdDoc = xsdBuilder.parse(in);
92         } catch (Exception JavaDoc ex) {
93             Message msg = new Message("FAIL_TO_PARSE_TOOLSPEC", LOG);
94             throw new ToolException(msg, ex);
95         }
96     }
97
98     private void addWSDLTypes() throws ToolException {
99
100         Element sourceElement = this.xsdDoc.getDocumentElement();
101         Element targetElement = (Element)sourceElement.cloneNode(true);
102
103         this.wsdlDefinition.setTargetNamespace((String JavaDoc)env.get(ToolConstants.CFG_NAMESPACE));
104         this.wsdlDefinition
105             .setQName(new QName JavaDoc(WSDLConstants.NS_WSDL, (String JavaDoc)env.get(ToolConstants.CFG_NAME)));
106
107         Types types = this.wsdlDefinition.createTypes();
108         ExtensibilityElement extElement;
109         try {
110             registry = wsdlFactory.newPopulatedExtensionRegistry();
111             registerJAXWSBinding(Definition.class);
112             registerJAXWSBinding(Types.class);
113             registerJAXWSBinding(Schema.class);
114             extElement = registry.createExtension(Types.class, new QName JavaDoc(WSDLConstants.XSD_NAMESPACE,
115                                                                          "schema"));
116         } catch (WSDLException wse) {
117             Message msg = new Message("FAIL_TO_CREATE_SCHEMA_EXTENSION", LOG);
118             throw new ToolException(msg, wse);
119         }
120         ((Schema)extElement).setElement(targetElement);
121         types.addExtensibilityElement(extElement);
122         this.wsdlDefinition.setTypes(types);
123
124         WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
125         Writer JavaDoc outputWriter = getOutputWriter();
126
127         try {
128             wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
129         } catch (WSDLException wse) {
130             Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
131             throw new ToolException(msg, wse);
132         }
133         try {
134             outputWriter.close();
135         } catch (IOException JavaDoc ioe) {
136             Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG);
137             throw new ToolException(msg, ioe);
138         }
139     }
140
141     private void registerJAXWSBinding(Class JavaDoc clz) {
142         registry.registerSerializer(clz, ToolConstants.JAXWS_BINDINGS, new JAXWSBindingSerializer());
143
144         registry.registerDeserializer(clz, ToolConstants.JAXWS_BINDINGS, new JAXWSBindingDeserializer());
145         registry.mapExtensionTypes(clz, ToolConstants.JAXWS_BINDINGS, JAXWSBinding.class);
146     }
147     
148     private Writer JavaDoc getOutputWriter() throws ToolException {
149         Writer JavaDoc writer = null;
150         String JavaDoc newName = null;
151         String JavaDoc outputDir;
152
153         if (env.get(ToolConstants.CFG_OUTPUTFILE) != null) {
154             newName = (String JavaDoc)env.get(ToolConstants.CFG_OUTPUTFILE);
155         } else {
156             String JavaDoc oldName = (String JavaDoc)env.get(ToolConstants.CFG_XSDURL);
157             int position = oldName.lastIndexOf("/");
158             if (position < 0) {
159                 position = oldName.lastIndexOf("\\");
160             }
161             if (position >= 0) {
162                 oldName = oldName.substring(position + 1, oldName.length());
163             }
164             if (oldName.toLowerCase().indexOf(XSD_FILE_NAME_EXT) >= 0) {
165                 newName = oldName.substring(0, oldName.length() - 4) + WSDL_FILE_NAME_EXT;
166             } else {
167                 newName = oldName + WSDL_FILE_NAME_EXT;
168             }
169         }
170         if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) {
171             outputDir = (String JavaDoc)env.get(ToolConstants.CFG_OUTPUTDIR);
172             if (!("/".equals(outputDir.substring(outputDir.length() - 1))
173                   || "\\".equals(outputDir.substring(outputDir.length() - 1)))) {
174                 outputDir = outputDir + "/";
175             }
176         } else {
177             outputDir = "./";
178         }
179         FileWriterUtil fw = new FileWriterUtil(outputDir);
180         try {
181             writer = fw.getWriter("", newName);
182         } catch (IOException JavaDoc ioe) {
183             Message msg = new Message("FAIl_TO_WRITE_FILE", LOG, env.get(ToolConstants.CFG_OUTPUTDIR)
184                                     + System.getProperty("file.seperator") + newName);
185             throw new ToolException(msg, ioe);
186         }
187         return writer;
188     }
189
190 }
191
Popular Tags