001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.misc.processor;
019:
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.Writer;
024: import java.util.logging.Logger;
025:
026: import javax.wsdl.Definition;
027: import javax.wsdl.Types;
028: import javax.wsdl.WSDLException;
029: import javax.wsdl.extensions.ExtensibilityElement;
030: import javax.wsdl.extensions.ExtensionRegistry;
031: import javax.wsdl.extensions.schema.Schema;
032: import javax.wsdl.factory.WSDLFactory;
033: import javax.wsdl.xml.WSDLWriter;
034: import javax.xml.namespace.QName;
035:
036: import org.w3c.dom.Document;
037: import org.w3c.dom.Element;
038:
039: import org.apache.cxf.common.i18n.Message;
040: import org.apache.cxf.common.logging.LogUtils;
041: import org.apache.cxf.tools.common.Processor;
042: import org.apache.cxf.tools.common.ToolConstants;
043: import org.apache.cxf.tools.common.ToolContext;
044: import org.apache.cxf.tools.common.ToolException;
045: import org.apache.cxf.tools.common.dom.ExtendedDocumentBuilder;
046: import org.apache.cxf.tools.util.FileWriterUtil;
047: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBinding;
048: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBindingDeserializer;
049: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBindingSerializer;
050: import org.apache.cxf.wsdl.WSDLConstants;
051:
052: public class XSDToWSDLProcessor implements Processor {
053: private static final Logger LOG = LogUtils
054: .getL7dLogger(XSDToWSDLProcessor.class);
055: private static final String XSD_FILE_NAME_EXT = ".xsd";
056: private static final String WSDL_FILE_NAME_EXT = ".wsdl";
057:
058: private Definition wsdlDefinition;
059: private ExtensionRegistry registry;
060: private WSDLFactory wsdlFactory;
061:
062: private String xsdUrl;
063: private final ExtendedDocumentBuilder xsdBuilder = new ExtendedDocumentBuilder();
064: private Document xsdDoc;
065: private ToolContext env;
066:
067: public void process() throws ToolException {
068: envParamSetting();
069: initXSD();
070: initWSDL();
071: addWSDLTypes();
072: }
073:
074: public void setEnvironment(ToolContext newEnv) {
075: this .env = newEnv;
076: }
077:
078: private void envParamSetting() {
079: xsdUrl = (String) env.get(ToolConstants.CFG_XSDURL);
080:
081: if (!env.optionSet(ToolConstants.CFG_NAME)) {
082: env.put(ToolConstants.CFG_NAME, xsdUrl.substring(0, xsdUrl
083: .length() - 4));
084: }
085: }
086:
087: private void initWSDL() throws ToolException {
088: try {
089: wsdlFactory = WSDLFactory.newInstance();
090: wsdlDefinition = wsdlFactory.newDefinition();
091: } catch (WSDLException we) {
092: Message msg = new Message("FAIL_TO_CREATE_WSDL_DEFINITION",
093: LOG);
094: throw new ToolException(msg, we);
095: }
096: }
097:
098: private void initXSD() throws ToolException {
099: InputStream in;
100: try {
101: in = new FileInputStream(xsdUrl);
102: } catch (IOException ioe) {
103: Message msg = new Message("FAIL_TO_OPEN_XSD_FILE", LOG,
104: xsdUrl);
105: throw new ToolException(msg, ioe);
106: }
107: if (in == null) {
108: throw new NullPointerException(
109: "Cannot create a ToolSpec object from a null stream");
110: }
111: try {
112: xsdBuilder.setValidating(false);
113: this .xsdDoc = xsdBuilder.parse(in);
114: } catch (Exception ex) {
115: Message msg = new Message("FAIL_TO_PARSE_TOOLSPEC", LOG);
116: throw new ToolException(msg, ex);
117: }
118: }
119:
120: private void addWSDLTypes() throws ToolException {
121:
122: Element sourceElement = this .xsdDoc.getDocumentElement();
123: Element targetElement = (Element) sourceElement.cloneNode(true);
124:
125: this .wsdlDefinition.setTargetNamespace((String) env
126: .get(ToolConstants.CFG_NAMESPACE));
127: this .wsdlDefinition.setQName(new QName(
128: WSDLConstants.WSDL11_NAMESPACE, (String) env
129: .get(ToolConstants.CFG_NAME)));
130:
131: Types types = this .wsdlDefinition.createTypes();
132: ExtensibilityElement extElement;
133: try {
134: registry = wsdlFactory.newPopulatedExtensionRegistry();
135: registerJAXWSBinding(Definition.class);
136: registerJAXWSBinding(Types.class);
137: registerJAXWSBinding(Schema.class);
138: extElement = registry.createExtension(Types.class,
139: WSDLConstants.SCHEMA_QNAME);
140: } catch (WSDLException wse) {
141: Message msg = new Message(
142: "FAIL_TO_CREATE_SCHEMA_EXTENSION", LOG);
143: throw new ToolException(msg, wse);
144: }
145: ((Schema) extElement).setElement(targetElement);
146: types.addExtensibilityElement(extElement);
147: this .wsdlDefinition.setTypes(types);
148:
149: WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
150: Writer outputWriter = getOutputWriter();
151:
152: try {
153: wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
154: } catch (WSDLException wse) {
155: Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
156: throw new ToolException(msg, wse);
157: }
158: try {
159: outputWriter.close();
160: } catch (IOException ioe) {
161: Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG);
162: throw new ToolException(msg, ioe);
163: }
164: }
165:
166: private void registerJAXWSBinding(Class clz) {
167: registry.registerSerializer(clz, ToolConstants.JAXWS_BINDINGS,
168: new JAXWSBindingSerializer());
169:
170: registry.registerDeserializer(clz,
171: ToolConstants.JAXWS_BINDINGS,
172: new JAXWSBindingDeserializer());
173: registry.mapExtensionTypes(clz, ToolConstants.JAXWS_BINDINGS,
174: JAXWSBinding.class);
175: }
176:
177: private Writer getOutputWriter() throws ToolException {
178: Writer writer = null;
179: String newName = null;
180: String outputDir;
181:
182: if (env.get(ToolConstants.CFG_OUTPUTFILE) != null) {
183: newName = (String) env.get(ToolConstants.CFG_OUTPUTFILE);
184: } else {
185: String oldName = (String) env.get(ToolConstants.CFG_XSDURL);
186: int position = oldName.lastIndexOf("/");
187: if (position < 0) {
188: position = oldName.lastIndexOf("\\");
189: }
190: if (position >= 0) {
191: oldName = oldName.substring(position + 1, oldName
192: .length());
193: }
194: if (oldName.toLowerCase().indexOf(XSD_FILE_NAME_EXT) >= 0) {
195: newName = oldName.substring(0, oldName.length() - 4)
196: + WSDL_FILE_NAME_EXT;
197: } else {
198: newName = oldName + WSDL_FILE_NAME_EXT;
199: }
200: }
201: if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) {
202: outputDir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
203: if (!("/".equals(outputDir
204: .substring(outputDir.length() - 1)) || "\\"
205: .equals(outputDir.substring(outputDir.length() - 1)))) {
206: outputDir = outputDir + "/";
207: }
208: } else {
209: outputDir = "./";
210: }
211: FileWriterUtil fw = new FileWriterUtil(outputDir);
212: try {
213: writer = fw.getWriter("", newName);
214: } catch (IOException ioe) {
215: Message msg = new Message("FAIL_TO_WRITE_FILE", LOG, env
216: .get(ToolConstants.CFG_OUTPUTDIR)
217: + System.getProperty("file.seperator") + newName);
218: throw new ToolException(msg, ioe);
219: }
220: return writer;
221: }
222:
223: }
|