2 *******************************************************************************
3 * Copyright (C) 2004-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Represent a file of APIInfo records.
12 package com.ibm.icu.dev.tool.docs;
14 import java.io.BufferedReader;
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;
28 public final class APIData {
34 static APIData read(BufferedReader br, boolean internal) {
36 APIData data = new APIData();
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 + ")");
44 data.name = APIInfo.readToken(br);
45 data.base = APIInfo.readToken(br); // base
48 data.set = new TreeSet(APIInfo.defaultComparator());
49 for (APIInfo info = new APIInfo(); info.read(br); info = new APIInfo()) {
50 if (internal || !info.isInternal()) {
54 // System.out.println("read " + data.set.size() + " record(s)");
57 catch (IOException e) {
58 RuntimeException re = new RuntimeException("error reading api data");
64 static APIData read(File file, boolean internal) {
65 String fileName = file.getName();
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!!!
76 throw new IOException("zip file is empty");
79 is = new FileInputStream(file);
80 if (fileName.endsWith(".gz")) {
81 is = new GZIPInputStream(is);
84 InputStreamReader isr = new InputStreamReader(is);
85 return read(new BufferedReader(isr), internal);
87 catch (IOException e) {
88 RuntimeException re = new RuntimeException("error getting info stream: " + fileName);
94 static APIData read(String fileName, boolean internal) {
95 return read(new File(fileName), internal);
98 private static final String[] stanames = {
99 "draft", "stable", "deprecated", "obsolete", "internal"
101 private static final String[] catnames = {
102 "classes", "fields", "constructors", "methods"
105 public void printStats(PrintWriter pw) {
106 // classes, methods, fields
107 // draft, stable, other
109 int[] stats = new int[catnames.length * stanames.length];
111 Iterator iter = set.iterator();
112 while (iter.hasNext()) {
113 APIInfo info = (APIInfo)iter.next();
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;
123 for (int cat = 0; cat < catnames.length; ++cat) {
124 pw.println(catnames[cat]);
126 for (int sta = 0; sta < stanames.length; ++sta) {
127 int v = stats[cat * stanames.length + sta];
129 pw.println(" " + stanames[sta] + ": " + v);
132 pw.println("total: " + t);
135 pw.println("total apis: " + tt);
138 public static void main(String[] args) {
139 PrintWriter pw = new PrintWriter(System.out);
141 boolean internal = false;
142 String path = "src/com/ibm/icu/dev/tool/docs/";
144 String fn = "icu4j341.api.gz";
145 if (args.length == 0) {
146 args = new String[] { "-file", fn };
149 for (int i = 0; i < args.length; ++i) {
150 String arg = args[i];
151 if (arg.equals("-path:")) {
153 } else if (arg.equals("-internal:")) {
154 internal = args[++i].toLowerCase().charAt(0) == 't';
155 } else if (arg.equals("-file")) {
158 File f = new File(path, fn);
159 read(f,internal).printStats(pw);