• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
64         CommandLineParser CommandParser = new GnuParser();
65         CommandLine cmd;
66 
67         try {
68             cmd = CommandParser.parse(options, args);
69         } catch (ParseException e) {
70             System.err.println(e.getMessage());
71             help(options);
72             return;
73         }
74 
75         String[] xsdFile = cmd.getArgs();
76         String packageName = cmd.getOptionValue('p', null);
77         String outDir = cmd.getOptionValue('o', null);
78 
79         if (xsdFile.length != 1 || packageName == null) {
80             System.err.println("Error: no xsd files or pacakge name");
81             help(options);
82         }
83 
84         if (outDir == null) {
85             outDir = ".";
86         }
87 
88         XmlSchema xmlSchema;
89         try (FileInputStream in = new FileInputStream(xsdFile[0])) {
90             SAXParserFactory factory = SAXParserFactory.newInstance();
91             factory.setNamespaceAware(true);
92             SAXParser parser = factory.newSAXParser();
93             XsdHandler xsdHandler = new XsdHandler();
94             parser.parse(in, xsdHandler);
95             xmlSchema = xsdHandler.getSchema();
96         }
97 
98         if (cmd.hasOption('j')) {
99             File packageDir = new File(Paths.get(outDir, packageName.replace(".", "/")).toString());
100             packageDir.mkdirs();
101             FileSystem fs = new FileSystem(packageDir);
102             JavaCodeGenerator javaCodeGenerator = new JavaCodeGenerator(xmlSchema, packageName);
103             javaCodeGenerator.print(fs);
104         } else if (cmd.hasOption('c')) {
105             File includeDir = new File(Paths.get(outDir, "include").toString());
106             includeDir.mkdirs();
107             FileSystem fs = new FileSystem(new File(outDir));
108             CppCodeGenerator cppCodeGenerator = new CppCodeGenerator(xmlSchema,
109                     packageName.replace(".", "_"));
110             cppCodeGenerator.print(fs);
111         }
112     }
113 
help(Options options)114     private static void help(Options options) {
115         new HelpFormatter().printHelp(
116                 "xsdc path/to/xsd_file.xsd","", options, null, true);
117         System.exit(1);
118     }
119 }
120