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