KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > scm > CVSChangeLogSet


1 package hudson.scm;
2
3 import hudson.model.AbstractBuild;
4 import hudson.model.User;
5 import hudson.scm.CVSChangeLogSet.CVSChangeLog;
6 import hudson.util.IOException2;
7 import org.apache.commons.digester.Digester;
8 import org.xml.sax.SAXException JavaDoc;
9
10 import java.io.IOException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.AbstractList JavaDoc;
17
18 /**
19  * {@link ChangeLogSet} for CVS.
20  * @author Kohsuke Kawaguchi
21  */

22 public final class CVSChangeLogSet extends ChangeLogSet<CVSChangeLog> {
23     private List JavaDoc<CVSChangeLog> logs;
24
25     public CVSChangeLogSet(AbstractBuild<?,?> build, List JavaDoc<CVSChangeLog> logs) {
26         super(build);
27         this.logs = Collections.unmodifiableList(logs);
28         for (CVSChangeLog log : logs)
29             log.setParent(this);
30     }
31
32     /**
33      * Returns the read-only list of changes.
34      */

35     public List JavaDoc<CVSChangeLog> getLogs() {
36         return logs;
37     }
38
39     @Override JavaDoc
40     public boolean isEmptySet() {
41         return logs.isEmpty();
42     }
43
44
45     public Iterator JavaDoc<CVSChangeLog> iterator() {
46         return logs.iterator();
47     }
48
49     public static CVSChangeLogSet parse( AbstractBuild build, java.io.File JavaDoc f ) throws IOException JavaDoc, SAXException JavaDoc {
50         Digester digester = new Digester();
51         ArrayList JavaDoc<CVSChangeLog> r = new ArrayList JavaDoc<CVSChangeLog>();
52         digester.push(r);
53
54         digester.addObjectCreate("*/entry",CVSChangeLog.class);
55         digester.addBeanPropertySetter("*/entry/date");
56         digester.addBeanPropertySetter("*/entry/time");
57         digester.addBeanPropertySetter("*/entry/author","user");
58         digester.addBeanPropertySetter("*/entry/msg");
59         digester.addSetNext("*/entry","add");
60
61         digester.addObjectCreate("*/entry/file",File.class);
62         digester.addBeanPropertySetter("*/entry/file/name");
63         digester.addBeanPropertySetter("*/entry/file/revision");
64         digester.addBeanPropertySetter("*/entry/file/prevrevision");
65         digester.addCallMethod("*/entry/file/dead","setDead");
66         digester.addSetNext("*/entry/file","addFile");
67
68         try {
69             digester.parse(f);
70         } catch (IOException JavaDoc e) {
71             throw new IOException2("Failed to parse "+f,e);
72         } catch (SAXException JavaDoc e) {
73             throw new IOException2("Failed to parse "+f,e);
74         }
75
76         // merge duplicate entries. Ant task somehow seems to report duplicate entries.
77
for(int i=r.size()-1; i>=0; i--) {
78             CVSChangeLog log = r.get(i);
79             boolean merged = false;
80             for(int j=0;j<i;j++) {
81                 CVSChangeLog c = r.get(j);
82                 if(c.canBeMergedWith(log)) {
83                     c.merge(log);
84                     merged = true;
85                     break;
86                 }
87             }
88             if(merged)
89                 r.remove(log);
90         }
91
92         return new CVSChangeLogSet(build,r);
93     }
94
95     /**
96      * In-memory representation of CVS Changelog.
97      */

98     public static class CVSChangeLog extends ChangeLogSet.Entry {
99         private String JavaDoc date;
100         private String JavaDoc time;
101         private User author;
102         private String JavaDoc msg;
103         private final List JavaDoc<File> files = new ArrayList JavaDoc<File>();
104
105         /**
106          * Checks if two {@link CVSChangeLog} entries can be merged.
107          * This is to work around the duplicate entry problems.
108          */

109         public boolean canBeMergedWith(CVSChangeLog that) {
110             if(!this.date.equals(that.date))
111                 return false;
112             if(!this.time.equals(that.time)) // TODO: perhaps check this loosely?
113
return false;
114             if(this.author==null || that.author==null || !this.author.equals(that.author))
115                 return false;
116             if(!this.msg.equals(that.msg))
117                 return false;
118             return true;
119         }
120
121         public void merge(CVSChangeLog that) {
122             this.files.addAll(that.files);
123         }
124
125         public String JavaDoc getDate() {
126             return date;
127         }
128
129         public void setDate(String JavaDoc date) {
130             this.date = date;
131         }
132
133         public String JavaDoc getTime() {
134             return time;
135         }
136
137         public void setTime(String JavaDoc time) {
138             this.time = time;
139         }
140
141         public User getAuthor() {
142             return author;
143         }
144
145         public Collection JavaDoc<String JavaDoc> getAffectedPaths() {
146             return new AbstractList JavaDoc<String JavaDoc>() {
147                 public String JavaDoc get(int index) {
148                     return files.get(index).getName();
149                 }
150
151                 public int size() {
152                     return files.size();
153                 }
154             };
155         }
156
157         public void setUser(String JavaDoc author) {
158             this.author = User.get(author);
159         }
160
161         public String JavaDoc getUser() {// digester wants read/write property, even though it never reads. Duh.
162
return author.getDisplayName();
163         }
164
165         public String JavaDoc getMsg() {
166             return msg;
167         }
168
169         public void setMsg(String JavaDoc msg) {
170             this.msg = msg;
171         }
172
173         public void addFile( File f ) {
174             files.add(f);
175         }
176
177         public List JavaDoc<File> getFiles() {
178             return files;
179         }
180     }
181
182     public static class File {
183         private String JavaDoc name;
184         private String JavaDoc revision;
185         private String JavaDoc prevrevision;
186         private boolean dead;
187
188         public String JavaDoc getName() {
189             return name;
190         }
191
192         public void setName(String JavaDoc name) {
193             this.name = name;
194         }
195
196         public String JavaDoc getRevision() {
197             return revision;
198         }
199
200         public void setRevision(String JavaDoc revision) {
201             this.revision = revision;
202         }
203
204         public String JavaDoc getPrevrevision() {
205             return prevrevision;
206         }
207
208         public void setPrevrevision(String JavaDoc prevrevision) {
209             this.prevrevision = prevrevision;
210         }
211
212         public boolean isDead() {
213             return dead;
214         }
215
216         public void setDead() {
217             this.dead = true;
218         }
219
220         public EditType getEditType() {
221             // see issue #73. Can't do much better right now
222
if(dead)
223                 return EditType.DELETE;
224             if(revision.equals("1.1"))
225                 return EditType.ADD;
226             return EditType.EDIT;
227         }
228     }
229
230 }
231
Popular Tags