1/*
2 * Copyright (C) 2010 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
17package com.android.cts.apicoverage;
18
19import org.xml.sax.Attributes;
20import org.xml.sax.SAXException;
21import org.xml.sax.helpers.DefaultHandler;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * {@link DefaultHander} that parses the output of dexdeps and adds the coverage information to
28 * an {@link ApiCoverage} object.
29 */
30class DexDepsXmlHandler extends DefaultHandler {
31
32    private final ApiCoverage mPackageMap;
33
34    private String mCurrentPackageName;
35
36    private String mCurrentClassName;
37
38    private String mCurrentMethodName;
39
40    private String mCurrentMethodReturnType;
41
42    private List<String> mCurrentParameterTypes = new ArrayList<String>();
43
44    DexDepsXmlHandler(ApiCoverage packageMap) {
45        this.mPackageMap = packageMap;
46    }
47
48    @Override
49    public void startElement(String uri, String localName, String name, Attributes attributes)
50            throws SAXException {
51        super.startElement(uri, localName, name, attributes);
52        if ("package".equalsIgnoreCase(localName)) {
53            mCurrentPackageName = CurrentXmlHandler.getValue(attributes, "name");
54        } else if ("class".equalsIgnoreCase(localName)
55                || "interface".equalsIgnoreCase(localName)) {
56            mCurrentClassName = CurrentXmlHandler.getValue(attributes, "name");
57        } else if ("constructor".equalsIgnoreCase(localName)) {
58            mCurrentParameterTypes.clear();
59        }  else if ("method".equalsIgnoreCase(localName)) {
60            mCurrentMethodName = CurrentXmlHandler.getValue(attributes, "name");
61            mCurrentMethodReturnType = CurrentXmlHandler.getValue(attributes, "return");
62            mCurrentParameterTypes.clear();
63        } else if ("parameter".equalsIgnoreCase(localName)) {
64            mCurrentParameterTypes.add(CurrentXmlHandler.getValue(attributes, "type"));
65        }
66    }
67
68    @Override
69    public void endElement(String uri, String localName, String name) throws SAXException {
70        super.endElement(uri, localName, name);
71        if ("constructor".equalsIgnoreCase(localName)) {
72            ApiPackage apiPackage = mPackageMap.getPackage(mCurrentPackageName);
73            if (apiPackage != null) {
74                ApiClass apiClass = apiPackage.getClass(mCurrentClassName);
75                if (apiClass != null) {
76                    ApiConstructor apiConstructor = apiClass.getConstructor(mCurrentParameterTypes);
77                    if (apiConstructor != null) {
78                        apiConstructor.setCovered(true);
79                    }
80                }
81            }
82        }  else if ("method".equalsIgnoreCase(localName)) {
83            ApiPackage apiPackage = mPackageMap.getPackage(mCurrentPackageName);
84            if (apiPackage != null) {
85                ApiClass apiClass = apiPackage.getClass(mCurrentClassName);
86                if (apiClass != null) {
87                    ApiMethod apiMethod = apiClass.getMethod(mCurrentMethodName,
88                            mCurrentParameterTypes, mCurrentMethodReturnType);
89                    if (apiMethod != null) {
90                        apiMethod.setCovered(true);
91                    }
92                }
93            }
94        }
95    }
96}