2 *******************************************************************************
\r
3 * Copyright (C) 2006-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.tool.docs;
\r
9 import java.io.BufferedReader;
\r
10 import java.io.File;
\r
11 import java.io.FileInputStream;
\r
12 import java.io.FileOutputStream;
\r
13 import java.io.FilenameFilter;
\r
14 import java.io.IOException;
\r
15 import java.io.InputStream;
\r
16 import java.io.InputStreamReader;
\r
17 import java.io.OutputStream;
\r
18 import java.io.PrintStream;
\r
19 import java.io.PrintWriter;
\r
20 import java.text.SimpleDateFormat;
\r
21 import java.util.Date;
\r
23 public class SwatDeprecated {
\r
24 private File srcFile;
\r
25 private File dstFile;
\r
26 private int maxLength = 85;
\r
27 private String srcPrefix;
\r
28 private String dstPrefix;
\r
29 private String srcTag;
\r
30 private String trgTag;
\r
31 private boolean overwrite;
\r
32 private int verbosity;
\r
33 private int cc; // changed file count
\r
34 //private boolean inPlace;
\r
35 private String copyYear;
\r
37 private PrintWriter pw = new PrintWriter(System.out);
\r
39 private static FilenameFilter ff = new FilenameFilter() {
\r
40 public boolean accept(File dir, String name) {
\r
41 return (new File(dir, name).isDirectory() && !"CVS".equals(name)) ||
\r
42 (!name.equals("SwatDeprecated.java") && name.endsWith(".java"));
\r
46 public static void main(String[] args) {
\r
47 String src = System.getProperty("user.dir");
\r
50 boolean ovr = false;
\r
53 for (int i = 0; i < args.length; ++i) {
\r
54 String arg = args[i].toLowerCase();
\r
55 if (arg.charAt(0) == '-') {
\r
56 if (arg.equals("-src")) {
\r
59 else if (arg.equals("-dst")) {
\r
62 else if (arg.equals("-dep")) {
\r
65 else if (arg.equals("-prov")) {
\r
68 else if (arg.equals("-overwrite")) {
\r
71 else if (arg.equals("-silent")) { // no output
\r
74 else if (arg.equals("-quiet")) { // output parameters and count of changed files (default)
\r
77 else if (arg.equals("-verbose")) { // output names of modified files
\r
80 else if (arg.equals("-noisy")) { // output names of files not modified
\r
83 else if (arg.equals("-copydebug")) { // output copyright debugging
\r
86 else if (arg.equals("-debug")) { // output all debugging
\r
92 new SwatDeprecated(src, dst, dep, ovr, vrb).run();
\r
95 public SwatDeprecated(String src, String dst, boolean dep, boolean overwrite, int verbosity) {
\r
96 this.srcFile = new File(src);
\r
97 this.dstFile = new File(dst);
\r
98 this.overwrite = overwrite;
\r
99 this.verbosity = verbosity;
\r
100 this.copyYear = new SimpleDateFormat("yyyy").format(new Date());
\r
102 this.srcTag = "@deprecated This is a draft API and might change in a future release of ICU.";
\r
103 this.trgTag = "@provisional This API might change or be removed in a future release.";
\r
105 String temp = srcTag;
\r
110 this.srcPrefix = srcFile.getCanonicalPath();
\r
111 this.dstPrefix = dstFile.getCanonicalPath();
\r
113 catch (IOException e) {
\r
114 RuntimeException re = new RuntimeException(e.getMessage());
\r
119 //this.inPlace = srcPrefix.equals(dstPrefix);
\r
122 if (verbosity >= 1) {
\r
123 pw.println("replacing '" + srcTag + "'");
\r
124 pw.println(" with '" + trgTag + "'");
\r
126 pw.println(" source: '" + srcPrefix + "'");
\r
127 pw.println("destination: '" + dstPrefix + "'");
\r
128 pw.println(" overwrite: " + overwrite);
\r
129 pw.println(" verbosity: " + verbosity);
\r
134 public void run() {
\r
135 if (!srcFile.exists()) {
\r
136 throw new RuntimeException("file " + srcFile.getPath() + " does not exist.");
\r
139 if (verbosity >= 1) {
\r
140 pw.println("changed " + cc + " file(s)");
\r
145 public void doList(File file) {
\r
146 String[] filenames = file.list(ff);
\r
147 if (verbosity >= 5) {
\r
148 pw.println(file.getPath());
\r
149 dumpList(filenames);
\r
152 for (int i = 0; i < filenames.length; ++i) {
\r
153 File f = new File(file, filenames[i]);
\r
154 if (f.isDirectory()) {
\r
162 public void processFile(File inFile) {
\r
163 File bakFile = null;
\r
165 String inPath = inFile.getCanonicalPath();
\r
166 if (verbosity >= 5) {
\r
167 pw.println("processFile: " + inPath);
\r
170 String outPath = dstPrefix + inPath.substring(srcPrefix.length());
\r
171 File outFile = new File(outPath);
\r
173 File tmpFile = null;
\r
174 if (outFile.exists()) {
\r
176 throw new RuntimeException("no permission to overwrite file: " + outPath);
\r
179 tmpFile = File.createTempFile(inFile.getName(), null, inFile.getParentFile());
\r
183 File parent = tmpFile.getParentFile();
\r
185 tmpFile.createNewFile();
\r
188 String tmpPath = tmpFile.getPath();
\r
189 if (verbosity >= 5) {
\r
190 pw.println("tmpFile: " + tmpPath);
\r
193 InputStream is = new FileInputStream(inFile);
\r
194 OutputStream os = new FileOutputStream(tmpFile);
\r
196 PrintStream ps = new PrintStream(os);
\r
197 BufferedReader br = new BufferedReader(new InputStreamReader(is));
\r
202 // boolean debug = false;
\r
203 while (null != (line = br.readLine())) {
\r
204 // int temp = line.indexOf("@deprecated");
\r
205 int ix = line.indexOf(srcTag);
\r
206 // if (temp != -1 && ix == -1) {
\r
207 // if (debug == false) {
\r
209 // pw.println("file: " + name);
\r
211 // pw.println("[" + n + "] " + line);
\r
215 if (verbosity >= 5) {
\r
216 pw.println("[" + n + "] " + line);
\r
219 line = line.substring(0,ix) + trgTag;
\r
222 } else if (n < 20) {
\r
223 // swat copyrights in the first 20 lines while we're at it
\r
224 ix = line.indexOf("opyright");
\r
226 String nline = null;
\r
228 if (verbosity == 4) {
\r
229 pw.println("[" + n + "] " + line);
\r
231 ix = line.indexOf("-200");
\r
233 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+5);
\r
236 ix = line.indexOf("- 200");
\r
238 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+6);
\r
241 ix = line.indexOf("-199");
\r
243 nline = line.substring(0, ix) + "-" + copyYear + line.substring(ix+5);
\r
246 ix = line.indexOf(copyYear);
\r
248 break; // nothing needs changing
\r
250 ix = line.indexOf("200");
\r
252 nline = line.substring(0, ix+4) + "-" + copyYear + line.substring(ix+4);
\r
255 ix = line.indexOf("199");
\r
257 nline = line.substring(0, ix+4) + "-" + copyYear + line.substring(ix+4);
\r
262 if (nline != null) {
\r
263 if (verbosity >= 4) {
\r
264 pw.println(" --> " + nline);
\r
277 if (tc == 0) { // nothing changed, forget this file
\r
278 if (verbosity >= 3) {
\r
279 pw.println("no changes in file: " + inPath);
\r
281 if (!tmpFile.delete()) {
\r
282 throw new RuntimeException("unable to delete unneeded temporary file: " + tmpPath);
\r
288 if (bakFile != null) {
\r
289 if (bakFile.exists()) {
\r
292 if (!tmpFile.renameTo(bakFile)) {
\r
293 pw.println("warning: couldn't rename temp file to: " + outPath);
\r
297 outFile.setLastModified(inFile.lastModified());
\r
299 if (verbosity >= 2) {
\r
300 pw.println(inPath);
\r
304 catch (IOException e) {
\r
305 RuntimeException re = new RuntimeException(e.getMessage());
\r
316 public void dumpList(String[] names) {
\r
317 if (names == null) {
\r
322 if (names.length > 0) {
\r
327 for (int i = 0; i < names.length; ++i) {
\r
328 String name = names[i];
\r
329 int nl = name.length();
\r
330 if (lc + nl > maxLength) {
\r
339 if (names.length > 0) {
\r