1 18 package org.apache.tools.ant.taskdefs.cvslib; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStreamWriter ; 25 import java.io.PrintWriter ; 26 import java.io.UnsupportedEncodingException ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 import java.util.Enumeration ; 30 import java.util.Properties ; 31 import java.util.Vector ; 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.DirectoryScanner; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.taskdefs.AbstractCvsTask; 36 import org.apache.tools.ant.types.FileSet; 37 import org.apache.tools.ant.util.FileUtils; 38 39 79 public class ChangeLogTask extends AbstractCvsTask { 80 81 private File usersFile; 82 83 84 private Vector cvsUsers = new Vector (); 85 86 87 private File inputDir; 88 89 90 private File destFile; 91 92 93 private Date startDate; 94 95 96 private Date endDate; 97 98 103 private final Vector filesets = new Vector (); 104 105 106 111 public void setDir(final File inputDir) { 112 this.inputDir = inputDir; 113 } 114 115 116 121 public void setDestfile(final File destFile) { 122 this.destFile = destFile; 123 } 124 125 126 131 public void setUsersfile(final File usersFile) { 132 this.usersFile = usersFile; 133 } 134 135 136 141 public void addUser(final CvsUser user) { 142 cvsUsers.addElement(user); 143 } 144 145 146 151 public void setStart(final Date start) { 152 this.startDate = start; 153 } 154 155 156 161 public void setEnd(final Date endDate) { 162 this.endDate = endDate; 163 } 164 165 166 171 public void setDaysinpast(final int days) { 172 final long time = System.currentTimeMillis() 173 - (long) days * 24 * 60 * 60 * 1000; 174 175 setStart(new Date (time)); 176 } 177 178 179 184 public void addFileset(final FileSet fileSet) { 185 filesets.addElement(fileSet); 186 } 187 188 189 195 public void execute() throws BuildException { 196 File savedDir = inputDir; 198 try { 199 200 validate(); 201 final Properties userList = new Properties (); 202 203 loadUserlist(userList); 204 205 for (int i = 0, size = cvsUsers.size(); i < size; i++) { 206 final CvsUser user = (CvsUser) cvsUsers.get(i); 207 user.validate(); 208 userList.put(user.getUserID(), user.getDisplayname()); 209 } 210 211 setCommand("log"); 212 213 if (getTag() != null) { 214 CvsVersion myCvsVersion = new CvsVersion(); 215 myCvsVersion.setProject(getProject()); 216 myCvsVersion.setTaskName("cvsversion"); 217 myCvsVersion.setCvsRoot(getCvsRoot()); 218 myCvsVersion.setCvsRsh(getCvsRsh()); 219 myCvsVersion.setPassfile(getPassFile()); 220 myCvsVersion.setDest(inputDir); 221 myCvsVersion.execute(); 222 if (myCvsVersion.supportsCvsLogWithSOption()) { 223 addCommandArgument("-S"); 224 } 225 } 226 if (null != startDate) { 227 final SimpleDateFormat outputDate = 228 new SimpleDateFormat ("yyyy-MM-dd"); 229 230 final String dateRange = ">=" + outputDate.format(startDate); 232 233 addCommandArgument("-d"); 235 addCommandArgument(dateRange); 236 } 237 238 if (!filesets.isEmpty()) { 240 final Enumeration e = filesets.elements(); 241 242 while (e.hasMoreElements()) { 243 final FileSet fileSet = (FileSet) e.nextElement(); 244 final DirectoryScanner scanner = 245 fileSet.getDirectoryScanner(getProject()); 246 final String [] files = scanner.getIncludedFiles(); 247 248 for (int i = 0; i < files.length; i++) { 249 addCommandArgument(files[i]); 250 } 251 } 252 } 253 254 final ChangeLogParser parser = new ChangeLogParser(); 255 final RedirectingStreamHandler handler = 256 new RedirectingStreamHandler(parser); 257 258 log(getCommand(), Project.MSG_VERBOSE); 259 260 setDest(inputDir); 261 setExecuteStreamHandler(handler); 262 try { 263 super.execute(); 264 } finally { 265 final String errors = handler.getErrors(); 266 267 if (null != errors) { 268 log(errors, Project.MSG_ERR); 269 } 270 } 271 final CVSEntry[] entrySet = parser.getEntrySetAsArray(); 272 final CVSEntry[] filteredEntrySet = filterEntrySet(entrySet); 273 274 replaceAuthorIdWithName(userList, filteredEntrySet); 275 276 writeChangeLog(filteredEntrySet); 277 278 } finally { 279 inputDir = savedDir; 280 } 281 } 282 283 288 private void validate() 289 throws BuildException { 290 if (null == inputDir) { 291 inputDir = getProject().getBaseDir(); 292 } 293 if (null == destFile) { 294 final String message = "Destfile must be set."; 295 296 throw new BuildException(message); 297 } 298 if (!inputDir.exists()) { 299 final String message = "Cannot find base dir " 300 + inputDir.getAbsolutePath(); 301 302 throw new BuildException(message); 303 } 304 if (null != usersFile && !usersFile.exists()) { 305 final String message = "Cannot find user lookup list " 306 + usersFile.getAbsolutePath(); 307 308 throw new BuildException(message); 309 } 310 } 311 312 319 private void loadUserlist(final Properties userList) 320 throws BuildException { 321 if (null != usersFile) { 322 try { 323 userList.load(new FileInputStream (usersFile)); 324 } catch (final IOException ioe) { 325 throw new BuildException(ioe.toString(), ioe); 326 } 327 } 328 } 329 330 336 private CVSEntry[] filterEntrySet(final CVSEntry[] entrySet) { 337 final Vector results = new Vector (); 338 339 for (int i = 0; i < entrySet.length; i++) { 340 final CVSEntry cvsEntry = entrySet[i]; 341 final Date date = cvsEntry.getDate(); 342 343 355 if (null == date) { 357 continue; 358 } 359 360 if (null != startDate && startDate.after(date)) { 361 continue; 363 } 364 if (null != endDate && endDate.before(date)) { 365 continue; 367 } 368 results.addElement(cvsEntry); 369 } 370 371 final CVSEntry[] resultArray = new CVSEntry[results.size()]; 372 373 results.copyInto(resultArray); 374 return resultArray; 375 } 376 377 380 private void replaceAuthorIdWithName(final Properties userList, 381 final CVSEntry[] entrySet) { 382 for (int i = 0; i < entrySet.length; i++) { 383 384 final CVSEntry entry = entrySet[ i ]; 385 if (userList.containsKey(entry.getAuthor())) { 386 entry.setAuthor(userList.getProperty(entry.getAuthor())); 387 } 388 } 389 } 390 391 397 private void writeChangeLog(final CVSEntry[] entrySet) 398 throws BuildException { 399 FileOutputStream output = null; 400 401 try { 402 output = new FileOutputStream (destFile); 403 404 final PrintWriter writer = 405 new PrintWriter (new OutputStreamWriter (output, "UTF-8")); 406 407 final ChangeLogWriter serializer = new ChangeLogWriter(); 408 409 serializer.printChangeLog(writer, entrySet); 410 } catch (final UnsupportedEncodingException uee) { 411 getProject().log(uee.toString(), Project.MSG_ERR); 412 } catch (final IOException ioe) { 413 throw new BuildException(ioe.toString(), ioe); 414 } finally { 415 FileUtils.close(output); 416 } 417 } 418 } 419 420 | Popular Tags |