KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > Modification


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37
38 package net.sourceforge.cruisecontrol;
39
40 import org.apache.log4j.Logger;
41 import org.jdom.CDATA;
42 import org.jdom.Element;
43 import org.jdom.output.XMLOutputter;
44
45 import java.text.DateFormat JavaDoc;
46 import java.text.ParseException JavaDoc;
47 import java.text.SimpleDateFormat JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Date JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 /**
54  * data structure for holding data about a single modification
55  * to a source control tool.
56  *
57  * <modification type="" date="" user="" email="">
58  * <comment></comment>
59  * <file >
60  * </modification>
61  *
62  * @author <a HREF="mailto:alden@thoughtworks.com">alden almagro</a>
63  */

64 public class Modification implements Comparable JavaDoc {
65
66     private static final Logger LOG = Logger.getLogger(Modification.class);
67
68     private static final String JavaDoc TAGNAME_MODIFICATION = "modification";
69     private static final String JavaDoc TAGNAME_TYPE = "type";
70     private static final String JavaDoc TAGNAME_FILE = "file";
71     private static final String JavaDoc TAGNAME_FILENAME = "filename";
72     private static final String JavaDoc TAGNAME_FOLDERNAME = "project";
73     private static final String JavaDoc TAGNAME_DATE = "date";
74     private static final String JavaDoc TAGNAME_USER = "user";
75     private static final String JavaDoc TAGNAME_COMMENT = "comment";
76     private static final String JavaDoc TAGNAME_EMAIL = "email";
77     private static final String JavaDoc TAGNAME_REVISION = "revision";
78     private static final String JavaDoc TAGNAME_ACTION = "action";
79
80     public class ModifiedFile {
81
82         public String JavaDoc fileName;
83         public String JavaDoc revision;
84         public String JavaDoc folderName;
85         public String JavaDoc action = "unknown";
86
87         protected ModifiedFile() {
88         }
89
90         public Element toElement(DateFormat JavaDoc formatter) {
91
92             Element element = new Element(TAGNAME_FILE);
93
94             if (revision != null && revision.trim().length() > 0) {
95                 Element revisionElement = new Element(TAGNAME_REVISION);
96                 revisionElement.addContent(revision);
97                 element.addContent(revisionElement);
98             }
99
100             if (action != null && action.trim().length() > 0) {
101                 element.setAttribute(TAGNAME_ACTION, action);
102             }
103
104             Element fileElement = new Element(TAGNAME_FILENAME);
105             fileElement.addContent(fileName);
106             element.addContent(fileElement);
107
108             if (folderName != null && folderName.trim().length() > 0) {
109                 Element folderElement = new Element(TAGNAME_FOLDERNAME);
110                 folderElement.addContent(folderName);
111                 element.addContent(folderElement);
112             }
113
114             return element;
115
116         }
117
118         public void fromElement(Element modification, DateFormat JavaDoc formatter) {
119             fileName = modification.getChildText(TAGNAME_FILENAME);
120             folderName = modification.getChildText(TAGNAME_FOLDERNAME);
121             revision = modification.getChildText(TAGNAME_REVISION);
122             action = modification.getAttributeValue(TAGNAME_ACTION);
123         }
124
125         public boolean equals(Object JavaDoc o) {
126             if (!(o instanceof ModifiedFile)) {
127                 return false;
128             }
129
130             ModifiedFile mod = (ModifiedFile) o;
131
132             boolean folderNamesAreEqual = (folderName != null)
133                 ? folderName.equals(mod.folderName)
134                 : (mod.folderName == null);
135
136             boolean revisionsAreEqual = (revision != null)
137                 ? revision.equals(mod.revision)
138                 : (mod.revision == null);
139
140             return (action.equals(mod.action)
141                     && fileName.equals(mod.fileName)
142                     && folderNamesAreEqual
143                     && revisionsAreEqual);
144         }
145
146         public int hashCode() {
147             int code = 1;
148             if (fileName != null) {
149                 code += fileName.hashCode() * 2;
150             }
151             if (revision != null) {
152                 code += revision.hashCode() * 3;
153             }
154             if (folderName != null) {
155                 code += folderName.hashCode() * 5;
156             }
157             if (action != null) {
158                 code += action.hashCode() * 7;
159             }
160             return code;
161         }
162     }
163
164     public String JavaDoc type = "unknown";
165     public Date JavaDoc modifiedTime;
166     public String JavaDoc userName;
167     public String JavaDoc emailAddress;
168     public String JavaDoc revision;
169     public String JavaDoc comment = "";
170
171     public List JavaDoc files = new ArrayList JavaDoc();
172
173     public Modification() {
174         this("unknown");
175     }
176
177     public Modification(String JavaDoc type) {
178         this.type = type;
179     }
180
181
182     public final ModifiedFile createModifiedFile(String JavaDoc filename, String JavaDoc folder) {
183         ModifiedFile file = newModifiedFile();
184         file.fileName = filename;
185         file.folderName = folder;
186         files.add(file);
187         return file;
188     }
189
190     protected ModifiedFile newModifiedFile() {
191         return new ModifiedFile();
192     }
193
194     public Element toElement(DateFormat JavaDoc formatter) {
195         Element modificationElement = new Element(TAGNAME_MODIFICATION);
196         modificationElement.setAttribute(TAGNAME_TYPE, type);
197
198         Iterator JavaDoc i = files.iterator();
199         while (i.hasNext()) {
200             ModifiedFile f = (ModifiedFile) i.next();
201             modificationElement.addContent(f.toElement(formatter));
202         }
203
204         Element dateElement = new Element(TAGNAME_DATE);
205         dateElement.addContent(formatter.format(modifiedTime));
206         Element userElement = new Element(TAGNAME_USER);
207         userElement.addContent(userName);
208         Element commentElement = new Element(TAGNAME_COMMENT);
209
210         CDATA cd;
211         try {
212             cd = new CDATA(comment);
213         } catch (org.jdom.IllegalDataException e) {
214             LOG.error(e);
215             cd = new CDATA("Unable to parse comment. It contains illegal data.");
216         }
217         commentElement.addContent(cd);
218
219         modificationElement.addContent(dateElement);
220         modificationElement.addContent(userElement);
221         modificationElement.addContent(commentElement);
222
223         if (revision != null && revision.trim().length() > 0) {
224             Element revisionElement = new Element(TAGNAME_REVISION);
225             revisionElement.addContent(revision);
226             modificationElement.addContent(revisionElement);
227         }
228
229         // not all sourcecontrols guarantee a non-null email address
230
if (emailAddress != null) {
231             Element emailAddressElement = new Element(TAGNAME_EMAIL);
232             emailAddressElement.addContent(emailAddress);
233             modificationElement.addContent(emailAddressElement);
234         }
235
236         return modificationElement;
237     }
238
239     public String JavaDoc toXml(DateFormat JavaDoc formatter) {
240         return new XMLOutputter().outputString(toElement(formatter));
241     }
242
243     public String JavaDoc toString() {
244         SimpleDateFormat JavaDoc formatter =
245             new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss");
246         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
247         sb.append("Type: ").append(type).append('\n');
248         sb.append("Last Modified: ").append(formatter.format(modifiedTime)).append('\n');
249         sb.append("Revision: ").append(revision).append('\n');
250         sb.append("UserName: ").append(userName).append('\n');
251         sb.append("EmailAddress: ").append(emailAddress).append('\n');
252         sb.append("Comment: ").append(comment).append('\n');
253         return sb.toString();
254     }
255
256     public void log(DateFormat JavaDoc formatter) {
257         if (LOG.isDebugEnabled()) {
258             LOG.debug("Type: " + type);
259             LOG.debug("Last Modified: " + formatter.format(modifiedTime));
260             LOG.debug("UserName: " + userName);
261             LOG.debug("EmailAddress: " + emailAddress);
262             LOG.debug("Comment: " + comment);
263             LOG.debug("");
264         }
265     }
266
267     /**
268      * Convenience method for getting the filename of the first file
269      */

270     public String JavaDoc getFileName() {
271         if (files.isEmpty()) {
272             return null;
273         } else {
274             return ((ModifiedFile) files.get(0)).fileName;
275         }
276     }
277
278     /**
279      * Convenience method for getting the foldername of the first file
280      */

281     public String JavaDoc getFolderName() {
282         if (files.isEmpty()) {
283             return null;
284         } else {
285             return ((ModifiedFile) files.get(0)).folderName;
286         }
287     }
288
289
290     public int compareTo(Object JavaDoc o) {
291         Modification modification = (Modification) o;
292         return modifiedTime.compareTo(modification.modifiedTime);
293     }
294
295     public boolean equals(Object JavaDoc o) {
296         if (!(o instanceof Modification)) {
297             return false;
298         }
299
300         Modification mod = (Modification) o;
301
302         boolean emailsAreEqual = (emailAddress != null)
303             ? emailAddress.equals(mod.emailAddress)
304             : (mod.emailAddress == null);
305
306         boolean revisionsAreEqual = (revision != null)
307             ? revision.equals(mod.revision)
308             : (mod.revision == null);
309
310         boolean filesAreEqual = files.size() == mod.files.size();
311         for (int i = 0; filesAreEqual && i < files.size(); i++) {
312             filesAreEqual = mod.files.get(i).equals(files.get(i));
313         }
314
315         return (
316             type.equals(mod.type)
317                 && modifiedTime.equals(mod.modifiedTime)
318                 && userName.equals(mod.userName)
319                 && revisionsAreEqual
320                 && emailsAreEqual
321                 && comment.equals(mod.comment));
322     }
323
324     public int hashCode() {
325         int code = 1;
326         if (type != null) {
327             code += type.hashCode() * 2;
328         }
329         if (modifiedTime != null) {
330             code += modifiedTime.getTime();
331         }
332         if (userName != null) {
333             code += userName.hashCode() * 5;
334         }
335         if (emailAddress != null) {
336             code += emailAddress.hashCode() * 7;
337         }
338         if (comment != null) {
339             code += comment.hashCode() * 11;
340         }
341         if (revision != null) {
342             code += revision.hashCode() * 13;
343         }
344         if (files != null) {
345             code += fileHashComponent();
346         }
347         return code;
348     }
349
350     private int fileHashComponent() {
351         int code = 1;
352         for (int i = 0; i < files.size(); i++) {
353             Modification.ModifiedFile file = (ModifiedFile) files.get(i);
354             code += file.hashCode();
355         }
356         return code;
357     }
358
359     public void fromElement(Element modification, DateFormat JavaDoc formatter) {
360
361         type = modification.getAttributeValue(TAGNAME_TYPE);
362         try {
363             String JavaDoc s = modification.getChildText(TAGNAME_DATE);
364             if (s == null) {
365                 XMLOutputter outputter = new XMLOutputter();
366                 LOG.info("XML: " + outputter.outputString(modification));
367             }
368
369             modifiedTime = formatter.parse(s);
370         } catch (ParseException JavaDoc e) {
371             //maybe we should do something different
372
modifiedTime = new Date JavaDoc();
373         }
374
375         revision = modification.getChildText(TAGNAME_REVISION);
376         userName = modification.getChildText(TAGNAME_USER);
377         comment = modification.getChildText(TAGNAME_COMMENT);
378         emailAddress = modification.getChildText(TAGNAME_EMAIL);
379
380         files.clear();
381         List JavaDoc modfiles = modification.getChildren(TAGNAME_FILE);
382         if (modfiles != null && modfiles.size() > 0) {
383
384             Iterator JavaDoc it = modfiles.iterator();
385             while (it.hasNext()) {
386                 Element modfileElement = (Element) it.next();
387                 ModifiedFile modfile = newModifiedFile();
388                 modfile.fromElement(modfileElement, formatter);
389                 files.add(modfile);
390             }
391         }
392     }
393
394     /**
395      * Concatenates the folderName and fileName of the Modification into a
396      * <code>String</code>. If the folderName is null then it is not included.
397      * All backward slashes ("\") are converted to forward slashes
398      * ("/").
399      *
400      * @return A <code>String</code> containing the full path
401      * of the modification
402      */

403     public String JavaDoc getFullPath() {
404         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
405         String JavaDoc folderName = getFolderName();
406
407         if (folderName != null) {
408             result.append(folderName).append("/");
409         }
410
411         result.append(getFileName());
412         return result.toString().replace('\\', '/');
413     }
414
415 }
416
Popular Tags