aboutsummaryrefslogtreecommitdiffstats
path: root/apigenerator/src/com/android/apigenerator/ApiClass.java
blob: 50ee8c8df58fc86e936786c11aedca9f7e0d78e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.apigenerator;

import com.android.utils.Pair;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * Represents a class and its methods/fields.
 * This is used to write the simplified XML file containing all the public API.
 *
 */
public class ApiClass {

    private final String mName;
    private final int mSince;

    private final List<Pair<String, Integer>> mSuperClasses =
            new ArrayList<Pair<String, Integer>>();

    private final List<Pair<String, Integer>> mInterfaces = new ArrayList<Pair<String, Integer>>();

    private final Map<String, Integer> mFields = new HashMap<String, Integer>();
    private final Map<String, Integer> mMethods = new HashMap<String, Integer>();

    public ApiClass(String name, int since) {
        mName = name;
        mSince = since;
    }

    public String getName() {
        return mName;
    }

    int getSince() {
        return mSince;
    }

    public void addField(String name, int since) {
        Integer i = mFields.get(name);
        if (i == null || i.intValue() > since) {
            mFields.put(name, Integer.valueOf(since));
        }
    }

    public void addMethod(String name, int since) {
        Integer i = mMethods.get(name);
        if (i == null || i.intValue() > since) {
            mMethods.put(name, Integer.valueOf(since));
        }
    }

    public Map<String, Integer> getMethods() {
        return mMethods;
    }

    public void replaceMethods(Map<String, Integer> fixedMethods) {
        mMethods.clear();
        mMethods.putAll(fixedMethods);
    }

    public void addSuperClass(String superClass, int since) {
        addToArray(mSuperClasses, superClass, since);
    }

    public List<Pair<String, Integer>> getSuperClasses() {
        return mSuperClasses;
    }

    public void addInterface(String interfaceClass, int since) {
        addToArray(mInterfaces, interfaceClass, since);
    }

    public List<Pair<String, Integer>> getInterfaces() {
        return mInterfaces;
    }

    void addToArray(List<Pair<String, Integer>> list, String name, int value) {
        // check if we already have that name (at a lower level)
        for (Pair<String, Integer> pair : list) {
            if (name.equals(pair.getFirst()) && pair.getSecond() < value) {
                return;
            }
        }

        list.add(Pair.of(name, Integer.valueOf(value)));
    }

    public void print(PrintStream stream) {
        stream.print("\t<class name=\"");
        stream.print(mName);
        stream.print("\" since=\"");
        stream.print(mSince);
        stream.println("\">");

        print(mSuperClasses, "extends", stream);
        print(mInterfaces, "implements", stream);
        print(mMethods, "method", stream);
        print(mFields, "field", stream);

        stream.println("\t</class>");
    }

    private void print(List<Pair<String, Integer> > list, String name, PrintStream stream) {
        Collections.sort(list, new Comparator<Pair<String, Integer> >() {

            @Override
            public int compare(Pair<String, Integer> o1, Pair<String, Integer> o2) {
                return o1.getFirst().compareTo(o2.getFirst());
            }
        });

        for (Pair<String, Integer> pair : list) {
            if (mSince == pair.getSecond()) {
                stream.print("\t\t<");
                stream.print(name);
                stream.print(" name=\"");
                stream.print(encodeAttribute(pair.getFirst()));
                stream.println("\" />");
            } else {
                stream.print("\t\t<");
                stream.print(name);
                stream.print(" name=\"");
                stream.print(encodeAttribute(pair.getFirst()));
                stream.print("\" since=\"");
                stream.print(pair.getSecond());
                stream.println("\" />");
            }
        }
    }

    private void print(Map<String, Integer> map, String name, PrintStream stream) {
        TreeMap<String, Integer> map2 = new TreeMap<String, Integer>(map);

        for (Entry<String, Integer> entry : map2.entrySet()) {
            if (mSince == entry.getValue()) {
                stream.print("\t\t<");
                stream.print(name);
                stream.print(" name=\"");
                stream.print(encodeAttribute(entry.getKey()));
                stream.println("\" />");
            } else {
                stream.print("\t\t<");
                stream.print(name);
                stream.print(" name=\"");
                stream.print(encodeAttribute(entry.getKey()));
                stream.print("\" since=\"");
                stream.print(entry.getValue());
                stream.println("\" />");
            }
        }
    }

    private String encodeAttribute(String attribute) {
        StringBuilder sb = new StringBuilder();
        int n = attribute.length();
        // &, ", ' and < are illegal in attributes; see http://www.w3.org/TR/REC-xml/#NT-AttValue
        // (' legal in a " string and " is legal in a ' string but here we'll stay on the safe
        // side)
        for (int i = 0; i < n; i++) {
            char c = attribute.charAt(i);
            if (c == '"') {
                sb.append("&quot;"); //$NON-NLS-1$
            } else if (c == '<') {
                sb.append("&lt;"); //$NON-NLS-1$
            } else if (c == '\'') {
                sb.append("&apos;"); //$NON-NLS-1$
            } else if (c == '&') {
                sb.append("&amp;"); //$NON-NLS-1$
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    @Override
    public String toString() {
        return mName;
    }
}