001: /**
002: *******************************************************************************
003: * Copyright (C) 2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.dev.tool.docs;
007:
008: import java.io.File;
009: import java.io.FilenameFilter;
010: import java.io.PrintWriter;
011: import java.io.BufferedReader;
012: import java.io.InputStreamReader;
013: import java.io.FileInputStream;
014: import java.util.regex.*;
015:
016: public final class Deprecator {
017: private boolean undep;
018: private int log;
019:
020: Deprecator(boolean undep, int log) {
021: this .undep = undep;
022: this .log = log;
023: }
024:
025: public static void main(String[] args) {
026: String srcPath = null;
027: String dstPath = null;
028: boolean undep = false;
029:
030: int log = 1;
031: boolean help = false;
032: StringBuffer err = new StringBuffer();
033:
034: for (int i = 0; i < args.length; ++i) {
035: String arg = args[i];
036: if (arg.equals("-src")) {
037: srcPath = args[++i];
038: } else if (arg.equals("-dst")) {
039: dstPath = args[++i];
040: } else if (arg.equals("-undep")) {
041: undep = true;
042: } else if (arg.equals("-help")) {
043: help = true;
044: } else if (arg.equals("-silent")) {
045: log = 0;
046: } else if (arg.equals("-log")) {
047: log = 2;
048: } else if (arg.equals("-logfiles")) {
049: log = 3;
050: } else if (arg.equals("-verbose")) {
051: log = 4;
052: } else {
053: err.append("\nunrecognized argument: " + arg);
054: }
055: }
056:
057: File srcDir = null;
058: File dstDir = null;
059:
060: if (srcPath == null) {
061: err.append("\nsrc must be defined");
062: } else {
063: srcDir = new File(srcPath);
064: if (!(srcDir.exists() && srcDir.isDirectory())) {
065: err.append("\nsrc must be an existing directory: '"
066: + srcPath + "'");
067: }
068: }
069: if (dstPath == null) {
070: err.append("\ndst must be defined");
071: } else {
072: dstDir = new File(dstPath);
073: if (!dstDir.exists()) {
074: if (!dstDir.mkdirs()) {
075: err.append("\nunable to create dst: '" + dstPath
076: + "'");
077: }
078: } else if (!dstDir.isDirectory()) {
079: err.append("\ndst exists but is not directory: '"
080: + dstPath + "'");
081: }
082: }
083:
084: if (help || err.length() > 0) {
085: if (!help) {
086: System.err.println("Error: " + err.toString());
087: }
088: usage();
089: return;
090: }
091:
092: try {
093: if (log > 0) {
094: System.out.println("src: " + srcDir.getCanonicalPath());
095: System.out.println("dst: " + dstDir.getCanonicalPath());
096: System.out.println("undep: " + undep);
097: System.out.flush();
098: }
099:
100: new Deprecator(undep, log).process(srcDir, dstDir);
101:
102: if (log > 0) {
103: System.out.println("done");
104: System.out.flush();
105: }
106: } catch (Exception e) {
107: System.err.println("Unexpected error: " + e);
108: }
109: }
110:
111: static void usage() {
112: PrintWriter pw = new PrintWriter(System.out);
113: pw.println("Usage: Deprecator -src path -dst path [-help]");
114: pw
115: .println(" -src path : the root of the tree of files to work on");
116: pw
117: .println(" -dst path : the root of the tree to put the resulting files");
118: pw
119: .println(" -help : print this usage message and exit, doing nothing");
120: pw
121: .println(" -undep : remove deprecation tags if present (default false)");
122: pw.println();
123: pw
124: .println(" Add or remove warning deprecations for ICU @draft and @internal APIs");
125: pw.flush();
126: }
127:
128: static final String stoplist = "!CVS";
129: static final FilenameFilter ff = new FilenameFilter() {
130: public boolean accept(File dir, String name) {
131: if (name.endsWith(".java"))
132: return true;
133: if (new File(dir, name).isDirectory()) {
134: if (stoplist.indexOf("!" + name) == -1) {
135: return true;
136: }
137: }
138: return false;
139: }
140: };
141:
142: void process(File srcDir, File dstDir) {
143: File[] files = srcDir.listFiles(ff);
144: for (int i = 0; i < files.length; ++i) {
145: File f = files[i];
146: File d = new File(dstDir, f.getName());
147: if (f.isDirectory()) {
148: if (!d.exists()) {
149: if (!d.mkdir()) {
150: System.err.println("cannot create directory: "
151: + d.getPath());
152: continue;
153: }
154: } else if (!d.isDirectory()) {
155: System.err
156: .println("file already exists but is not directory: "
157: + d.getPath());
158: continue;
159: }
160: if (log > 1) {
161: System.out.println("process dir: " + f.getPath());
162: }
163: process(f, d);
164: } else {
165: processFile(f, d);
166: }
167: }
168: }
169:
170: /*
171: @ deprecated
172: *** @deprecated
173: ** ** ** @deprecated
174: */
175: static final Pattern pat = Pattern
176: .compile("^[\\s*]*@\\s*deprecated.*");
177:
178: void processFile(File srcFile, File dstFile) {
179: if (log > 2) {
180: System.out.println("process '" + srcFile.getPath() + "'");
181: }
182:
183: try {
184: BufferedReader r = new BufferedReader(
185: new InputStreamReader(new FileInputStream(srcFile)));
186: int n = 0;
187: String line = null;
188: while (null != (line = r.readLine())) {
189: ++n;
190: Matcher m = pat.matcher(line);
191: if (m.matches()) {
192: if (log > 3) {
193: System.out.println(String.valueOf(n) + ": "
194: + line);
195: }
196: }
197: }
198: r.close();
199: } catch (Exception e) {
200: System.out.flush();
201: System.err.println("caught exception: " + e);
202: }
203: }
204: }
|