1 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 DEFAULT_ENCODING = "utf-8"; 42 public static final String DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd"; 43 44 private static final Perl5Util perl = new Perl5Util(); 45 46 private StringBuffer buffer = new StringBuffer (); 47 48 private String indentText = " "; 49 private int indentLevel = 0; 50 private boolean compress; 51 52 private String name = ""; 53 private String oldVersion = ""; 54 private String 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 encoding, String dtdPrefix) { 67 this(DEFAULT_COMPRESS, encoding, dtdPrefix); 68 } 69 70 public ListDiffPrinter(boolean compress, String encoding, String dtdPrefix) { 71 this.compress = compress; 72 73 appendHeader(encoding, dtdPrefix); 74 } 75 76 public String getIndentText() { 77 return indentText; 78 } 79 80 public void setIndentText(String indentText) { 81 this.indentText = indentText; 82 } 83 84 private void appendHeader(String encoding, String 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 getName() { 92 return name; 93 } 94 95 public void setName(String name) { 96 this.name = name; 97 } 98 99 public String getOldVersion() { 100 return oldVersion; 101 } 102 103 public void setOldVersion(String oldVersion) { 104 this.oldVersion = oldVersion; 105 } 106 107 public String getNewVersion() { 108 return newVersion; 109 } 110 111 public void setNewVersion(String 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 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 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 obj) { 180 buffer.append(obj); 181 return this; 182 } 183 184 protected ListDiffPrinter append(String 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 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 buffer, Collection lines) { 236 Iterator i = lines.iterator(); 237 while (i.hasNext()) { 238 String line = (String ) 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 line = (String ) i.next(); 255 256 boolean add = true; 257 if (line.endsWith(" [C]")) { 258 String packageName = extractPackageName(line); 259 260 add = !lines.contains(packageName + " [P]"); 261 } else if (line.endsWith(" [F]")) { 262 String className = extractClassName(line); 263 String 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 extractPackageName(String className) { 277 String 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 extractClassName(String featureName) { 288 String 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 |