1 18 package org.apache.tools.ant.taskdefs.cvslib; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.io.PrintWriter ; 27 import java.io.UnsupportedEncodingException ; 28 import java.util.Vector ; 29 import java.util.StringTokenizer ; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.taskdefs.AbstractCvsTask; 34 import org.apache.tools.ant.util.DOMElementWriter; 35 import org.apache.tools.ant.util.DOMUtils; 36 import org.apache.tools.ant.util.FileUtils; 37 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 41 72 public class CvsTagDiff extends AbstractCvsTask { 73 74 77 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 78 79 80 private static final DOMElementWriter DOM_WRITER = new DOMElementWriter(); 81 82 85 static final String FILE_STRING = "File "; 86 89 static final String TO_STRING = " to "; 90 93 static final String FILE_IS_NEW = " is new;"; 94 97 static final String REVISION = "revision "; 98 99 102 static final String FILE_HAS_CHANGED = " changed from revision "; 103 104 107 static final String FILE_WAS_REMOVED = " is removed"; 108 109 112 private String mypackage; 113 114 117 private String mystartTag; 118 119 122 private String myendTag; 123 124 127 private String mystartDate; 128 129 132 private String myendDate; 133 134 137 private File mydestfile; 138 139 143 public void setPackage(String p) { 144 mypackage = p; 145 } 146 147 152 public void setStartTag(String s) { 153 mystartTag = s; 154 } 155 156 161 public void setStartDate(String s) { 162 mystartDate = s; 163 } 164 165 170 public void setEndTag(String s) { 171 myendTag = s; 172 } 173 174 179 public void setEndDate(String s) { 180 myendDate = s; 181 } 182 183 188 public void setDestFile(File f) { 189 mydestfile = f; 190 } 191 192 197 public void execute() throws BuildException { 198 validate(); 200 201 addCommandArgument("rdiff"); 203 addCommandArgument("-s"); 204 if (mystartTag != null) { 205 addCommandArgument("-r"); 206 addCommandArgument(mystartTag); 207 } else { 208 addCommandArgument("-D"); 209 addCommandArgument(mystartDate); 210 } 211 if (myendTag != null) { 212 addCommandArgument("-r"); 213 addCommandArgument(myendTag); 214 } else { 215 addCommandArgument("-D"); 216 addCommandArgument(myendDate); 217 } 218 StringTokenizer myTokenizer = new StringTokenizer (mypackage); 220 while (myTokenizer.hasMoreTokens()) { 221 addCommandArgument(myTokenizer.nextToken()); 222 } 223 setCommand(""); 225 File tmpFile = null; 226 try { 227 tmpFile = FILE_UTILS.createTempFile("cvstagdiff", ".log", null); 228 tmpFile.deleteOnExit(); 229 setOutput(tmpFile); 230 231 super.execute(); 233 234 CvsTagEntry[] entries = parseRDiff(tmpFile); 236 237 writeTagDiff(entries); 239 240 } finally { 241 if (tmpFile != null) { 242 tmpFile.delete(); 243 } 244 } 245 } 246 247 255 private CvsTagEntry[] parseRDiff(File tmpFile) throws BuildException { 256 BufferedReader reader = null; 258 259 try { 260 reader = new BufferedReader (new FileReader (tmpFile)); 261 262 String toBeRemoved = FILE_STRING + mypackage + "/"; 277 int headerLength = toBeRemoved.length(); 278 Vector entries = new Vector (); 279 280 String line = reader.readLine(); 281 int index; 282 CvsTagEntry entry = null; 283 284 while (null != line) { 285 if (line.length() > headerLength) { 286 if (line.startsWith(toBeRemoved)) { 287 line = line.substring(headerLength); 288 } else { 289 line = line.substring(FILE_STRING.length()); 290 } 291 292 if ((index = line.indexOf(FILE_IS_NEW)) != -1) { 293 String filename = line.substring(0, index); 296 String rev = null; 297 int indexrev = -1; 298 if ((indexrev = line.indexOf(REVISION, index)) != -1) { 299 rev = line.substring(indexrev + REVISION.length()); 300 } 301 entry = new CvsTagEntry(filename, rev); 302 entries.addElement(entry); 303 log(entry.toString(), Project.MSG_VERBOSE); 304 } else if ((index = line.indexOf(FILE_HAS_CHANGED)) != -1) { 305 String filename = line.substring(0, index); 308 int revSeparator = line.indexOf(" to ", index); 309 String prevRevision = 310 line.substring(index + FILE_HAS_CHANGED.length(), 311 revSeparator); 312 String revision = line.substring(revSeparator + TO_STRING.length()); 313 entry = new CvsTagEntry(filename, 314 revision, 315 prevRevision); 316 entries.addElement(entry); 317 log(entry.toString(), Project.MSG_VERBOSE); 318 } else if ((index = line.indexOf(FILE_WAS_REMOVED)) != -1) { 319 String filename = line.substring(0, index); 321 String rev = null; 322 int indexrev = -1; 323 if ((indexrev = line.indexOf(REVISION, index)) != -1) { 324 rev = line.substring(indexrev + REVISION.length()); 325 } 326 entry = new CvsTagEntry(filename, null, rev); 327 entries.addElement(entry); 328 log(entry.toString(), Project.MSG_VERBOSE); 329 } 330 } 331 line = reader.readLine(); 332 } 333 334 CvsTagEntry[] array = new CvsTagEntry[entries.size()]; 335 entries.copyInto(array); 336 337 return array; 338 } catch (IOException e) { 339 throw new BuildException("Error in parsing", e); 340 } finally { 341 if (reader != null) { 342 try { 343 reader.close(); 344 } catch (IOException e) { 345 log(e.toString(), Project.MSG_ERR); 346 } 347 } 348 } 349 } 350 351 357 private void writeTagDiff(CvsTagEntry[] entries) throws BuildException { 358 FileOutputStream output = null; 359 try { 360 output = new FileOutputStream (mydestfile); 361 PrintWriter writer = new PrintWriter ( 362 new OutputStreamWriter (output, "UTF-8")); 363 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 364 Document doc = DOMUtils.newDocument(); 365 Element root = doc.createElement("tagdiff"); 366 if (mystartTag != null) { 367 root.setAttribute("startTag", mystartTag); 368 } else { 369 root.setAttribute("startDate", mystartDate); 370 } 371 if (myendTag != null) { 372 root.setAttribute("endTag", myendTag); 373 } else { 374 root.setAttribute("endDate", myendDate); 375 } 376 377 root.setAttribute("cvsroot", getCvsRoot()); 378 root.setAttribute("package", mypackage); 379 DOM_WRITER.openElement(root, writer, 0, "\t"); 380 writer.println(); 381 for (int i = 0, c = entries.length; i < c; i++) { 382 writeTagEntry(doc, writer, entries[i]); 383 } 384 DOM_WRITER.closeElement(root, writer, 0, "\t", true); 385 writer.flush(); 386 writer.close(); 387 } catch (UnsupportedEncodingException uee) { 388 log(uee.toString(), Project.MSG_ERR); 389 } catch (IOException ioe) { 390 throw new BuildException(ioe.toString(), ioe); 391 } finally { 392 if (null != output) { 393 try { 394 output.close(); 395 } catch (IOException ioe) { 396 log(ioe.toString(), Project.MSG_ERR); 397 } 398 } 399 } 400 } 401 402 409 private void writeTagEntry(Document doc, PrintWriter writer, 410 CvsTagEntry entry) 411 throws IOException { 412 Element ent = doc.createElement("entry"); 413 Element f = DOMUtils.createChildElement(ent, "file"); 414 DOMUtils.appendCDATAElement(f, "name", entry.getFile()); 415 if (entry.getRevision() != null) { 416 DOMUtils.appendTextElement(f, "revision", entry.getRevision()); 417 } 418 if (entry.getPreviousRevision() != null) { 419 DOMUtils.appendTextElement(f, "prevrevision", 420 entry.getPreviousRevision()); 421 } 422 DOM_WRITER.write(ent, writer, 1, "\t"); 423 } 424 425 430 private void validate() throws BuildException { 431 if (null == mypackage) { 432 throw new BuildException("Package/module must be set."); 433 } 434 435 if (null == mydestfile) { 436 throw new BuildException("Destfile must be set."); 437 } 438 439 if (null == mystartTag && null == mystartDate) { 440 throw new BuildException("Start tag or start date must be set."); 441 } 442 443 if (null != mystartTag && null != mystartDate) { 444 throw new BuildException("Only one of start tag and start date " 445 + "must be set."); 446 } 447 448 if (null == myendTag && null == myendDate) { 449 throw new BuildException("End tag or end date must be set."); 450 } 451 452 if (null != myendTag && null != myendDate) { 453 throw new BuildException("Only one of end tag and end date must " 454 + "be set."); 455 } 456 } 457 } 458 | Popular Tags |