001: package org.objectweb.celtix.tools.processors.xsd2;
002:
003: import java.io.FileInputStream;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.Writer;
007: import java.util.logging.Logger;
008:
009: import javax.wsdl.Definition;
010: import javax.wsdl.Types;
011: import javax.wsdl.WSDLException;
012: import javax.wsdl.extensions.ExtensibilityElement;
013: import javax.wsdl.extensions.ExtensionRegistry;
014: import javax.wsdl.extensions.schema.Schema;
015: import javax.wsdl.factory.WSDLFactory;
016: import javax.wsdl.xml.WSDLWriter;
017: import javax.xml.namespace.QName;
018:
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021:
022: import org.objectweb.celtix.common.i18n.Message;
023: import org.objectweb.celtix.common.logging.LogUtils;
024: import org.objectweb.celtix.tools.common.Processor;
025: import org.objectweb.celtix.tools.common.ProcessorEnvironment;
026: import org.objectweb.celtix.tools.common.ToolConstants;
027: import org.objectweb.celtix.tools.common.ToolException;
028: import org.objectweb.celtix.tools.common.WSDLConstants;
029: import org.objectweb.celtix.tools.common.dom.ExtendedDocumentBuilder;
030: import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBinding;
031: import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBindingDeserializer;
032: import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBindingSerializer;
033: import org.objectweb.celtix.tools.utils.FileWriterUtil;
034:
035: public class XSDToWSDLProcessor implements Processor {
036: private static final Logger LOG = LogUtils
037: .getL7dLogger(XSDToWSDLProcessor.class);
038: private static final String XSD_FILE_NAME_EXT = ".xsd";
039: private static final String WSDL_FILE_NAME_EXT = ".wsdl";
040:
041: private Definition wsdlDefinition;
042: private ExtensionRegistry registry;
043: private WSDLFactory wsdlFactory;
044:
045: private String xsdUrl;
046: private final ExtendedDocumentBuilder xsdBuilder = new ExtendedDocumentBuilder();
047: private Document xsdDoc;
048: private ProcessorEnvironment env;
049:
050: public void process() throws ToolException {
051: envParamSetting();
052: initXSD();
053: initWSDL();
054: addWSDLTypes();
055: }
056:
057: public void setEnvironment(ProcessorEnvironment newEnv) {
058: this .env = newEnv;
059: }
060:
061: private void envParamSetting() {
062: xsdUrl = (String) env.get(ToolConstants.CFG_XSDURL);
063:
064: if (!env.optionSet(ToolConstants.CFG_NAME)) {
065: env.put(ToolConstants.CFG_NAME, xsdUrl.substring(0, xsdUrl
066: .length() - 4));
067: }
068: }
069:
070: private void initWSDL() throws ToolException {
071: try {
072: wsdlFactory = WSDLFactory.newInstance();
073: wsdlDefinition = wsdlFactory.newDefinition();
074: } catch (WSDLException we) {
075: Message msg = new Message("FAIL_TO_CREATE_WSDL_DEFINITION",
076: LOG);
077: throw new ToolException(msg, we);
078: }
079: }
080:
081: private void initXSD() throws ToolException {
082: InputStream in;
083: try {
084: in = new FileInputStream(xsdUrl);
085: } catch (IOException ioe) {
086: Message msg = new Message("FAIL_TO_OPEN_XSD_FILE", LOG);
087: throw new ToolException(msg, ioe);
088: }
089: if (in == null) {
090: throw new NullPointerException(
091: "Cannot create a ToolSpec object from a null stream");
092: }
093: try {
094: xsdBuilder.setValidating(false);
095: this .xsdDoc = xsdBuilder.parse(in);
096: } catch (Exception ex) {
097: Message msg = new Message("FAIL_TO_PARSE_TOOLSPEC", LOG);
098: throw new ToolException(msg, ex);
099: }
100: }
101:
102: private void addWSDLTypes() throws ToolException {
103:
104: Element sourceElement = this .xsdDoc.getDocumentElement();
105: Element targetElement = (Element) sourceElement.cloneNode(true);
106:
107: this .wsdlDefinition.setTargetNamespace((String) env
108: .get(ToolConstants.CFG_NAMESPACE));
109: this .wsdlDefinition.setQName(new QName(WSDLConstants.NS_WSDL,
110: (String) env.get(ToolConstants.CFG_NAME)));
111:
112: Types types = this .wsdlDefinition.createTypes();
113: ExtensibilityElement extElement;
114: try {
115: registry = wsdlFactory.newPopulatedExtensionRegistry();
116: registerJAXWSBinding(Definition.class);
117: registerJAXWSBinding(Types.class);
118: registerJAXWSBinding(Schema.class);
119: extElement = registry.createExtension(Types.class,
120: new QName(WSDLConstants.XSD_NAMESPACE, "schema"));
121: } catch (WSDLException wse) {
122: Message msg = new Message(
123: "FAIL_TO_CREATE_SCHEMA_EXTENSION", LOG);
124: throw new ToolException(msg, wse);
125: }
126: ((Schema) extElement).setElement(targetElement);
127: types.addExtensibilityElement(extElement);
128: this .wsdlDefinition.setTypes(types);
129:
130: WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
131: Writer outputWriter = getOutputWriter();
132:
133: try {
134: wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
135: } catch (WSDLException wse) {
136: Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
137: throw new ToolException(msg, wse);
138: }
139: try {
140: outputWriter.close();
141: } catch (IOException ioe) {
142: Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG);
143: throw new ToolException(msg, ioe);
144: }
145: }
146:
147: private void registerJAXWSBinding(Class clz) {
148: registry.registerSerializer(clz, ToolConstants.JAXWS_BINDINGS,
149: new JAXWSBindingSerializer());
150:
151: registry.registerDeserializer(clz,
152: ToolConstants.JAXWS_BINDINGS,
153: new JAXWSBindingDeserializer());
154: registry.mapExtensionTypes(clz, ToolConstants.JAXWS_BINDINGS,
155: JAXWSBinding.class);
156: }
157:
158: private Writer getOutputWriter() throws ToolException {
159: Writer writer = null;
160: String newName = null;
161: String outputDir;
162:
163: if (env.get(ToolConstants.CFG_OUTPUTFILE) != null) {
164: newName = (String) env.get(ToolConstants.CFG_OUTPUTFILE);
165: } else {
166: String oldName = (String) env.get(ToolConstants.CFG_XSDURL);
167: int position = oldName.lastIndexOf("/");
168: if (position < 0) {
169: position = oldName.lastIndexOf("\\");
170: }
171: if (position >= 0) {
172: oldName = oldName.substring(position + 1, oldName
173: .length());
174: }
175: if (oldName.toLowerCase().indexOf(XSD_FILE_NAME_EXT) >= 0) {
176: newName = oldName.substring(0, oldName.length() - 4)
177: + WSDL_FILE_NAME_EXT;
178: } else {
179: newName = oldName + WSDL_FILE_NAME_EXT;
180: }
181: }
182: if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) {
183: outputDir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
184: if (!("/".equals(outputDir
185: .substring(outputDir.length() - 1)) || "\\"
186: .equals(outputDir.substring(outputDir.length() - 1)))) {
187: outputDir = outputDir + "/";
188: }
189: } else {
190: outputDir = "./";
191: }
192: FileWriterUtil fw = new FileWriterUtil(outputDir);
193: try {
194: writer = fw.getWriter("", newName);
195: } catch (IOException ioe) {
196: Message msg = new Message("FAIl_TO_WRITE_FILE", LOG, env
197: .get(ToolConstants.CFG_OUTPUTDIR)
198: + System.getProperty("file.seperator") + newName);
199: throw new ToolException(msg, ioe);
200: }
201: return writer;
202: }
203:
204: }
|