1 37 package net.sourceforge.cruisecontrol.util; 38 39 import java.text.DateFormat ; 40 import java.text.SimpleDateFormat ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 import java.util.Set ; 44 45 import net.sourceforge.cruisecontrol.CruiseControlException; 46 import net.sourceforge.cruisecontrol.DateFormatFactory; 47 import net.sourceforge.cruisecontrol.Modification; 48 49 import org.jdom.Element; 50 51 85 public class XMLLogHelper { 86 87 private Element log; 88 private DateFormat dateFormat; 89 90 public XMLLogHelper(Element log) { 91 this(log, DateFormatFactory.getDateFormat()); 92 } 93 94 public XMLLogHelper(Element log, String dateFormatString) { 96 this(log, new SimpleDateFormat (dateFormatString)); 97 } 98 99 public XMLLogHelper(Element log, DateFormat dateFormat) { 100 this.log = log; 101 this.dateFormat = dateFormat; 102 } 103 104 107 public String getLogFileName() throws CruiseControlException { 108 return getCruiseControlInfoProperty("logfile"); 109 } 110 111 114 public String getLabel() throws CruiseControlException { 115 return getCruiseControlInfoProperty("label"); 116 } 117 118 public String getBuildTimestamp() throws CruiseControlException { 119 return getCruiseControlInfoProperty("cctimestamp"); 120 } 121 122 125 public boolean wasPreviousBuildSuccessful() throws CruiseControlException { 126 return getCruiseControlInfoProperty("lastbuildsuccessful").equals("true"); 127 } 128 129 132 public boolean isBuildNecessary() { 133 if (log.getChild("build") != null && log.getChild("build").getAttributeValue("error") != null) { 134 135 return !log.getChild("build").getAttributeValue("error").equals("No Build Necessary"); } 136 return true; 137 } 138 139 142 public String getProjectName() throws CruiseControlException { 143 return getCruiseControlInfoProperty("projectname"); 144 } 145 146 149 public boolean isBuildSuccessful() { 150 return (log.getChild("build").getAttribute("error") == null); 151 } 152 153 157 public Set getBuildParticipants() { 158 Set results = new HashSet (); 159 if (isP4Modifications()) { 160 161 Iterator changelistIterator = log.getChild("modifications").getChildren("changelist").iterator(); 162 while (changelistIterator.hasNext()) { 163 Element changelistElement = (Element) changelistIterator.next(); 164 String val = changelistElement.getAttributeValue("email"); 165 if ((val == null) || (val.length() == 0)) { 166 val = changelistElement.getAttributeValue("user"); 167 } 168 results.add(val); 169 } 170 } else { 171 Iterator modificationIterator = log.getChild("modifications").getChildren("modification") 172 .iterator(); 173 while (modificationIterator.hasNext()) { 174 Element modification = (Element) modificationIterator.next(); 175 Element emailElement = modification.getChild("email"); 176 if (emailElement == null) { 177 emailElement = modification.getChild("user"); 178 } 179 results.add(emailElement.getText()); 180 } 181 } 182 return results; 183 } 184 185 private boolean isP4Modifications() { 186 return log.getChild("modifications").getChildren("changelist") != null 187 && !log.getChild("modifications").getChildren("changelist").isEmpty(); 188 } 189 190 194 public String getAntProperty(String propertyName) throws CruiseControlException { 195 Iterator propertyIterator = log.getChild("build").getChild("properties").getChildren("property") 196 .iterator(); 197 while (propertyIterator.hasNext()) { 198 Element property = (Element) propertyIterator.next(); 199 if (property.getAttributeValue("name").equals(propertyName)) { return property 200 .getAttributeValue("value"); } 201 } 202 throw new CruiseControlException("Property: " + propertyName + " not found."); 203 } 204 205 public String getCruiseControlInfoProperty(String propertyName) throws CruiseControlException { 206 Iterator propertyIterator = log.getChild("info").getChildren("property").iterator(); 207 while (propertyIterator.hasNext()) { 208 Element property = (Element) propertyIterator.next(); 209 if (property.getAttributeValue("name").equals(propertyName)) { return property 210 .getAttributeValue("value"); } 211 } 212 throw new CruiseControlException("Property: " + propertyName + " not found."); 213 } 214 215 public Set getModifications() { 216 Set results = new HashSet (); 217 if (isP4Modifications()) { 218 } else { 220 Iterator modificationIterator = log.getChild("modifications").getChildren("modification") 221 .iterator(); 222 while (modificationIterator.hasNext()) { 223 Element modification = (Element) modificationIterator.next(); 224 Modification mod = new Modification(); 225 mod.fromElement(modification, dateFormat); 226 results.add(mod); 227 } 228 } 229 return results; 230 } 231 232 public boolean isBuildFix() throws CruiseControlException { 233 return !this.wasPreviousBuildSuccessful() && this.isBuildSuccessful(); 234 } 235 236 } 237 | Popular Tags |