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