/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.yoko.tools.processors.idl;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Writer;
import java.util.StringTokenizer;
import javax.wsdl.Binding;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.Service;
import javax.xml.namespace.QName;
import antlr.collections.AST;
import org.apache.cxf.tools.common.Processor;
import org.apache.cxf.tools.common.ToolConstants;
import org.apache.cxf.tools.common.ToolContext;
import org.apache.cxf.tools.common.ToolException;
import org.apache.cxf.tools.util.FileWriterUtil;
import org.apache.schemas.yoko.bindings.corba.AddressType;
import org.apache.yoko.tools.common.ProcessorEnvironment;
import org.apache.yoko.tools.common.ToolCorbaConstants;
import org.apache.yoko.wsdl.CorbaConstants;
public class IDLToWSDLProcessor implements Processor {
protected ToolContext toolContext;
private String idl;
private ProcessorEnvironment env;
private Writer outputWriter;
public void process() throws ToolException {
idl = getBaseFilename(env.get(ToolCorbaConstants.CFG_IDLFILE).toString());
try {
parseIDL();
} catch (Exception e) {
throw new ToolException(e);
}
}
public void setEnvironment(ToolContext toolCtx) {
toolContext = toolCtx;
}
public void setOutputWriter(Writer writer) {
outputWriter = writer;
}
public void parseIDL() throws Exception {
InputStream stream = new FileInputStream(env.get(ToolCorbaConstants.CFG_IDLFILE).toString());
IDLParser parser = new IDLParser(new IDLLexer(stream));
parser.specification();
AST idlTree = parser.getAST();
System.out.println(idlTree.toStringTree());
Object obj = env.get(ToolConstants.CFG_OUTPUTDIR);
String outputDir = ".";
if (obj != null) {
outputDir = obj.toString();
}
String tns = (String) env.get(ToolCorbaConstants.CFG_TNS);
if (tns == null) {
tns = CorbaConstants.WSDL_NS_URI + idl;
}
try {
WSDLASTVisitor visitor = new WSDLASTVisitor(tns);
visitor.visit(idlTree);
if (outputWriter == null) {
FileWriterUtil fw = new FileWriterUtil(outputDir);
outputWriter = fw.getWriter("", idl + ".wsdl");
}
Definition def = visitor.getDefinition();
Binding[] bindings = visitor.getCorbaBindings();
generateCORBAService(def, bindings);
visitor.writeDefinition(outputWriter);
} catch (Exception ex) {
throw new ToolException(ex.getMessage(), ex);
}
}
public void setEnvironment(ProcessorEnvironment penv) {
env = penv;
}
public ProcessorEnvironment getEnvironment() {
return env;
}
public String getBaseFilename(String ifile) {
String fileName = ifile;
StringTokenizer token = new StringTokenizer(ifile, "\\/");
while (token.hasMoreTokens()) {
fileName = token.nextToken();
}
if (fileName.endsWith(".idl")) {
fileName = new String(fileName.substring(0, fileName.length() - 4));
}
return fileName;
}
public void generateCORBAService(Definition def, Binding[] bindings) throws Exception {
for (int i = 0; i < bindings.length; i++) {
String portTypeName = bindings[i].getPortType().getQName().getLocalPart();
QName serviceName = new QName(def.getTargetNamespace(),
portTypeName + "CORBAService");
Service service = def.createService();
service.setQName(serviceName);
Port port = def.createPort();
port.setName(portTypeName + "CORBAPort");
AddressType address =
(AddressType) def.getExtensionRegistry().createExtension(Port.class,
CorbaConstants.NE_CORBA_ADDRESS);
String addr = (String) env.get(ToolCorbaConstants.CFG_ADDRESS);
if (addr == null) {
addr = "IOR:";
}
address.setLocation(addr);
port.addExtensibilityElement(address);
service.addPort(port);
port.setBinding(bindings[i]);
def.addService(service);
}
}
}