1 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 ; 46 import java.text.ParseException ; 47 import java.text.SimpleDateFormat ; 48 import java.util.ArrayList ; 49 import java.util.Date ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 53 64 public class Modification implements Comparable { 65 66 private static final Logger LOG = Logger.getLogger(Modification.class); 67 68 private static final String TAGNAME_MODIFICATION = "modification"; 69 private static final String TAGNAME_TYPE = "type"; 70 private static final String TAGNAME_FILE = "file"; 71 private static final String TAGNAME_FILENAME = "filename"; 72 private static final String TAGNAME_FOLDERNAME = "project"; 73 private static final String TAGNAME_DATE = "date"; 74 private static final String TAGNAME_USER = "user"; 75 private static final String TAGNAME_COMMENT = "comment"; 76 private static final String TAGNAME_EMAIL = "email"; 77 private static final String TAGNAME_REVISION = "revision"; 78 private static final String TAGNAME_ACTION = "action"; 79 80 public class ModifiedFile { 81 82 public String fileName; 83 public String revision; 84 public String folderName; 85 public String action = "unknown"; 86 87 protected ModifiedFile() { 88 } 89 90 public Element toElement(DateFormat 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 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 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 type = "unknown"; 165 public Date modifiedTime; 166 public String userName; 167 public String emailAddress; 168 public String revision; 169 public String comment = ""; 170 171 public List files = new ArrayList (); 172 173 public Modification() { 174 this("unknown"); 175 } 176 177 public Modification(String type) { 178 this.type = type; 179 } 180 181 182 public final ModifiedFile createModifiedFile(String filename, String 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 formatter) { 195 Element modificationElement = new Element(TAGNAME_MODIFICATION); 196 modificationElement.setAttribute(TAGNAME_TYPE, type); 197 198 Iterator 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 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 toXml(DateFormat formatter) { 240 return new XMLOutputter().outputString(toElement(formatter)); 241 } 242 243 public String toString() { 244 SimpleDateFormat formatter = 245 new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); 246 StringBuffer sb = new StringBuffer (); 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 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 270 public String getFileName() { 271 if (files.isEmpty()) { 272 return null; 273 } else { 274 return ((ModifiedFile) files.get(0)).fileName; 275 } 276 } 277 278 281 public String 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 o) { 291 Modification modification = (Modification) o; 292 return modifiedTime.compareTo(modification.modifiedTime); 293 } 294 295 public boolean equals(Object 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 formatter) { 360 361 type = modification.getAttributeValue(TAGNAME_TYPE); 362 try { 363 String 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 e) { 371 modifiedTime = new Date (); 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 modfiles = modification.getChildren(TAGNAME_FILE); 382 if (modfiles != null && modfiles.size() > 0) { 383 384 Iterator 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 403 public String getFullPath() { 404 StringBuffer result = new StringBuffer (); 405 String 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
|