]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/tools/build/src/com/ibm/icu/dev/tool/docs/Deprecator.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / tools / build / src / com / ibm / icu / dev / tool / docs / Deprecator.java
1 /**\r
2 *******************************************************************************\r
3 * Copyright (C) 2005-2010, International Business Machines Corporation and    *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.dev.tool.docs;\r
9 \r
10 import java.io.BufferedReader;\r
11 import java.io.File;\r
12 import java.io.FileInputStream;\r
13 import java.io.FilenameFilter;\r
14 import java.io.InputStreamReader;\r
15 import java.io.PrintWriter;\r
16 import java.util.regex.Matcher;\r
17 import java.util.regex.Pattern;\r
18 \r
19 public final class Deprecator {\r
20     private boolean undep;\r
21     private int log;\r
22 \r
23     Deprecator(boolean undep, int log) {\r
24         this.undep = undep;\r
25         this.log = log;\r
26     }\r
27 \r
28     public static void main(String[] args) {\r
29         String srcPath = null;\r
30         String dstPath = null;\r
31         boolean undep = false;\r
32 \r
33         int log = 1;\r
34         boolean help = false;\r
35         StringBuffer err = new StringBuffer();\r
36 \r
37         for (int i = 0; i < args.length; ++i) {\r
38             String arg = args[i];\r
39             if (arg.equals("-src")) {\r
40                 srcPath = args[++i];\r
41             } else if (arg.equals("-dst")) {\r
42                 dstPath = args[++i];\r
43             } else if (arg.equals("-undep")) {\r
44                 undep = true;\r
45             } else if (arg.equals("-help")) {\r
46                 help = true;\r
47             } else if (arg.equals("-silent")) {\r
48                 log = 0;\r
49             } else if (arg.equals("-log")) {\r
50                 log = 2;\r
51             } else if (arg.equals("-logfiles")) {\r
52                 log = 3;\r
53             } else if (arg.equals("-verbose")) {\r
54                 log = 4;\r
55             } else {\r
56                 err.append("\nunrecognized argument: " + arg);\r
57             }\r
58         }\r
59 \r
60         File srcDir = null;\r
61         File dstDir = null;\r
62 \r
63         if (srcPath == null) {\r
64             err.append("\nsrc must be defined");\r
65         } else {\r
66             srcDir = new File(srcPath);\r
67             if (!(srcDir.exists() && srcDir.isDirectory())) {\r
68                 err.append("\nsrc must be an existing directory: '" + srcPath + "'");\r
69             }\r
70         }\r
71         if (dstPath == null) {\r
72             err.append("\ndst must be defined");\r
73         } else {\r
74             dstDir = new File(dstPath);\r
75             if (!dstDir.exists()) {\r
76                 if (!dstDir.mkdirs()) {\r
77                     err.append("\nunable to create dst: '" + dstPath + "'");\r
78                 }\r
79             } else if (!dstDir.isDirectory()) {\r
80                 err.append("\ndst exists but is not directory: '" + dstPath + "'");\r
81             }\r
82         }\r
83 \r
84         if (help || err.length() > 0) {\r
85             if (!help) {\r
86                 System.err.println("Error: " + err.toString());\r
87             }\r
88             usage();\r
89             return;\r
90         }\r
91 \r
92         try {\r
93             if (log > 0) {\r
94                 System.out.println("src: " + srcDir.getCanonicalPath());\r
95                 System.out.println("dst: " + dstDir.getCanonicalPath());\r
96                 System.out.println("undep: " + undep);\r
97                 System.out.flush();\r
98             }\r
99 \r
100             new Deprecator(undep, log).process(srcDir, dstDir);\r
101 \r
102             if (log > 0) {\r
103                 System.out.println("done");\r
104                 System.out.flush();\r
105             }\r
106         }\r
107         catch(Exception e) {\r
108             System.err.println("Unexpected error: " + e);\r
109         }\r
110     }\r
111 \r
112     static void usage() {\r
113         PrintWriter pw = new PrintWriter(System.out);\r
114         pw.println("Usage: Deprecator -src path -dst path [-help]");\r
115         pw.println("  -src path : the root of the tree of files to work on");\r
116         pw.println("  -dst path : the root of the tree to put the resulting files");\r
117         pw.println("  -help     : print this usage message and exit, doing nothing");\r
118         pw.println("  -undep    : remove deprecation tags if present (default false)");\r
119         pw.println();\r
120         pw.println("  Add or remove warning deprecations for ICU @draft and @internal APIs");\r
121         pw.flush();\r
122     }\r
123 \r
124     static final String stoplist = "!CVS";\r
125     static final FilenameFilter ff = new FilenameFilter() {\r
126             public boolean accept(File dir, String name) {\r
127                 if (name.endsWith(".java")) return true;\r
128                 if (new File(dir, name).isDirectory()) {\r
129                     if (stoplist.indexOf("!"+name) == -1) {\r
130                         return true;\r
131                     }\r
132                 }\r
133                 return false;\r
134             }\r
135         };\r
136             \r
137     void process(File srcDir, File dstDir) {\r
138         File[] files = srcDir.listFiles(ff);\r
139         for (int i = 0; i < files.length; ++i) {\r
140             File f = files[i];\r
141             File d = new File(dstDir, f.getName());\r
142             if (f.isDirectory()) {\r
143                 if (!d.exists()) {\r
144                     if (!d.mkdir()) {\r
145                         System.err.println("cannot create directory: " + d.getPath());\r
146                         continue;\r
147                     }\r
148                 } else if (!d.isDirectory()) {\r
149                     System.err.println("file already exists but is not directory: " + d.getPath());\r
150                     continue;\r
151                 }\r
152                 if (log > 1) {\r
153                     System.out.println("process dir: " + f.getPath());\r
154                 }\r
155                 process(f, d);\r
156             } else {\r
157                 processFile(f, d);\r
158             }\r
159         }\r
160     }\r
161 \r
162     /*\r
163  @ deprecated\r
164  *** @deprecated\r
165  ** ** ** @deprecated\r
166     */\r
167     static final Pattern pat = Pattern.compile("^[\\s*]*@\\s*deprecated.*");\r
168 \r
169     void processFile(File srcFile, File dstFile) {\r
170         if (log > 2) {\r
171             System.out.println("process '" + srcFile.getPath() + "'");\r
172         }\r
173 \r
174         try {\r
175             BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));\r
176             int n = 0;\r
177             String line = null;\r
178             while (null != (line = r.readLine())) {\r
179                 ++n;\r
180                 Matcher m = pat.matcher(line);\r
181                 if (m.matches()) {\r
182                     if (log > 3) {\r
183                         System.out.println(String.valueOf(n) + ": " + line);\r
184                     }\r
185                 }\r
186             }\r
187             r.close();\r
188         }\r
189         catch (Exception e) {\r
190             System.out.flush();\r
191             System.err.println("caught exception: " + e);\r
192         }\r
193     }\r
194 }\r