1 23 package org.hammurapi.inspectors.techstack; 24 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.StringTokenizer ; 31 import java.util.TreeMap ; 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.traversal.NodeIterator; 39 40 import com.pavelvlasov.xml.dom.AbstractDomObject; 41 import com.pavelvlasov.xml.dom.DomSerializable; 42 43 47 public class TechStack extends AbstractDomObject implements DomSerializable { 48 private static final String 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 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 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 UNKNOWN_COMMERCIAL_LICENSE = "unknown commercial license"; 63 private static final String UNKNOWN_OS_LICENSE = "unknown open source license"; 64 private static final String UNKNOWN_LICENSE = "unknown license"; 65 private static final String JAVA_LICENSE = "Java license"; 66 67 private Collection publishers=new ArrayList (); 68 private Map licenses=new TreeMap (); 69 70 public TechStack() { 71 } 73 74 public TechStack(Element holder) throws TransformerException { 75 CachedXPathAPI cxpa=new CachedXPathAPI(); 76 NodeIterator nit = cxpa.selectNodeIterator(holder, "publisher"); 77 Node n; 78 while ((n=nit.nextNode())!=null) { 79 publishers.add(new Publisher(this, (Element ) n, cxpa)); 80 } 81 82 nit = cxpa.selectNodeIterator(holder, "license"); 83 while ((n=nit.nextNode())!=null) { 84 BasicDescriptor baseDescriptor = new BasicDescriptor((Element ) n, cxpa); 85 licenses.put(baseDescriptor.getKey(), baseDescriptor); 86 } 87 } 88 89 public void toDom(Element holder) { 90 Iterator 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 106 public static String capitalize(String 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 126 public void addPackage(String packageName, Collection clients) { 127 Iterator 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 pit=publishers.iterator(); 137 while (pit.hasNext()) { 138 Publisher publisher=(Publisher) pit.next(); 139 String [] 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 StringTokenizer st=new StringTokenizer (packageName, "."); 166 String [] tokens=new String [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 if (tokens.length==2) { 175 String 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 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 330 public BasicDescriptor findLicense(String license) { 331 return license==null ? null : (BasicDescriptor) licenses.get(license); 332 } 333 334 } 335 | Popular Tags |