1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.diff; |
34 | |
|
35 | |
import java.util.*; |
36 | |
|
37 | |
import org.apache.oro.text.perl.*; |
38 | |
|
39 | |
import com.jeantessier.text.*; |
40 | |
|
41 | |
public class ListDiffPrinter { |
42 | |
public static final boolean DEFAULT_COMPRESS = false; |
43 | |
public static final String DEFAULT_ENCODING = "utf-8"; |
44 | |
public static final String DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd"; |
45 | |
public static final String DEFAULT_INDENT_TEXT = PrinterBuffer.DEFAULT_INDENT_TEXT; |
46 | |
|
47 | 1 | private static final Perl5Util perl = new Perl5Util(); |
48 | |
|
49 | 10 | private PrinterBuffer buffer = new PrinterBuffer(); |
50 | |
|
51 | |
private boolean compress; |
52 | |
|
53 | 10 | private String name = ""; |
54 | 10 | private String oldVersion = ""; |
55 | 10 | private String newVersion = ""; |
56 | 10 | private Collection<String> removed = new TreeSet<String>(); |
57 | 10 | private Collection<String> added = new TreeSet<String>(); |
58 | |
|
59 | |
public ListDiffPrinter() { |
60 | 2 | this(DEFAULT_COMPRESS, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX); |
61 | 2 | } |
62 | |
|
63 | |
public ListDiffPrinter(boolean compress) { |
64 | 0 | this(compress, DEFAULT_ENCODING, DEFAULT_DTD_PREFIX); |
65 | 0 | } |
66 | |
|
67 | |
public ListDiffPrinter(String encoding, String dtdPrefix) { |
68 | 3 | this(DEFAULT_COMPRESS, encoding, dtdPrefix); |
69 | 3 | } |
70 | |
|
71 | 10 | public ListDiffPrinter(boolean compress, String encoding, String dtdPrefix) { |
72 | 10 | this.compress = compress; |
73 | |
|
74 | 10 | appendHeader(encoding, dtdPrefix); |
75 | 10 | } |
76 | |
|
77 | |
public void setIndentText(String indentText) { |
78 | 0 | buffer.setIndentText(indentText); |
79 | 0 | } |
80 | |
|
81 | |
private void appendHeader(String encoding, String dtdPrefix) { |
82 | 10 | append("<?xml version=\"1.0\" encoding=\"").append(encoding).append("\" ?>").eol(); |
83 | 10 | eol(); |
84 | 10 | append("<!DOCTYPE list-diff SYSTEM \"").append(dtdPrefix).append("/list-diff.dtd\">").eol(); |
85 | 10 | eol(); |
86 | 10 | } |
87 | |
|
88 | |
public String getName() { |
89 | 10 | return name; |
90 | |
} |
91 | |
|
92 | |
public void setName(String name) { |
93 | 0 | this.name = name; |
94 | 0 | } |
95 | |
|
96 | |
public String getOldVersion() { |
97 | 10 | return oldVersion; |
98 | |
} |
99 | |
|
100 | |
public void setOldVersion(String oldVersion) { |
101 | 0 | this.oldVersion = oldVersion; |
102 | 0 | } |
103 | |
|
104 | |
public String getNewVersion() { |
105 | 10 | return newVersion; |
106 | |
} |
107 | |
|
108 | |
public void setNewVersion(String newVersion) { |
109 | 0 | this.newVersion = newVersion; |
110 | 0 | } |
111 | |
|
112 | |
public Collection<String> getRemoved() { |
113 | 10 | return Collections.unmodifiableCollection(removed); |
114 | |
} |
115 | |
|
116 | |
public void remove(String line) { |
117 | 46 | this.removed.add(line); |
118 | 46 | } |
119 | |
|
120 | |
public Collection<String> getAdded() { |
121 | 10 | return Collections.unmodifiableCollection(added); |
122 | |
} |
123 | |
|
124 | |
public void add(String line) { |
125 | 46 | this.added.add(line); |
126 | 46 | } |
127 | |
|
128 | |
protected ListDiffPrinter append(boolean b) { |
129 | 0 | buffer.append(b); |
130 | 0 | return this; |
131 | |
} |
132 | |
|
133 | |
protected ListDiffPrinter append(char c) { |
134 | 0 | buffer.append(c); |
135 | 0 | return this; |
136 | |
} |
137 | |
|
138 | |
protected ListDiffPrinter append(char[] str, int offset, int len) { |
139 | 0 | buffer.append(str, offset, len); |
140 | 0 | return this; |
141 | |
} |
142 | |
|
143 | |
protected ListDiffPrinter append(char[] str) { |
144 | 0 | buffer.append(str); |
145 | 0 | return this; |
146 | |
} |
147 | |
|
148 | |
protected ListDiffPrinter append(double d) { |
149 | 0 | buffer.append(d); |
150 | 0 | return this; |
151 | |
} |
152 | |
|
153 | |
protected ListDiffPrinter append(float f) { |
154 | 0 | buffer.append(f); |
155 | 0 | return this; |
156 | |
} |
157 | |
|
158 | |
protected ListDiffPrinter append(int i) { |
159 | 0 | buffer.append(i); |
160 | 0 | return this; |
161 | |
} |
162 | |
|
163 | |
protected ListDiffPrinter append(long l) { |
164 | 0 | buffer.append(l); |
165 | 0 | return this; |
166 | |
} |
167 | |
|
168 | |
protected ListDiffPrinter append(Object obj) { |
169 | 0 | buffer.append(obj); |
170 | 0 | return this; |
171 | |
} |
172 | |
|
173 | |
protected ListDiffPrinter append(String str) { |
174 | 432 | buffer.append(str); |
175 | 432 | return this; |
176 | |
} |
177 | |
|
178 | |
protected ListDiffPrinter indent() { |
179 | 164 | buffer.indent(); |
180 | 164 | return this; |
181 | |
} |
182 | |
|
183 | |
protected ListDiffPrinter eol() { |
184 | 204 | buffer.eol(); |
185 | 204 | return this; |
186 | |
} |
187 | |
|
188 | |
protected void raiseIndent() { |
189 | 30 | buffer.raiseIndent(); |
190 | 30 | } |
191 | |
|
192 | |
protected void lowerIndent() { |
193 | 30 | buffer.lowerIndent(); |
194 | 30 | } |
195 | |
|
196 | |
public String toString() { |
197 | 10 | indent().append("<list-diff>").eol(); |
198 | 10 | raiseIndent(); |
199 | |
|
200 | 10 | indent().append("<name>").append(getName()).append("</name>").eol(); |
201 | 10 | indent().append("<old>").append(getOldVersion()).append("</old>").eol(); |
202 | 10 | indent().append("<new>").append(getNewVersion()).append("</new>").eol(); |
203 | |
|
204 | 10 | indent().append("<removed>").eol(); |
205 | 10 | raiseIndent(); |
206 | 10 | printLines(compress ? compress(getRemoved()) : getRemoved()); |
207 | 10 | lowerIndent(); |
208 | 10 | indent().append("</removed>").eol(); |
209 | |
|
210 | 10 | indent().append("<added>").eol(); |
211 | 10 | raiseIndent(); |
212 | 10 | printLines(compress ? compress(getAdded()) : getAdded()); |
213 | 10 | lowerIndent(); |
214 | 10 | indent().append("</added>").eol(); |
215 | |
|
216 | 10 | lowerIndent(); |
217 | 10 | indent().append("</list-diff>").eol(); |
218 | |
|
219 | 10 | return buffer.toString(); |
220 | |
} |
221 | |
|
222 | |
private void printLines(Collection<String> lines) { |
223 | 20 | for (String line : lines) { |
224 | 74 | int pos = line.lastIndexOf(" ["); |
225 | 74 | if (pos != -1) { |
226 | 16 | indent().append("<line>").append(line.substring(0, pos)).append("</line>").eol(); |
227 | |
} else { |
228 | 58 | indent().append("<line>").append(line).append("</line>").eol(); |
229 | |
} |
230 | 74 | } |
231 | 20 | } |
232 | |
|
233 | |
private Collection<String> compress(Collection<String> lines) { |
234 | 8 | Collection<String> result = new TreeSet<String>(); |
235 | |
|
236 | 8 | for (String line : lines) { |
237 | 68 | boolean add = true; |
238 | 68 | if (line.endsWith(" [C]")) { |
239 | 8 | String packageName = extractPackageName(line); |
240 | |
|
241 | 8 | add = !lines.contains(packageName + " [P]"); |
242 | 8 | } else if (line.endsWith(" [F]")) { |
243 | 18 | String className = extractClassName(line); |
244 | 18 | String packageName = extractPackageName(className); |
245 | |
|
246 | 18 | add = !lines.contains(packageName + " [P]") && !lines.contains(className + " [C]"); |
247 | |
} |
248 | |
|
249 | 68 | if (add) { |
250 | 50 | result.add(line); |
251 | |
} |
252 | 68 | } |
253 | |
|
254 | 8 | return result; |
255 | |
} |
256 | |
|
257 | |
private String extractPackageName(String className) { |
258 | 26 | String result = ""; |
259 | |
|
260 | 26 | int pos = className.lastIndexOf('.'); |
261 | 26 | if (pos != -1) { |
262 | 26 | result = className.substring(0, pos); |
263 | |
} |
264 | |
|
265 | 26 | return result; |
266 | |
} |
267 | |
|
268 | |
private String extractClassName(String featureName) { |
269 | 18 | String result = ""; |
270 | |
|
271 | 18 | synchronized (perl) { |
272 | 18 | if (perl.match("/^(.*)\\.[^\\.]*\\(.*\\)/", featureName)) { |
273 | 14 | result = perl.group(1); |
274 | 4 | } else if (perl.match("/^(.*)\\.[\\^.]*/", featureName)) { |
275 | 4 | result = perl.group(1); |
276 | |
} |
277 | 18 | } |
278 | |
|
279 | 18 | return result; |
280 | |
} |
281 | |
} |