001: /*
002: * Project: BeautyJ - Customizable Java Source Code Transformer
003: * Class: de.gulden.util.javasource.sourclet.AbstractSourclet
004: * Version: 1.1
005: *
006: * Date: 2004-09-29
007: *
008: * Note: Contains auto-generated Javadoc comments created by BeautyJ.
009: *
010: * This is licensed under the GNU General Public License (GPL)
011: * and comes with NO WARRANTY. See file license.txt for details.
012: *
013: * Author: Jens Gulden
014: * Email: beautyj@jensgulden.de
015: */
016:
017: package de.gulden.util.javasource.sourclet;
018:
019: import de.gulden.util.javasource.SourceObjectDeclared;
020: import java.io.*;
021: import java.util.*;
022:
023: /**
024: * Class AbstractSourclet.
025: *
026: * @author Jens Gulden
027: * @version 1.0
028: */
029: public abstract class AbstractSourclet implements Sourclet,
030: SourcletOptions {
031:
032: // ------------------------------------------------------------------------
033: // --- static field ---
034: // ------------------------------------------------------------------------
035:
036: /**
037: * Helper for quick newline character(s) access.
038: */
039: public static String nl = System.getProperty("line.separator");
040:
041: // ------------------------------------------------------------------------
042: // --- field ---
043: // ------------------------------------------------------------------------
044:
045: /**
046: * Proxy object that may work as provider of option values.
047: *
048: * @see #setOptions
049: */
050: protected SourcletOptions options = null;
051:
052: // ------------------------------------------------------------------------
053: // --- constructor ---
054: // ------------------------------------------------------------------------
055:
056: /**
057: * Creates a new instance of AbstractSourclet.
058: */
059: protected AbstractSourclet() {
060: super ();
061: }
062:
063: // ------------------------------------------------------------------------
064: // --- methods ---
065: // ------------------------------------------------------------------------
066:
067: /**
068: * Sets an object that works as a proxy for asccessing option values.
069: * The default implementations of <code>getOption</code>,
070: * <code>getIntOption</code> and <code>isOption</code> acess this proxy
071: * to deliver option values to the Sourclet.<br>
072: * Alternatively, <code>getOption</code>,
073: * <code>getIntOption</code> and <code>isOption</code> can be reimplemented
074: * (overwritten) by the Sourclet implementation to use its own option
075: * retrieval mechanism.
076: *
077: * @see #getOption
078: * @see #getIntOption
079: * @see #isOption
080: */
081: public void setOptions(SourcletOptions options) {
082: this .options = options;
083: }
084:
085: /**
086: * Returns the options.
087: *
088: * @return the options proxy, if it exists. Otherwise returns <code>null</code>.
089: */
090: public SourcletOptions getOptions() {
091: return options;
092: }
093:
094: /**
095: * Initializes the Sourclet. This is called once before the
096: * first SourceObject's code is being generated.
097: */
098: public void init(SourcletOptions options) {
099: setOptions(options);
100: }
101:
102: /**
103: * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
104: * May be overwritten by a subclass to provide options from a different source.
105: */
106: public String getOption(String name) {
107: return options.getOption(name);
108: }
109:
110: /**
111: * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
112: * May be overwritten by a subclass to provide options from a different source.
113: */
114: public int getIntOption(String name) {
115: return options.getIntOption(name);
116: }
117:
118: /**
119: * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
120: * May be overwritten by a subclass to provide options from a different source.
121: */
122: public boolean isOption(String name) {
123: return options.isOption(name);
124: }
125:
126: /**
127: * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
128: * May be overwritten by a subclass to provide options from a different source.
129: */
130: public boolean isOption(String name, String value) {
131: return options.isOption(name, value);
132: }
133:
134: /**
135: * Passes requests for option values to the SourcletOptions proxy object that has been set through setOptions(..).
136: * May be overwritten by a subclass to provide options from a different source.
137: */
138: public boolean hasOption(String name, String value) {
139: return options.hasOption(name, value);
140: }
141:
142: /**
143: * Outputs the source code for an entire SourceObject to an OutputStream.
144: *
145: * @throws IOException if an i/o error occurs
146: */
147: public void buildSource(OutputStream out, SourceObjectDeclared o)
148: throws IOException {
149: buildStartSource(out, o);
150: buildHeadSource(out, o);
151: buildBodySource(out, o);
152: buildEndSource(out, o);
153: }
154:
155: /**
156: * Outputs the start part of a source object.
157: * This is the part which comes before the 'normal' head
158: * (e.g. before a method's signature), so usually this is
159: * the place where to output Javadoc comments.
160: *
161: * @throws IOException if an i/o error occurs
162: */
163: public abstract void buildStartSource(OutputStream out,
164: 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,
174: SourceObjectDeclared o) throws IOException;
175:
176: /**
177: * Outputs the body content of the source object. For example,
178: * in case of methods this is Java code, in case of classes this recursively
179: * contains other SourceObjects' code.
180: *
181: * @throws IOException if an i/o error occurs
182: */
183: public abstract void buildBodySource(OutputStream out,
184: SourceObjectDeclared o) throws IOException;
185:
186: /**
187: * Outputs everything that occurs after the SourceObject.
188: *
189: * @throws IOException if an i/o error occurs
190: */
191: public abstract void buildEndSource(OutputStream out,
192: SourceObjectDeclared o) throws IOException;
193:
194: // ------------------------------------------------------------------------
195: // --- static methods ---
196: // ------------------------------------------------------------------------
197:
198: /**
199: * Tool method for writing a string to an OutputStream.
200: *
201: * @throws IOException if an i/o error occurs
202: */
203: public static void write(OutputStream out, String s)
204: throws IOException {
205: out.write(s.getBytes());
206: }
207:
208: /**
209: * Tool method for writing a string concatenated with a newline-feed to an OutputStream.
210: *
211: * @throws IOException if an i/o error occurs
212: */
213: public static void writeln(OutputStream out, String s)
214: throws IOException {
215: write(out, s + nl);
216: }
217:
218: } // end AbstractSourclet
|