1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.xsdc;
18 
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.nio.file.Paths;
22 
23 import static java.lang.System.exit;
24 
25 import com.android.xsdc.java.JavaCodeGenerator;
26 import com.android.xsdc.cpp.CppCodeGenerator;
27 
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.CommandLineParser;
30 import org.apache.commons.cli.GnuParser;
31 import org.apache.commons.cli.HelpFormatter;
32 import org.apache.commons.cli.OptionBuilder;
33 import org.apache.commons.cli.Options;
34 import org.apache.commons.cli.ParseException;
35 
36 import javax.xml.parsers.SAXParser;
37 import javax.xml.parsers.SAXParserFactory;
38 
39 public class Main {
main(String[] args)40     public static void main(String[] args) throws Exception {
41         Options options = new Options();
42         options.addOption(OptionBuilder
43                 .withLongOpt("package")
44                 .hasArgs(1)
45                 .withDescription("Package name of the generated java file. " +
46                         "file name of generated cpp file and header")
47                 .create("p"));
48         options.addOption(OptionBuilder
49                 .withLongOpt("outDir")
50                 .hasArgs(1)
51                 .withDescription("Out Directory")
52                 .create("o"));
53         options.addOption(OptionBuilder
54                 .withLongOpt("java")
55                 .hasArgs(0)
56                 .withDescription("Generate Java code.")
57                 .create("j"));
58         options.addOption(OptionBuilder
59                 .withLongOpt("cpp")
60                 .hasArgs(0)
61                 .withDescription("Generate Cpp code.")
62                 .create("c"));
63         options.addOption(OptionBuilder
64                 .withLongOpt("writer")
65                 .hasArgs(0)
66                 .withDescription("Generate Writer code.")
67                 .create("w"));
68 
69         CommandLineParser CommandParser = new GnuParser();
70         CommandLine cmd;
71 
72         try {
73             cmd = CommandParser.parse(options, args);
74         } catch (ParseException e) {
75             System.err.println(e.getMessage());
76             help(options);
77             return;
78         }
79 
80         String[] xsdFile = cmd.getArgs();
81         String packageName = cmd.getOptionValue('p', null);
82         String outDir = cmd.getOptionValue('o', null);
83         boolean writer = cmd.hasOption('w');
84 
85         if (xsdFile.length != 1 || packageName == null) {
86             System.err.println("Error: no xsd files or pacakge name");
87             help(options);
88         }
89 
90         if (outDir == null) {
91             outDir = ".";
92         }
93 
94         XmlSchema xmlSchema;
95         try (FileInputStream in = new FileInputStream(xsdFile[0])) {
96             SAXParserFactory factory = SAXParserFactory.newInstance();
97             factory.setNamespaceAware(true);
98             SAXParser parser = factory.newSAXParser();
99             XsdHandler xsdHandler = new XsdHandler();
100             parser.parse(in, xsdHandler);
101             xmlSchema = xsdHandler.getSchema();
102         }
103 
104         if (cmd.hasOption('j')) {
105             File packageDir = new File(Paths.get(outDir, packageName.replace(".", "/")).toString());
106             packageDir.mkdirs();
107             FileSystem fs = new FileSystem(packageDir);
108             JavaCodeGenerator javaCodeGenerator =
109                     new JavaCodeGenerator(xmlSchema, packageName, writer);
110             javaCodeGenerator.print(fs);
111         } else if (cmd.hasOption('c')) {
112             File includeDir = new File(Paths.get(outDir, "include").toString());
113             includeDir.mkdirs();
114             FileSystem fs = new FileSystem(new File(outDir));
115             CppCodeGenerator cppCodeGenerator =
116                     new CppCodeGenerator(xmlSchema, packageName, writer);
117             cppCodeGenerator.print(fs);
118         }
119     }
120 
help(Options options)121     private static void help(Options options) {
122         new HelpFormatter().printHelp(
123                 "xsdc path/to/xsd_file.xsd","", options, null, true);
124         System.exit(1);
125     }
126 }
127