KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > wsdl > toJava > JavaClassWriter


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.toJava;
56
57 import org.jboss.axis.utils.Messages;
58
59 import java.io.File JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.io.PrintWriter JavaDoc;
62
63 /**
64  * Emitter knows about WSDL writers, one each for PortType, Binding, Service,
65  * Definition, Type. But for some of these WSDL types, Wsdl2java generates
66  * multiple files. Each of these files has a corresponding writer that extends
67  * JavaWriter. So the Java WSDL writers (JavaPortTypeWriter, JavaBindingWriter,
68  * etc.) each calls a file writer (JavaStubWriter, JavaSkelWriter, etc.) for
69  * each file that that WSDL generates.
70  * <p/>
71  * <p>For example, when Emitter calls JavaWriterFactory for a Binding Writer, it
72  * returns a JavaBindingWriter. JavaBindingWriter, in turn, contains a
73  * JavaStubWriter, JavaSkelWriter, and JavaImplWriter since a Binding may cause
74  * a stub, skeleton, and impl template to be generated.
75  * <p/>
76  * <p>Note that the writers that are given to Emitter by JavaWriterFactory DO NOT
77  * extend JavaWriter. They simply implement Writer and delegate the actual
78  * task of writing to extensions of JavaWriter.
79  * <p/>
80  * <p>All of Wsdl2java's Writer implementations follow a common behaviour.
81  * JavaWriter is the abstract base class that dictates this common behaviour.
82  * Many of the files generated are .java files, so this abstract class -
83  * JavaClassWriter - exists. It extends JavaWriter and adds a bit of Java-
84  * relative behaviour. This behaviour is primarily placed within the generate
85  * method. The generate method calls, in succession (note: the starred methods
86  * are the ones you are probably most interested in):
87  * <dl>
88  * <dt> getFileName
89  * <dd> This method is abstract in JavaWriter, but JavaClassWriter implements
90  * this method. Subclasses should have no need to override it. It
91  * returns the fully-qualified file name based on the fully-qualified
92  * classname + ".java".
93  * <dt> isFileGenerated(file)
94  * <dd> You should not need to override this method. It checks to see whether
95  * this file is in the List returned by emitter.getGeneratedFileNames.
96  * <dt> registerFile(file)
97  * <dd> You should not need to override this method. It registers this file by
98  * calling emitter.getGeneratedFileInfo().add(...).
99  * <dt> * verboseMessage(file)
100  * <dd> You may override this method if you want to provide more information.
101  * The generate method only calls verboseMessage if verbose is turned on.
102  * <dt> getPrintWriter(file)
103  * <dd> You should not need to override this method. Given the file name, it
104  * creates a PrintWriter for it.
105  * <dt> writeFileHeader(pw)
106  * <dd> JavaClassWriter implements this method, so you should not need to
107  * override it. This method generates a javadoc giving the filename and
108  * a comment stating that this file is generated by WSDL2Java, and it
109  * generates the class definition including the opening curly brace..
110  * <dt> * writeFileBody(pw)
111  * <dd> This is an abstract method that must be implemented by the subclass.
112  * This is where the body of a file is generated.
113  * <dt> * writeFileFooter(pw)
114  * <dd> JavaClassWriter implements this method, so you should not need to
115  * override it. It generates the closing curly brace for the class.
116  * <dt> closePrintWriter(pw)
117  * <dd> You should not need to override this method. It simply closes the
118  * PrintWriter.
119  * </dl>
120  * <p/>
121  * Additional behaviour that JavaClassWriter introduces beyond JavaWriter is
122  * related to the class header and definition:
123  * <dl>
124  * <dt> writeHeaderComments
125  * <dd> Write the header comments, such as the file name and that the file was
126  * generated by WSDL2Java. You need not override this method unless you
127  * want a tailored comment.
128  * <dt> writePackage
129  * <dd> Write the package statement, if necessary. You should not need to
130  * override this method.
131  * <dt> getClassModifiers
132  * <dd> Modifiers, such as "public", "final", "abstract" would be returned by
133  * this method. The default implementation only generates "public ", so
134  * any subclass that needs more must override this method.
135  * <dt> getClassText
136  * <dd> This simply returns "class ". If anything else is desired, for
137  * instance, JavaInterfaceWriter prefers "interface ", then this method
138  * must be overridden.
139  * <dt> getExtendsText
140  * <dd> The default implementation returns "". If a subclass desires to list
141  * a set of classes this one extends, then this method must be overridden.
142  * <dt> getImplementsText
143  * <dd> Same as getExtendsText except for the implements clause.
144  * </dl>
145  */

