001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors.techstack;
024:
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.StringTokenizer;
031: import java.util.TreeMap;
032:
033: import javax.xml.transform.TransformerException;
034:
035: import org.apache.xpath.CachedXPathAPI;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.traversal.NodeIterator;
039:
040: import com.pavelvlasov.xml.dom.AbstractDomObject;
041: import com.pavelvlasov.xml.dom.DomSerializable;
042:
043: /**
044: * @author Pavel Vlasov
045: * @version $Revision: 1.2 $
046: */
047: public class TechStack extends AbstractDomObject implements
048: DomSerializable {
049: private static final String PUBLISHER_DESCRIPTION = "This publisher was inferred "
050: + "by the technology stack inspector from the package name. "
051: + "You should explicitly add proper information into the technology stack inspector configuration "
052: + "file";
053: private static final String PRODUCT_DESCRIPTION = "This product was inferred "
054: + "by the technology stack inspector from the package name. "
055: + "You should explicitly add proper information into the technology stack inspector configuration "
056: + "file";
057:
058: private static final String LICENSE_DESCRIPTION = "This license was inferred "
059: + "by the technology stack inspector from the package name. "
060: + "You should explicitly add proper information into the technology stack inspector configuration "
061: + "file";
062:
063: private static final String UNKNOWN_COMMERCIAL_LICENSE = "unknown commercial license";
064: private static final String UNKNOWN_OS_LICENSE = "unknown open source license";
065: private static final String UNKNOWN_LICENSE = "unknown license";
066: private static final String JAVA_LICENSE = "Java license";
067:
068: private Collection publishers = new ArrayList();
069: private Map licenses = new TreeMap();
070:
071: public TechStack() {
072: // Default constructor
073: }
074:
075: public TechStack(Element holder) throws TransformerException {
076: CachedXPathAPI cxpa = new CachedXPathAPI();
077: NodeIterator nit = cxpa.selectNodeIterator(holder, "publisher");
078: Node n;
079: while ((n = nit.nextNode()) != null) {
080: publishers.add(new Publisher(this , (Element) n, cxpa));
081: }
082:
083: nit = cxpa.selectNodeIterator(holder, "license");
084: while ((n = nit.nextNode()) != null) {
085: BasicDescriptor baseDescriptor = new BasicDescriptor(
086: (Element) n, cxpa);
087: licenses.put(baseDescriptor.getKey(), baseDescriptor);
088: }
089: }
090:
091: public void toDom(Element holder) {
092: Iterator it = licenses.values().iterator();
093: while (it.hasNext()) {
094: ((DomSerializable) it.next()).toDom(addElement(holder,
095: "license"));
096: }
097:
098: it = publishers.iterator();
099: while (it.hasNext()) {
100: ((DomSerializable) it.next()).toDom(addElement(holder,
101: "publisher"));
102: }
103: }
104:
105: /**
106: * Converts first char to uppercase
107: * @param str
108: * @return
109: */
110: public static String capitalize(String str) {
111: if (str == null) {
112: return null;
113: }
114:
115: switch (str.length()) {
116: case 0:
117: return str;
118: case 1:
119: return str.toUpperCase();
120: default:
121: return str.substring(0, 1).toUpperCase() + str.substring(1);
122: }
123: }
124:
125: /**
126: *
127: * @param packageName
128: * @param clients
129: */
130: public void addPackage(String packageName, Collection clients) {
131: Iterator pit = publishers.iterator();
132: while (pit.hasNext()) {
133: Publisher publisher = (Publisher) pit.next();
134: if (publisher.match(packageName, clients)) {
135: return;
136: }
137: }
138:
139: // Publisher's root packages
140: pit = publishers.iterator();
141: while (pit.hasNext()) {
142: Publisher publisher = (Publisher) pit.next();
143: String[] productMatch = publisher.match(packageName);
144: if (productMatch != null) {
145: Product product = new Product(this , publisher);
146: if (publisher.getLicense() == null) {
147: product.setLicenseRef(getUnknownLicense().getKey());
148: } else {
149: product.setLicenseRef(publisher.getLicense()
150: .getKey());
151: }
152: if (productMatch[1] == null) {
153: product.setName(publisher.getName()
154: + " flagship product");
155: product.setDescription(PRODUCT_DESCRIPTION);
156: product.addPackage(productMatch[0]);
157: } else {
158: product.setName(capitalize(productMatch[1]));
159: product.setDescription(PRODUCT_DESCRIPTION);
160: if (publisher.getUrl() != null) {
161: product.setUrl(publisher.getUrl()
162: + "/products/" + productMatch[1]);
163: }
164: product.addPackage(productMatch[0] + "."
165: + productMatch[1]);
166: }
167: product.addClients(clients);
168: return;
169: }
170: }
171:
172: // AI starts here.
173: StringTokenizer st = new StringTokenizer(packageName, ".");
174: String[] tokens = new String[st.countTokens()];
175: for (int i = 0; st.hasMoreTokens(); i++) {
176: tokens[i] = st.nextToken();
177: }
178:
179: if (tokens.length > 1) {
180: if ("com".equals(tokens[0])) {
181: // Pattern com.<product>
182: if (tokens.length == 2) {
183: String productName = capitalize(tokens[1]);
184: Publisher publisher = new Publisher(this );
185: publisher.setName("Publisher of " + productName);
186: publisher
187: .setUrl("http://www." + tokens[1] + ".com");
188: publisher
189: .setLicenseRef(getUnknownCommercialLicense()
190: .getKey());
191: publisher.setDescription(PUBLISHER_DESCRIPTION);
192: publishers.add(publisher);
193:
194: Product product = new Product(this , publisher);
195: product.setUrl("http://www." + tokens[1] + ".com");
196: product.setName(productName);
197: product.setDescription(PRODUCT_DESCRIPTION);
198: product.setLicenseRef(getUnknownCommercialLicense()
199: .getKey());
200: product.addPackage("com." + tokens[1]);
201: product.addClients(clients);
202: } else {
203: Publisher publisher = new Publisher(this );
204: publisher.setName(capitalize(tokens[1]));
205: publisher.setDescription(PUBLISHER_DESCRIPTION);
206: publisher
207: .setUrl("http://www." + tokens[1] + ".com");
208: publisher.addPackage(tokens[0] + "." + tokens[1]);
209: publisher
210: .setLicenseRef(getUnknownCommercialLicense()
211: .getKey());
212: publishers.add(publisher);
213:
214: Product product = new Product(this , publisher);
215: product.setUrl("http://www." + tokens[1]
216: + ".com/products/" + tokens[2]);
217: product.setName(capitalize(tokens[2]));
218: product.setDescription(PRODUCT_DESCRIPTION);
219: product.setLicenseRef(getUnknownCommercialLicense()
220: .getKey());
221: product.addPackage("com." + tokens[1] + "."
222: + tokens[2]);
223: product.addClients(clients);
224: }
225: return;
226: } else if ("org".equals(tokens[0])
227: || "net".equals(tokens[0])) {
228: if (tokens.length == 2) {
229: String productName = capitalize(tokens[1]);
230: Publisher publisher = new Publisher(this );
231: publisher.setName("Publisher of " + productName);
232: publisher.setDescription(PUBLISHER_DESCRIPTION);
233: publisher.setUrl("http://www." + tokens[1] + "."
234: + tokens[0]);
235: publisher
236: .setLicenseRef(getUnknownOpenSourceLicense()
237: .getKey());
238: publishers.add(publisher);
239:
240: Product product = new Product(this , publisher);
241: product.setUrl("http://www." + tokens[1] + "."
242: + tokens[0]);
243: product.setName(productName);
244: product.setDescription(PRODUCT_DESCRIPTION);
245: product.setLicenseRef(getUnknownOpenSourceLicense()
246: .getKey());
247: product.addPackage(tokens[0] + "." + tokens[1]);
248: product.addClients(clients);
249: } else {
250: Publisher publisher = new Publisher(this );
251: publisher.setName(capitalize(tokens[1]));
252: publisher.setDescription(PUBLISHER_DESCRIPTION);
253: publisher.setUrl("http://www." + tokens[1] + "."
254: + tokens[0]);
255: publisher.addPackage(tokens[0] + "." + tokens[1]);
256: publisher
257: .setLicenseRef(getUnknownOpenSourceLicense()
258: .getKey());
259: publishers.add(publisher);
260:
261: Product product = new Product(this , publisher);
262: product.setUrl("http://www." + tokens[1] + "."
263: + tokens[0] + "/products/" + tokens[2]);
264: product.setName(capitalize(tokens[2]));
265: product.setDescription(PRODUCT_DESCRIPTION);
266: product.setLicenseRef(getUnknownOpenSourceLicense()
267: .getKey());
268: product.addPackage(tokens[0] + "." + tokens[1]
269: + "." + tokens[2]);
270: product.addClients(clients);
271: }
272: return;
273: }
274: }
275:
276: if (tokens.length == 0 || "(default)".equals(packageName)
277: || "java".equals(tokens[0])
278: || "javax".equals(tokens[0])) {
279: Publisher publisher = new Publisher(this );
280: publisher.setName("Sun Microsystems");
281: publisher.setUrl("http://www.sun.com");
282: publishers.add(publisher);
283:
284: Product product = new Product(this , publisher);
285: product.setName("Java platform");
286: product.setUrl("http://java.sun.com");
287: product.setLicenseRef(getJavaLicense().getKey());
288: product.addPackage("(default)");
289: product.addPackage("java");
290: product.addPackage("javax");
291: product.addClients(clients);
292: return;
293: }
294:
295: Publisher publisher = new Publisher(this );
296: publisher.setName("Publisher of " + packageName);
297: publisher.setDescription(PUBLISHER_DESCRIPTION);
298: publishers.add(publisher);
299:
300: Product product = new Product(this , publisher);
301: product.setName(packageName);
302: product.setDescription(PRODUCT_DESCRIPTION);
303: product.setLicenseRef(getUnknownLicense().getKey());
304: product.addPackage(packageName);
305: product.addClients(clients);
306: }
307:
308: private BasicDescriptor getUnknownCommercialLicense() {
309: if (unknownCommercialLicense == null) {
310: unknownCommercialLicense = new BasicDescriptor();
311: unknownCommercialLicense.setKey(UNKNOWN_COMMERCIAL_LICENSE);
312: unknownCommercialLicense
313: .setName("Unknown commercial license");
314: unknownCommercialLicense
315: .setDescription(LICENSE_DESCRIPTION);
316: unknownCommercialLicense.setCategory("commercial");
317: licenses.put(unknownCommercialLicense.getKey(),
318: unknownCommercialLicense);
319: }
320: return unknownCommercialLicense;
321: }
322:
323: private BasicDescriptor getUnknownOpenSourceLicense() {
324: if (unknownOpenSourceLicense == null) {
325: unknownOpenSourceLicense = new BasicDescriptor();
326: unknownOpenSourceLicense.setKey(UNKNOWN_OS_LICENSE);
327: unknownOpenSourceLicense
328: .setName("Unknown open source license");
329: unknownOpenSourceLicense
330: .setDescription(LICENSE_DESCRIPTION);
331: unknownOpenSourceLicense.setCategory("open source");
332: licenses.put(unknownOpenSourceLicense.getKey(),
333: unknownOpenSourceLicense);
334: }
335: return unknownOpenSourceLicense;
336: }
337:
338: private BasicDescriptor getUnknownLicense() {
339: if (unknownLicense == null) {
340: unknownLicense = new BasicDescriptor();
341: unknownLicense.setKey(UNKNOWN_LICENSE);
342: unknownLicense.setName("Unknown license");
343: unknownLicense.setDescription(LICENSE_DESCRIPTION);
344: licenses.put(unknownLicense.getKey(), unknownLicense);
345: }
346: return unknownLicense;
347: }
348:
349: private BasicDescriptor getJavaLicense() {
350: if (javaLicense == null) {
351: javaLicense = new BasicDescriptor();
352: javaLicense.setKey(JAVA_LICENSE);
353: javaLicense.setName("Java license");
354: licenses.put(javaLicense.getKey(), javaLicense);
355: }
356: return javaLicense;
357: }
358:
359: private BasicDescriptor unknownCommercialLicense;
360: private BasicDescriptor unknownOpenSourceLicense;
361: private BasicDescriptor unknownLicense;
362: private BasicDescriptor javaLicense;
363:
364: /**
365: * @param license key
366: * @return license
367: */
368: public BasicDescriptor findLicense(String license) {
369: return license == null ? null : (BasicDescriptor) licenses
370: .get(license);
371: }
372:
373: }
|