1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2006-2010, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.dev.tool.docs; 10 11 import java.io.BufferedReader; 12 import java.io.File; 13 import java.io.FileInputStream; 14 import java.io.FileOutputStream; 15 import java.io.FilenameFilter; 16 import java.io.IOException; 17 import java.io.InputStream; 18 import java.io.InputStreamReader; 19 import java.io.OutputStream; 20 import java.io.PrintStream; 21 import java.io.PrintWriter; 22 import java.text.SimpleDateFormat; 23 import java.util.Date; 24 25 public class SwatDeprecated { 26 private File srcFile; 27 private File dstFile; 28 private int maxLength = 85; 29 private String srcPrefix; 30 private String dstPrefix; 31 private String srcTag; 32 private String trgTag; 33 private boolean overwrite; 34 private int verbosity; 35 private int cc; // changed file count 36 //private boolean inPlace; 37 private String copyYear; 38 39 private PrintWriter pw = new PrintWriter(System.out); 40 41 private static FilenameFilter ff = new FilenameFilter() { 42 public boolean accept(File dir, String name) { 43 return (new File(dir, name).isDirectory() && !"CVS".equals(name)) || 44 (!name.equals("SwatDeprecated.java") && name.endsWith(".java")); 45 } 46 }; 47 main(String[] args)48 public static void main(String[] args) { 49 String src = System.getProperty("user.dir"); 50 String dst = src; 51 boolean dep = true; 52 boolean ovr = false; 53 int vrb = 1; 54 55 for (int i = 0; i < args.length; ++i) { 56 String arg = args[i].toLowerCase(); 57 if (arg.charAt(0) == '-') { 58 if (arg.equals("-src")) { 59 src = args[++i]; 60 } 61 else if (arg.equals("-dst")) { 62 dst = args[++i]; 63 } 64 else if (arg.equals("-dep")) { 65 dep = true; 66 } 67 else if (arg.equals("-prov")) { 68 dep = false; 69 } 70 else if (arg.equals("-overwrite")) { 71 ovr = true; 72 } 73 else if (arg.equals("-silent")) { // no output 74 vrb = 0; 75 } 76 else if (arg.equals("-quiet")) { // output parameters and count of changed files (default) 77 vrb = 1; 78 } 79 else if (arg.equals("-verbose")) { // output names of modified files 80 vrb = 2; 81 } 82 else if (arg.equals("-noisy")) { // output names of files not modified 83 vrb = 3; 84 } 85 else if (arg.equals("-copydebug")) { // output copyright debugging 86 vrb = 4; 87 } 88 else if (arg.equals("-debug")) { // output all debugging 89 vrb = 5; 90 } 91 } 92 } 93 94 new SwatDeprecated(src, dst, dep, ovr, vrb).run(); 95 } 96 SwatDeprecated(String src, String dst, boolean dep, boolean overwrite, int verbosity)97 public SwatDeprecated(String src, String dst, boolean dep, boolean overwrite, int verbosity) { 98 this.srcFile = new File(src); 99 this.dstFile = new File(dst); 100 this.overwrite = overwrite; 101 this.verbosity = verbosity; 102 this.copyYear = new SimpleDateFormat("yyyy").format(new Date()); 103 104 this.srcTag = "@deprecated This is a draft API and might change in a future release of ICU."; 105 this.trgTag = "@provisional This API might change or be removed in a future release."; 106 if (!dep) { 107 String temp = srcTag; 108 srcTag = trgTag; 109 trgTag = temp; 110 } 111 try { 112 this.srcPrefix = srcFile.getCanonicalPath(); 113 this.dstPrefix = dstFile.getCanonicalPath(); 114 } 115 catch (IOException e) { 116 RuntimeException re = new RuntimeException(e.getMessage()); 117 re.initCause(e); 118 throw re; 119 } 120 121 //this.inPlace = srcPrefix.equals(dstPrefix); 122 this.cc = 0; 123 124 if (verbosity >= 1) { 125 pw.println("replacing '" + srcTag + "'"); 126 pw.println(" with '" + trgTag + "'"); 127 pw.println(); 128 pw.println(" source: '" + srcPrefix + "'"); 129 pw.println("destination: '" + dstPrefix + "'"); 130 pw.println(" overwrite: " + overwrite); 131 pw.println(" verbosity: " + verbosity); 132 pw.flush(); 133 } 134 } 135 run()136 public void run() { 137 if (!srcFile.exists()) { 138 throw new RuntimeException("file " + srcFile.getPath() + " does not exist."); 139 } 140 doList(srcFile); 141 if (verbosity >= 1) { 142 pw.println("changed " + cc + " file(s)"); 143 pw.flush(); 144 } 145 } 146 doList(File file)147 public void doList(File file) { 148 String[] filenames = file.list(ff); 149 if (verbosity >= 5) { 150 pw.println(file.getPath()); 151 dumpList(filenames); 152 pw.flush(); 153 } 154 for (int i = 0; i < filenames.length; ++i) { 155 File f = new File(file, filenames[i]); 156 if (f.isDirectory()) { 157 doList(f); 158 } else { 159 processFile(f); 160 } 161 } 162 } 163 processFile(File inFile)164 public void processFile(File inFile) { 165 File bakFile = null; 166 try { 167 String inPath = inFile.getCanonicalPath(); 168 if (verbosity >= 5) { 169 pw.println("processFile: " + inPath); 170 } 171 172 String outPath = dstPrefix + inPath.substring(srcPrefix.length()); 173 File outFile = new File(outPath); 174 175 File tmpFile = null; 176 if (outFile.exists()) { 177 if (!overwrite) { 178 throw new RuntimeException("no permission to overwrite file: " + outPath); 179 } else { 180 bakFile = outFile; 181 tmpFile = File.createTempFile(inFile.getName(), null, inFile.getParentFile()); 182 } 183 } else { 184 tmpFile = outFile; 185 File parent = tmpFile.getParentFile(); 186 parent.mkdirs(); 187 tmpFile.createNewFile(); 188 } 189 190 String tmpPath = tmpFile.getPath(); 191 if (verbosity >= 5) { 192 pw.println("tmpFile: " + tmpPath); 193 } 194 195 InputStream is = new FileInputStream(inFile); 196 OutputStream os = new FileOutputStream(tmpFile); 197 198 PrintStream ps = new PrintStream(os); 199 BufferedReader br = new BufferedReader(new InputStreamReader(is)); 200 201 String line; 202 int n = 0; 203 int tc = 0; 204 // boolean debug = false; 205 while (null != (line = br.readLine())) { 206 // int temp = line.indexOf("@deprecated"); 207 int ix = line.indexOf(srcTag); 208 // if (temp != -1 && ix == -1) { 209 // if (debug == false) { 210 // debug = true; 211 // pw.println("file: " + name); 212 // } 213 // pw.println("[" + n + "] " + line); 214 // pw.flush(); 215 // } 216 if (ix != -1) { 217 if (verbosity >= 5) { 218 pw.println("[" + n + "] " + line); 219 } 220 221 line = line.substring(0,ix) + trgTag; 222 223 ++tc; 224 } else if (n < 20) { 225 // swat copyrights in the first 20 lines while we're at it 226 ix = line.indexOf("opyright"); 227 if (ix != -1) { 228 String nline = null; 229 do { 230 if (verbosity == 4) { 231 pw.println("[" + n + "] " + line); 232 } 233 ix = line.indexOf("-200"); 234 if (ix != -1) { 235 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+5); 236 break; 237 } 238 ix = line.indexOf("- 200"); 239 if (ix != -1) { 240 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+6); 241 break; 242 } 243 ix = line.indexOf("-199"); 244 if (ix != -1) { 245 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+5); 246 break; 247 } 248 ix = line.indexOf(copyYear); 249 if (ix != -1) { 250 break; // nothing needs changing 251 } 252 ix = line.indexOf("200"); 253 if (ix != -1) { 254 nline = line.substring(0, ix+4) + "-" + copyYear + line.substring(ix+4); 255 break; 256 } 257 ix = line.indexOf("199"); 258 if (ix != -1) { 259 nline = line.substring(0, ix+4) + "-" + copyYear + line.substring(ix+4); 260 break; 261 } 262 } while (false); 263 264 if (nline != null) { 265 if (verbosity >= 4) { 266 pw.println(" --> " + nline); 267 } 268 line = nline; 269 } 270 } 271 } 272 ps.println(line); 273 ++n; 274 } 275 ps.flush(); 276 is.close(); 277 os.close(); 278 279 if (tc == 0) { // nothing changed, forget this file 280 if (verbosity >= 3) { 281 pw.println("no changes in file: " + inPath); 282 } 283 if (!tmpFile.delete()) { 284 throw new RuntimeException("unable to delete unneeded temporary file: " + tmpPath); 285 } 286 287 return; 288 } 289 290 if (bakFile != null) { 291 if (bakFile.exists()) { 292 bakFile.delete(); 293 } 294 if (!tmpFile.renameTo(bakFile)) { 295 pw.println("warning: couldn't rename temp file to: " + outPath); 296 } 297 } 298 299 outFile.setLastModified(inFile.lastModified()); 300 301 if (verbosity >= 2) { 302 pw.println(inPath); 303 pw.flush(); 304 } 305 } 306 catch (IOException e) { 307 RuntimeException re = new RuntimeException(e.getMessage()); 308 re.initCause(e); 309 throw re; 310 } 311 finally { 312 pw.flush(); 313 } 314 315 ++cc; 316 } 317 dumpList(String[] names)318 public void dumpList(String[] names) { 319 if (names == null) { 320 pw.print("null"); 321 } else { 322 pw.print("{"); 323 int lc = 0; 324 if (names.length > 0) { 325 pw.println(); 326 pw.print(" "); 327 lc = 4; 328 } 329 for (int i = 0; i < names.length; ++i) { 330 String name = names[i]; 331 int nl = name.length(); 332 if (lc + nl > maxLength) { 333 pw.println(); 334 pw.print(" "); 335 lc = 4; 336 } 337 pw.print(name); 338 pw.print(", "); 339 lc += nl + 2; 340 } 341 if (names.length > 0) { 342 pw.println(); 343 } 344 pw.print("} "); 345 } 346 } 347 } 348