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