1 23 package org.hammurapi; 24 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.xml.transform.TransformerException ; 34 35 import org.apache.xpath.CachedXPathAPI; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.traversal.NodeIterator; 40 41 import com.pavelvlasov.config.ConfigurationException; 42 import com.pavelvlasov.config.DomConfigFactory; 43 import com.pavelvlasov.config.Parameterizable; 44 import com.pavelvlasov.xml.dom.AbstractDomObject; 45 46 50 public class DomInspectorDescriptor extends AbstractDomObject implements InspectorDescriptor { 51 private String fixSample; 52 private String message; 53 private String category; 54 private String name; 55 private Integer order; 56 private String rationale; 57 private String resources; 58 private Integer severity; 59 private String violationSample; 60 private Boolean isEnabled; 61 private Boolean isWaivable; 62 private String description; 63 private Element inspectorElement; 64 private Inspector inspector; 65 private List parameters=new LinkedList (); 66 private List waiveCases=new LinkedList (); 67 private Map messages=new HashMap (); 68 69 private class FilterNameEntry { 70 boolean exclude; 71 String name; 72 String category; 73 } 74 75 private Collection filterEntries=new LinkedList (); 76 77 public DomInspectorDescriptor(Element holder) throws HammurapiException { 78 super(); 79 try { 80 CachedXPathAPI cxpa=new CachedXPathAPI(); 81 fixSample=getElementText(holder, "fix-sample", cxpa); 82 83 NodeIterator it=cxpa.selectNodeIterator(holder, "message"); 84 Node messageNode; 85 while ((messageNode=it.nextNode())!=null) { 86 if (messageNode instanceof Element ) { 87 Element messageElement=(Element ) messageNode; 88 if (messageElement.hasAttribute("key")) { 89 messages.put(messageElement.getAttribute("key"), getElementText(messageElement)); 90 } else { 91 message=getElementText(messageElement); 92 } 93 } 94 } 95 name=getElementText(holder, "name", cxpa); 96 String orderVal=getElementText(holder, "order", cxpa); 97 if (orderVal!=null) { 98 order=new Integer (orderVal); 99 } 100 rationale=getElementText(holder, "rationale", cxpa); 101 category=getElementText(holder, "category", cxpa); 102 resources=getElementText(holder, "resource", cxpa); 103 String severityVal=getElementText(holder, "severity", cxpa); 104 if (severityVal!=null) { 105 severity=new Integer (severityVal); 106 } 107 violationSample=getElementText(holder, "violation-sample", cxpa); 108 109 String enabledVal=getElementText(holder, "enabled", cxpa); 110 if (enabledVal!=null) { 111 isEnabled="yes".equalsIgnoreCase(enabledVal) || "true".equalsIgnoreCase(enabledVal) ? Boolean.TRUE : Boolean.FALSE; 112 } 113 114 String waivableVal=getElementText(holder, "waivable", cxpa); 115 if (waivableVal!=null) { 116 isWaivable="yes".equalsIgnoreCase(waivableVal) || "true".equalsIgnoreCase(waivableVal) ? Boolean.TRUE : Boolean.FALSE; 117 } 118 119 description=getElementText(holder, "description", cxpa); 120 121 inspectorElement=(Element ) cxpa.selectSingleNode(holder, "inspector"); 122 123 DomConfigFactory factory=new DomConfigFactory(); 124 NodeIterator nit=cxpa.selectNodeIterator(holder, "parameter"); 125 Node n; 126 while ((n=nit.nextNode())!=null) { 127 parameters.add(new ParameterEntry(((Element ) n).getAttribute("name"),factory.create(n))); 128 } 129 130 nit=cxpa.selectNodeIterator(holder, "waive-case"); 131 while ((n=nit.nextNode())!=null) { 132 waiveCases.add(cxpa.eval(n, "text()").toString()); 133 } 134 135 nit=cxpa.selectNodeIterator(holder, "waives"); 136 while ((n=nit.nextNode())!=null) { 137 if (n instanceof Element ) { 138 Element waivesElement=(Element ) n; 139 if (waivesElement.hasAttribute("key")) { 140 String wKey=waivesElement.getAttribute("key"); 141 String iName=cxpa.eval(n, "name/text()").toString(); 142 if (iName.trim().length()==0) { 143 throw new HammurapiException("<waives> must have nested <name> element"); 144 } 145 146 waivedInspectorNames.put(wKey, iName); 147 waiveReasons.put(wKey, cxpa.eval(n, "reason/text()").toString()); 148 } else { 149 throw new HammurapiException("<waives> must have 'key' attribute"); 150 } 151 } 152 } 153 154 nit=cxpa.selectNodeIterator(holder, "after"); 155 while ((n=nit.nextNode())!=null) { 156 afterInspectorNames.add(getElementText(n)); 157 } 158 159 NodeList nl=holder.getChildNodes(); 160 for (int i=0; i<nl.getLength(); i++) { 161 Node fn=nl.item(i); 162 if (fn instanceof Element ) { 163 Element el=(Element ) fn; 164 if ("filter".equals(el.getNodeName())) { 165 FilterNameEntry fne=new FilterNameEntry(); 166 if (el.hasAttribute("name")) { 167 fne.name=el.getAttribute("name"); 168 } else if (el.hasAttribute("category")) { 169 fne.category=el.getAttribute("category"); 170 } else { 171 throw new ConfigurationException("<filter> element shall have either name or category attribute"); 172 } 173 filterEntries.add(fne); 174 } else if ("filter-exclude".equals(el.getNodeName())) { 175 FilterNameEntry fne=new FilterNameEntry(); 176 fne.exclude=true; 177 if (el.hasAttribute("name")) { 178 fne.name=el.getAttribute("name"); 179 } else if (el.hasAttribute("category")) { 180 fne.category=el.getAttribute("category"); 181 } else { 182 throw new ConfigurationException("<filter-exclude> element shall have either name or category attribute"); 183 } 184 filterEntries.add(fne); 185 } 186 } 187 } 188 } catch (TransformerException e) { 190 throw new HammurapiException(e); 191 } catch (ConfigurationException e) { 192 throw new HammurapiException(e); 193 } 194 } 195 196 199 public String getFixSample() { 200 return fixSample; 201 } 202 203 206 public Boolean isEnabled() { 207 return isEnabled; 208 } 209 210 213 public String getMessage() { 214 return message; 215 } 216 217 220 public String getCategory() { 221 return category; 222 } 223 224 227 public String getName() { 228 return name; 229 } 230 231 234 public Integer getOrder() { 235 return order; 236 } 237 238 241 public String getRationale() { 242 return rationale; 243 } 244 245 248 public String getResources() { 249 return resources; 250 } 251 252 255 public Integer getSeverity() { 256 return severity; 257 } 258 259 262 public String getViolationSample() { 263 return violationSample; 264 } 265 266 public String getDescription() { 267 return description; 268 } 269 270 public Inspector getInspector() throws ConfigurationException { 271 if (inspector==null && inspectorElement!=null) { 272 DomConfigFactory factory=new DomConfigFactory(); 273 Object o = factory.create(inspectorElement); 274 if (o instanceof Inspector) { 275 inspector=(Inspector) o; 276 if (!getParameters().isEmpty()) { 277 if (inspector instanceof Parameterizable) { 278 Iterator it=getParameters().iterator(); 279 while (it.hasNext()) { 280 ParameterEntry pe=(ParameterEntry) it.next(); 281 if (!((Parameterizable) inspector).setParameter(pe.getName(), pe.getValue())) { 282 throw new ConfigurationException(o.getClass().getName()+" does not support parameter "+pe.getName()); 283 } 284 } 285 } else { 286 throw new ConfigurationException(inspector.getClass().getName()+" does not implement "+Parameterizable.class.getName()); 287 } 288 } 289 } else { 290 throw new ConfigurationException(o.getClass().getName()+" doesn't implement "+Inspector.class.getName()); 291 } 292 } 293 return inspector; 294 } 295 296 public Collection getParameters() { 297 return parameters; 298 } 299 300 public String getMessage(String key) { 301 return (String ) messages.get(key); 302 } 303 304 public Boolean isWaivable() { 305 return isWaivable; 306 } 307 308 public Collection getWaiveCases() { 309 return waiveCases; 310 } 311 312 private Map waivedInspectorNames=new HashMap (); 313 314 public String getWaivedInspectorName(String inspectorKey) { 315 return (String ) waivedInspectorNames.get(inspectorKey); 316 } 317 318 private Map waiveReasons=new HashMap (); 319 private Collection afterInspectorNames = new HashSet (); 320 321 public String getWaiveReason(String inspectorKey) { 322 return (String ) waiveReasons.get(inspectorKey); 323 } 324 325 public Collection getWaivedInspectorNames() { 326 return waivedInspectorNames.values(); 327 } 328 329 public Collection getAfterInspectorNames() { 330 return afterInspectorNames; 331 } 332 333 public Collection getFilteredInspectorDesriptors(InspectorSet inspectorSet, Collection chain) { 334 if (chain==null) { 335 chain=new LinkedList (); 336 } 337 338 Iterator it=filterEntries.iterator(); 339 while (it.hasNext()) { 340 FilterNameEntry fne=(FilterNameEntry) it.next(); 341 if (fne.exclude) { 342 if (fne.name==null) { 343 Iterator dit=chain.iterator(); 344 while (dit.hasNext()) { 345 if (fne.category.equals(((InspectorDescriptor) dit.next()).getCategory())) { 346 dit.remove(); 347 } 348 } 349 } else { 350 if ("*".equals(fne.name)) { 351 chain.clear(); 352 } else { 353 Iterator dit=chain.iterator(); 354 while (dit.hasNext()) { 355 if (fne.name.equals(((InspectorDescriptor) dit.next()).getName())) { 356 dit.remove(); 357 } 358 } 359 } 360 } 361 } else { 362 if (fne.name==null) { 363 Iterator dit=inspectorSet.getDescriptors().iterator(); 364 while (dit.hasNext()) { 365 InspectorDescriptor inspectorDescriptor = (InspectorDescriptor) dit.next(); 366 if (fne.category.equals(inspectorDescriptor.getCategory())) { 367 chain.add(inspectorDescriptor); 368 } 369 } 370 } else { 371 if ("*".equals(fne.name)) { 372 chain.addAll(inspectorSet.getDescriptors()); 373 } else { 374 Iterator dit=inspectorSet.getDescriptors().iterator(); 375 while (dit.hasNext()) { 376 InspectorDescriptor inspectorDescriptor = (InspectorDescriptor) dit.next(); 377 if (fne.name.equals(inspectorDescriptor.getName())) { 378 chain.add(inspectorDescriptor); 379 } 380 } 381 } 382 } 383 } 384 } 385 386 return chain; 387 } 388 } 389 | Popular Tags |