package org.jboss.axis.wsdl.gen;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.wsdl.symbolTable.BindingEntry;
import org.jboss.axis.wsdl.symbolTable.CollectionElement;
import org.jboss.axis.wsdl.symbolTable.MessageEntry;
import org.jboss.axis.wsdl.symbolTable.PortTypeEntry;
import org.jboss.axis.wsdl.symbolTable.ServiceEntry;
import org.jboss.axis.wsdl.symbolTable.SymTabEntry;
import org.jboss.axis.wsdl.symbolTable.SymbolTable;
import org.jboss.axis.wsdl.symbolTable.Type;
import org.jboss.axis.wsdl.symbolTable.TypeEntry;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.wsdl.Binding;
import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
public class Parser
{
protected boolean debug = false;
protected boolean imports = true;
protected boolean verbose = false;
protected boolean nowrap = false;
protected String username = null;
protected String password = null;
private long timeoutms = 45000;
private GeneratorFactory genFactory = null;
private SymbolTable symbolTable = null;
public boolean isDebug()
{
return debug;
}
public void setDebug(boolean debug)
{
this.debug = debug;
}
public boolean isImports()
{
return imports;
}
public void setImports(boolean imports)
{
this.imports = imports;
}
public boolean isVerbose()
{
return verbose;
}
public void setVerbose(boolean verbose)
{
this.verbose = verbose;
}
public boolean isNowrap()
{
return nowrap;
}
public void setNowrap(boolean nowrap)
{
this.nowrap = nowrap;
}
public long getTimeout()
{
return timeoutms;
}
public void setTimeout(long timeout)
{
this.timeoutms = timeout;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public GeneratorFactory getFactory()
{
return genFactory;
}
public void setFactory(GeneratorFactory factory)
{
this.genFactory = factory;
}
public SymbolTable getSymbolTable()
{
return symbolTable;
}
public Definition getCurrentDefinition()
{
return symbolTable == null ? null : symbolTable.getDefinition();
}
public String getWSDLURI()
{
return symbolTable == null ? null : symbolTable.getWSDLURI();
}
public void run(String wsdlURI) throws Exception
{
if (getFactory() == null)
{
setFactory(new NoopFactory());
}
symbolTable = new SymbolTable(genFactory.getBaseTypeMapping(),
imports,
verbose,
nowrap);
WSDLRunnable runnable = new WSDLRunnable(symbolTable, wsdlURI);
Thread wsdlThread = new Thread(runnable);
wsdlThread.start();
try
{
if (timeoutms > 0)
wsdlThread.join(timeoutms);
else
wsdlThread.join();
}
catch (InterruptedException e)
{
}
if (wsdlThread.isAlive())
{
wsdlThread.interrupt();
throw new IOException(Messages.getMessage("timedOut"));
}
if (runnable.getFailure() != null)
{
throw runnable.getFailure();
}
}
private class WSDLRunnable implements Runnable
{
private SymbolTable symbolTable;
private String wsdlURI;
private Exception failure = null;
public WSDLRunnable(SymbolTable symbolTable, String wsdlURI)
{
this.symbolTable = symbolTable;
this.wsdlURI = wsdlURI;
}
public void run()
{
try
{
symbolTable.populate(wsdlURI, username, password);
generate(symbolTable);
}
catch (Exception e)
{
failure = e;
}
}
public Exception getFailure()
{
return failure;
} }
public void run(String context, Document doc)
throws IOException, SAXException, WSDLException,
ParserConfigurationException
{
if (getFactory() == null)
{
setFactory(new NoopFactory());
}
symbolTable = new SymbolTable(genFactory.getBaseTypeMapping(),
imports,
verbose,
nowrap);
symbolTable.populate(context, doc);
generate(symbolTable);
}
protected void sanityCheck(SymbolTable symbolTable)
{
}
private void generate(SymbolTable symbolTable) throws IOException
{
sanityCheck(symbolTable);
Definition def = symbolTable.getDefinition();
genFactory.generatorPass(def, symbolTable);
if (isDebug())
{
symbolTable.dump(System.out);
}
generateTypes(symbolTable);
Iterator it = symbolTable.getHashMap().values().iterator();
while (it.hasNext())
{
Vector v = (Vector)it.next();
for (int i = 0; i < v.size(); ++i)
{
SymTabEntry entry = (SymTabEntry)v.elementAt(i);
Generator gen = null;
if (entry instanceof MessageEntry)
{
gen = genFactory.getGenerator(((MessageEntry)entry).getMessage(), symbolTable);
}
else if (entry instanceof PortTypeEntry)
{
PortTypeEntry pEntry = (PortTypeEntry)entry;
if (pEntry.getPortType().isUndefined())
{
continue;
}
gen = genFactory.getGenerator(pEntry.getPortType(), symbolTable);
}
else if (entry instanceof BindingEntry)
{
BindingEntry bEntry = (BindingEntry)entry;
Binding binding = bEntry.getBinding();
if (binding.isUndefined() || !bEntry.isReferenced())
{
continue;
}
gen = genFactory.getGenerator(binding, symbolTable);
}
else if (entry instanceof ServiceEntry)
{
gen = genFactory.getGenerator(((ServiceEntry)entry).getService(), symbolTable);
}
if (gen != null)
{
gen.generate();
}
}
}
Generator gen = genFactory.getGenerator(def, symbolTable);
gen.generate();
}
private void generateTypes(SymbolTable symbolTable) throws IOException
{
Vector types = symbolTable.getTypes();
for (int i = 0; i < types.size(); ++i)
{
TypeEntry type = (TypeEntry)types.elementAt(i);
boolean isType = (type instanceof Type ||
type instanceof CollectionElement);
if (type.getNode() != null &&
type.isReferenced() &&
isType &&
type.getBaseType() == null)
{
Generator gen = genFactory.getGenerator(type, symbolTable);
gen.generate();
}
}
}
}