KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 // Zeus imports
26
import org.enhydra.zeus.Binder;
27 import org.enhydra.zeus.Binding;
28 import org.enhydra.zeus.Generator;
29 import org.enhydra.zeus.ZeusException;
30 import org.enhydra.zeus.binder.DTDBinder;
31 import org.enhydra.zeus.generator.SimpleGenerator;
32 import org.enhydra.zeus.source.DTDSource;
33 import org.enhydra.zeus.source.StreamDTDSource;
34
35 /**
36  * <p>
37  * This is a standalone "utility" class. It allows source code
38  * generation from a DTD.
39  * </p>
40  *
41  * @author Brett McLaughlin
42  */

43 public class DTDSourceGenerator extends BaseSourceGenerator
44     implements SourceGenerator {
45
46     /** The interface package name to use. */
47     private static String JavaDoc interfacePackageName;
48     
49     /** The root element to use */
50     private String JavaDoc rootElementName;
51
52     /**
53      * <p>
54      * This sets up defaults for the generator.
55      * </p>
56      */

57     public DTDSourceGenerator() {
58         super();
59     }
60
61     /**
62      * <p>
63      * This defines a contract for subclasses to allow them to get the
64      * Zeus <code>{@link org.enhydra.zeus.Binding}</code> objects to
65      * generate code from.
66      * </p>
67      *
68      * @return <code>List</code> - a list of the Binding
69      * objects to generate code from.
70      * @exception <code>IOException</code> - when binding creation fails
71      */

72     protected List JavaDoc getConstraintBindings() throws IOException JavaDoc {
73
74         // Create a source to read the DTD from.
75
DTDSource dtdSource = new StreamDTDSource(constraintsReader);
76
77         // Create the binder we will use.
78
DTDBinder dtdBinder = new DTDBinder(dtdSource, rootElementName);
79         dtdBinder.setIsCollapsingSimpleElements(collapseSimpleElements);
80         dtdBinder.setIsIgnoringIDAttributes(ignoreIDAttributes);
81
82         // return the bindings.
83
return dtdBinder.getBindings();
84     }
85
86     /**
87      * <p>
88      * This handles the process of creating a <code>{@link Generator}</code>
89      * for use in data binding.
90      * </p><p>
91      * This version returns determines the <code>Generator</code>
92      * implementation to use based on user input.
93      * </p>
94      *
95      * @return <code>Generator</code> - the <code>Generator</code> to use.
96      */

97     protected Generator getGenerator() {
98         Generator generator = new SimpleGenerator();
99         return generator;
100     }
101     
102     /**
103      * <p>
104      * This sets the name of the root element to use for generation of classes
105      * in this DTD.
106      * </p>
107      *
108      * @param rootElementName the root element's name to use.
109      */

110     public void setRootElementName(String JavaDoc rootElementName) {
111         this.rootElementName = rootElementName;
112     }
113
114     /**
115      * <p>
116      * This provides command-line class generation.
117      * </p>
118      *
119      * @param args <code>String</code> array of command-line arguments.
120      */

121     public static void main(String JavaDoc[] args) {
122         try {
123             Arguments arguments = new Arguments(args);
124
125             // Generate source
126
DTDSourceGenerator sourceGenerator =
127                 new DTDSourceGenerator();
128
129             // Ensure correct usage
130
String JavaDoc constraintsFilename =
131                 arguments.getValue("constraints");
132             if (constraintsFilename == null) {
133                 System.out.println("Usage: java " +
134                     sourceGenerator.getClass().getName() +
135                     "\n -constraints=<constraints filename>" +
136                     "\n [-outputDir=<output directory>]" +
137                     "\n [-collapseSimpleElements=<true | false>]" +
138                     "\n [-ignoreIDAttributes=<true | false>]" +
139                     "\n [-javaPackage=<Java package name>]" +
140                     "\n [-root=<Root Element Name>]");
141                 return;
142             }
143
144             // Get command-line options
145
String JavaDoc outputDirName = arguments.getValue("outputDir");
146             String JavaDoc collapseSimpleElements =
147                 arguments.getValue("collapseSimpleElements");
148             String JavaDoc ignoreIDAttributes =
149                 arguments.getValue("ignoreIDAttributes");
150             String JavaDoc javaPackage = arguments.getValue("javaPackage");
151             String JavaDoc rootElementName = arguments.getValue("root");
152
153             sourceGenerator.setConstraintsInput(constraintsFilename);
154
155             if (outputDirName != null) {
156                 sourceGenerator.setOutputDir(outputDirName);
157             }
158             if (javaPackage != null) {
159                 sourceGenerator.setJavaPackage(javaPackage);
160             }
161             if ((collapseSimpleElements != null) &&
162                 (collapseSimpleElements.equalsIgnoreCase("true"))) {
163                 if ((ignoreIDAttributes != null) &&
164                     (ignoreIDAttributes.equalsIgnoreCase("true"))) {
165                     sourceGenerator.setCollapseSimpleElements(true, true);
166                 } else {
167                     sourceGenerator.setCollapseSimpleElements(true, false);
168                 }
169             }
170             if (rootElementName != null) {
171                 sourceGenerator.setRootElementName(rootElementName);
172             }
173
174             // Perform actual class generation
175
sourceGenerator.generate();
176
177         } catch (IOException JavaDoc e) {
178             System.out.println("Error generating source code: " +
179                                e.getMessage());
180             e.printStackTrace();
181         } catch (ZeusException e) {
182             System.out.println("Error generating source code: " +
183                                e.getMessage());
184             e.printStackTrace();
185         }
186     }
187 }
188
Popular Tags