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