1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.creadur.tentacles;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.codehaus.swizzle.stream.DelimitedTokenReplacementInputStream;
27 import org.codehaus.swizzle.stream.ExcludeFilterInputStream;
28 import org.codehaus.swizzle.stream.StringTokenHandler;
29
30
31
32
33
34
35 public class Deauthorize {
36
37
38
39
40
41
42
43
44
45
46
47 public static void main(final String[] args) throws Exception {
48
49 if (args.length == 0) {
50 throw new IllegalArgumentException(
51 "At least one directory must be specified");
52 }
53
54 final List<File> dirs = new ArrayList<File>();
55
56
57 for (final String arg : args) {
58 final File dir = new File(arg);
59
60 if (not(dir.exists(), "Does not exist: %s", arg)) {
61 continue;
62 }
63 if (not(dir.isDirectory(), "Not a directory: %s", arg)) {
64 continue;
65 }
66
67 dirs.add(dir);
68 }
69
70
71 if (dirs.size() != args.length) {
72 System.exit(1);
73 }
74
75
76 for (final File dir : dirs) {
77 deauthorize(dir);
78 }
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 private static void deauthorize(final File dir) throws IOException {
101 deauthorize(dir, new IOSystem());
102 }
103
104 private static void deauthorize(final File dir, final IOSystem io)
105 throws IOException {
106 for (final File file : new FileSystem().collect(dir, ".*\\.java")) {
107
108 if (not(file.canRead(), "File not readable: %s",
109 file.getAbsolutePath())) {
110 continue;
111 }
112
113 final String text = io.slurp(file);
114
115
116 final String eol = text.contains("\r\n") ? "\r\n" : "\n";
117 final String startComment = eol + "/*";
118 final String endComment = "*/" + eol;
119
120 final InputStream baseIn = new ByteArrayInputStream(text.getBytes());
121
122
123 InputStream in = new ExcludeFilterInputStream(baseIn, " * @author",
124 eol);
125
126
127 in = new DelimitedTokenReplacementInputStream(in, startComment,
128 endComment, new StringTokenHandler() {
129 @Override
130 public String handleToken(final String commentBlock)
131 throws IOException {
132
133
134 if (commentBlock.replaceAll("[\\s*]", "").length() == 0) {
135 return eol;
136 }
137
138
139 return startComment + commentBlock + endComment;
140 }
141 });
142
143 final byte[] content = io.read(in);
144
145 if (content.length != file.length()) {
146
147 if (not(file.canWrite(), "File not writable: %s",
148 file.getAbsolutePath())) {
149 continue;
150 }
151
152 io.copy(content, file);
153 }
154 }
155
156 }
157
158 private static boolean not(boolean b, final String message,
159 final Object... details) {
160 b = !b;
161 if (b) {
162 System.err.printf(message, details);
163 System.err.println();
164 }
165 return b;
166 }
167 }