001: /*
002: *******************************************************************************
003: * Copyright (C) 2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.tool.docs;
008:
009: import java.io.*;
010: import java.text.SimpleDateFormat;
011: import java.util.Date;
012:
013: public class SwatDeprecated {
014: private File srcFile;
015: private File dstFile;
016: private int maxLength = 85;
017: private String srcPrefix;
018: private String dstPrefix;
019: private String srcTag;
020: private String trgTag;
021: private boolean overwrite;
022: private int verbosity;
023: private int cc; // changed file count
024: private boolean inPlace;
025: private String copyYear;
026:
027: private PrintWriter pw = new PrintWriter(System.out);
028:
029: private static FilenameFilter ff = new FilenameFilter() {
030: public boolean accept(File dir, String name) {
031: return (new File(dir, name).isDirectory() && !"CVS"
032: .equals(name))
033: || (!name.equals("SwatDeprecated.java") && name
034: .endsWith(".java"));
035: }
036: };
037:
038: public static void main(String[] args) {
039: String src = System.getProperty("user.dir");
040: String dst = src;
041: boolean dep = true;
042: boolean ovr = false;
043: int vrb = 1;
044:
045: for (int i = 0; i < args.length; ++i) {
046: String arg = args[i].toLowerCase();
047: if (arg.charAt(0) == '-') {
048: if (arg.equals("-src")) {
049: src = args[++i];
050: } else if (arg.equals("-dst")) {
051: dst = args[++i];
052: } else if (arg.equals("-dep")) {
053: dep = true;
054: } else if (arg.equals("-prov")) {
055: dep = false;
056: } else if (arg.equals("-overwrite")) {
057: ovr = true;
058: } else if (arg.equals("-silent")) { // no output
059: vrb = 0;
060: } else if (arg.equals("-quiet")) { // output parameters and count of changed files (default)
061: vrb = 1;
062: } else if (arg.equals("-verbose")) { // output names of modified files
063: vrb = 2;
064: } else if (arg.equals("-noisy")) { // output names of files not modified
065: vrb = 3;
066: } else if (arg.equals("-copydebug")) { // output copyright debugging
067: vrb = 4;
068: } else if (arg.equals("-debug")) { // output all debugging
069: vrb = 5;
070: }
071: }
072: }
073:
074: new SwatDeprecated(src, dst, dep, ovr, vrb).run();
075: }
076:
077: public SwatDeprecated(String src, String dst, boolean dep,
078: boolean overwrite, int verbosity) {
079: this .srcFile = new File(src);
080: this .dstFile = new File(dst);
081: this .overwrite = overwrite;
082: this .verbosity = verbosity;
083: this .copyYear = new SimpleDateFormat("yyyy").format(new Date());
084:
085: this .srcTag = "@deprecated This is a draft API and might change in a future release of ICU.";
086: this .trgTag = "@provisional This API might change or be removed in a future release.";
087: if (!dep) {
088: String temp = srcTag;
089: srcTag = trgTag;
090: trgTag = temp;
091: }
092: try {
093: this .srcPrefix = srcFile.getCanonicalPath();
094: this .dstPrefix = dstFile.getCanonicalPath();
095: } catch (IOException e) {
096: RuntimeException re = new RuntimeException(e.getMessage());
097: re.initCause(e);
098: throw re;
099: }
100:
101: this .inPlace = srcPrefix.equals(dstPrefix);
102: this .cc = 0;
103:
104: if (verbosity >= 1) {
105: pw.println("replacing '" + srcTag + "'");
106: pw.println(" with '" + trgTag + "'");
107: pw.println();
108: pw.println(" source: '" + srcPrefix + "'");
109: pw.println("destination: '" + dstPrefix + "'");
110: pw.println(" overwrite: " + overwrite);
111: pw.println(" verbosity: " + verbosity);
112: pw.flush();
113: }
114: }
115:
116: public void run() {
117: if (!srcFile.exists()) {
118: throw new RuntimeException("file " + srcFile.getPath()
119: + " does not exist.");
120: }
121: doList(srcFile);
122: if (verbosity >= 1) {
123: pw.println("changed " + cc + " file(s)");
124: pw.flush();
125: }
126: }
127:
128: public void doList(File file) {
129: String[] filenames = file.list(ff);
130: if (verbosity >= 5) {
131: pw.println(file.getPath());
132: dumpList(filenames);
133: pw.flush();
134: }
135: for (int i = 0; i < filenames.length; ++i) {
136: File f = new File(file, filenames[i]);
137: if (f.isDirectory()) {
138: doList(f);
139: } else {
140: processFile(f);
141: }
142: }
143: }
144:
145: public void processFile(File inFile) {
146: File bakFile = null;
147: File oldFile = null;
148: try {
149: String inPath = inFile.getCanonicalPath();
150: if (verbosity >= 5) {
151: pw.println("processFile: " + inPath);
152: }
153:
154: String outPath = dstPrefix
155: + inPath.substring(srcPrefix.length());
156: File outFile = new File(outPath);
157:
158: File tmpFile = null;
159: if (outFile.exists()) {
160: if (!overwrite) {
161: throw new RuntimeException(
162: "no permission to overwrite file: "
163: + outPath);
164: } else {
165: bakFile = outFile;
166: tmpFile = File.createTempFile(inFile.getName(),
167: null, inFile.getParentFile());
168: }
169: } else {
170: tmpFile = outFile;
171: File parent = tmpFile.getParentFile();
172: parent.mkdirs();
173: tmpFile.createNewFile();
174: }
175:
176: String tmpPath = tmpFile.getPath();
177: if (verbosity >= 5) {
178: pw.println("tmpFile: " + tmpPath);
179: }
180:
181: InputStream is = new FileInputStream(inFile);
182: OutputStream os = new FileOutputStream(tmpFile);
183:
184: PrintStream ps = new PrintStream(os);
185: BufferedReader br = new BufferedReader(
186: new InputStreamReader(is));
187:
188: String line;
189: int n = 0;
190: int tc = 0;
191: boolean debug = false;
192: while (null != (line = br.readLine())) {
193: // int temp = line.indexOf("@deprecated");
194: int ix = line.indexOf(srcTag);
195: // if (temp != -1 && ix == -1) {
196: // if (debug == false) {
197: // debug = true;
198: // pw.println("file: " + name);
199: // }
200: // pw.println("[" + n + "] " + line);
201: // pw.flush();
202: // }
203: if (ix != -1) {
204: if (verbosity >= 5) {
205: pw.println("[" + n + "] " + line);
206: }
207:
208: line = line.substring(0, ix) + trgTag;
209:
210: ++tc;
211: } else if (n < 20) {
212: // swat copyrights in the first 20 lines while we're at it
213: ix = line.indexOf("opyright");
214: if (ix != -1) {
215: String nline = null;
216: do {
217: if (verbosity == 4) {
218: pw.println("[" + n + "] " + line);
219: }
220: ix = line.indexOf("-200");
221: if (ix != -1) {
222: nline = line.substring(0, ix) + "-"
223: + copyYear
224: + line.substring(ix + 5);
225: break;
226: }
227: ix = line.indexOf("- 200");
228: if (ix != -1) {
229: nline = line.substring(0, ix) + "-"
230: + copyYear
231: + line.substring(ix + 6);
232: break;
233: }
234: ix = line.indexOf("-199");
235: if (ix != -1) {
236: nline = line.substring(0, ix) + "-"
237: + copyYear
238: + line.substring(ix + 5);
239: break;
240: }
241: ix = line.indexOf(copyYear);
242: if (ix != -1) {
243: break; // nothing needs changing
244: }
245: ix = line.indexOf("200");
246: if (ix != -1) {
247: nline = line.substring(0, ix + 4) + "-"
248: + copyYear
249: + line.substring(ix + 4);
250: break;
251: }
252: ix = line.indexOf("199");
253: if (ix != -1) {
254: nline = line.substring(0, ix + 4) + "-"
255: + copyYear
256: + line.substring(ix + 4);
257: break;
258: }
259: } while (false);
260:
261: if (nline != null) {
262: if (verbosity >= 4) {
263: pw.println(" --> " + nline);
264: }
265: line = nline;
266: }
267: }
268: }
269: ps.println(line);
270: ++n;
271: }
272: ps.flush();
273: is.close();
274: os.close();
275:
276: if (tc == 0) { // nothing changed, forget this file
277: if (verbosity >= 3) {
278: pw.println("no changes in file: " + inPath);
279: }
280: if (!tmpFile.delete()) {
281: throw new RuntimeException(
282: "unable to delete unneeded temporary file: "
283: + tmpPath);
284: }
285:
286: return;
287: }
288:
289: if (bakFile != null) {
290: if (bakFile.exists()) {
291: bakFile.delete();
292: }
293: if (!tmpFile.renameTo(bakFile)) {
294: pw
295: .println("warning: couldn't rename temp file to: "
296: + outPath);
297: }
298: }
299:
300: outFile.setLastModified(inFile.lastModified());
301:
302: if (verbosity >= 2) {
303: pw.println(inPath);
304: pw.flush();
305: }
306: } catch (IOException e) {
307: RuntimeException re = new RuntimeException(e.getMessage());
308: re.initCause(e);
309: throw re;
310: } finally {
311: pw.flush();
312: }
313:
314: ++cc;
315: }
316:
317: public void dumpList(String[] names) {
318: if (names == null) {
319: pw.print("null");
320: } else {
321: pw.print("{");
322: int lc = 0;
323: if (names.length > 0) {
324: pw.println();
325: pw.print(" ");
326: lc = 4;
327: }
328: for (int i = 0; i < names.length; ++i) {
329: String name = names[i];
330: int nl = name.length();
331: if (lc + nl > maxLength) {
332: pw.println();
333: pw.print(" ");
334: lc = 4;
335: }
336: pw.print(name);
337: pw.print(", ");
338: lc += nl + 2;
339: }
340: if (names.length > 0) {
341: pw.println();
342: }
343: pw.print("} ");
344: }
345: }
346: }
|