1 23 package org.hammurapi.inspectors.techstack; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Properties ; 32 import java.util.TreeSet ; 33 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.FactoryConfigurationError ; 36 import javax.xml.parsers.ParserConfigurationException ; 37 import javax.xml.transform.TransformerException ; 38 39 import org.apache.xpath.CachedXPathAPI; 40 import org.hammurapi.ParameterizableInspectorBase; 41 import org.hammurapi.HammurapiException; 42 import org.hammurapi.results.AnnotationContext; 43 import org.hammurapi.results.LinkedAnnotation; 44 import org.hammurapi.results.AnnotationContext.FileEntry; 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.Element ; 47 import org.w3c.dom.traversal.NodeIterator; 48 49 import com.pavelvlasov.config.ConfigurationException; 50 import com.pavelvlasov.config.XmlSource; 51 import com.pavelvlasov.jsel.CompilationUnit; 52 import com.pavelvlasov.jsel.JselException; 53 import com.pavelvlasov.jsel.Repository; 54 import com.pavelvlasov.xml.dom.AbstractDomObject; 55 import com.pavelvlasov.xml.dom.DOMUtils; 56 57 62 public class TechStackInspector extends ParameterizableInspectorBase { 63 64 private XmlSource config; 65 private XmlSource style; 66 private XmlSource licenseSummaryStyle; 67 private XmlSource licenseClientsStyle; 68 private XmlSource productClientsStyle; 69 private XmlSource publisherClientsStyle; 70 71 public TechStackInspector() { 72 config=new XmlSource("config", getClass(), ".xml"); 73 addConfigurator(config); 74 75 style=new XmlSource("style", getClass(), ".xsl"); 76 addConfigurator(style); 77 78 licenseSummaryStyle=new XmlSource("license-summary-style", getClass(), "!licenses.xsl"); 79 addConfigurator(licenseSummaryStyle); 80 81 licenseClientsStyle=new XmlSource("license-clients-style", getClass(), "!license-clients.xsl"); 82 addConfigurator(licenseClientsStyle); 83 84 productClientsStyle=new XmlSource("product-clients-style", getClass(), "!product-clients.xsl"); 85 addConfigurator(productClientsStyle); 86 87 publisherClientsStyle=new XmlSource("publisher-clients-style", getClass(), "!publisher-clients.xsl"); 88 addConfigurator(publisherClientsStyle); 89 } 90 91 public void leave(final Repository repo) { 92 try { 93 Document configDoc=config.getConfigDocument(); 94 final TechStack techStack = configDoc==null ? new TechStack() : new TechStack(configDoc.getDocumentElement()); 95 Iterator it=repo.getExternalSuppliers().entrySet().iterator(); 96 while (it.hasNext()) { 97 Map.Entry entry=(Map.Entry ) it.next(); 98 String packageName=(String ) entry.getKey(); 99 Collection clients=new TreeSet (); 100 Iterator cit=repo.getExternalSupplierClients(packageName).iterator(); 101 while (cit.hasNext()) { 102 CompilationUnit cu=(CompilationUnit) cit.next(); 103 String cpn = cu.getPackage().getName(); 104 if (cpn.length()==0) { 105 clients.add(cu.getName()); 106 } else { 107 clients.add(cpn.replace('.','/')+"/"+cu.getName()); 108 } 109 } 110 techStack.addPackage(packageName, clients); 111 } 112 113 final Document techStackDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 114 techStack.toDom(AbstractDomObject.addElement(techStackDoc, "tech-stack")); 115 116 context.annotate(new LinkedAnnotation() { 117 String path; 118 119 public String getPath() { 120 return path; 121 } 122 123 public String getName() { 124 return "Technology stack"; 125 } 126 127 public void render(AnnotationContext context) throws HammurapiException { 128 if (".HTML".equalsIgnoreCase(context.getExtension())) { 129 FileEntry root = context.getNextFile(context.getExtension()); 130 path=root.getPath(); 131 Element tsRoot = techStackDoc.getDocumentElement(); 132 tsRoot.setAttribute("root-path", root.getPath()); 133 134 FileEntry licenseSummary = context.getNextFile(context.getExtension()); 135 tsRoot.setAttribute("license-summary", licenseSummary.getPath()); 136 137 CachedXPathAPI cxpa=new CachedXPathAPI(); 138 139 try { 140 Map params=new HashMap (); 141 NodeIterator nit=cxpa.selectNodeIterator(tsRoot, "publisher/product[client]"); 142 Element element; 143 while ((element=(Element ) nit.nextNode())!=null) { 144 FileEntry fe=context.getNextFile(context.getExtension()); 145 element.setAttribute("path", fe.getPath()); 146 params.put("product", element.getAttribute("key")); 147 DOMUtils.style(techStackDoc, fe.getFile(), productClientsStyle.getStream(), params); 148 } 149 150 nit=cxpa.selectNodeIterator(tsRoot, "publisher[client]"); 151 while ((element=(Element ) nit.nextNode())!=null) { 152 FileEntry fe=context.getNextFile(context.getExtension()); 153 element.setAttribute("path", fe.getPath()); 154 params.put("publisher", element.getAttribute("key")); 155 DOMUtils.style(techStackDoc, fe.getFile(), publisherClientsStyle.getStream(), params); 156 } 157 158 nit=cxpa.selectNodeIterator(tsRoot, "//license[client]"); 159 while ((element=(Element ) nit.nextNode())!=null) { 160 FileEntry fe=context.getNextFile(context.getExtension()); 161 element.setAttribute("path", fe.getPath()); 162 params.put("license", element.getAttribute("key")); 163 DOMUtils.style(techStackDoc, fe.getFile(), licenseClientsStyle.getStream(), params); 164 } 165 166 DOMUtils.style(techStackDoc, root.getFile(), style.getStream(), null); 167 DOMUtils.style(techStackDoc, licenseSummary.getFile(), licenseSummaryStyle.getStream(), null); 168 169 DOMUtils.serialize(techStackDoc,new File ("techStack.xml")); 170 } catch (IOException e) { 171 throw new HammurapiException("Could not render tech stack to HTML: "+e, e); 172 } catch (TransformerException e) { 173 throw new HammurapiException("Could not render tech stack to HTML: "+e, e); 174 } catch (ConfigurationException e) { 175 throw new HammurapiException("Could not render tech stack to HTML: "+e, e); 176 } 177 } else { 178 FileEntry root = context.getNextFile(context.getExtension()); 179 path=root.getPath(); 180 try { 181 DOMUtils.serialize(techStackDoc,root.getFile()); 182 } catch (IOException e) { 183 throw new HammurapiException("Could not render tech stack to xml: "+e,e); 184 } catch (TransformerException e) { 185 throw new HammurapiException("Could not render tech stack to xml: "+e,e); 186 } 187 } 188 } 189 190 public Properties getProperties() { 191 return null; 192 } 193 }); 194 195 getContext().getSession().setAttribute("tech-stack", techStack); 196 } catch (ConfigurationException e) { 197 disable("Cannot load tech stack configuration: "+e); 198 } catch (TransformerException e) { 199 disable("Cannot load tech stack configuration: "+e); 200 } catch (JselException e) { 201 context.warn(null, "Cannot obtain external suppliers information: "+e); 202 e.printStackTrace(); 203 } catch (ParserConfigurationException e) { 204 context.warn(null, "Could not save tech stack to file: "+e); 205 } catch (FactoryConfigurationError e) { 206 context.warn(null, "Could not save tech stack to file: "+e); 207 } 208 } 209 } 210 | Popular Tags |