01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.schema;
20:
21: import org.apache.axis2.schema.i18n.SchemaCompilerMessages;
22: import org.apache.ws.commons.schema.XmlSchema;
23: import org.apache.ws.commons.schema.XmlSchemaCollection;
24: import org.w3c.dom.Document;
25:
26: import javax.xml.parsers.DocumentBuilder;
27: import javax.xml.parsers.DocumentBuilderFactory;
28: import java.io.File;
29: import java.io.IOException;
30:
31: public class XSD2Java {
32:
33: /**
34: * for now the arguments this main method accepts is the source schema and the output
35: * location
36: *
37: * @param args
38: */
39: public static void main(String[] args) throws Exception {
40: if (args.length != 2) {
41: // printout the options
42: System.out.println(SchemaCompilerMessages
43: .getMessage("schema.xsdarg1"));
44: System.out.println(SchemaCompilerMessages
45: .getMessage("schema.xsdarg2"));
46: } else {
47: compile(args[0], args[1]);
48: }
49:
50: }
51:
52: /**
53: * @param xsdName
54: * @param outputLocation
55: */
56: private static void compile(String xsdName, String outputLocation)
57: throws Exception {
58: //load the current Schema through a file
59: //first read the file into a DOM
60: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
61: .newInstance();
62: documentBuilderFactory.setNamespaceAware(true);
63:
64: DocumentBuilder builder = documentBuilderFactory
65: .newDocumentBuilder();
66: Document doc = builder.parse(new File(xsdName));
67:
68: //now read it to a schema
69: XmlSchemaCollection schemaCol = new XmlSchemaCollection();
70: XmlSchema currentSchema = schemaCol.read(doc, null);
71:
72: File outputFolder = new File(outputLocation);
73: if (outputFolder.exists()) {
74: if (outputFolder.isFile()) {
75: throw new IOException(SchemaCompilerMessages
76: .getMessage("schema.locationNotFolder"));
77: }
78: } else {
79: outputFolder.mkdirs();
80: }
81:
82: CompilerOptions compilerOptions = new CompilerOptions();
83: compilerOptions.setOutputLocation(outputFolder);
84: compilerOptions.setGenerateAll(true);
85:
86: //todo - this should come from the users preferences
87: compilerOptions.setWrapClasses(false);
88:
89: //there's no point in not writing the classes here.
90: compilerOptions.setWriteOutput(true);
91:
92: SchemaCompiler compiler = new SchemaCompiler(compilerOptions);
93: compiler.compile(currentSchema);
94: }
95: }
|