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.cts.releaseparser;
18 
19 import com.android.cts.releaseparser.ReleaseProto.*;
20 
21 import org.xml.sax.InputSource;
22 import org.xml.sax.XMLReader;
23 import org.xml.sax.helpers.XMLReaderFactory;
24 
25 import java.io.File;
26 import java.io.FileReader;
27 import java.util.HashMap;
28 import java.util.logging.Logger;
29 
30 public class XmlParser extends FileParser {
31     private XmlHandler mHandler;
32     private HashMap<String, PermissionList> mPermissions;
33 
XmlParser(File file)34     public XmlParser(File file) {
35         super(file);
36     }
37 
38     @Override
getType()39     public Entry.EntryType getType() {
40         return Entry.EntryType.XML;
41     }
42 
43     @Override
setAdditionalInfo()44     public void setAdditionalInfo() {
45         HashMap<String, PermissionList> permissions = getPermissions();
46         if (permissions != null) {
47             getFileEntryBuilder().putAllDevicePermissions(permissions);
48         }
49     }
50 
getPermissions()51     public HashMap<String, PermissionList> getPermissions() {
52         if (mPermissions == null) {
53             parse();
54         }
55         return mPermissions;
56     }
57 
58     // Todo readPermissions() from frameworks/base/core/java/com/android/server/SystemConfig.java
59     // for Feature set
parse()60     private void parse() {
61         try {
62             mHandler = new XmlHandler(getFileName());
63             XMLReader xmlReader = XMLReaderFactory.createXMLReader();
64             xmlReader.setContentHandler(mHandler);
65             FileReader fileReader = new FileReader(getFile());
66             xmlReader.parse(new InputSource(fileReader));
67             mPermissions = mHandler.getPermissions();
68             fileReader.close();
69         } catch (Exception e) {
70             // file is not a Test Module Config
71             System.err.println("Fail to parse:" + getFileName() + "\n" + e.getMessage());
72         }
73     }
74 
75     private static final String USAGE_MESSAGE =
76             "Usage: java -jar releaseparser.jar "
77                     + XmlParser.class.getCanonicalName()
78                     + " [-options <parameter>]...\n"
79                     + "           to prase platform permissions xml file meta data\n"
80                     + "Options:\n"
81                     + "\t-i PATH\t The file path of the file to be parsed.\n"
82                     + "\t-of PATH\t The file path of the output file instead of printing to System.out.\n";
83 
main(String[] args)84     public static void main(String[] args) {
85         try {
86             ArgumentParser argParser = new ArgumentParser(args);
87             String fileName = argParser.getParameterElement("i", 0);
88             String outputFileName = argParser.getParameterElement("of", 0);
89 
90             File aFile = new File(fileName);
91             XmlParser aParser = new XmlParser(aFile);
92             Entry.Builder fileEntryBuilder = aParser.getFileEntryBuilder();
93             writeTextFormatMessage(outputFileName, fileEntryBuilder.build());
94         } catch (Exception ex) {
95             System.out.println(USAGE_MESSAGE);
96             ex.printStackTrace();
97         }
98     }
99 
getLogger()100     private static Logger getLogger() {
101         return Logger.getLogger(XmlParser.class.getSimpleName());
102     }
103 }
104