summaryrefslogtreecommitdiffstats
path: root/src/com/google/doclava/apicheck/ApiCheck.java
blob: 28d7ce0ab401ea507d9c4cb6f28e79c07f026ecb (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (C) 2010 Google Inc.
 *
 * 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.google.doclava.apicheck;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Set;
import java.util.Stack;

import com.google.doclava.Errors;
import com.google.doclava.Errors.ErrorMessage;
import com.google.doclava.Stubs;

public class ApiCheck {
  // parse out and consume the -whatever command line flags
  private static ArrayList<String[]> parseFlags(ArrayList<String> allArgs) {
    ArrayList<String[]> ret = new ArrayList<String[]>();

    int i;
    for (i = 0; i < allArgs.size(); i++) {
      // flags with one value attached
      String flag = allArgs.get(i);
      if (flag.equals("-error") || flag.equals("-warning") || flag.equals("-hide")) {
        String[] arg = new String[2];
        arg[0] = flag;
        arg[1] = allArgs.get(++i);
        ret.add(arg);
      } else {
        // we've consumed all of the -whatever args, so we're done
        break;
      }
    }

    // i now points to the first non-flag arg; strip what came before
    for (; i > 0; i--) {
      allArgs.remove(0);
    }
    return ret;
  }

  public static void main(String[] originalArgs) {
    if (originalArgs.length == 3 && "-convert".equals(originalArgs[0])) {
      System.exit(convertToApi(originalArgs[1], originalArgs[2]));
    } else if (originalArgs.length == 3 && "-convert2xml".equals(originalArgs[0])) {
      System.exit(convertToXml(originalArgs[1], originalArgs[2]));
    } else {
      ApiCheck acheck = new ApiCheck();
      Report report = acheck.checkApi(originalArgs);

      Errors.printErrors(report.errors());
      System.exit(report.code);
    }
  }
  
  /**
   * Compares two api xml files for consistency.
   */
  public Report checkApi(String[] originalArgs) {
    // translate to an ArrayList<String> for munging
    ArrayList<String> args = new ArrayList<String>(originalArgs.length);
    for (String a : originalArgs) {
      args.add(a);
    }

    ArrayList<String[]> flags = ApiCheck.parseFlags(args);
    for (String[] a : flags) {
      if (a[0].equals("-error") || a[0].equals("-warning") || a[0].equals("-hide")) {
        try {
          int level = -1;
          if (a[0].equals("-error")) {
            level = Errors.ERROR;
          } else if (a[0].equals("-warning")) {
            level = Errors.WARNING;
          } else if (a[0].equals("-hide")) {
            level = Errors.HIDDEN;
          }
          Errors.setErrorLevel(Integer.parseInt(a[1]), level);
        } catch (NumberFormatException e) {
          System.err.println("Bad argument: " + a[0] + " " + a[1]);
          return new Report(2, Errors.getErrors());
        }
      }
    }

    ApiInfo oldApi;
    ApiInfo newApi;
    ApiInfo oldRemovedApi;
    ApiInfo newRemovedApi;

    // commandline options look like:
    // [other optoins] old_api.txt new_api.txt old_removed_api.txt new_removed_api.txt
    try {
      oldApi = parseApi(args.get(0));
      newApi = parseApi(args.get(1));
      oldRemovedApi = parseApi(args.get(2));
      newRemovedApi = parseApi(args.get(3));
    } catch (ApiParseException e) {
      e.printStackTrace();
      System.err.println("Error parsing API");
      return new Report(1, Errors.getErrors());
    }

    // only run the consistency check if we haven't had XML parse errors
    if (!Errors.hadError) {
      oldApi.isConsistent(newApi);
    }

    if (!Errors.hadError) {
      oldRemovedApi.isConsistent(newRemovedApi);
    }

    return new Report(Errors.hadError ? 1 : 0, Errors.getErrors());
  }

  public static ApiInfo parseApi(String filename) throws ApiParseException {
    InputStream stream = null;
    Throwable textParsingError = null;
    // try it as our format
    try {
      stream = new FileInputStream(filename);
    } catch (IOException e) {
      throw new ApiParseException("Could not open file for parsing: " + filename, e);
    }
    try {
      return ApiFile.parseApi(filename, stream);
    } catch (ApiParseException ignored) {
      textParsingError = ignored;
      if (false) {
        return null;
      }
    } finally {
      try {
        stream.close();
      } catch (IOException ignored) {}
    }
    // try it as xml
    try {
      stream = new FileInputStream(filename);
    } catch (IOException e) {
      throw new ApiParseException("Could not open file for parsing: " + filename, e);
    }
    try {
      return XmlApiFile.parseApi(stream);
    } catch (ApiParseException ignored) {
        System.out.println("Couldn't parse API file \"" + filename + "\"");
        System.out.println("  ...as text: " + textParsingError.toString());
        System.out.println("  ...as XML:  " + ignored.toString());
        if (false) {
          if (textParsingError != null) textParsingError.printStackTrace();
          ignored.printStackTrace();
          return null;
        }
    } finally {
      try {
        stream.close();
      } catch (IOException ignored) {}
    }
    return null;
  }

  public ApiInfo parseApi(URL url) throws ApiParseException {
    InputStream stream = null;
    // try it as our format
    try {
      stream = url.openStream();
    } catch (IOException e) {
      throw new ApiParseException("Could not open stream for parsing: " + url, e);
    }
    try {
      return ApiFile.parseApi(url.toString(), stream);
    } catch (ApiParseException ignored) {
    } finally {
      try {
        stream.close();
      } catch (IOException ignored) {}
    }
    // try it as xml
    try {
      stream = url.openStream();
    } catch (IOException e) {
      throw new ApiParseException("Could not open stream for parsing: " + url, e);
    }
    try {
      return XmlApiFile.parseApi(stream);
    } finally {
      try {
        stream.close();
      } catch (IOException ignored) {}
    }
  }

  public class Report {
    private int code;
    private Set<ErrorMessage> errors;
    
    private Report(int code, Set<ErrorMessage> errors) {
      this.code = code;
      this.errors = errors;
    }
    
    public int code() {
      return code;
    }
    
    public Set<ErrorMessage> errors() {
      return errors;
    }
  }

  static int convertToApi(String src, String dst) {
    ApiInfo api;
    try {
      api = parseApi(src);
    } catch (ApiParseException e) {
      e.printStackTrace();
      System.err.println("Error parsing API: " + src);
      return 1;
    }

    PrintStream apiWriter = null;
    try {
      apiWriter = new PrintStream(dst);
    } catch (FileNotFoundException ex) {
      System.err.println("can't open file: " + dst);
    }

    Stubs.writeApi(apiWriter, api.getPackages().values());

    return 0;
  }

  static int convertToXml(String src, String dst) {
    ApiInfo api;
    try {
      api = parseApi(src);
    } catch (ApiParseException e) {
      e.printStackTrace();
      System.err.println("Error parsing API: " + src);
      return 1;
    }

    PrintStream apiWriter = null;
    try {
      apiWriter = new PrintStream(dst);
    } catch (FileNotFoundException ex) {
      System.err.println("can't open file: " + dst);
    }

    Stubs.writeXml(apiWriter, api.getPackages().values());

    return 0;
  }

}