1 23 24 29 30 package com.sun.enterprise.tools.verifier.apiscan.stdapis; 31 32 import java.io.InputStream ; 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 36 import java.io.File ; 37 import java.util.ArrayList ; 38 import java.util.Collection ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.StringTokenizer ; 42 import java.util.logging.Handler ; 43 import java.util.logging.Level ; 44 import java.util.logging.Logger ; 45 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.Element ; 48 import org.w3c.dom.Node ; 49 import org.w3c.dom.NodeList ; 50 import org.xml.sax.SAXException ; 51 import org.xml.sax.SAXParseException ; 52 import org.xml.sax.helpers.DefaultHandler ; 53 54 64 public class APIRepository { 65 private static Logger logger = Logger.getLogger("apiscan.stdapis"); private static String myClsName = "APIRepository"; private HashMap <String , API> apis = new HashMap <String , API>(); 68 private static APIRepository me; 70 71 74 public static APIRepository Instance() { 75 assert(me != null); 76 return me; 77 } 78 79 84 public synchronized static void Initialize(String fileName) 85 throws Exception { 86 logger.logp(Level.FINE, myClsName, "Initialize", fileName); me = new APIRepository(fileName); 91 } 93 94 108 public boolean isClassPartOf(String class_name, String api_name_version) { 109 if (getClassesFor(api_name_version).contains(class_name)) { 110 return true; 111 } else if (getPackagesFor(api_name_version). 112 contains(getPackageName(class_name))) { 113 return true; 114 } else { 115 for (String pattern : getPatternsFor(api_name_version)) { 116 if (class_name.startsWith(pattern)) { 117 return true; 118 } 119 } 120 } 121 return false; 122 } 123 124 138 public boolean isPackagePartOf(String pkg_name, String api_name_version) { 139 if (getPackagesFor(api_name_version).contains(pkg_name)) { 140 return true; 141 } else { 142 for (String pattern : getPatternsFor(api_name_version)) { 143 if (pkg_name.startsWith(pattern)) { 144 return true; 145 } 146 } 147 } 148 return false; 149 } 150 151 protected Collection <String > getPackagesFor(String api_name_version) { 152 return ((API) apis.get(api_name_version)).getPackages(); 153 } 154 155 protected Collection <String > getPatternsFor(String api_name_version) { 156 return ((API) apis.get(api_name_version)).getPatterns(); 157 } 158 159 protected Collection <String > getClassesFor(String api_name_version) { 160 return ((API) apis.get(api_name_version)).getClasses(); 161 } 162 163 public String toString() { 164 StringBuffer sb = new StringBuffer (); 165 for (Iterator i = apis.values().iterator(); i.hasNext();) { 166 sb.append("\n").append(i.next().toString()); } 168 return sb.toString(); 169 } 170 171 private APIRepository(String fileName) throws Exception { 172 logger.entering(myClsName, "init<>", fileName); final File file = new File (fileName); 174 Document d = getDocumentBuilder().parse(file); 175 traverseTree(d.getDocumentElement()); 176 } 177 178 private DocumentBuilder getDocumentBuilder() throws Exception { 179 DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance(); 180 bf.setValidating(true); 181 bf.setIgnoringComments(false); 182 bf.setIgnoringElementContentWhitespace(true); 183 bf.setCoalescing(true); 184 bf.setNamespaceAware(true); 185 bf.setAttribute( 186 "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); DocumentBuilder builder = bf.newDocumentBuilder(); 189 builder.setErrorHandler(new DefaultHandler () { 190 public void error(SAXParseException e) throws SAXException { 191 throw e; 192 } 193 }); 194 return builder; 195 } 196 197 private void traverseTree(Node node) { 198 if (node == null) { 199 return; 200 } 201 if (node.getNodeType() == Node.ELEMENT_NODE) { 202 Element e = (Element ) node; 203 String tagName = e.getTagName(); 204 if (tagName.equals("api")) { API a = new API(e); 206 apis.put(a.name_version, a); 207 } 208 NodeList childNodes = node.getChildNodes(); 209 for (int i = 0; i < childNodes.getLength(); i++) { 210 traverseTree(childNodes.item(i)); 211 } 212 } 213 } 214 215 private static String getPackageName(String externalClassName) { 217 int idx = externalClassName.lastIndexOf('.'); 218 if (idx != -1) { 219 return externalClassName.substring(0, idx); 220 } else 221 return ""; 222 } 223 224 class APIRef { 225 private String api_name_version; 226 227 public APIRef(Element node) { 228 if (node.getTagName().equals("api_ref")) { api_name_version = node.getAttribute("api_name_version"); } else 231 throw new IllegalArgumentException (node.toString()); 232 } 233 234 public APIRef(String api_name_version) { 235 this.api_name_version = api_name_version; 236 } 237 238 public API deref() { 239 API result = (API) apis.get(api_name_version); 240 if (result == null) 241 throw new NullPointerException ( 242 "No API with name_version [" + api_name_version + "]"); return result; 244 } 245 } 246 247 class API { 248 private String name_version; 249 private ArrayList <APIRef> apiRefs = new ArrayList <APIRef>(); 250 ArrayList <String > packages = new ArrayList <String >(), patterns = new ArrayList <String >(), classes = new ArrayList <String >(); 251 252 public API(Element node) { 253 if (node.getTagName().equals("api")) { name_version = node.getAttribute("name_version"); NodeList refNodes = node.getElementsByTagName("api_ref"); for (int loopIndex = 0; 257 loopIndex < refNodes.getLength(); loopIndex++) { 258 apiRefs.add(new APIRef((Element ) refNodes.item(loopIndex))); 259 } 260 NodeList pkgsNodes = node.getElementsByTagName("packages"); for (int i = 0; i < pkgsNodes.getLength(); ++i) { 262 Element pkgsNode = (Element ) pkgsNodes.item(i); 263 NodeList children = pkgsNode.getChildNodes(); 264 for (int j = 0; j < children.getLength(); j++) { 265 Node next = children.item(j); 266 if (next.getNodeType() == Node.TEXT_NODE) { 267 String names = next.getNodeValue().trim(); 268 for (StringTokenizer st = new StringTokenizer ( 269 names); 270 st.hasMoreTokens();) { 271 packages.add(st.nextToken()); 272 } 273 } 274 } 275 } 276 NodeList patternsNodes = node.getElementsByTagName("patterns"); for (int i = 0; i < patternsNodes.getLength(); ++i) { 278 Element patternsNode = (Element ) patternsNodes.item(i); 279 NodeList children = patternsNode.getChildNodes(); 280 for (int j = 0; j < children.getLength(); j++) { 281 Node next = children.item(j); 282 if (next.getNodeType() == Node.TEXT_NODE) { 283 String names = next.getNodeValue().trim(); 284 for (StringTokenizer st = new StringTokenizer ( 285 names); 286 st.hasMoreTokens();) { 287 patterns.add(st.nextToken()); 288 } 289 } 290 } 291 } 292 NodeList classesNodes = node.getElementsByTagName("classes"); for (int i = 0; i < classesNodes.getLength(); ++i) { 294 Element classesNode = (Element ) classesNodes.item(i); 295 String package_name = classesNode.getAttribute("package") .trim(); 297 NodeList children = classesNode.getChildNodes(); 298 for (int j = 0; j < children.getLength(); j++) { 299 Node next = children.item(j); 300 if (next.getNodeType() == Node.TEXT_NODE) { 301 String names = next.getNodeValue().trim(); 302 for (StringTokenizer st = new StringTokenizer ( 303 names); 304 st.hasMoreTokens();) { 305 String clsName = package_name + "." + st.nextToken(); 307 classes.add(clsName); 308 } 309 } 310 } 311 } 312 } else { 313 throw new IllegalArgumentException (node.toString()); 314 } 315 } 317 public Collection <String > getPackages() { 318 ArrayList <String > results = new ArrayList <String >(); 319 for (Iterator i = apiRefs.iterator(); i.hasNext();) { 320 results.addAll(((APIRef) i.next()).deref().getPackages()); 321 } 322 results.addAll(packages); 323 return results; 324 } 325 326 public Collection <String > getPatterns() { 327 ArrayList <String > results = new ArrayList <String >(); 328 for (Iterator i = apiRefs.iterator(); i.hasNext();) { 329 results.addAll(((APIRef) i.next()).deref().getPatterns()); 330 } 331 results.addAll(patterns); 332 return results; 333 } 334 335 public Collection <String > getClasses() { 336 ArrayList <String > results = new ArrayList <String >(); 337 for (Iterator i = apiRefs.iterator(); i.hasNext();) { 338 results.addAll(((APIRef) i.next()).deref().getClasses()); 339 } 340 results.addAll(classes); 341 return results; 342 } 343 344 public String toString() { 345 StringBuffer sb = new StringBuffer (); 346 sb.append("<api name_version=\"" + name_version + "\">"); sb.append("\n\t<classes>"); for (Iterator i = getClasses().iterator(); i.hasNext();) sb.append( 349 "\n\t\t") .append(i.next().toString()); 351 sb.append("\n\t</classes>"); sb.append("\n\t<packages>"); for (Iterator i = getPackages().iterator(); i.hasNext();) sb.append( 354 "\n\t\t") .append(i.next().toString()); 356 sb.append("\n\t</packages>"); sb.append("\n\t<patterns>"); for (Iterator i = getPatterns().iterator(); i.hasNext();) sb.append( 359 "\n\t\t") .append(i.next().toString()); 361 sb.append("\n\t</patterns>"); sb.append("\n</api>"); return sb.toString(); 364 } 365 } 367 public static void main(String [] args) { 368 if (args.length < 1) { 369 usage(); 370 } 371 Logger logger = Logger.getLogger("apiscan.stdapis"); try { 373 APIRepository.Initialize(args[0]); 374 APIRepository apiRep = APIRepository.Instance(); 375 switch(args.length) { 376 case 1: 377 System.out.println(apiRep); 378 break; 379 case 2: 380 System.out.println(apiRep.apis.get(args[1])); 381 break; 382 case 3: 383 System.out.println(apiRep.isClassPartOf(args[2], args[1])); 384 break; 385 default: 386 usage(); 387 } 388 } catch (Exception e) { 389 e.printStackTrace(); 390 } 391 } 392 393 private static void usage(){ 394 System.out.println( 395 "Usage: java " + APIRepository.class.getName() + " <file_name> [api_name_version] [class_name]"); System.out.println("\nExamples:\n"); System.out.println( 399 "java " + APIRepository.class.getName() + " src/standard-apis.xml ejb_jar_2.0 javax.ejb.Timer"); System.out.println("The above command prints true if javax.ejb.Timer is part of ejb_api_2.0 API.\n"); System.out.println( 403 "java " + APIRepository.class.getName() + " src/standard-apis.xml ejb_jar_2.0"); System.out.println("The above command prints details about all classes and packages for ejb_api_2.0 API.\n"); System.out.println( 407 "java " + APIRepository.class.getName() + " src/standard-apis.xml"); System.out.println("The above command prints details about all APIs.\n"); System.exit(1); 411 } 412 } | Popular Tags |