KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.TreeMap JavaDoc;
32
33 import javax.xml.transform.TransformerException JavaDoc;
34
35 import org.apache.xpath.CachedXPathAPI;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.traversal.NodeIterator;
39
40 import com.pavelvlasov.xml.dom.AbstractDomObject;
41 import com.pavelvlasov.xml.dom.DomSerializable;
42
43 /**
44  * @author Pavel Vlasov
45  * @version $Revision: 1.2 $
46  */

47 public class TechStack extends AbstractDomObject implements DomSerializable {
48     private static final String JavaDoc PUBLISHER_DESCRIPTION = "This publisher was inferred " +
49             "by the technology stack inspector from the package name. " +
50             "You should explicitly add proper information into the technology stack inspector configuration " +
51             "file";
52     private static final String JavaDoc PRODUCT_DESCRIPTION = "This product was inferred " +
53             "by the technology stack inspector from the package name. " +
54             "You should explicitly add proper information into the technology stack inspector configuration " +
55             "file";
56     
57     private static final String JavaDoc LICENSE_DESCRIPTION = "This license was inferred " +
58             "by the technology stack inspector from the package name. " +
59             "You should explicitly add proper information into the technology stack inspector configuration " +
60             "file";
61     
62     private static final String JavaDoc UNKNOWN_COMMERCIAL_LICENSE = "unknown commercial license";
63     private static final String JavaDoc UNKNOWN_OS_LICENSE = "unknown open source license";
64     private static final String JavaDoc UNKNOWN_LICENSE = "unknown license";
65     private static final String JavaDoc JAVA_LICENSE = "Java license";
66     
67     private Collection JavaDoc publishers=new ArrayList JavaDoc();
68     private Map JavaDoc licenses=new TreeMap JavaDoc();
69     
70     public TechStack() {
71         // Default constructor
72
}
73     
74     public TechStack(Element JavaDoc holder) throws TransformerException JavaDoc {
75         CachedXPathAPI cxpa=new CachedXPathAPI();
76         NodeIterator nit = cxpa.selectNodeIterator(holder, "publisher");
77         Node JavaDoc n;
78         while ((n=nit.nextNode())!=null) {
79             publishers.add(new Publisher(this, (Element JavaDoc) n, cxpa));
80         }
81        
82         nit = cxpa.selectNodeIterator(holder, "license");
83         while ((n=nit.nextNode())!=null) {
84             BasicDescriptor baseDescriptor = new BasicDescriptor((Element JavaDoc) n, cxpa);
85             licenses.put(baseDescriptor.getKey(), baseDescriptor);
86         }
87     }
88
89     public void toDom(Element JavaDoc holder) {
90         Iterator JavaDoc it=licenses.values().iterator();
91         while (it.hasNext()) {
92             ((DomSerializable) it.next()).toDom(addElement(holder, "license"));
93         }
94         
95         it=publishers.iterator();
96         while (it.hasNext()) {
97             ((DomSerializable) it.next()).toDom(addElement(holder, "publisher"));
98         }
99     }
100     
101     /**
102      * Converts first char to uppercase
103      * @param str
104      * @return
105      */

106     public static String JavaDoc capitalize(String JavaDoc str) {
107         if (str==null) {
108             return null;
109         }
110         
111         switch (str.length()) {
112             case 0:
113                 return str;
114             case 1:
115                 return str.toUpperCase();
116             default:
117                 return str.substring(0, 1).toUpperCase()+str.substring(1);
118         }
119     }
120     
121     /**
122      *
123      * @param packageName
124      * @param clients
125      */

126     public void addPackage(String JavaDoc packageName, Collection JavaDoc clients) {
127         Iterator JavaDoc pit=publishers.iterator();
128         while (pit.hasNext()) {
129             Publisher publisher=(Publisher) pit.next();
130             if (publisher.match(packageName, clients)) {
131                 return;
132             }
133         }
134         
135         // Publisher's root packages
136
pit=publishers.iterator();
137         while (pit.hasNext()) {
138             Publisher publisher=(Publisher) pit.next();
139             String JavaDoc[] productMatch=publisher.match(packageName);
140             if (productMatch!=null) {
141                 Product product=new Product(this, publisher);
142                 if (publisher.getLicense()==null) {
143                     product.setLicenseRef(getUnknownLicense().getKey());
144                 } else {
145                     product.setLicenseRef(publisher.getLicense().getKey());
146                 }
147                 if (productMatch[1]==null) {
148                     product.setName(publisher.getName()+" flagship product");
149                     product.setDescription(PRODUCT_DESCRIPTION);
150                     product.addPackage(productMatch[0]);
151                 } else {
152                     product.setName(capitalize(productMatch[1]));
153                     product.setDescription(PRODUCT_DESCRIPTION);
154                     if (publisher.getUrl()!=null) {
155                         product.setUrl(publisher.getUrl()+"/products/"+productMatch[1]);
156                     }
157                     product.addPackage(productMatch[0]+"."+productMatch[1]);
158                 }
159                 product.addClients(clients);
160                 return;
161             }
162         }
163         
164         // AI starts here.
165
StringTokenizer JavaDoc st=new StringTokenizer JavaDoc(packageName, ".");
166         String JavaDoc[] tokens=new String JavaDoc[st.countTokens()];
167         for (int i=0; st.hasMoreTokens(); i++) {
168             tokens[i]=st.nextToken();
169         }
170         
171         if (tokens.length>1) {
172             if ("com".equals(tokens[0])) {
173                 // Pattern com.<product>
174
if (tokens.length==2) {
175                     String JavaDoc productName = capitalize(tokens[1]);
176                     Publisher publisher=new Publisher(this);
177                     publisher.setName("Publisher of "+productName);
178                     publisher.setUrl("http://www."+tokens[1]+".com");
179                     publisher.setLicenseRef(getUnknownCommercialLicense().getKey());
180                     publisher.setDescription(PUBLISHER_DESCRIPTION);
181                     publishers.add(publisher);
182                     
183                     Product product=new Product(this, publisher);
184                     product.setUrl("http://www."+tokens[1]+".com");
185                     product.setName(productName);
186                     product.setDescription(PRODUCT_DESCRIPTION);
187                     product.setLicenseRef(getUnknownCommercialLicense().getKey());
188                     product.addPackage("com."+tokens[1]);
189                     product.addClients(clients);
190                 } else {
191                     Publisher publisher=new Publisher(this);
192                     publisher.setName(capitalize(tokens[1]));
193                     publisher.setDescription(PUBLISHER_DESCRIPTION);
194                     publisher.setUrl("http://www."+tokens[1]+".com");
195                     publisher.addPackage(tokens[0]+"."+tokens[1]);
196                     publisher.setLicenseRef(getUnknownCommercialLicense().getKey());
197                     publishers.add(publisher);
198                     
199                     Product product=new Product(this, publisher);
200                     product.setUrl("http://www."+tokens[1]+".com/products/"+tokens[2]);
201                     product.setName(capitalize(tokens[2]));
202                     product.setDescription(PRODUCT_DESCRIPTION);
203                     product.setLicenseRef(getUnknownCommercialLicense().getKey());
204                     product.addPackage("com."+tokens[1]+"."+tokens[2]);
205                     product.addClients(clients);
206                 }
207                 return;
208             } else if ("org".equals(tokens[0]) || "net".equals(tokens[0])) {
209                 if (tokens.length==2) {
210                     String JavaDoc productName = capitalize(tokens[1]);
211                     Publisher publisher=new Publisher(this);
212                     publisher.setName("Publisher of "+productName);
213                     publisher.setDescription(PUBLISHER_DESCRIPTION);
214                     publisher.setUrl("http://www."+tokens[1]+"."+tokens[0]);
215                     publisher.setLicenseRef(getUnknownOpenSourceLicense().getKey());
216                     publishers.add(publisher);
217                     
218                     Product product=new Product(this, publisher);
219                     product.setUrl("http://www."+tokens[1]+"."+tokens[0]);
220                     product.setName(productName);
221                     product.setDescription(PRODUCT_DESCRIPTION);
222                     product.setLicenseRef(getUnknownOpenSourceLicense().getKey());
223                     product.addPackage(tokens[0]+"."+tokens[1]);
224                     product.addClients(clients);
225                 } else {
226                     Publisher publisher=new Publisher(this);
227                     publisher.setName(capitalize(tokens[1]));
228                     publisher.setDescription(PUBLISHER_DESCRIPTION);
229                     publisher.setUrl("http://www."+tokens[1]+"."+tokens[0]);
230                     publisher.addPackage(tokens[0]+"."+tokens[1]);
231                     publisher.setLicenseRef(getUnknownOpenSourceLicense().getKey());
232                     publishers.add(publisher);
233                     
234                     Product product=new Product(this, publisher);
235                     product.setUrl("http://www."+tokens[1]+"."+tokens[0]+"/products/"+tokens[2]);
236                     product.setName(capitalize(tokens[2]));
237                     product.setDescription(PRODUCT_DESCRIPTION);
238                     product.setLicenseRef(getUnknownOpenSourceLicense().getKey());
239                     product.addPackage(tokens[0]+"."+tokens[1]+"."+tokens[2]);
240                     product.addClients(clients);
241                 }
242                 return;
243             }
244         }
245         
246         if (tokens.length==0 || "(default)".equals(packageName) || "java".equals(tokens[0]) || "javax".equals(tokens[0])) {
247             Publisher publisher=new Publisher(this);
248             publisher.setName("Sun Microsystems");
249             publisher.setUrl("http://www.sun.com");
250             publishers.add(publisher);
251             
252             Product product=new Product(this, publisher);
253             product.setName("Java platform");
254             product.setUrl("http://java.sun.com");
255             product.setLicenseRef(getJavaLicense().getKey());
256             product.addPackage("(default)");
257             product.addPackage("java");
258             product.addPackage("javax");
259             product.addClients(clients);
260             return;
261         }
262         
263         Publisher publisher=new Publisher(this);
264         publisher.setName("Publisher of "+packageName);
265         publisher.setDescription(PUBLISHER_DESCRIPTION);
266         publishers.add(publisher);
267         
268         Product product=new Product(this, publisher);
269         product.setName(packageName);
270         product.setDescription(PRODUCT_DESCRIPTION);
271         product.setLicenseRef(getUnknownLicense().getKey());
272         product.addPackage(packageName);
273         product.addClients(clients);
274     }
275     
276     private BasicDescriptor getUnknownCommercialLicense() {
277         if (unknownCommercialLicense==null) {
278             unknownCommercialLicense=new BasicDescriptor();
279             unknownCommercialLicense.setKey(UNKNOWN_COMMERCIAL_LICENSE);
280             unknownCommercialLicense.setName("Unknown commercial license");
281             unknownCommercialLicense.setDescription(LICENSE_DESCRIPTION);
282             unknownCommercialLicense.setCategory("commercial");
283             licenses.put(unknownCommercialLicense.getKey(), unknownCommercialLicense);
284         }
285         return unknownCommercialLicense;
286     }
287
288     private BasicDescriptor getUnknownOpenSourceLicense() {
289         if (unknownOpenSourceLicense==null) {
290             unknownOpenSourceLicense=new BasicDescriptor();
291             unknownOpenSourceLicense.setKey(UNKNOWN_OS_LICENSE);
292             unknownOpenSourceLicense.setName("Unknown open source license");
293             unknownOpenSourceLicense.setDescription(LICENSE_DESCRIPTION);
294             unknownOpenSourceLicense.setCategory("open source");
295             licenses.put(unknownOpenSourceLicense.getKey(), unknownOpenSourceLicense);
296         }
297         return unknownOpenSourceLicense;
298     }
299
300     private BasicDescriptor getUnknownLicense() {
301         if (unknownLicense==null) {
302             unknownLicense=new BasicDescriptor();
303             unknownLicense.setKey(UNKNOWN_LICENSE);
304             unknownLicense.setName("Unknown license");
305             unknownLicense.setDescription(LICENSE_DESCRIPTION);
306             licenses.put(unknownLicense.getKey(), unknownLicense);
307         }
308         return unknownLicense;
309     }
310
311     private BasicDescriptor getJavaLicense() {
312         if (javaLicense==null) {
313             javaLicense=new BasicDescriptor();
314             javaLicense.setKey(JAVA_LICENSE);
315             javaLicense.setName("Java license");
316             licenses.put(javaLicense.getKey(), javaLicense);
317         }
318         return javaLicense;
319     }
320
321     private BasicDescriptor unknownCommercialLicense;
322     private BasicDescriptor unknownOpenSourceLicense;
323     private BasicDescriptor unknownLicense;
324     private BasicDescriptor javaLicense;
325     
326     /**
327      * @param license key
328      * @return license
329      */

330     public BasicDescriptor findLicense(String JavaDoc license) {
331         return license==null ? null : (BasicDescriptor) licenses.get(license);
332     }
333
334 }
335
Popular Tags