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