2 *******************************************************************************
\r
3 * Copyright (C) 2004-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Represent a file of APIInfo records.
\r
12 package com.ibm.icu.dev.tool.docs;
\r
14 import java.io.BufferedReader;
\r
15 import java.io.File;
\r
16 import java.io.FileInputStream;
\r
17 import java.io.IOException;
\r
18 import java.io.InputStream;
\r
19 import java.io.InputStreamReader;
\r
20 import java.io.PrintWriter;
\r
21 import java.util.Enumeration;
\r
22 import java.util.Iterator;
\r
23 import java.util.TreeSet;
\r
24 import java.util.zip.GZIPInputStream;
\r
25 import java.util.zip.ZipEntry;
\r
26 import java.util.zip.ZipFile;
\r
28 public final class APIData {
\r
32 TreeSet<APIInfo> set;
\r
34 static APIData read(BufferedReader br, boolean internal) {
\r
36 APIData data = new APIData();
\r
38 data.version = Integer.parseInt(APIInfo.readToken(br)); // version
\r
39 if (data.version > APIInfo.VERSION) {
\r
40 throw new IllegalArgumentException(
\r
41 "data version " + data.version
\r
42 + " is newer than current version (" + APIInfo.VERSION + ")");
\r
44 data.name = APIInfo.readToken(br);
\r
45 data.base = APIInfo.readToken(br); // base
\r
48 data.set = new TreeSet(APIInfo.defaultComparator());
\r
49 for (APIInfo info = new APIInfo(); info.read(br); info = new APIInfo()) {
\r
50 if (internal || !info.isInternal()) {
\r
54 // System.out.println("read " + data.set.size() + " record(s)");
\r
57 catch (IOException e) {
\r
58 RuntimeException re = new RuntimeException("error reading api data");
\r
64 static APIData read(File file, boolean internal) {
\r
65 String fileName = file.getName();
\r
68 if (fileName.endsWith(".zip")) {
\r
69 ZipFile zf = new ZipFile(file);
\r
70 Enumeration entryEnum = zf.entries();
\r
71 if (entryEnum.hasMoreElements()) {
\r
72 ZipEntry entry = (ZipEntry)entryEnum.nextElement();
\r
73 is = zf.getInputStream(entry);
\r
74 // we only handle one!!!
\r
76 throw new IOException("zip file is empty");
\r
79 is = new FileInputStream(file);
\r
80 if (fileName.endsWith(".gz")) {
\r
81 is = new GZIPInputStream(is);
\r
84 InputStreamReader isr = new InputStreamReader(is);
\r
85 return read(new BufferedReader(isr), internal);
\r
87 catch (IOException e) {
\r
88 RuntimeException re = new RuntimeException("error getting info stream: " + fileName);
\r
94 static APIData read(String fileName, boolean internal) {
\r
95 return read(new File(fileName), internal);
\r
98 private static final String[] stanames = {
\r
99 "draft", "stable", "deprecated", "obsolete", "internal"
\r
101 private static final String[] catnames = {
\r
102 "classes", "fields", "constructors", "methods"
\r
105 public void printStats(PrintWriter pw) {
\r
106 // classes, methods, fields
\r
107 // draft, stable, other
\r
109 int[] stats = new int[catnames.length * stanames.length];
\r
111 Iterator iter = set.iterator();
\r
112 while (iter.hasNext()) {
\r
113 APIInfo info = (APIInfo)iter.next();
\r
115 if (info.isPublic() || info.isProtected()) {
\r
116 int sta = info.getVal(APIInfo.STA);
\r
117 int cat = info.getVal(APIInfo.CAT);
\r
118 stats[cat * stanames.length + sta] += 1;
\r
123 for (int cat = 0; cat < catnames.length; ++cat) {
\r
124 pw.println(catnames[cat]);
\r
126 for (int sta = 0; sta < stanames.length; ++sta) {
\r
127 int v = stats[cat * stanames.length + sta];
\r
129 pw.println(" " + stanames[sta] + ": " + v);
\r
132 pw.println("total: " + t);
\r
135 pw.println("total apis: " + tt);
\r
138 public static void main(String[] args) {
\r
139 PrintWriter pw = new PrintWriter(System.out);
\r
141 boolean internal = false;
\r
142 String path = "src/com/ibm/icu/dev/tool/docs/";
\r
144 String fn = "icu4j341.api.gz";
\r
145 if (args.length == 0) {
\r
146 args = new String[] { "-file", fn };
\r
149 for (int i = 0; i < args.length; ++i) {
\r
150 String arg = args[i];
\r
151 if (arg.equals("-path:")) {
\r
153 } else if (arg.equals("-internal:")) {
\r
154 internal = args[++i].toLowerCase().charAt(0) == 't';
\r
155 } else if (arg.equals("-file")) {
\r
158 File f = new File(path, fn);
\r
159 read(f,internal).printStats(pw);
\r