1 /*
2  * Copyright (C) 2024 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.aslgen;
18 
19 import com.android.asllib.AslConverter;
20 import com.android.asllib.AslConverter.Format;
21 import com.android.asllib.util.MalformedXmlException;
22 
23 import org.xml.sax.SAXException;
24 
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.transform.TransformerException;
31 
32 public class Main {
33 
34     /** Takes the options to make file conversion. */
main(String[] args)35     public static void main(String[] args)
36             throws IOException,
37                     ParserConfigurationException,
38                     SAXException,
39                     TransformerException,
40                     MalformedXmlException {
41 
42         String inFile = null;
43         String outFile = null;
44         Format inFormat = AslConverter.Format.NULL;
45         Format outFormat = AslConverter.Format.NULL;
46 
47         // Except for "--help", all arguments require a value currently.
48         // So just make sure we have an even number and
49         // then process them all two at a time.
50         if (args.length == 1 && "--help".equals(args[0])) {
51             showUsage();
52             return;
53         }
54         if (args.length % 2 != 0) {
55             throw new IllegalArgumentException("Argument is missing corresponding value");
56         }
57         for (int i = 0; i < args.length - 1; i += 2) {
58             final String arg = args[i].trim();
59             final String argValue = args[i + 1].trim();
60             if ("--in-path".equals(arg)) {
61                 inFile = argValue;
62             } else if ("--out-path".equals(arg)) {
63                 outFile = argValue;
64             } else if ("--in-format".equals(arg)) {
65                 inFormat = getFormat(argValue);
66             } else if ("--out-format".equals(arg)) {
67                 outFormat = getFormat(argValue);
68             } else {
69                 throw new IllegalArgumentException("Unknown argument: " + arg);
70             }
71         }
72 
73         if (inFile == null) {
74             throw new IllegalArgumentException("input file is required");
75         }
76 
77         if (outFile == null) {
78             throw new IllegalArgumentException("output file is required");
79         }
80 
81         if (inFormat == AslConverter.Format.NULL) {
82             throw new IllegalArgumentException("input format is required");
83         }
84 
85         if (outFormat == AslConverter.Format.NULL) {
86             throw new IllegalArgumentException("output format is required");
87         }
88 
89         System.out.println("in path: " + inFile);
90         System.out.println("out path: " + outFile);
91         System.out.println("in format: " + inFormat);
92         System.out.println("out format: " + outFormat);
93 
94         var asl = AslConverter.readFromStream(new FileInputStream(inFile), inFormat);
95         AslConverter.writeToStream(new FileOutputStream(outFile), asl, outFormat);
96     }
97 
getFormat(String argValue)98     private static Format getFormat(String argValue) {
99         if ("hr".equals(argValue)) {
100             return AslConverter.Format.HUMAN_READABLE;
101         } else if ("od".equals(argValue)) {
102             return AslConverter.Format.ON_DEVICE;
103         } else {
104             return AslConverter.Format.NULL;
105         }
106     }
107 
showUsage()108     private static void showUsage() {
109         System.err.println(
110                 "Usage: aslgen --in-path [input-file] --out-path [output-file] --in-format [hr|od]"
111                         + " --out-format [hr|od]");
112     }
113 }
114