1 23 package org.hammurapi; 24 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.taskdefs.Property; 34 35 import com.pavelvlasov.ant.ObjectEntry; 36 import com.pavelvlasov.ant.Param; 37 import com.pavelvlasov.config.ConfigurationException; 38 import com.pavelvlasov.config.Parameterizable; 39 40 46 public class InspectorEntry extends ObjectEntry implements InspectorDescriptor { 47 private String fixSample; 48 private String message; 49 private String name; 50 private Integer order; 51 private String rationale; 52 private String resources; 53 private Integer severity; 54 private String violationSample; 55 private Boolean isEnabled; 56 private Boolean isWaivable; 57 private String description; 58 private String category; 59 private List waiveCases=new LinkedList (); 60 61 public void addConfiguredWaiveCase(WaiveCaseEntry entry) { 62 waiveCases.add(entry.getText()); 63 } 64 65 public Boolean isEnabled() { 66 return isEnabled; 67 } 68 69 public Boolean isWaivable() { 70 return isWaivable; 71 } 72 73 public String getName() { 74 return name; 75 } 76 77 public Integer getSeverity() { 78 return severity; 79 } 80 81 public Integer getOrder() { 82 return order; 83 } 84 85 public String getRationale() { 86 return rationale; 87 } 88 89 public String getViolationSample() { 90 return violationSample; 91 } 92 93 public String getFixSample() { 94 return fixSample; 95 } 96 97 public String getResources() { 98 return resources; 99 } 100 101 public String getMessage() { 102 return message; 103 } 104 105 private List filterEntries=new LinkedList (); 106 107 113 public FilterEntry createFilterInclude() { 114 FilterEntry fe=new FilterEntry(); 115 filterEntries.add(fe); 116 return fe; 117 } 118 119 125 public FilterEntry createFilterExclude() { 126 FilterEntry fe=new FilterEntry(); 127 filterEntries.add(fe); 128 fe.exclude=true; 129 return fe; 130 } 131 132 public Inspector getInspector() throws ConfigurationException { 133 if (getClassName()==null) { 134 return null; 135 } else { 136 Inspector ret = (Inspector) getObject(null); 137 if (!super.getParameters().isEmpty()) { 138 if (ret instanceof Parameterizable) { 139 Iterator it=getParameters().iterator(); 140 while (it.hasNext()) { 141 ParameterEntry pe=(ParameterEntry) it.next(); 142 if (!((Parameterizable) ret).setParameter(pe.getName(), pe.getValue())) { 143 throw new ConfigurationException(ret.getClass().getName()+" does not support parameter "+pe.getName()); 144 } 145 } 146 } else { 147 throw new ConfigurationException(ret.getClass().getName()+" does not implement "+Parameterizable.class.getName()); 148 } 149 } 150 return ret; 151 } 152 } 153 154 protected void validateClass(Class clazz) throws BuildException { 155 super.validateClass(clazz); 156 if (!Inspector.class.isAssignableFrom(clazz)) { 157 throw new BuildException(clazz.getName()+" doesn't implement "+Inspector.class); 158 } 159 } 160 161 165 public void setFixSample(String fixSample) { 166 this.fixSample = fixSample; 167 } 168 169 173 public void setMessage(String message) { 174 this.message = message; 175 } 176 177 181 public void setName(String name) { 182 this.name = name; 183 } 184 185 189 public void setOrder(int order) { 190 this.order = new Integer (order); 191 } 192 193 197 public void setRationale(String rationale) { 198 this.rationale = rationale; 199 } 200 201 205 public void setResources(String resources) { 206 this.resources = resources; 207 } 208 209 213 public void setSeverity(int severity) { 214 this.severity = new Integer (severity); 215 } 216 217 221 public void setViolationSample(String violationSample) { 222 this.violationSample = violationSample; 223 } 224 225 229 public void setEnabled(boolean isEnabled) { 230 this.isEnabled = isEnabled ? Boolean.TRUE : Boolean.FALSE; 231 } 232 233 237 public void setWaivable(boolean isWaivable) { 238 this.isWaivable = isWaivable ? Boolean.TRUE : Boolean.FALSE; 239 } 240 241 244 public String getDescription() { 245 return description; 246 } 247 248 251 public String getCategory() { 252 return category; 253 } 254 255 259 public void setDescription(String description) { 260 this.description = description; 261 } 262 263 267 public void setCategory(String category) { 268 this.category=category; 269 } 270 271 275 public Collection getParameters() { 276 List ret=new LinkedList (); 277 Iterator it=super.getParameters().iterator(); 278 while (it.hasNext()) { 279 Param p=(Param) it.next(); 280 ret.add(new ParameterEntry(p.getName(), p.getObject(null))); 281 } 282 return ret; 283 } 284 285 private Map messages=new HashMap (); 286 287 292 public void addConfiguredMessage(Property message) { 293 messages.put(message.getName(), message); 294 } 295 296 public String getMessage(String key) { 297 Property property=(Property) messages.get(key); 298 return property==null ? null : property.getValue(); 299 } 300 301 public Collection getWaiveCases() { 302 return waiveCases; 303 } 304 305 public String getWaivedInspectorName(String inspectorKey) { 306 return null; 308 } 309 310 public String getWaiveReason(String inspectorKey) { 311 return null; 313 } 314 315 public Collection getWaivedInspectorNames() { 316 return null; 318 } 319 320 public Collection getFilteredInspectorDesriptors(InspectorSet inspectorSet, Collection chain) { 321 if (chain==null) { 322 chain=new LinkedList (); 323 } 324 325 Iterator it=filterEntries.iterator(); 326 while (it.hasNext()) { 327 FilterEntry fe=(FilterEntry) it.next(); 328 if (fe.exclude) { 329 if (fe.name==null) { 330 Iterator dit=chain.iterator(); 331 while (dit.hasNext()) { 332 if (fe.category.equals(((InspectorDescriptor) dit.next()).getCategory())) { 333 dit.remove(); 334 } 335 } 336 } else { 337 if ("*".equals(fe.name)) { 338 chain.clear(); 339 } else { 340 Iterator dit=chain.iterator(); 341 while (dit.hasNext()) { 342 if (fe.name.equals(((InspectorDescriptor) dit.next()).getName())) { 343 dit.remove(); 344 } 345 } 346 } 347 } 348 } else { 349 if (fe.name==null) { 350 Iterator dit=inspectorSet.getDescriptors().iterator(); 351 while (dit.hasNext()) { 352 InspectorDescriptor inspectorDescriptor = (InspectorDescriptor) dit.next(); 353 if (fe.category.equals(inspectorDescriptor.getCategory())) { 354 chain.add(inspectorDescriptor); 355 } 356 } 357 } else { 358 if ("*".equals(fe.name)) { 359 chain.addAll(inspectorSet.getDescriptors()); 360 } else { 361 Iterator dit=inspectorSet.getDescriptors().iterator(); 362 while (dit.hasNext()) { 363 InspectorDescriptor inspectorDescriptor = (InspectorDescriptor) dit.next(); 364 if (fe.name.equals(inspectorDescriptor.getName())) { 365 chain.add(inspectorDescriptor); 366 } 367 } 368 } 369 } 370 } 371 } 372 373 return chain; 374 } 375 376 public Collection getAfterInspectorNames() { 377 return null; 378 } 379 } 380 | Popular Tags |