1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /** 4 ******************************************************************************* 5 * Copyright (C) 2005-2010, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 10 package com.ibm.icu.dev.tool.docs; 11 12 import java.io.BufferedReader; 13 import java.io.File; 14 import java.io.FileInputStream; 15 import java.io.FilenameFilter; 16 import java.io.InputStreamReader; 17 import java.io.PrintWriter; 18 import java.util.regex.Matcher; 19 import java.util.regex.Pattern; 20 21 public final class Deprecator { 22 private boolean undep; 23 private int log; 24 25 Deprecator(boolean undep, int log) { 26 this.undep = undep; 27 this.log = log; 28 } 29 30 public static void main(String[] args) { 31 String srcPath = null; 32 String dstPath = null; 33 boolean undep = false; 34 35 int log = 1; 36 boolean help = false; 37 StringBuffer err = new StringBuffer(); 38 39 for (int i = 0; i < args.length; ++i) { 40 String arg = args[i]; 41 if (arg.equals("-src")) { 42 srcPath = args[++i]; 43 } else if (arg.equals("-dst")) { 44 dstPath = args[++i]; 45 } else if (arg.equals("-undep")) { 46 undep = true; 47 } else if (arg.equals("-help")) { 48 help = true; 49 } else if (arg.equals("-silent")) { 50 log = 0; 51 } else if (arg.equals("-log")) { 52 log = 2; 53 } else if (arg.equals("-logfiles")) { 54 log = 3; 55 } else if (arg.equals("-verbose")) { 56 log = 4; 57 } else { 58 err.append("\nunrecognized argument: " + arg); 59 } 60 } 61 62 File srcDir = null; 63 File dstDir = null; 64 65 if (srcPath == null) { 66 err.append("\nsrc must be defined"); 67 } else { 68 srcDir = new File(srcPath); 69 if (!(srcDir.exists() && srcDir.isDirectory())) { 70 err.append("\nsrc must be an existing directory: '" + srcPath + "'"); 71 } 72 } 73 if (dstPath == null) { 74 err.append("\ndst must be defined"); 75 } else { 76 dstDir = new File(dstPath); 77 if (!dstDir.exists()) { 78 if (!dstDir.mkdirs()) { 79 err.append("\nunable to create dst: '" + dstPath + "'"); 80 } 81 } else if (!dstDir.isDirectory()) { 82 err.append("\ndst exists but is not directory: '" + dstPath + "'"); 83 } 84 } 85 86 if (help || err.length() > 0) { 87 if (!help) { 88 System.err.println("Error: " + err.toString()); 89 } 90 usage(); 91 return; 92 } 93 94 try { 95 if (log > 0) { 96 System.out.println("src: " + srcDir.getCanonicalPath()); 97 System.out.println("dst: " + dstDir.getCanonicalPath()); 98 System.out.println("undep: " + undep); 99 System.out.flush(); 100 } 101 102 new Deprecator(undep, log).process(srcDir, dstDir); 103 104 if (log > 0) { 105 System.out.println("done"); 106 System.out.flush(); 107 } 108 } 109 catch(Exception e) { 110 System.err.println("Unexpected error: " + e); 111 } 112 } 113 114 static void usage() { 115 PrintWriter pw = new PrintWriter(System.out); 116 pw.println("Usage: Deprecator -src path -dst path [-help]"); 117 pw.println(" -src path : the root of the tree of files to work on"); 118 pw.println(" -dst path : the root of the tree to put the resulting files"); 119 pw.println(" -help : print this usage message and exit, doing nothing"); 120 pw.println(" -undep : remove deprecation tags if present (default false)"); 121 pw.println(); 122 pw.println(" Add or remove warning deprecations for ICU @draft and @internal APIs"); 123 pw.flush(); 124 } 125 126 static final String stoplist = "!CVS"; 127 static final FilenameFilter ff = new FilenameFilter() { 128 public boolean accept(File dir, String name) { 129 if (name.endsWith(".java")) return true; 130 if (new File(dir, name).isDirectory()) { 131 if (stoplist.indexOf("!"+name) == -1) { 132 return true; 133 } 134 } 135 return false; 136 } 137 }; 138 139 void process(File srcDir, File dstDir) { 140 File[] files = srcDir.listFiles(ff); 141 for (int i = 0; i < files.length; ++i) { 142 File f = files[i]; 143 File d = new File(dstDir, f.getName()); 144 if (f.isDirectory()) { 145 if (!d.exists()) { 146 if (!d.mkdir()) { 147 System.err.println("cannot create directory: " + d.getPath()); 148 continue; 149 } 150 } else if (!d.isDirectory()) { 151 System.err.println("file already exists but is not directory: " + d.getPath()); 152 continue; 153 } 154 if (log > 1) { 155 System.out.println("process dir: " + f.getPath()); 156 } 157 process(f, d); 158 } else { 159 processFile(f, d); 160 } 161 } 162 } 163 164 /* 165 @ deprecated 166 *** @deprecated 167 ** ** ** @deprecated 168 */ 169 static final Pattern pat = Pattern.compile("^[\\s*]*@\\s*deprecated.*"); 170 171 void processFile(File srcFile, File dstFile) { 172 if (log > 2) { 173 System.out.println("process '" + srcFile.getPath() + "'"); 174 } 175 176 try { 177 BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile))); 178 int n = 0; 179 String line = null; 180 while (null != (line = r.readLine())) { 181 ++n; 182 Matcher m = pat.matcher(line); 183 if (m.matches()) { 184 if (log > 3) { 185 System.out.println(String.valueOf(n) + ": " + line); 186 } 187 } 188 } 189 r.close(); 190 } 191 catch (Exception e) { 192 System.out.flush(); 193 System.err.println("caught exception: " + e); 194 } 195 } 196 } 197