KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > wsdl > gen > Parser


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.wsdl.gen;
56
57 import org.jboss.axis.utils.Messages;
58 import org.jboss.axis.wsdl.symbolTable.BindingEntry;
59 import org.jboss.axis.wsdl.symbolTable.CollectionElement;
60 import org.jboss.axis.wsdl.symbolTable.MessageEntry;
61 import org.jboss.axis.wsdl.symbolTable.PortTypeEntry;
62 import org.jboss.axis.wsdl.symbolTable.ServiceEntry;
63 import org.jboss.axis.wsdl.symbolTable.SymTabEntry;
64 import org.jboss.axis.wsdl.symbolTable.SymbolTable;
65 import org.jboss.axis.wsdl.symbolTable.Type;
66 import org.jboss.axis.wsdl.symbolTable.TypeEntry;
67 import org.w3c.dom.Document JavaDoc;
68 import org.xml.sax.SAXException JavaDoc;
69
70 import javax.wsdl.Binding;
71 import javax.wsdl.Definition;
72 import javax.wsdl.WSDLException;
73 import javax.xml.parsers.ParserConfigurationException JavaDoc;
74 import java.io.IOException JavaDoc;
75 import java.util.Iterator JavaDoc;
76 import java.util.Vector JavaDoc;
77
78 /**
79  * This is a class with no documentation.
80  */

