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