KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestDTDBinder


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
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream 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.Result;
30 import org.enhydra.zeus.Transformer;
31 import org.enhydra.zeus.binder.DTDBinder;
32 import org.enhydra.zeus.binding.Container;
33 import org.enhydra.zeus.binding.Property;
34 import org.enhydra.zeus.generator.SimpleGenerator;
35 import org.enhydra.zeus.result.StreamResult;
36 import org.enhydra.zeus.source.DTDSource;
37 import org.enhydra.zeus.source.StreamDTDSource;
38 import org.enhydra.zeus.transform.DefaultsTransformer;
39 import org.enhydra.zeus.util.Arguments;
40
41 public class TestDTDBinder {
42
43     public static void main(String JavaDoc[] args) {
44         Arguments theArguments = new Arguments(args);
45
46         // Using Arguments, parameters can be sent in any order
47
if (!theArguments.hasValue("file")) {
48             System.out.println(
49                 "Usage: java samples.TestDTDBinder -file=<file name> " +
50                     "[-package=<package>] [-quiet=true/false] " +
51                     "[-generateAsSerializable=true/false]");
52             return;
53         }
54
55         try {
56             // This is to match the original default action
57
boolean isQuiet = false;
58             if (theArguments.hasValue("quiet")) {
59                 isQuiet = theArguments.getValue("quiet")
60                                       .trim()
61                                       .equalsIgnoreCase("true");
62             }
63
64             // Handle Java package for generated classes
65
String JavaDoc javaPackage;
66             if (theArguments.hasValue("package")) {
67                 javaPackage = theArguments.getValue("package");
68             } else {
69
70                 // Set the package to the default package if not specified.
71
javaPackage = "";
72             }
73             if (!isQuiet) {
74                 System.out.println("+ Set Java package to '" + javaPackage + "'");
75             }
76
77             // Get handle to the DTD
78
File JavaDoc dtd = new File JavaDoc(theArguments.getValue("file"));
79             DTDSource source = new StreamDTDSource(new FileInputStream JavaDoc(dtd));
80             if (!isQuiet) {
81                 System.out.println("+ Reading DTD from '" + dtd.getAbsolutePath() + "'");
82             }
83
84             // Get the Zeus Bindings
85
Binder dtdBinder = new DTDBinder(source);
86             List JavaDoc bindings = dtdBinder.getBindings();
87             if (!isQuiet) {
88                 System.out.println("+ Loaded bindings using the DTDBinder");
89             }
90
91             // Transform the bindings
92
Transformer transformer = new DefaultsTransformer();
93             transformer.getTransformerOptions().setDefaultPackage(javaPackage);
94             List JavaDoc transformedBindings = transformer.transform(bindings);
95             if (!isQuiet) {
96                 System.out.println("+ Transformed the bindings using the DefaultsTransformer");
97             }
98
99             // Do some printing
100
if (!isQuiet) {
101                 for (Iterator JavaDoc i = transformedBindings.iterator(); i.hasNext();) {
102                     Binding binding = (Binding) i.next();
103                     System.out.println("+ --> Binding Type: " + binding.getClass().getName());
104                     System.out.println("+ --> Binding Name: " + binding.getJavaName());
105                     System.out.println("+ --> Properties");
106
107                     try {
108                         Container container = (Container)binding;
109                         List JavaDoc properties = container.getProperties();
110                         if (properties.size() > 0) {
111                             for (Iterator JavaDoc j = properties.iterator(); j.hasNext();) {
112                                 Property property = (Property) j.next();
113                                 System.out.println("+ -----> Property Name: " +
114                                     property.getJavaName());
115                                 System.out.println("+ -----> Property Type: " +
116                                     property.getJavaType());
117                             }
118                         } else {
119                             System.out.println(" (None)");
120                         }
121                     } catch (ClassCastException JavaDoc e) {
122                         System.out.println("+ -----> (None)");
123                     }
124                     System.out.println("+");
125                 }
126             }
127
128             // Prepare for class generation
129
Generator generator = new SimpleGenerator();
130             generator.setOutputDirectory(new File JavaDoc("output"));
131             if (!isQuiet) {
132                 System.out.println("+ Set Output Directory to '" +
133                     new File JavaDoc("output").getAbsolutePath() + "'");
134             }
135
136
137             String JavaDoc generateAsSerializable =
138                 (String JavaDoc)theArguments.getValue("generateAsSerializable");
139             if ((generateAsSerializable != null) &&
140                 (generateAsSerializable.equalsIgnoreCase("true"))) {
141
142                 //generator.setGenerateAsSerializable(true);
143
}
144
145
146             for (Iterator JavaDoc i = transformedBindings.iterator(); i.hasNext();) {
147                 Binding binding = (Binding) i.next();
148                 generator.generate(binding);
149             }
150
151         } catch (Exception JavaDoc e) {
152             e.printStackTrace();
153         }
154     }
155 }
156
Popular Tags