81 public class Parser
82 {
83
84    protected boolean debug = false;
85    protected boolean imports = true;
86    protected boolean verbose = false;
87    protected boolean nowrap = false;
88
89    // Username and password for Authentication
90
protected String JavaDoc username = null;
91    protected String JavaDoc password = null;
92
93    // Timeout, in milliseconds, to let the Emitter do its work
94
private long timeoutms = 45000; // 45 sec default
95

96    private GeneratorFactory genFactory = null;
97    private SymbolTable symbolTable = null;
98
99    public boolean isDebug()
100    {
101       return debug;
102    } // isDebug
103

104    public void setDebug(boolean debug)
105    {
106       this.debug = debug;
107    } // setDebug
108

109    public boolean isImports()
110    {
111       return imports;
112    } // isImports
113

114    public void setImports(boolean imports)
115    {
116       this.imports = imports;
117    } // setImports
118

119    public boolean isVerbose()
120    {
121       return verbose;
122    } // isVerbose
123

124    public void setVerbose(boolean verbose)
125    {
126       this.verbose = verbose;
127    } // setVerbose
128

129    public boolean isNowrap()
130    {
131       return nowrap;
132    }
133
134    public void setNowrap(boolean nowrap)
135    {
136       this.nowrap = nowrap;
137    }
138
139    /**
140     * Return the current timeout setting
141     */

142    public long getTimeout()
143    {
144       return timeoutms;
145    }
146
147    /**
148     * Set the timeout, in milliseconds
149     */

150    public void setTimeout(long timeout)
151    {
152       this.timeoutms = timeout;
153    }
154
155    public String JavaDoc getUsername()
156    {
157       return username;
158    } // getUsername
159

160    public void setUsername(String JavaDoc username)
161    {
162       this.username = username;
163    } // setUsername
164

165    public String JavaDoc getPassword()
166    {
167       return password;
168    } // getPassword
169

170    public void setPassword(String JavaDoc password)
171    {
172       this.password = password;
173    } // setPassword
174

175    public GeneratorFactory getFactory()
176    {
177       return genFactory;
178    } // getFactory
179

180    public void setFactory(GeneratorFactory factory)
181    {
182       this.genFactory = factory;
183    } // setFactory
184

185    /**
186     * Get the symbol table. The symbol table is null until
187     * run is called.
188     */

189    public SymbolTable getSymbolTable()
190    {
191       return symbolTable;
192    } // getSymbolTable
193

194    /**
195     * Return the current definition. The current definition is
196     * null until run is called.
197     */

198    public Definition getCurrentDefinition()
199    {
200       return symbolTable == null ? null : symbolTable.getDefinition();
201    } // getCurrentDefinition
202

203    /**
204     * Get the current WSDL URI. The WSDL URI is null until
205     * run is called.
206     */

207    public String JavaDoc getWSDLURI()
208    {
209       return symbolTable == null ? null : symbolTable.getWSDLURI();
210    } // getWSDLURI
211

212    /**
213     * Parse a WSDL at a given URL.
214     * <p/>
215     * This method will time out after the number of milliseconds specified
216     * by our timeoutms member.
217     */

218    public void run(String JavaDoc wsdlURI) throws Exception JavaDoc
219    {
220       if (getFactory() == null)
221       {
222          setFactory(new NoopFactory());
223       }
224       symbolTable = new SymbolTable(genFactory.getBaseTypeMapping(),
225               imports,
226               verbose,
227               nowrap);
228
229       // We run the actual Emitter in a thread that we can kill
230
WSDLRunnable runnable = new WSDLRunnable(symbolTable, wsdlURI);
231       Thread JavaDoc wsdlThread = new Thread JavaDoc(runnable);
232       wsdlThread.start();
233
234       try
235       {
236          if (timeoutms > 0)
237             wsdlThread.join(timeoutms);
238          else
239             wsdlThread.join();
240       }
241       catch (InterruptedException JavaDoc e)
242       {
243       }
244
245       if (wsdlThread.isAlive())
246       {
247          wsdlThread.interrupt();
248          throw new IOException JavaDoc(Messages.getMessage("timedOut"));
249       }
250
251       if (runnable.getFailure() != null)
252       {
253          throw runnable.getFailure();
254       }
255    } // run
256

257    private class WSDLRunnable implements Runnable JavaDoc
258    {
259       private SymbolTable symbolTable;
260       private String JavaDoc wsdlURI;
261       private Exception JavaDoc failure = null;
262
263       public WSDLRunnable(SymbolTable symbolTable, String JavaDoc wsdlURI)
264       {
265          this.symbolTable = symbolTable;
266          this.wsdlURI = wsdlURI;
267       } // ctor
268

269       public void run()
270       {
271          try
272          {
273             symbolTable.populate(wsdlURI, username, password);
274             generate(symbolTable);
275          }
276          catch (Exception JavaDoc e)
277          {
278             failure = e;
279          }
280       } // run
281

282       public Exception JavaDoc getFailure()
283       {
284          return failure;
285       } // getFailure
286
} // WSDLRunnable
287

288    /**
289     * Call this method if your WSDL document has already been parsed as an XML DOM document.
290     *
291     * @param context context This is directory context for the Document. If the Document were from file "/x/y/z.wsdl" then the context could be "/x/y" (even "/x/y/z.wsdl" would work). If context is null, then the context becomes the current directory.
292     * @param doc doc This is the XML Document containing the WSDL.
293     */

294    public void run(String JavaDoc context, Document JavaDoc doc)
295            throws IOException JavaDoc, SAXException JavaDoc, WSDLException,
296            ParserConfigurationException JavaDoc
297    {
298       if (getFactory() == null)
299       {
300          setFactory(new NoopFactory());
301       }
302       symbolTable = new SymbolTable(genFactory.getBaseTypeMapping(),
303               imports,
304               verbose,
305               nowrap);
306       symbolTable.populate(context, doc);
307       generate(symbolTable);
308    } // run
309

310    protected void sanityCheck(SymbolTable symbolTable)
311    {
312       // do nothing.
313
}
314
315    private void generate(SymbolTable symbolTable) throws IOException JavaDoc
316    {
317       sanityCheck(symbolTable);
318       Definition def = symbolTable.getDefinition();
319       genFactory.generatorPass(def, symbolTable);
320       if (isDebug())
321       {
322          symbolTable.dump(System.out);
323       }
324
325       // Generate bindings for types
326
generateTypes(symbolTable);
327
328       Iterator JavaDoc it = symbolTable.getHashMap().values().iterator();
329       while (it.hasNext())
330       {
331          Vector JavaDoc v = (Vector JavaDoc)it.next();
332          for (int i = 0; i < v.size(); ++i)
333          {
334             SymTabEntry entry = (SymTabEntry)v.elementAt(i);
335             Generator gen = null;
336             if (entry instanceof MessageEntry)
337             {
338                gen = genFactory.getGenerator(((MessageEntry)entry).getMessage(), symbolTable);
339             }
340             else if (entry instanceof PortTypeEntry)
341             {
342                PortTypeEntry pEntry = (PortTypeEntry)entry;
343                // If the portType is undefined, then we're parsing a Definition
344
// that didn't contain a portType, merely a binding that referred
345
// to a non-existent port type. Don't bother writing it.
346
if (pEntry.getPortType().isUndefined())
347                {
348                   continue;
349                }
350                gen = genFactory.getGenerator(pEntry.getPortType(), symbolTable);
351             }
352             else if (entry instanceof BindingEntry)
353             {
354                BindingEntry bEntry = (BindingEntry)entry;
355                Binding binding = bEntry.getBinding();
356
357                // If the binding is undefined, then we're parsing a Definition
358
// that didn't contain a binding, merely a service that referred
359
// to a non-existent binding. Don't bother writing it.
360
if (binding.isUndefined() || !bEntry.isReferenced())
361                {
362                   continue;
363                }
364                gen = genFactory.getGenerator(binding, symbolTable);
365             }
366             else if (entry instanceof ServiceEntry)
367             {
368                gen = genFactory.getGenerator(((ServiceEntry)entry).getService(), symbolTable);
369             }
370             if (gen != null)
371             {
372                gen.generate();
373             }
374          }
375       }
376
377       // Output extra stuff (deployment files and faults)
378
// outside of the recursive emit method.
379
Generator gen = genFactory.getGenerator(def, symbolTable);
380       gen.generate();
381    } // generate
382

383    /**
384     * Generate bindings (classes and class holders) for the complex types.
385     * If generating serverside (skeleton) spit out beanmappings
386     */

387    private void generateTypes(SymbolTable symbolTable) throws IOException JavaDoc
388    {
389       Vector JavaDoc types = symbolTable.getTypes();
390       for (int i = 0; i < types.size(); ++i)
391       {
392          TypeEntry type = (TypeEntry)types.elementAt(i);
393
394          // Write out the type if and only if:
395
// - we found its definition (getNode())
396
// - it is referenced
397
// - it is not a base type
398
// - it is a Type (not an Element) or a CollectionElement
399
// (Note that types that are arrays are passed to getGenerator
400
// because they may require a Holder)
401

402          // A CollectionElement is an array that might need a holder
403
boolean isType = (type instanceof Type ||
404                  type instanceof CollectionElement);
405          if (type.getNode() != null &&
406                  type.isReferenced() &&
407                  isType &&
408                  type.getBaseType() == null)
409          {
410             Generator gen = genFactory.getGenerator(type, symbolTable);
411             gen.generate();
412          }
413       }
414    } // generateTypes
415

416 } // class Parser
417
Popular Tags