• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html#License
3 /**
4 *******************************************************************************
5 * Copyright (C) 2004-2015, International Business Machines Corporation and    *
6 * others. All Rights Reserved.                                                *
7 *******************************************************************************
8 */
9 
10 /**
11  * Represent a file of APIInfo records.
12  */
13 
14 package com.ibm.icu.dev.tool.docs;
15 
16 import java.io.BufferedReader;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.PrintWriter;
23 import java.util.Collections;
24 import java.util.Enumeration;
25 import java.util.Iterator;
26 import java.util.Set;
27 import java.util.TreeSet;
28 import java.util.zip.GZIPInputStream;
29 import java.util.zip.ZipEntry;
30 import java.util.zip.ZipFile;
31 
32 public final class APIData {
33     int version;
34     String name;
35     String base;
36     TreeSet<APIInfo> set;
37 
read(BufferedReader br, boolean internal)38     static APIData read(BufferedReader br, boolean internal) {
39         try {
40             APIData data = new APIData();
41 
42             data.version = Integer.parseInt(APIInfo.readToken(br)); // version
43             if (data.version > APIInfo.VERSION) {
44                 throw new IllegalArgumentException(
45                     "data version " + data.version
46                     + " is newer than current version (" + APIInfo.VERSION + ")");
47             }
48             data.name = APIInfo.readToken(br);
49             data.base = APIInfo.readToken(br); // base
50             br.readLine();
51 
52             data.set = new TreeSet(APIInfo.defaultComparator());
53             for (APIInfo info = new APIInfo(); info.read(br); info = new APIInfo()) {
54                 if (internal || !info.isInternal()) {
55                     data.set.add(info);
56                 }
57             }
58             // System.out.println("read " + data.set.size() + " record(s)");
59             return data;
60         }
61         catch (IOException e) {
62             RuntimeException re = new RuntimeException("error reading api data");
63             re.initCause(e);
64             throw re;
65         }
66     }
67 
read(File file, boolean internal)68     public static APIData read(File file, boolean internal) {
69         String fileName = file.getName();
70         ZipFile zf = null;
71         try {
72             InputStream is;
73             if (fileName.endsWith(".zip")) {
74                 zf = new ZipFile(file);
75                 Enumeration entryEnum = zf.entries();
76                 if (entryEnum.hasMoreElements()) {
77                     ZipEntry entry = (ZipEntry)entryEnum.nextElement();
78                     is = zf.getInputStream(entry);
79                     // we only handle one!!!
80                 } else {
81                     throw new IOException("zip file is empty");
82                 }
83             } else {
84                 is = new FileInputStream(file);
85                 if (fileName.endsWith(".gz")) {
86                     is = new GZIPInputStream(is);
87                 }
88             }
89             InputStreamReader isr = new InputStreamReader(is);
90             return read(new BufferedReader(isr), internal);
91         } catch (IOException e) {
92             RuntimeException re = new RuntimeException("error getting info stream: " + fileName);
93             re.initCause(e);
94             throw re;
95         } finally {
96             if (zf != null) {
97                 try {
98                     zf.close();
99                 } catch (IOException e) {
100                     RuntimeException re = new RuntimeException("failed to close the zip file: " + fileName);
101                     re.initCause(e);
102                     throw re;
103                 }
104             }
105         }
106     }
107 
read(String fileName, boolean internal)108     static APIData read(String fileName, boolean internal) {
109         return read(new File(fileName), internal);
110     }
111 
112     private static final String[] stanames = {
113         "draft", "stable", "deprecated", "obsolete", "internal"
114     };
115     private static final String[] catnames = {
116         "classes", "fields", "constructors", "methods"
117     };
118 
printStats(PrintWriter pw)119     public void printStats(PrintWriter pw) {
120         // classes, methods, fields
121         // draft, stable, other
122 
123         int[] stats = new int[catnames.length * stanames.length];
124 
125         Iterator iter = set.iterator();
126         while (iter.hasNext()) {
127             APIInfo info = (APIInfo)iter.next();
128 
129             if (info.isPublic() || info.isProtected()) {
130                 int sta = info.getVal(APIInfo.STA);
131                 int cat = info.getVal(APIInfo.CAT);
132                 stats[cat * stanames.length + sta] += 1;
133             }
134         }
135 
136         int tt = 0;
137         for (int cat = 0; cat < catnames.length; ++cat) {
138             pw.println(catnames[cat]);
139             int t = 0;
140             for (int sta = 0; sta < stanames.length; ++sta) {
141                 int v = stats[cat * stanames.length + sta];
142                 t += v;
143                 pw.println("   " + stanames[sta] + ": " + v);
144             }
145             tt += t;
146             pw.println("total: " + t);
147             pw.println();
148         }
149         pw.println("total apis: " + tt);
150     }
151 
getAPIInfoSet()152     public Set<APIInfo> getAPIInfoSet() {
153         return Collections.unmodifiableSet(set);
154     }
155 
main(String[] args)156     public static void main(String[] args) {
157         PrintWriter pw = new PrintWriter(System.out);
158 
159         boolean internal = false;
160         String path = "src/com/ibm/icu/dev/tool/docs/";
161 
162         String fn = "icu4j52.api3.gz";
163         if (args.length == 0) {
164             args = new String[] { "-file", fn };
165         }
166 
167         for (int i = 0; i < args.length; ++i) {
168             String arg = args[i];
169             if (arg.equals("-path:")) {
170                 path = args[++i];
171             } else if (arg.equals("-internal:")) {
172                 internal = args[++i].toLowerCase().charAt(0) == 't';
173             } else if (arg.equals("-file")) {
174                 fn = args[++i];
175 
176                 File f = new File(path, fn);
177                 read(f,internal).printStats(pw);
178                 pw.flush();
179             }
180         }
181     }
182 }
183