1 32 33 package com.jeantessier.dependencyfinder.ant; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.tools.ant.*; 39 import org.apache.tools.ant.types.*; 40 41 import com.jeantessier.diff.*; 42 43 public class ListDiff extends Task { 44 private String name = ""; 45 private File oldFile; 46 private String oldLabel; 47 private File newFile; 48 private String newLabel; 49 private boolean compress = false; 50 private String encoding = ListDiffPrinter.DEFAULT_ENCODING; 51 private String dtdPrefix = ListDiffPrinter.DEFAULT_DTD_PREFIX; 52 private String indentText; 53 private File destfile; 54 55 public String getName() { 56 return name; 57 } 58 59 public void setName(String name) { 60 this.name = name; 61 } 62 63 public File getOld() { 64 return oldFile; 65 } 66 67 public void setOld(File oldFile) { 68 this.oldFile = oldFile; 69 } 70 71 public String getOldlabel() { 72 return oldLabel; 73 } 74 75 public void setOldlabel(String oldLabel) { 76 this.oldLabel = oldLabel; 77 } 78 79 public File getNew() { 80 return newFile; 81 } 82 83 public void setNew(File newFile) { 84 this.newFile = newFile; 85 } 86 87 public String getNewlabel() { 88 return newLabel; 89 } 90 91 public void setNewlabel(String newLabel) { 92 this.newLabel = newLabel; 93 } 94 95 public boolean getCompress() { 96 return compress; 97 } 98 99 public void setCompress(boolean compress) { 100 this.compress = compress; 101 } 102 103 public String getEncoding() { 104 return dtdPrefix; 105 } 106 107 public void setEncoding(String encoding) { 108 this.encoding = encoding; 109 } 110 111 public String getDtdprefix() { 112 return dtdPrefix; 113 } 114 115 public void setDtdprefix(String dtdPrefix) { 116 this.dtdPrefix = dtdPrefix; 117 } 118 119 public String getIndenttext() { 120 return indentText; 121 } 122 123 public void setIntenttext(String indentText) { 124 this.indentText = indentText; 125 } 126 127 public File getDestfile() { 128 return destfile; 129 } 130 131 public void setDestfile(File destfile) { 132 this.destfile = destfile; 133 } 134 135 public void execute() throws BuildException { 136 138 if (getOld() == null) { 139 throw new BuildException("old must be set!"); 140 } 141 142 if (!getOld().exists()) { 143 throw new BuildException("old does not exist!"); 144 } 145 146 if (!getOld().isFile()) { 147 throw new BuildException("old is not a file!"); 148 } 149 150 if (getNew() == null) { 151 throw new BuildException("new must be set!"); 152 } 153 154 if (!getNew().exists()) { 155 throw new BuildException("new does not exist!"); 156 } 157 158 if (!getNew().isFile()) { 159 throw new BuildException("new is not a file!"); 160 } 161 162 if (getDestfile() == null) { 163 throw new BuildException("destfile must be set!"); 164 } 165 166 VerboseListener verboseListener = new VerboseListener(this); 167 168 try { 169 String line; 170 171 log("Loading old list from " + getOld().getAbsolutePath()); 172 Collection oldAPI = new TreeSet(); 173 BufferedReader oldIn = new BufferedReader(new FileReader(getOld())); 174 while((line = oldIn.readLine()) != null) { 175 oldAPI.add(line); 176 } 177 oldIn.close(); 178 179 log("Loading new list from " + getNew().getAbsolutePath()); 180 Collection newAPI = new TreeSet(); 181 BufferedReader newIn = new BufferedReader(new FileReader(getNew())); 182 while((line = newIn.readLine()) != null) { 183 newAPI.add(line); 184 } 185 newIn.close(); 186 187 log("Comparing old and new lists ..."); 188 189 ListDiffPrinter printer = new ListDiffPrinter(getCompress(), getEncoding(), getDtdprefix()); 190 printer.setName(getName()); 191 printer.setOldVersion(getOldlabel()); 192 printer.setNewVersion(getNewlabel()); 193 if (getIndenttext() != null) { 194 printer.setIndentText(getIndenttext()); 195 } 196 197 Iterator i; 198 199 i = oldAPI.iterator(); 200 while (i.hasNext()) { 201 line = (String ) i.next(); 202 if (!newAPI.contains(line)) { 203 printer.remove(line); 204 } 205 } 206 207 i = newAPI.iterator(); 208 while (i.hasNext()) { 209 line = (String ) i.next(); 210 if (!oldAPI.contains(line)) { 211 printer.add(line); 212 } 213 } 214 215 log("Saving difference report to " + getDestfile().getAbsolutePath()); 216 217 PrintWriter out = new PrintWriter(new FileWriter(getDestfile())); 218 out.print(printer); 219 out.close(); 220 } catch (IOException ex) { 221 throw new BuildException(ex); 222 } 223 } 224 } 225 | Popular Tags |