KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > javasource > sourclet > AbstractSourclet


1 /*
2  * Project: BeautyJ - Customizable Java Source Code Transformer
3  * Class: de.gulden.util.javasource.sourclet.AbstractSourclet
4  * Version: 1.1
5  *
6  * Date: 2004-09-29
7  *
8  * Note: Contains auto-generated Javadoc comments created by BeautyJ.
9  *
10  * This is licensed under the GNU General Public License (GPL)
11  * and comes with NO WARRANTY. See file license.txt for details.
12  *
13  * Author: Jens Gulden
14  * Email: beautyj@jensgulden.de
15  */

16
17 package de.gulden.util.javasource.sourclet;
18
19 import de.gulden.util.javasource.SourceObjectDeclared;
20 import java.io.*;
21 import java.util.*;
22
23 /**
24  * Class AbstractSourclet.
25  *
26  * @author Jens Gulden
27  * @version 1.0
28  */

29 public abstract class AbstractSourclet implements Sourclet, SourcletOptions {
30
31     // ------------------------------------------------------------------------
32
// --- static field ---
33
// ------------------------------------------------------------------------
34

35     /**
36      * Helper for quick newline character(s) access.
37      */

38     public static String JavaDoc nl = System.getProperty("line.separator");
39
40
41     // ------------------------------------------------------------------------
42
// --- field ---
43
// ------------------------------------------------------------------------
44

45     /**
46      * Proxy object that may work as provider of option values.
47      *
48      * @see #setOptions
49      */

50     protected SourcletOptions options = null;
51
52
53     // ------------------------------------------------------------------------
54
// --- constructor ---
55
// ------------------------------------------------------------------------
56

57     /**
58      * Creates a new instance of AbstractSourclet.
59      */

60     protected AbstractSourclet() {
61         super();
62     }
63
64
65     // ------------------------------------------------------------------------
66
// --- methods ---
67
// ------------------------------------------------------------------------
68

69     /**
70      * Sets an object that works as a proxy for asccessing option values.
71      * The default implementations of <code>getOption</code>,
72      * <code>getIntOption</code> and <code>isOption</code> acess this proxy
73      * to deliver option values to the Sourclet.<br>
74      * Alternatively, <code>getOption</code>,
75      * <code>getIntOption</code> and <code>isOption</code> can be reimplemented
76      * (overwritten) by the Sourclet implementation to use its own option
77      * retrieval mechanism.
78      *
79      * @see #getOption
80      * @see #getIntOption
81      * @see #isOption
82      */

83     public void setOptions(SourcletOptions options) {
84         this.options=options;
85     }
86
87     /**
88      * Returns the options.
89      *
90      * @return the options proxy, if it exists. Otherwise returns <code>null</code>.
91      */

92     public SourcletOptions getOptions() {
93         return options;
94     }
95
96     /**
97      * Initializes the Sourclet. This is called once before the
98      * first SourceObject's code is being generated.
99      */

100     public void init(SourcletOptions options) {
101         setOptions(options);
102     }
103
104     /**
105      * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
106      * May be overwritten by a subclass to provide options from a different source.
107      */

108     public String JavaDoc getOption(String JavaDoc name) {
109         return options.getOption(name);
110     }
111
112     /**
113      * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
114      * May be overwritten by a subclass to provide options from a different source.
115      */

116     public int getIntOption(String JavaDoc name) {
117         return options.getIntOption(name);
118     }
119
120     /**
121      * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
122      * May be overwritten by a subclass to provide options from a different source.
123      */

124     public boolean isOption(String JavaDoc name) {
125         return options.isOption(name);
126     }
127
128     /**
129      * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
130      * May be overwritten by a subclass to provide options from a different source.
131      */

132     public boolean isOption(String JavaDoc name, String JavaDoc value) {
133         return options.isOption(name, value);
134     }
135
136     /**
137      * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
138      * May be overwritten by a subclass to provide options from a different source.
139      */

140     public boolean hasOption(String JavaDoc name, String JavaDoc value) {
141         return options.hasOption(name, value);
142     }
143
144     /**
145      * Outputs the source code for an entire SourceObject to an OutputStream.
146      *
147      * @throws IOException if an i/o error occurs
148      */

149     public void buildSource(OutputStream out, SourceObjectDeclared o) throws IOException {
150         buildStartSource(out,o);
151         buildHeadSource(out,o);
152         buildBodySource(out,o);
153         buildEndSource(out,o);
154     }
155
156     /**
157      * Outputs the start part of a source object.
158      * This is the part which comes before the 'normal' head
159      * (e.g. before a method's signature), so usually this is
160      * the place where to output Javadoc comments.
161      *
162      * @throws IOException if an i/o error occurs
163      */

164     public abstract void buildStartSource(OutputStream out, SourceObjectDeclared o) throws IOException;
165
166     /**
167      * Outputs the head part of a source object.
168      * This is the actual Java code that declares the source object,
169      * for example a method's signature.
170      *
171      * @throws IOException if an i/o error occurs
172      */

173     public abstract void buildHeadSource(OutputStream out, SourceObjectDeclared o) throws IOException;
174
175     /**
176      * Outputs the body content of the source object. For example,
177      * in case of methods this is Java code, in case of classes this recursively
178      * contains other SourceObjects' code.
179      *
180      * @throws IOException if an i/o error occurs
181      */

182     public abstract void buildBodySource(OutputStream out, SourceObjectDeclared o) throws IOException;
183
184     /**
185      * Outputs everything that occurs after the SourceObject.
186      *
187      * @throws IOException if an i/o error occurs
188      */

189     public abstract void buildEndSource(OutputStream out, SourceObjectDeclared o) throws IOException;
190
191
192     // ------------------------------------------------------------------------
193
// --- static methods ---
194
// ------------------------------------------------------------------------
195

196     /**
197      * Tool method for writing a string to an OutputStream.
198      *
199      * @throws IOException if an i/o error occurs
200      */

201     public static void write(OutputStream out, String JavaDoc s) throws IOException {
202         out.write(s.getBytes());
203     }
204
205     /**
206      * Tool method for writing a string concatenated with a newline-feed to an OutputStream.
207      *
208      * @throws IOException if an i/o error occurs
209      */

210     public static void writeln(OutputStream out, String JavaDoc s) throws IOException {
211         write(out,s+nl);
212     }
213
214 } // end AbstractSourclet
215
Popular Tags