2 *******************************************************************************
\r
3 * Copyright (C) 2006-2007, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.tool.docs;
\r
10 import java.text.SimpleDateFormat;
\r
11 import java.util.Date;
\r
13 public class SwatDeprecated {
\r
14 private File srcFile;
\r
15 private File dstFile;
\r
16 private int maxLength = 85;
\r
17 private String srcPrefix;
\r
18 private String dstPrefix;
\r
19 private String srcTag;
\r
20 private String trgTag;
\r
21 private boolean overwrite;
\r
22 private int verbosity;
\r
23 private int cc; // changed file count
\r
24 //private boolean inPlace;
\r
25 private String copyYear;
\r
27 private PrintWriter pw = new PrintWriter(System.out);
\r
29 private static FilenameFilter ff = new FilenameFilter() {
\r
30 public boolean accept(File dir, String name) {
\r
31 return (new File(dir, name).isDirectory() && !"CVS".equals(name)) ||
\r
32 (!name.equals("SwatDeprecated.java") && name.endsWith(".java"));
\r
36 public static void main(String[] args) {
\r
37 String src = System.getProperty("user.dir");
\r
40 boolean ovr = false;
\r
43 for (int i = 0; i < args.length; ++i) {
\r
44 String arg = args[i].toLowerCase();
\r
45 if (arg.charAt(0) == '-') {
\r
46 if (arg.equals("-src")) {
\r
49 else if (arg.equals("-dst")) {
\r
52 else if (arg.equals("-dep")) {
\r
55 else if (arg.equals("-prov")) {
\r
58 else if (arg.equals("-overwrite")) {
\r
61 else if (arg.equals("-silent")) { // no output
\r
64 else if (arg.equals("-quiet")) { // output parameters and count of changed files (default)
\r
67 else if (arg.equals("-verbose")) { // output names of modified files
\r
70 else if (arg.equals("-noisy")) { // output names of files not modified
\r
73 else if (arg.equals("-copydebug")) { // output copyright debugging
\r
76 else if (arg.equals("-debug")) { // output all debugging
\r
82 new SwatDeprecated(src, dst, dep, ovr, vrb).run();
\r
85 public SwatDeprecated(String src, String dst, boolean dep, boolean overwrite, int verbosity) {
\r
86 this.srcFile = new File(src);
\r
87 this.dstFile = new File(dst);
\r
88 this.overwrite = overwrite;
\r
89 this.verbosity = verbosity;
\r
90 this.copyYear = new SimpleDateFormat("yyyy").format(new Date());
\r
92 this.srcTag = "@deprecated This is a draft API and might change in a future release of ICU.";
\r
93 this.trgTag = "@provisional This API might change or be removed in a future release.";
\r
95 String temp = srcTag;
\r
100 this.srcPrefix = srcFile.getCanonicalPath();
\r
101 this.dstPrefix = dstFile.getCanonicalPath();
\r
103 catch (IOException e) {
\r
104 RuntimeException re = new RuntimeException(e.getMessage());
\r
109 //this.inPlace = srcPrefix.equals(dstPrefix);
\r
112 if (verbosity >= 1) {
\r
113 pw.println("replacing '" + srcTag + "'");
\r
114 pw.println(" with '" + trgTag + "'");
\r
116 pw.println(" source: '" + srcPrefix + "'");
\r
117 pw.println("destination: '" + dstPrefix + "'");
\r
118 pw.println(" overwrite: " + overwrite);
\r
119 pw.println(" verbosity: " + verbosity);
\r
124 public void run() {
\r
125 if (!srcFile.exists()) {
\r
126 throw new RuntimeException("file " + srcFile.getPath() + " does not exist.");
\r
129 if (verbosity >= 1) {
\r
130 pw.println("changed " + cc + " file(s)");
\r
135 public void doList(File file) {
\r
136 String[] filenames = file.list(ff);
\r
137 if (verbosity >= 5) {
\r
138 pw.println(file.getPath());
\r
139 dumpList(filenames);
\r
142 for (int i = 0; i < filenames.length; ++i) {
\r
143 File f = new File(file, filenames[i]);
\r
144 if (f.isDirectory()) {
\r
152 public void processFile(File inFile) {
\r
153 File bakFile = null;
\r
155 String inPath = inFile.getCanonicalPath();
\r
156 if (verbosity >= 5) {
\r
157 pw.println("processFile: " + inPath);
\r
160 String outPath = dstPrefix + inPath.substring(srcPrefix.length());
\r
161 File outFile = new File(outPath);
\r
163 File tmpFile = null;
\r
164 if (outFile.exists()) {
\r
166 throw new RuntimeException("no permission to overwrite file: " + outPath);
\r
169 tmpFile = File.createTempFile(inFile.getName(), null, inFile.getParentFile());
\r
173 File parent = tmpFile.getParentFile();
\r
175 tmpFile.createNewFile();
\r
178 String tmpPath = tmpFile.getPath();
\r
179 if (verbosity >= 5) {
\r
180 pw.println("tmpFile: " + tmpPath);
\r
183 InputStream is = new FileInputStream(inFile);
\r
184 OutputStream os = new FileOutputStream(tmpFile);
\r
186 PrintStream ps = new PrintStream(os);
\r
187 BufferedReader br = new BufferedReader(new InputStreamReader(is));
\r
192 // boolean debug = false;
\r
193 while (null != (line = br.readLine())) {
\r
194 // int temp = line.indexOf("@deprecated");
\r
195 int ix = line.indexOf(srcTag);
\r
196 // if (temp != -1 && ix == -1) {
\r
197 // if (debug == false) {
\r
199 // pw.println("file: " + name);
\r
201 // pw.println("[" + n + "] " + line);
\r
205 if (verbosity >= 5) {
\r
206 pw.println("[" + n + "] " + line);
\r
209 line = line.substring(0,ix) + trgTag;
\r
212 } else if (n < 20) {
\r
213 // swat copyrights in the first 20 lines while we're at it
\r
214 ix = line.indexOf("opyright");
\r
216 String nline = null;
\r
218 if (verbosity == 4) {
\r
219 pw.println("[" + n + "] " + line);
\r
221 ix = line.indexOf("-200");
\r
223 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+5);
\r
226 ix = line.indexOf("- 200");
\r
228 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+6);
\r
231 ix = line.indexOf("-199");
\r
233 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+5);
\r
236 ix = line.indexOf(copyYear);
\r
238 break; // nothing needs changing
\r
240 ix = line.indexOf("200");
\r
242 nline = line.substring(0, ix+4) + "-" + copyYear + line.substring(ix+4);
\r
245 ix = line.indexOf("199");
\r
247 nline = line.substring(0, ix+4) + "-" + copyYear + line.substring(ix+4);
\r
252 if (nline != null) {
\r
253 if (verbosity >= 4) {
\r
254 pw.println(" --> " + nline);
\r
267 if (tc == 0) { // nothing changed, forget this file
\r
268 if (verbosity >= 3) {
\r
269 pw.println("no changes in file: " + inPath);
\r
271 if (!tmpFile.delete()) {
\r
272 throw new RuntimeException("unable to delete unneeded temporary file: " + tmpPath);
\r
278 if (bakFile != null) {
\r
279 if (bakFile.exists()) {
\r
282 if (!tmpFile.renameTo(bakFile)) {
\r
283 pw.println("warning: couldn't rename temp file to: " + outPath);
\r
287 outFile.setLastModified(inFile.lastModified());
\r
289 if (verbosity >= 2) {
\r
290 pw.println(inPath);
\r
294 catch (IOException e) {
\r
295 RuntimeException re = new RuntimeException(e.getMessage());
\r
306 public void dumpList(String[] names) {
\r
307 if (names == null) {
\r
312 if (names.length > 0) {
\r
317 for (int i = 0; i < names.length; ++i) {
\r
318 String name = names[i];
\r
319 int nl = name.length();
\r
320 if (lc + nl > maxLength) {
\r
329 if (names.length > 0) {
\r