2 *******************************************************************************
3 * Copyright (C) 2005-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.dev.tool.docs;
10 import java.io.BufferedReader;
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;
19 public final class Deprecator {
20 private boolean undep;
23 Deprecator(boolean undep, int log) {
28 public static void main(String[] args) {
29 String srcPath = null;
30 String dstPath = null;
31 boolean undep = false;
35 StringBuffer err = new StringBuffer();
37 for (int i = 0; i < args.length; ++i) {
39 if (arg.equals("-src")) {
41 } else if (arg.equals("-dst")) {
43 } else if (arg.equals("-undep")) {
45 } else if (arg.equals("-help")) {
47 } else if (arg.equals("-silent")) {
49 } else if (arg.equals("-log")) {
51 } else if (arg.equals("-logfiles")) {
53 } else if (arg.equals("-verbose")) {
56 err.append("\nunrecognized argument: " + arg);
63 if (srcPath == null) {
64 err.append("\nsrc must be defined");
66 srcDir = new File(srcPath);
67 if (!(srcDir.exists() && srcDir.isDirectory())) {
68 err.append("\nsrc must be an existing directory: '" + srcPath + "'");
71 if (dstPath == null) {
72 err.append("\ndst must be defined");
74 dstDir = new File(dstPath);
75 if (!dstDir.exists()) {
76 if (!dstDir.mkdirs()) {
77 err.append("\nunable to create dst: '" + dstPath + "'");
79 } else if (!dstDir.isDirectory()) {
80 err.append("\ndst exists but is not directory: '" + dstPath + "'");
84 if (help || err.length() > 0) {
86 System.err.println("Error: " + err.toString());
94 System.out.println("src: " + srcDir.getCanonicalPath());
95 System.out.println("dst: " + dstDir.getCanonicalPath());
96 System.out.println("undep: " + undep);
100 new Deprecator(undep, log).process(srcDir, dstDir);
103 System.out.println("done");
108 System.err.println("Unexpected error: " + e);
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)");
120 pw.println(" Add or remove warning deprecations for ICU @draft and @internal APIs");
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) {
137 void process(File srcDir, File dstDir) {
138 File[] files = srcDir.listFiles(ff);
139 for (int i = 0; i < files.length; ++i) {
141 File d = new File(dstDir, f.getName());
142 if (f.isDirectory()) {
145 System.err.println("cannot create directory: " + d.getPath());
148 } else if (!d.isDirectory()) {
149 System.err.println("file already exists but is not directory: " + d.getPath());
153 System.out.println("process dir: " + f.getPath());
167 static final Pattern pat = Pattern.compile("^[\\s*]*@\\s*deprecated.*");
169 void processFile(File srcFile, File dstFile) {
171 System.out.println("process '" + srcFile.getPath() + "'");
175 BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
178 while (null != (line = r.readLine())) {
180 Matcher m = pat.matcher(line);
183 System.out.println(String.valueOf(n) + ": " + line);
189 catch (Exception e) {
191 System.err.println("caught exception: " + e);