Coverage Report - net.sf.statcvs.pages.xml.CommitListXml
 
Classes in this File Line Coverage Branch Coverage Complexity
CommitListXml
0%
0/127
0%
0/76
2.8
 
 1  
 package net.sf.statcvs.pages.xml;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Collections;
 5  
 import java.util.HashMap;
 6  
 import java.util.HashSet;
 7  
 import java.util.Iterator;
 8  
 import java.util.List;
 9  
 import java.util.Set;
 10  
 
 11  
 import net.sf.statcvs.model.Commit;
 12  
 import net.sf.statcvs.model.Revision;
 13  
 import net.sf.statcvs.model.SymbolicName;
 14  
 import net.sf.statcvs.renderer.FileCollectionFormatter;
 15  
 
 16  
 import org.jdom.CDATA;
 17  
 import org.jdom.Element;
 18  
 
 19  
 /**
 20  
  * This is a mere copy of the CommitListFormatter class with dfew minor changes done
 21  
  * Class for formatting a list of commits as XML.
 22  
  *
 23  
  * @author Anja Jentzsch
 24  
  * @author Richard Cyganiak (richard@cyganiak.de)
 25  
  * @version $Id: CommitListXml.java,v 1.4 2008/04/02 11:52:02 benoitx Exp $
 26  
  */
 27  
 public class CommitListXml {
 28  
     private final List commits;
 29  
     private final List tags;
 30  0
     private final HashMap commitHashMap = new HashMap();
 31  
 
 32  0
     /**
 33  
      * Creates a new instance for the list of commits.
 34  
      * @param commits A list of {@link Commit} objects
 35  
      */
 36  
     public CommitListXml(final List commits, final List tags, final boolean withPermalinks) {
 37  0
         this(commits, tags, Integer.MAX_VALUE, withPermalinks);
 38  0
     }
 39  0
 
 40  0
     /**
 41  
      * Creates a new instance for the list of commits.
 42  
      * @param commits A list of {@link Commit} objects
 43  
      * @param max maximum number of commits for the log; if there
 44  
      * are more, only the most recent will be used
 45  
      */
 46  0
     public CommitListXml(final List commit, final List tags, final int max, final boolean withPermalinks) {
 47  0
         this.commits = new ArrayList(commit);
 48  0
         this.tags = tags;
 49  0
         Collections.reverse(this.commits);
 50  0
     }
 51  0
 
 52  0
     public Element renderCommitList(final List commitList) {
 53  0
         if (commitList.isEmpty()) {
 54  0
             return null;
 55  
         }
 56  0
         int id = commitList.size();
 57  0
         final Element lg = new Element(XmlTags.TAG_COMMIT_LIST);
 58  0
         final Iterator commitIt = commitList.iterator();
 59  0
         Commit nextCommit = commitIt.hasNext() ? (Commit) commitIt.next() : null;
 60  0
         final Iterator tagIt = this.tags.iterator();
 61  0
         SymbolicName nextTag = tagIt.hasNext() ? (SymbolicName) tagIt.next() : null;
 62  0
         while (nextCommit != null) {
 63  0
             Element log = null;
 64  0
             if (nextTag == null || nextCommit.getDate().getTime() > nextTag.getDate().getTime()) {
 65  0
                 log = renderCommit(nextCommit, id);
 66  0
                 nextCommit = commitIt.hasNext() ? (Commit) commitIt.next() : null;
 67  0
                 id--;
 68  0
             } else {
 69  0
                 nextTag = tagIt.hasNext() ? (SymbolicName) tagIt.next() : null;
 70  0
             }
 71  0
             lg.addContent(log);
 72  0
         }
 73  0
         return lg;
 74  0
     }
 75  0
 
 76  0
     private Element renderCommit(final Commit commit, final int id) {
 77  0
         final Element cmt = new Element(XmlTags.TAG_COMMIT);
 78  
 
 79  0
         cmt.setAttribute("date", getDate(commit));
 80  0
         cmt.setAttribute("author", getAuthor(commit));
 81  0
         cmt.setAttribute("loc_change", getLinesOfCode(commit));
 82  0
         final String revision = getRevisionNumber(commit);
 83  0
         if (revision != null) {
 84  0
             cmt.setAttribute("revision", revision);
 85  0
         }
 86  0
         final Element comment = new Element(XmlTags.TAG_COMMENT);
 87  0
         final CDATA theActualComment = new CDATA(commit.getComment());
 88  0
         comment.addContent(theActualComment);
 89  0
         cmt.addContent(comment);
 90  0
         cmt.addContent(getAffectedFiles(commit));
 91  0
 
 92  0
         return cmt;
 93  0
     }
 94  
 
 95  0
     private String getRevisionNumber(final Commit commit) {
 96  0
         final Set rev = new HashSet();
 97  0
         for (final Iterator it = commit.getRevisions().iterator(); it.hasNext();) {
 98  0
             rev.add(((Revision) it.next()).getRevisionNumber());
 99  0
         }
 100  0
         if (rev.size() == 1) {
 101  0
             return (String) rev.iterator().next();
 102  
         } else {
 103  0
             return null;
 104  0
         }
 105  
     }
 106  0
 
 107  
     private String getDate(final Commit commit) {
 108  0
         return XML.getDateAndTime(commit.getDate());
 109  
     }
 110  
 
 111  0
     private String getAuthor(final Commit commit) {
 112  0
         return XML.getAuthorLink(commit.getAuthor());
 113  
     }
 114  
 
 115  0
     private String getLinesOfCode(final Commit commit) {
 116  0
         final Iterator it = commit.getRevisions().iterator();
 117  0
         int locSum = 0;
 118  0
         while (it.hasNext()) {
 119  0
             final Revision each = (Revision) it.next();
 120  0
             locSum += each.getNewLines();
 121  0
             saveRevision(each);
 122  0
         }
 123  0
         return Integer.toString(locSum);
 124  0
     }
 125  0
 
 126  0
     private void saveRevision(final Revision revision) {
 127  0
         commitHashMap.put(revision.getFile().getFilenameWithPath(), revision);
 128  0
     }
 129  
 
 130  0
     private Element getAffectedFiles(final Commit commit) {
 131  0
         final Element result = new Element(XmlTags.TAG_FILES_AFFECTED);
 132  0
         final FileCollectionFormatter formatter = new FileCollectionFormatter(commit.getAffectedFiles());
 133  0
         final Iterator it = formatter.getDirectories().iterator();
 134  0
         while (it.hasNext()) {
 135  0
             final String directory = (String) it.next();
 136  0
             final Iterator files = formatter.getFiles(directory).iterator();
 137  0
             final StringBuffer fileList = new StringBuffer();
 138  0
             while (files.hasNext()) {
 139  0
                 final Element File = new Element(XmlTags.TAG_FILE);
 140  0
                 if (fileList.length() > 0) {
 141  0
                     fileList.append(",\n");
 142  0
                 }
 143  0
                 final String file = (String) files.next();
 144  0
                 final Element path = new Element(XmlTags.TAG_PATH).setText(directory + file);
 145  0
                 File.addContent(path);
 146  0
                 final Revision revision = (Revision) commitHashMap.get(directory + file);
 147  0
                 if (revision.isInitialRevision()) {
 148  0
                     final int linesAdded = revision.getLines();
 149  0
                     File.setAttribute("action", "new");
 150  0
                     if (linesAdded > 0) {
 151  0
                         File.addContent(new Element(XmlTags.TAG_LOC_ADDED).setText(Integer.toString(linesAdded)));
 152  0
                     }
 153  0
                 } else if (revision.isDead()) {
 154  0
                     File.setAttribute("action", "deleted");
 155  
                 } else {
 156  0
                     final int delta = revision.getLinesDelta();
 157  0
                     final int linesAdded = revision.getReplacedLines() + ((delta > 0) ? delta : 0);
 158  0
                     final int linesRemoved = revision.getReplacedLines() - ((delta < 0) ? delta : 0);
 159  0
                     if (linesAdded > 0) {
 160  0
                         File.setAttribute("action", "changed");
 161  0
                         File.addContent(new Element(XmlTags.TAG_LOC_ADDED).setText(Integer.toString(linesAdded)));
 162  0
                         if (linesRemoved > 0) {
 163  0
                             File.addContent(new Element(XmlTags.TAG_LOC_REMOVED).setText(Integer.toString(linesRemoved)));
 164  0
                         }
 165  0
                     } else if (linesRemoved > 0) {
 166  0
                         File.setAttribute("action", "changed");
 167  0
                         File.addContent(new Element(XmlTags.TAG_LOC_ADDED).setText("0"));
 168  0
                         File.addContent(new Element(XmlTags.TAG_LOC_REMOVED).setText(Integer.toString(linesRemoved)));
 169  0
                     } else { // linesAdded == linesRemoved == 0
 170  0
                         // should be binary file or keyword subst change
 171  0
                         File.setAttribute("action", "binary file or keyword subst change");
 172  
                     }
 173  
                 }
 174  0
                 result.addContent(File);
 175  0
             }
 176  0
         }
 177  0
         return result;
 178  0
     }
 179  0
 }