KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > util > BaseSourceGenerator


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.util;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 // Zeus imports
32
import org.enhydra.zeus.Binding;
33 import org.enhydra.zeus.Generator;
34 import org.enhydra.zeus.Transformer;
35 import org.enhydra.zeus.ZeusException;
36 import org.enhydra.zeus.generator.SimpleGenerator;
37 import org.enhydra.zeus.transform.DefaultsTransformer;
38
39 /**
40  * <p>
41  * This provides basic functionality for classes that need to
42  * implement the <code>{@link SourceGenerator}</code> interface.
43  * </p>
44  *
45  * @author Brett McLaughlin
46  */

47 public abstract class BaseSourceGenerator {
48
49     /** The reader for reading constraints from */
50     protected Reader JavaDoc constraintsReader;
51
52     /** The directory to output classes to */
53     protected File JavaDoc outputDir;
54
55     /** The package to generate classes within */
56     protected String JavaDoc javaPackage;
57     
58     /** Whether or not to collapse simple elements */
59     protected boolean collapseSimpleElements;
60     
61     /** Whether or not to ignore ID attributes */
62     protected boolean ignoreIDAttributes;
63
64     /**
65      * <p>
66      * This sets up defaults for the generator.
67      * </p>
68      */

69     public BaseSourceGenerator() {
70         javaPackage = "";
71         outputDir = new File JavaDoc(".");
72     }
73
74     /**
75      * <p>
76      * This allows for supplying the constraints file to use
77      * for source code generation as a <code>String</code> URI.
78      * </p>
79      *
80      * @param fileURI <code>String</code> URI for constraints file.
81      * @throws <code>IOException</code> - when the specified file URI
82      * is invalid.
83      */

84     public void setConstraintsInput(String JavaDoc fileURI) throws IOException JavaDoc {
85         if (fileURI == null) {
86             throw new IllegalArgumentException JavaDoc("A Source Generator cannot " +
87                 "have a null file URI.");
88         }
89         
90         FileReader JavaDoc reader = new FileReader JavaDoc(new File JavaDoc(fileURI));
91         setConstraintsInput(reader);
92     }
93
94     /**
95      * <p>
96      * This allows for supplying the constraints file to use
97      * for source code generation as a <code>File</code>.
98      * </p>
99      *
100      * @param file <code>File</code> to read constraints from.
101      * @throws <code>IOException</code> - when the specified file URI
102      * is invalid.
103      */

104     public void setConstraintsInput(File JavaDoc file) throws IOException JavaDoc {
105         if (file == null) {
106             throw new IllegalArgumentException JavaDoc("A Source Generator cannot " +
107                 "have a null File.");
108         }
109         
110         setConstraintsInput(new FileReader JavaDoc(file));
111     }
112
113     /**
114      * <p>
115      * This allows for supplying the constraints file to use
116      * for source code generation as a <code>FileReader</code>.
117      * </p>
118      *
119      * @param reader <code>Reader</code> to read constraints
120      * from.
121      */

122     public void setConstraintsInput(Reader JavaDoc reader) {
123         if (reader == null) {
124             throw new IllegalArgumentException JavaDoc("A Source Generator cannot " +
125                 "have a null Reader.");
126         }
127         constraintsReader = reader;
128     }
129
130     /**
131      * <p>
132      * This allows for supplying the constraints file to use
133      * for source code generation as an
134      * <code>InputStream</code>.
135      * </p>
136      *
137      * @param inputStream <code>InputStream</code> to read
138      * constraints from.
139      */

140     public void setConstraintsInput(InputStream JavaDoc inputStream) {
141         if (inputStream == null) {
142             throw new IllegalArgumentException JavaDoc("A Source Generator cannot " +
143                 "have a null InputStream.");
144         }
145         setConstraintsInput(new InputStreamReader JavaDoc(inputStream));
146     }
147
148     /**
149      * <p>
150      * This allows specification of an output directory for the
151      * generated classes.
152      * </p>
153      *
154      * @param outputDirString <code>String</code> specifying output
155      * directory for generated classes.
156      * @throws <code>IOException</code> - when invalid direcotory is
157      * specified
158      */

159     public void setOutputDir(String JavaDoc outputDir) throws IOException JavaDoc {
160         if (outputDir == null) {
161             throw new IllegalArgumentException JavaDoc("A Source Generator cannot " +
162                 "have a null output directory.");
163         }
164         setOutputDir(new File JavaDoc(outputDir));
165     }
166
167     /**
168      * <p>
169      * This allows specification of an output directory for the
170      * generated classes.
171      * </p>
172      *
173      * @param outputDir <code>File</code> specifying output directory
174      * for generated classes.
175      * @throws <code>IOException</code> - when invalid direcotory is
176      * specified
177      */

178     public void setOutputDir(File JavaDoc outputDir) throws IOException JavaDoc {
179         if (outputDir == null) {
180             throw new IllegalArgumentException JavaDoc("A Source Generator cannot " +
181                 "have a null output directory.");
182         }
183         
184         if ((!outputDir.exists()) || (!outputDir.isDirectory())) {
185             throw new IOException JavaDoc("Output directory must exist before " +
186                                   "class generation.");
187         }
188         this.outputDir = outputDir;
189     }
190
191     /**
192      * <p>
193      * This will set the package for generating classes within.
194      * </p>
195      *
196      * @param javaPackage the package to generate classes within.
197      */

198     public void setJavaPackage(String JavaDoc javaPackage) {
199         if (javaPackage == null) {
200             throw new IllegalArgumentException JavaDoc("A Source Generator cannot " +
201                 "have a null Java package.");
202         }
203         
204         this.javaPackage = javaPackage;
205     }
206     
207     /**
208      * <p>
209      * This sets whether or not to collapse simple elements. By default,
210      * simple elements are <i>not</i> collapsed. By default, ID attributes
211      * are <i>not</i> ignored in this determination.
212      * </p>
213      *
214      * @param collapseSimpleElements whether or not to collapse simple
215      * elements.
216      */

217     public void setCollapseSimpleElements(boolean collapseSimpleElements) {
218         setCollapseSimpleElements(collapseSimpleElements, false);
219     }
220     
221     /**
222      * <p>
223      * This sets whether or not to collapse simple elements. By default,
224      * simple elements are <i>not</i> collapsed. It also allows specification
225      * of whether ID attributes should be ignored when making a determination
226      * if an element is simple.
227      * </p>
228      *
229      * @param collapseSimpleElements whether or not to collapse simple
230      * elements.
231      * @param ignoreIDAttributes whether or not to ignore ID attributes.
232      */

233     public void setCollapseSimpleElements(boolean collapseSimpleElements,
234                                           boolean ignoreIDAttributes) {
235     
236         this.collapseSimpleElements = collapseSimpleElements;
237         this.ignoreIDAttributes = ignoreIDAttributes;
238     }
239
240     /**
241      * <p>
242      * This method performs class generation.
243      * </p>
244      *
245      * @throws <code>IOException</code> - when class generation fails
246      * @throws <code>ZeusException</code> - when class generation fails.
247      */

248     public void generate() throws IOException JavaDoc, ZeusException {
249         // Transformer for bindings
250
Transformer transformer = new DefaultsTransformer();
251         transformer.getTransformerOptions().setDefaultPackage(javaPackage);
252         
253         // Generator for source code
254
Generator generator = getGenerator();
255         generator.setOutputDirectory(outputDir);
256
257         // Get the DTD bindings
258
List JavaDoc bindings = getConstraintBindings();
259         
260         // Transform the bindings
261
List JavaDoc transformedBindings = transformer.transform(bindings);
262         
263         // Generate source code
264
for (Iterator JavaDoc i = transformedBindings.iterator(); i.hasNext(); ) {
265             Binding binding = (Binding)i.next();
266             generator.generate(binding);
267         }
268     }
269
270     /**
271      * <p>
272      * This defines a contract for subclasses to allow them to get the
273      * Zeus <code>{@link Binding}</code> objects to generate code
274      * from.
275      * </p>
276      *
277      * @return <code>List</code> - a list of the <code>{@link Binding}</code>
278      * objects to generate code from.
279      * @throws <code>IOException</code> - when binding creation fails
280      */

281     protected abstract List JavaDoc getConstraintBindings() throws IOException JavaDoc;
282     
283     /**
284      * <p>
285      * This handles the process of creating a <code>{@link Generator}</code>
286      * for use in data binding.
287      * </p><p>
288      * This version returns a basic
289      * <code>{@link SimpleGenerator}</code>, but other <code>Generator</code>
290      * implementations can be used by overriding this method.
291      * </p>
292      *
293      * @return <code>Generator</code> - the <code>Generator</code> to use.
294      */

295     protected Generator getGenerator() {
296         return new SimpleGenerator();
297     }
298 }
299
Popular Tags