001: /**
002: *******************************************************************************
003: * Copyright (C) 2004-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Represent a file of APIInfo records.
010: */package com.ibm.icu.dev.tool.docs;
011:
012: import java.io.*;
013: import java.util.*;
014: import java.util.zip.*;
015:
016: public final class APIData {
017: int version;
018: String name;
019: String base;
020: TreeSet set;
021:
022: static APIData read(BufferedReader br, boolean internal) {
023: try {
024: APIData data = new APIData();
025:
026: data.version = Integer.parseInt(APIInfo.readToken(br)); // version
027: if (data.version > APIInfo.VERSION) {
028: throw new IllegalArgumentException("data version "
029: + data.version
030: + " is newer than current version ("
031: + APIInfo.VERSION + ")");
032: }
033: data.name = APIInfo.readToken(br);
034: data.base = APIInfo.readToken(br); // base
035: br.readLine();
036:
037: data.set = new TreeSet(APIInfo.defaultComparator());
038: for (APIInfo info = new APIInfo(); info.read(br); info = new APIInfo()) {
039: if (internal || !info.isInternal()) {
040: data.set.add(info);
041: }
042: }
043: // System.out.println("read " + data.set.size() + " record(s)");
044: return data;
045: } catch (IOException e) {
046: RuntimeException re = new RuntimeException(
047: "error reading api data");
048: re.initCause(e);
049: throw re;
050: }
051: }
052:
053: static APIData read(File file, boolean internal) {
054: String fileName = file.getName();
055: try {
056: InputStream is;
057: if (fileName.endsWith(".zip")) {
058: ZipFile zf = new ZipFile(file);
059: Enumeration entryEnum = zf.entries();
060: if (entryEnum.hasMoreElements()) {
061: ZipEntry entry = (ZipEntry) entryEnum.nextElement();
062: is = zf.getInputStream(entry);
063: // we only handle one!!!
064: } else {
065: throw new IOException("zip file is empty");
066: }
067: } else {
068: is = new FileInputStream(file);
069: if (fileName.endsWith(".gz")) {
070: is = new GZIPInputStream(is);
071: }
072: }
073: InputStreamReader isr = new InputStreamReader(is);
074: return read(new BufferedReader(isr), internal);
075: } catch (IOException e) {
076: RuntimeException re = new RuntimeException(
077: "error getting info stream: " + fileName);
078: re.initCause(e);
079: throw re;
080: }
081: }
082:
083: static APIData read(String fileName, boolean internal) {
084: return read(new File(fileName), internal);
085: }
086:
087: private static final String[] stanames = { "draft", "stable",
088: "deprecated", "obsolete", "internal" };
089: private static final String[] catnames = { "classes", "fields",
090: "constructors", "methods" };
091:
092: public void printStats(PrintWriter pw) {
093: // classes, methods, fields
094: // draft, stable, other
095:
096: int[] stats = new int[catnames.length * stanames.length];
097:
098: Iterator iter = set.iterator();
099: while (iter.hasNext()) {
100: APIInfo info = (APIInfo) iter.next();
101:
102: if (info.isPublic() || info.isProtected()) {
103: int sta = info.getVal(APIInfo.STA);
104: int cat = info.getVal(APIInfo.CAT);
105: stats[cat * stanames.length + sta] += 1;
106: }
107: }
108:
109: int tt = 0;
110: for (int cat = 0; cat < catnames.length; ++cat) {
111: pw.println(catnames[cat]);
112: int t = 0;
113: for (int sta = 0; sta < stanames.length; ++sta) {
114: int v = stats[cat * stanames.length + sta];
115: t += v;
116: pw.println(" " + stanames[sta] + ": " + v);
117: }
118: tt += t;
119: pw.println("total: " + t);
120: pw.println();
121: }
122: pw.println("total apis: " + tt);
123: }
124:
125: public static void main(String[] args) {
126: PrintWriter pw = new PrintWriter(System.out);
127:
128: boolean internal = false;
129: String path = "src/com/ibm/icu/dev/tool/docs/";
130:
131: String fn = "icu4j341.api.gz";
132: if (args.length == 0) {
133: args = new String[] { "-file", fn };
134: }
135:
136: for (int i = 0; i < args.length; ++i) {
137: String arg = args[i];
138: if (arg.equals("-path:")) {
139: path = args[++i];
140: } else if (arg.equals("-internal:")) {
141: internal = args[++i].toLowerCase().charAt(0) == 't';
142: } else if (arg.equals("-file")) {
143: fn = args[++i];
144:
145: File f = new File(path, fn);
146: read(f, internal).printStats(pw);
147: pw.flush();
148: }
149: }
150: }
151: }
|