146 public abstract class JavaClassWriter extends JavaWriter
147 {
148    protected Namespaces namespaces;
149    protected String JavaDoc className;
150    protected String JavaDoc packageName;
151
152    /**
153     * Constructor.
154     *
155     * @param emitter The emitter instance
156     * @param fullClassName The fully qualified class name of the class
157     * to be generated.
158     * @param type
159     */

160    protected JavaClassWriter(Emitter emitter,
161                              String JavaDoc fullClassName,
162                              String JavaDoc type)
163    {
164       super(emitter, type);
165       this.namespaces = emitter.getNamespaces();
166       this.packageName = Utils.getJavaPackageName(fullClassName);
167       this.className = Utils.getJavaLocalName(fullClassName);
168    } // ctor
169

170    /**
171     * Return the file name as a string of the form:
172     * "<directory-ized fully-qualified classname>.java"
173     */

174    protected String JavaDoc getFileName()
175    {
176       return namespaces.toDir(packageName) + className + ".java";
177    } // getFileName
178

179    /**
180     * You should not need to override this method.
181     * It registers the given file by calling
182     * emitter.getGeneratedFileInfo().add(...).
183     * JavaClassWriter overrides this method from JavaWriter because
184     * it add class name to the registration information.
185     */

186    protected void registerFile(String JavaDoc file)
187    {
188       String JavaDoc fqClass = getPackage() + '.' + getClassName();
189       emitter.getGeneratedFileInfo().add(file, fqClass, type);
190    } // registerFile
191

192    /**
193     * Write a common header, including the package name, the class
194     * declaration, and the opening curly brace.
195     */

196    protected void writeFileHeader(PrintWriter JavaDoc pw) throws IOException JavaDoc
197    {
198       writeHeaderComments(pw);
199       writePackage(pw);
200
201       // print class declaration
202
pw.println(getClassModifiers() + getClassText() + getClassName() + ' ' + getExtendsText() + getImplementsText() + "{");
203    } // writeFileHeader
204

205    /**
206     * Write the header comments.
207     */

208    protected void writeHeaderComments(PrintWriter JavaDoc pw) throws IOException JavaDoc
209    {
210       String JavaDoc localFile = getFileName();
211       int lastSepChar = localFile.lastIndexOf(File.separatorChar);
212       if (lastSepChar >= 0)
213       {
214          localFile = localFile.substring(lastSepChar + 1);
215       }
216       pw.println("/**");
217       pw.println(" * " + localFile);
218       pw.println(" *");
219       pw.println(" * " + Messages.getMessage("wsdlGenLine00"));
220       pw.println(" * " + Messages.getMessage("wsdlGenLine01"));
221       pw.println(" */");
222       pw.println();
223    } // writeHeaderComments
224

225    /**
226     * Write the package declaration statement.
227     */

228    protected void writePackage(PrintWriter JavaDoc pw) throws IOException JavaDoc
229    {
230       if (getPackage() != null)
231       {
232          pw.println("package " + getPackage() + ";");
233          pw.println();
234       }
235
236    } // writePackage
237

238    /**
239     * Return "public ". If more modifiers are needed, this method must be
240     * overridden.
241     */

242    protected String JavaDoc getClassModifiers()
243    {
244       return "public ";
245    } // getClassModifiers
246

247    /**
248     * Return "class ". If "interface " is needed instead, this method must be
249     * overridden.
250     */

251    protected String JavaDoc getClassText()
252    {
253       return "class ";
254    } // getClassString
255

256    /**
257     * Returns the appropriate extends clause. This default implementation
258     * simply returns "", but if you want "extends <class/interface list> "
259     * then you must override this method.
260     *
261     * @return ""
262     */

263    protected String JavaDoc getExtendsText()
264    {
265       return "";
266    } // getExtendsText
267

268    /**
269     * Returns the appropriate implements clause. This default implementation
270     * simply returns "", but if you want "implements <interface list> " then
271     * you must override this method.
272     *
273     * @return ""
274     */

275    protected String JavaDoc getImplementsText()
276    {
277       return "";
278    } // getImplementsText
279

280    /**
281     * Returns the package name.
282     */

283    protected String JavaDoc getPackage()
284    {
285       return packageName;
286    } // getPackage
287

288    /**
289     * Returns the class name.
290     */

291    protected String JavaDoc getClassName()
292    {
293       return className;
294    } // getClassName
295

296    /**
297     * Generate the closing curly brace.
298     */

299    protected void writeFileFooter(PrintWriter JavaDoc pw) throws IOException JavaDoc
300    {
301       super.writeFileFooter(pw);
302       pw.println('}');
303    } // writeFileFooter
304

305 } // abstract class JavaClassWriter
306
Popular Tags