KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
23
24 // Zeus imports
25
import org.enhydra.zeus.Binder;
26 import org.enhydra.zeus.Source;
27 import org.enhydra.zeus.ZeusException;
28 import org.enhydra.zeus.binder.SchemaBinder;
29 import org.enhydra.zeus.source.StreamSource;
30
31 /**
32  * <p>
33  * This is a standalone "utility" class. It allows source code
34  * generation from an XML Schema (XSD).
35  * </p>
36  *
37  * @author Brett McLaughlin
38  */

39 public class XSDSourceGenerator extends BaseSourceGenerator
40     implements SourceGenerator {
41
42     /**
43      * <p>
44      * This sets up defaults for the generator.
45      * </p>
46      */

47     public XSDSourceGenerator() {
48         super();
49     }
50
51     /**
52      * <p>
53      * This defines a contract for subclasses to allow them to get the
54      * Zeus <code>{@link org.enhydra.zeus.Binding}</code> objects to generate
55      * code from.
56      * </p>
57      *
58      * @return <code>List</code> - a list of the Binding
59      * objects to generate code from.
60      * @throws <code>IOException</code> - when binding creation fails
61      */

62     protected List JavaDoc getConstraintBindings() throws IOException JavaDoc {
63         Source source = new StreamSource(constraintsReader);
64         Binder xsdBinder = new SchemaBinder(source);
65         xsdBinder.setIsCollapsingSimpleElements(collapseSimpleElements);
66         xsdBinder.setIsIgnoringIDAttributes(ignoreIDAttributes);
67         
68         List JavaDoc bindings = xsdBinder.getBindings();
69         return bindings;
70     }
71
72     /**
73      * <p>
74      * This provides command-line class generation.
75      * </p>
76      *
77      * @param args <code>String</code> array of command-line arguments.
78      */

79     public static void main(String JavaDoc[] args) {
80         try {
81             Arguments arguments = new Arguments(args);
82
83             // Generate source
84
SourceGenerator sourceGenerator =
85                 new XSDSourceGenerator();
86
87             // Ensure correct usage
88
String JavaDoc constraintsFilename =
89                 arguments.getValue("constraints");
90             if (constraintsFilename == null) {
91                 System.out.println("Usage: java " +
92                                    sourceGenerator.getClass().getName() +
93                                    " -constraints=<constraints filename>" +
94                                    " [-outputDir=<output directory>" +
95                                    " [-collapseSimpleElements=" +
96                                        "<true | false>]" +
97                                    " [-ignoreIDAttributes=<true | false>]" +
98                                    " [-javaPackage=<Java package name>]");
99                 return;
100             }
101
102             // Get command-line options
103
String JavaDoc outputDirName = arguments.getValue("outputDir");
104             String JavaDoc collapseSimpleElements =
105                 arguments.getValue("collapseSimpleElements");
106             String JavaDoc ignoreIDAttributes =
107                 arguments.getValue("ignoreIDAttributes");
108             String JavaDoc javaPackage = arguments.getValue("javaPackage");
109             sourceGenerator.setConstraintsInput(constraintsFilename);
110             if (outputDirName != null) {
111                 sourceGenerator.setOutputDir(outputDirName);
112             }
113             if (javaPackage != null) {
114                 sourceGenerator.setJavaPackage(javaPackage);
115             }
116             if ((collapseSimpleElements != null) &&
117                 (collapseSimpleElements.equalsIgnoreCase("true"))) {
118                     
119                 if ((ignoreIDAttributes != null) &&
120                     (ignoreIDAttributes.equalsIgnoreCase("true"))) {
121                     
122                     sourceGenerator.setCollapseSimpleElements(true, true);
123                 }
124                 sourceGenerator.setCollapseSimpleElements(true, false);
125             }
126
127             sourceGenerator.generate();
128         } catch (IOException JavaDoc e) {
129             System.out.println("Error generating source code: " +
130                                e.getMessage());
131             e.printStackTrace();
132         } catch (ZeusException e) {
133             System.out.println("Error generating source code: " +
134                                e.getMessage());
135             e.printStackTrace();
136         }
137     }
138 }
139
Popular Tags