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