KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > diff > ListDiffPrinter


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

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