KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > techstack > TechStackInspector


1 /*
2   * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.inspectors.techstack;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.TreeSet JavaDoc;
33
34 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
35 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
46 import org.w3c.dom.Element JavaDoc;
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 /**
58  * Creates annotation, which displays dependencies on external packages.
59  * @author Pavel Vlasov
60  * @version $Revision: 1.4 $
61  */

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 JavaDoc configDoc=config.getConfigDocument();
94             final TechStack techStack = configDoc==null ? new TechStack() : new TechStack(configDoc.getDocumentElement());
95             Iterator JavaDoc it=repo.getExternalSuppliers().entrySet().iterator();
96             while (it.hasNext()) {
97                 Map.Entry JavaDoc entry=(Map.Entry JavaDoc) it.next();
98                 String JavaDoc packageName=(String JavaDoc) entry.getKey();
99                 Collection JavaDoc clients=new TreeSet JavaDoc();
100                 Iterator JavaDoc cit=repo.getExternalSupplierClients(packageName).iterator();
101                 while (cit.hasNext()) {
102                     CompilationUnit cu=(CompilationUnit) cit.next();
103                     String JavaDoc 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 JavaDoc techStackDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
114             techStack.toDom(AbstractDomObject.addElement(techStackDoc, "tech-stack"));
115                         
116             context.annotate(new LinkedAnnotation() {
117                 String JavaDoc path;
118
119                 public String JavaDoc getPath() {
120                     return path;
121                 }
122
123                 public String JavaDoc 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 JavaDoc 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 JavaDoc params=new HashMap JavaDoc();
141                             NodeIterator nit=cxpa.selectNodeIterator(tsRoot, "publisher/product[client]");
142                             Element JavaDoc element;
143                             while ((element=(Element JavaDoc) 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 JavaDoc) 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 JavaDoc) 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 JavaDoc("techStack.xml"));
170                         } catch (IOException JavaDoc e) {
171                             throw new HammurapiException("Could not render tech stack to HTML: "+e, e);
172                         } catch (TransformerException JavaDoc e) {
173                             throw new HammurapiException("Could not render tech stack to HTML: "+e, e);
174                         } catch (ConfigurationException JavaDoc 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 JavaDoc e) {
183                             throw new HammurapiException("Could not render tech stack to xml: "+e,e);
184                         } catch (TransformerException JavaDoc e) {
185                             throw new HammurapiException("Could not render tech stack to xml: "+e,e);
186                         }
187                     }
188                 }
189
190                 public Properties JavaDoc getProperties() {
191                     return null;
192                 }
193             });
194             
195             getContext().getSession().setAttribute("tech-stack", techStack);
196         } catch (ConfigurationException JavaDoc e) {
197             disable("Cannot load tech stack configuration: "+e);
198         } catch (TransformerException JavaDoc 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 JavaDoc e) {
204             context.warn(null, "Could not save tech stack to file: "+e);
205         } catch (FactoryConfigurationError JavaDoc e) {
206             context.warn(null, "Could not save tech stack to file: "+e);
207         }
208     }
209 }
210
Popular Tags