KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jboss.axis.wsdl.gen.Generator;
59 import org.w3c.dom.Element JavaDoc;
60 import org.w3c.dom.Node JavaDoc;
61
62 import java.io.File JavaDoc;
63 import java.io.FileWriter JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.io.PrintWriter JavaDoc;
66
67 /**
68  * Emitter knows about WSDL writers, one each for PortType, Binding, Service,
69  * Definition, Type. But for some of these WSDL types, Wsdl2java generates
70  * multiple files. Each of these files has a corresponding writer that extends
71  * JavaWriter. So the Java WSDL writers (JavaPortTypeWriter, JavaBindingWriter,
72  * etc.) each calls a file writer (JavaStubWriter, JavaSkelWriter, etc.) for
73  * each file that that WSDL generates.
74  * <p/>
75  * <p>For example, when Emitter calls JavaWriterFactory for a Binding Writer, it
76  * returns a JavaBindingWriter. JavaBindingWriter, in turn, contains a
77  * JavaStubWriter, JavaSkelWriter, and JavaImplWriter since a Binding may cause
78  * a stub, skeleton, and impl template to be generated.
79  * <p/>
80  * <p>Note that the writers that are given to Emitter by JavaWriterFactory DO NOT
81  * extend JavaWriter. They simply implement Writer and delegate the actual
82  * task of writing to extensions of JavaWriter.
83  * <p/>
84  * <p>All of Wsdl2java's Writer implementations follow a common behaviour.
85  * JavaWriter is the abstract base class that dictates this common behaviour.
86  * This behaviour is primarily placed within the generate method. The generate
87  * method calls, in succession (note: the starred methods are the ones you are
88  * probably most interested in):
89  * <dl>
90  * <dt> * getFileName
91  * <dd> This is an abstract method that must be implemented by the subclass.
92  * It returns the fully-qualified file name.
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> You may want to override this method. The default implementation
107  * generates nothing.
108  * <dt> * writeFileBody(pw)
109  * <dd> This is an abstract method that must be implemented by the subclass.
110  * This is where the body of a file is generated.
111  * <dt> * writeFileFooter(pw)
112  * <dd> You may want to override this method. The default implementation
113  * generates nothing.
114  * <dt> closePrintWriter(pw)
115  * <dd> You should not need to override this method. It simply closes the
116  * PrintWriter.
117  * </dl>
118  */

119 public abstract class JavaWriter implements Generator
120 {
121    protected Emitter emitter;
122    protected String JavaDoc type;
123
124    /**
125     * Constructor.
126     */

127    protected JavaWriter(Emitter emitter, String JavaDoc type)
128    {
129       this.emitter = emitter;
130       this.type = type;
131    } // ctor
132

133    /**
134     * Generate a file.
135     */

136    public void generate() throws IOException JavaDoc
137    {
138       String JavaDoc file = getFileName();
139       if (isFileGenerated(file))
140       {
141          throw new DuplicateFileException(Messages.getMessage("duplicateFile00", file), file);
142       }
143       registerFile(file);
144       if (emitter.isVerbose())
145       {
146          String JavaDoc msg = verboseMessage(file);
147          if (msg != null)
148          {
149             System.out.println(msg);
150          }
151       }
152       PrintWriter JavaDoc pw = getPrintWriter(file);
153       writeFileHeader(pw);
154       writeFileBody(pw);
155       writeFileFooter(pw);
156       closePrintWriter(pw);
157    } // generate
158

159    /**
160     * This method must be implemented by a subclass. It
161     * returns the fully-qualified name of the file to be
162     * generated.
163     */

164    protected abstract String JavaDoc getFileName();
165
166    /**
167     * You should not need to override this method. It checks
168     * to see whether the given file is in the List returned
169     * by emitter.getGeneratedFileNames.
170     */

171    protected boolean isFileGenerated(String JavaDoc file)
172    {
173       return emitter.getGeneratedFileNames().contains(file);
174    } // isFileGenerated
175

176    /**
177     * You should not need to override this method.
178     * It registers the given file by calling
179     * emitter.getGeneratedFileInfo().add(...).
180     */

181    protected void registerFile(String JavaDoc file)
182    {
183       emitter.getGeneratedFileInfo().add(file, null, type);
184    } // registerFile
185

186    /**
187     * Return the string: "Generating <file>". Override this
188     * method if you want to provide more information.
189     */

190    protected String JavaDoc verboseMessage(String JavaDoc file)
191    {
192       return Messages.getMessage("generating", file);
193    } // verboseMessage
194

195    /**
196     * You should not need to override this method.
197     * Given the file name, it creates a PrintWriter for it.
198     */

199    protected PrintWriter JavaDoc getPrintWriter(String JavaDoc filename) throws IOException JavaDoc
200    {
201       File JavaDoc file = new File JavaDoc(filename);
202       File JavaDoc parent = new File JavaDoc(file.getParent());
203       parent.mkdirs();
204       return new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
205    } // getPrintWriter
206

207    /**
208     * This method is intended to be overridden as necessary
209     * to generate file header information. This default
210     * implementation does nothing.
211     */

212    protected void writeFileHeader(PrintWriter JavaDoc pw) throws IOException JavaDoc
213    {
214    } // writeFileHeader
215

216    /**
217     * This method must be implemented by a subclass. This
218     * is where the body of a file is generated.
219     */

220    protected abstract void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc;
221
222    /**
223     * You may want to override this method. This default
224     * implementation generates nothing.
225     */

226    protected void writeFileFooter(PrintWriter JavaDoc pw) throws IOException JavaDoc
227    {
228    } // writeFileFooter
229

230    /**
231     * Close the print writer.
232     */

233    protected void closePrintWriter(PrintWriter JavaDoc pw)
234    {
235       pw.close();
236    } // closePrintWriter
237

238    /**
239     * Output a documentation element as a Java comment.
240     */

241    protected void writeComment(PrintWriter JavaDoc pw, Element JavaDoc element)
242    {
243       // This controls how many characters per line
244
final int LINE_LENGTH = 65;
245
246       if (element == null)
247       {
248          return;
249       }
250
251       Node JavaDoc child = element.getFirstChild();
252       if (child == null)
253       {
254          return;
255       }
256
257       String JavaDoc comment = child.getNodeValue();
258
259       // Strip out stuff that will really mess up our comments
260
comment = comment.replace('\r', ' ');
261       comment = comment.replace('\n', ' ');
262
263       if (comment != null)
264       {
265          int start = 0;
266
267          pw.println(); // blank line
268

269          // make the comment look pretty
270
while (start < comment.length())
271          {
272             int end = start + LINE_LENGTH;
273             if (end > comment.length())
274                end = comment.length();
275             // look for next whitespace
276
while (end < comment.length() &&
277                     !Character.isWhitespace(comment.charAt(end)))
278             {
279                end++;
280             }
281             pw.println(" // " + comment.substring(start, end).trim());
282             start = end + 1;
283          }
284       }
285    } // writeComment
286

287 } // abstract class JavaWriter
288
Popular Tags