]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/tool/docs/SwatDeprecated.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / tool / docs / SwatDeprecated.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2006-2007, 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.*;\r
10 import java.text.SimpleDateFormat;\r
11 import java.util.Date;\r
12 \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
26 \r
27     private PrintWriter pw = new PrintWriter(System.out);\r
28 \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
33             }\r
34         };\r
35 \r
36     public static void main(String[] args) {\r
37         String src = System.getProperty("user.dir");\r
38         String dst = src;\r
39         boolean dep = true;\r
40         boolean ovr = false;\r
41         int vrb = 1;\r
42 \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
47                     src = args[++i];\r
48                 }\r
49                 else if (arg.equals("-dst")) {\r
50                     dst = args[++i];\r
51                 }\r
52                 else if (arg.equals("-dep")) {\r
53                     dep = true;\r
54                 } \r
55                 else if (arg.equals("-prov")) {\r
56                     dep = false;\r
57                 }\r
58                 else if (arg.equals("-overwrite")) {\r
59                     ovr = true;\r
60                 }\r
61                 else if (arg.equals("-silent")) { // no output\r
62                     vrb = 0;\r
63                 }\r
64                 else if (arg.equals("-quiet")) { // output parameters and count of changed files (default)\r
65                     vrb = 1;\r
66                 } \r
67                 else if (arg.equals("-verbose")) { // output names of modified files\r
68                     vrb = 2;\r
69                 } \r
70                 else if (arg.equals("-noisy")) { // output names of files not modified\r
71                     vrb = 3;\r
72                 } \r
73                 else if (arg.equals("-copydebug")) { // output copyright debugging\r
74                     vrb = 4;\r
75                 }\r
76                 else if (arg.equals("-debug")) { // output all debugging\r
77                     vrb = 5;\r
78                 }\r
79             }\r
80         }\r
81 \r
82         new SwatDeprecated(src, dst, dep, ovr, vrb).run();\r
83     }\r
84 \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
91 \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
94         if (!dep) {\r
95             String temp = srcTag;\r
96             srcTag = trgTag;\r
97             trgTag = temp;\r
98         }\r
99         try {\r
100             this.srcPrefix = srcFile.getCanonicalPath();\r
101             this.dstPrefix = dstFile.getCanonicalPath();\r
102         }\r
103         catch (IOException e) {\r
104             RuntimeException re = new RuntimeException(e.getMessage());\r
105             re.initCause(e);\r
106             throw re;\r
107         }\r
108 \r
109         //this.inPlace = srcPrefix.equals(dstPrefix);\r
110         this.cc = 0;\r
111 \r
112         if (verbosity >= 1) {\r
113             pw.println("replacing '" + srcTag + "'");\r
114             pw.println("     with '" + trgTag + "'");\r
115             pw.println();\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
120             pw.flush();\r
121         }\r
122     }\r
123 \r
124     public void run() {\r
125         if (!srcFile.exists()) {\r
126             throw new RuntimeException("file " + srcFile.getPath() + " does not exist.");\r
127         }\r
128         doList(srcFile);\r
129         if (verbosity >= 1) {\r
130             pw.println("changed " + cc + " file(s)");\r
131             pw.flush();\r
132         }\r
133     }\r
134 \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
140             pw.flush();\r
141         }\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
145                 doList(f);\r
146             } else {\r
147                 processFile(f);\r
148             }\r
149         }\r
150     }\r
151 \r
152     public void processFile(File inFile) {\r
153         File bakFile = null;\r
154         try {\r
155             String inPath = inFile.getCanonicalPath();\r
156             if (verbosity >= 5) {\r
157                 pw.println("processFile: " + inPath);\r
158             }\r
159 \r
160             String outPath = dstPrefix + inPath.substring(srcPrefix.length());\r
161             File outFile = new File(outPath);\r
162 \r
163             File tmpFile = null;\r
164             if (outFile.exists()) {\r
165                 if (!overwrite) {\r
166                     throw new RuntimeException("no permission to overwrite file: " + outPath);\r
167                 } else {\r
168                     bakFile = outFile;\r
169                     tmpFile = File.createTempFile(inFile.getName(), null, inFile.getParentFile());\r
170                 }\r
171             } else {\r
172                 tmpFile = outFile;\r
173                 File parent = tmpFile.getParentFile();\r
174                 parent.mkdirs();\r
175                 tmpFile.createNewFile();\r
176             }\r
177 \r
178             String tmpPath = tmpFile.getPath();\r
179             if (verbosity >= 5) {\r
180                 pw.println("tmpFile: " + tmpPath);\r
181             }\r
182 \r
183             InputStream is = new FileInputStream(inFile);\r
184             OutputStream os = new FileOutputStream(tmpFile);\r
185 \r
186             PrintStream ps = new PrintStream(os);\r
187             BufferedReader br = new BufferedReader(new InputStreamReader(is));\r
188 \r
189             String line;\r
190             int n = 0;\r
191             int tc = 0;\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
198 //                         debug = true;\r
199 //                         pw.println("file: " + name);\r
200 //                     }\r
201 //                     pw.println("[" + n + "] " + line);\r
202 //                     pw.flush();\r
203 //                 }\r
204                 if (ix != -1) {\r
205                     if (verbosity >= 5) {\r
206                         pw.println("[" + n + "] " + line);\r
207                     }\r
208 \r
209                     line = line.substring(0,ix) + trgTag;\r
210                     \r
211                     ++tc;\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
215                     if (ix != -1) {\r
216                         String nline = null;\r
217                         do {\r
218                             if (verbosity == 4) {\r
219                                 pw.println("[" + n + "] " + line);\r
220                             }\r
221                             ix = line.indexOf("-200");\r
222                             if (ix != -1) {\r
223                                 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+5);\r
224                                 break;\r
225                             }\r
226                             ix = line.indexOf("- 200");\r
227                             if (ix != -1) {\r
228                                 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+6);\r
229                                 break;\r
230                             }\r
231                             ix = line.indexOf("-199");\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(copyYear);\r
237                             if (ix != -1) {\r
238                                 break; // nothing needs changing\r
239                             }\r
240                             ix = line.indexOf("200");\r
241                             if (ix != -1) {\r
242                                 nline = line.substring(0, ix+4) + "-" + copyYear + line.substring(ix+4);\r
243                                 break;\r
244                             }\r
245                             ix = line.indexOf("199");\r
246                             if (ix != -1) {\r
247                                 nline = line.substring(0, ix+4) + "-" + copyYear + line.substring(ix+4);\r
248                                 break;\r
249                             }\r
250                         } while (false);\r
251 \r
252                         if (nline != null) {\r
253                             if (verbosity >= 4) {\r
254                                 pw.println("  --> " + nline);\r
255                             }\r
256                             line = nline;\r
257                         }\r
258                     }\r
259                 }\r
260                 ps.println(line);\r
261                 ++n;\r
262             }\r
263             ps.flush();\r
264             is.close();\r
265             os.close();\r
266 \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
270                 }\r
271                 if (!tmpFile.delete()) {\r
272                     throw new RuntimeException("unable to delete unneeded temporary file: " + tmpPath);\r
273                 }\r
274 \r
275                 return;\r
276             }\r
277 \r
278             if (bakFile != null) {\r
279                 if (bakFile.exists()) {\r
280                     bakFile.delete();\r
281                 }\r
282                 if (!tmpFile.renameTo(bakFile)) {\r
283                     pw.println("warning: couldn't rename temp file to: " + outPath);\r
284                 }\r
285             }\r
286 \r
287             outFile.setLastModified(inFile.lastModified());\r
288 \r
289             if (verbosity >= 2) {\r
290                 pw.println(inPath);\r
291                 pw.flush();\r
292             }\r
293         }\r
294         catch (IOException e) {\r
295             RuntimeException re = new RuntimeException(e.getMessage());\r
296             re.initCause(e);\r
297             throw re;\r
298         }\r
299         finally {\r
300             pw.flush();\r
301         }\r
302 \r
303         ++cc;\r
304     }\r
305 \r
306     public void dumpList(String[] names) {\r
307         if (names == null) {\r
308             pw.print("null");\r
309         } else {\r
310             pw.print("{");\r
311             int lc = 0;\r
312             if (names.length > 0) {\r
313                 pw.println();\r
314                 pw.print("    ");\r
315                 lc = 4;\r
316             }\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
321                     pw.println();\r
322                     pw.print("    ");\r
323                     lc = 4;\r
324                 }\r
325                 pw.print(name);\r
326                 pw.print(", ");\r
327                 lc += nl + 2;\r
328             }\r
329             if (names.length > 0) {\r
330                 pw.println();\r
331             }\r
332             pw.print("} ");\r
333         }\r
334     }\r
335 }\r