From 590375cfa24240776a970b1e14effafc6009182b Mon Sep 17 00:00:00 2001 From: jlanuti <jlanuti> Date: Tue, 14 Nov 2006 16:32:53 +0000 Subject: [PATCH] Refactored CombineClass2Reference for usability, documentation, and reuse --- .../adopters/CombineClass2Reference.java | 1039 ++++++++++------- .../adopters/IComponentConstants.java | 52 + .../component/adopters/IOutputConstants.java | 16 + 3 files changed, 702 insertions(+), 405 deletions(-) create mode 100644 archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/IComponentConstants.java create mode 100644 archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/IOutputConstants.java diff --git a/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/CombineClass2Reference.java b/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/CombineClass2Reference.java index a8e7376d1..840d0bc86 100644 --- a/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/CombineClass2Reference.java +++ b/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/CombineClass2Reference.java @@ -18,9 +18,11 @@ import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.TreeMap; import org.eclipse.wtp.releng.tools.component.ILocation; @@ -33,510 +35,737 @@ import org.eclipse.wtp.releng.tools.component.model.Type; import org.eclipse.wtp.releng.tools.component.util.CommandOptionParser; import org.eclipse.wtp.releng.tools.component.xsl.XSLUtil; -public class CombineClass2Reference -{ - private HashMap plugin2complead; - ComponentLead chuck; - ComponentLead david; - ComponentLead derping; - ComponentLead chris; - ComponentLead craig; - ComponentLead tim; - ComponentLead unknown; +/** + * This class is used to take the output usage.xml files generated from SimpleClass2Reference + * and combine them into a helpful CSV or HTML file format displaying API and non-API internal + * usage for each logically grouped component team in WTP. The main method can be invoked with + * the following three command line arguments: + * + * "src" = the zip or folder location of the usage.xml files + * "api" = the zip or folder location of the component.xml files defining the API base + * "output" = the location of the output csv and html combined files + */ +public class CombineClass2Reference implements IComponentConstants, IOutputConstants { + // Command Line arguments + public static final String ARG_SOURCE = "src"; //$NON-NLS-1$ + public static final String ARG_OUTPUT = "output"; //$NON-NLS-1$ + public static final String ARG_API = "api"; //$NON-NLS-1$ + + // Instance variables + private HashMap plugin2compTeam; + private List componentTeams; private Collection src; private String output; private Collection api; + + // Class variables for String values + private static final String CLASS_CVS_FILE_EXTENSION = ".class.csv"; //$NON-NLS-1$ + private static final String PACKAGE_CVS_FILE_EXTENSION = ".pkg.csv"; //$NON-NLS-1$ + private static final String PLUGIN_CVS_FILE_EXTENSION = ".plugin.csv"; //$NON-NLS-1$ + private static final String CLASS_HTML_FILE_EXTENSION = ".class.html"; //$NON-NLS-1$ + private static final String PACKAGE_HTML_FILE_EXTENSION = ".pkg.html"; //$NON-NLS-1$ + private static final String PLUGIN_HTML_FILE_EXTENSION = ".plugin.html"; //$NON-NLS-1$ + private static final String COMBINE_PLUGINS_FILE = "org/eclipse/wtp/releng/tools/component/xsl/combine-plugin2ref.xsl"; //$NON-NLS-1$ + private static final String COMBINE_PACKAGES_FILE = "org/eclipse/wtp/releng/tools/component/xsl/combine-pkg2ref.xsl"; //$NON-NLS-1$ + private static final String COMBINE_CLASSES_FILE = "org/eclipse/wtp/releng/tools/component/xsl/combine-class2ref.xsl"; //$NON-NLS-1$ + + // Class variables for reference usage constants + private static final int CLASS_USAGE = 0; + private static final int PACKAGE_USAGE = 1; + private static final int PLUGIN_USAGE = 2; - private class ComponentLead - { - public String leadName; - public TreeMap class2refCount; - public TreeMap pkg2refCount; - public TreeMap plugin2refCount; - - public ComponentLead(String leadName) - { - this.leadName = leadName; - class2refCount = new TreeMap(); - pkg2refCount = new TreeMap(); - plugin2refCount = new TreeMap(); - } + /** + * The ComponentTeam class manages the number of references per logical grouping of plugins at + * either the class, package, or plugin level. + */ + private class ComponentTeam { + private String teamName; + private TreeMap class2refCounts; + private TreeMap pkg2refCounts; + private TreeMap plugin2refCounts; + + /** + * Simple constructor + * @param aTeamName + */ + public ComponentTeam(String aTeamName) { + teamName = aTeamName; + } + + /** + * @return String component team's name + */ + public String getTeamName() { + return teamName; + } + + /** + * + * @return TreeMap of class reference counts + */ + public TreeMap getClassReferenceCounts() { + if (class2refCounts==null) + class2refCounts = new TreeMap(); + return class2refCounts; + } + + /** + * + * @return TreeMap of package reference counts + */ + public TreeMap getPackageReferenceCounts() { + if (pkg2refCounts==null) + pkg2refCounts = new TreeMap(); + return pkg2refCounts; + } + + /** + * + * @return TreeMap of plugin reference counts + */ + public TreeMap getPluginReferenceCounts() { + if (plugin2refCounts==null) + plugin2refCounts = new TreeMap(); + return plugin2refCounts; + } } - private class UsageCount - { + /** + * The UsageCount class is a simple caching mechanism to track API and non API usages. + */ + private class UsageCount { + /** + * Uses in an accordance with API contract + */ public int apiUse = 0; + /** + * Internal or other non API usages outside API contract + */ public int nonAPIUse = 0; } - public CombineClass2Reference() - { - chuck = new ComponentLead("Chuck"); - david = new ComponentLead("David"); - derping = new ComponentLead("Der Ping"); - chris = new ComponentLead("Chris"); - craig = new ComponentLead("Craig"); - tim = new ComponentLead("Tim"); - unknown = new ComponentLead("Unknown"); - plugin2complead = new HashMap(); - plugin2complead.put("org.eclipse..st.common.(?!env|snippet|uri).*", chuck); - plugin2complead.put("org.eclipse..st.common.env.*", chris); - plugin2complead.put("org.eclipse..st.common.snippet.*", david); - plugin2complead.put("org.eclipse..st.common.uri.*", craig); - plugin2complead.put("org.eclipse..st.j2ee.*", chuck); - plugin2complead.put("org.eclipse..st.ejb.*", chuck); - plugin2complead.put("org.eclipse..st.jsp.*", david); - plugin2complead.put("org.eclipse..st.server.*", tim); - plugin2complead.put("org.eclipse..st.servlet.*", chuck); - plugin2complead.put("org.eclipse..st.ws", chris); - plugin2complead.put("org.eclipse..st.ws[.].*", chris); - plugin2complead.put("org.eclipse..st.command.*", chris); - plugin2complead.put("org.eclipse..st.css.*", david); - plugin2complead.put("org.eclipse..st.dtd.*", david); - plugin2complead.put("org.eclipse..st.html.*", david); - plugin2complead.put("org.eclipse..st.javascript.*", david); - plugin2complead.put("org.eclipse..st.rdb.*", derping); - plugin2complead.put("org.eclipse..st.sse.*", david); - plugin2complead.put("org.eclipse..st.validation.*", chuck); - plugin2complead.put("org.eclipse..st.web.*", chuck); - plugin2complead.put("org.eclipse..st.wsdl.*", craig); - plugin2complead.put("org.eclipse..st.xml.*", david); - plugin2complead.put("org.eclipse..st.xsd.*", craig); - plugin2complead.put("org.eclipse..st.internet.*", tim); + /** + * Default Constructor + */ + public CombineClass2Reference() { + super(); } - public Collection getSrc() - { + /** + * @return Collection of source usage.xml files + */ + public Collection getSrc() { return src; } - public void setSrc(Collection src) - { + /** + * Cache the command line argument for where the source usage.xml files are + * @param src + */ + public void setSrc(Collection src) { this.src = src; } - public String getOutput() - { + /** + * @return String location of the output for the generated files + */ + public String getOutput() { return output; } - public void setOutput(String output) - { + /** + * Cache the command line argument output location for the generated files + * @param output + */ + public void setOutput(String output) { this.output = output; } - public Collection getApi() - { + /** + * @return Collection of API locations for component.xml files + */ + public Collection getApi() { return api; } - public void setApi(Collection api) - { + /** + * Cache the command line argument for where the API component.xml file locations are + * @param api + */ + public void setApi(Collection api) { this.api = api; } - public void execute() - { - // Collect component.xml files - Map pluginId2CompXML = new HashMap(); - if (api != null) - { - for (Iterator i = api.iterator(); i.hasNext();) - { - ILocation apiLocation = Location.createLocation(new File((String)i.next())); - ComponentXMLVisitor compXMLVisitor = new ComponentXMLVisitor(); - apiLocation.accept(compXMLVisitor); - for (Iterator it = compXMLVisitor.getCompXMLs().iterator(); it.hasNext();) - { - ComponentXML compXML = (ComponentXML)it.next(); - for (Iterator it2 = compXML.getPlugins().iterator(); it2.hasNext();) - { - pluginId2CompXML.put(((Plugin)it2.next()).getId(), compXML); - } - } - } - } - for (Iterator it = src.iterator(); it.hasNext();) - { + /** + * This is a helper method to create a map of plugin ids and associated component.xml files. + * These component.xml files are the one specified by the collection in the "api" command + * line argument. + * + * @return Map of plugin ids to component.xml files + */ + private Map collectComponentXMLFiles() { + Map pluginId2CompXML = new HashMap(); + if (getApi() != null) { + for (Iterator i = getApi().iterator(); i.hasNext();) { + // For each API file or location, create a Location object + ILocation apiLocation = Location.createLocation(new File((String)i.next())); + // Create a visitor to traverse the location and collect all contained component.xml files + ComponentXMLVisitor compXMLVisitor = new ComponentXMLVisitor(); + apiLocation.accept(compXMLVisitor); + // For each component.xml found, find the plugins it corresponds to + for (Iterator it = compXMLVisitor.getCompXMLs().iterator(); it.hasNext();) { + ComponentXML compXML = (ComponentXML)it.next(); + // For each plugin, add a mapping for the plugin to the component.xml file + for (Iterator it2 = compXML.getPlugins().iterator(); it2.hasNext();) { + pluginId2CompXML.put(((Plugin)it2.next()).getId(), compXML); + } + } + } + } + return pluginId2CompXML; + } + + /** + * This method drives the combination of the usages in the usage.xml files and based on the + * API information in the component.xml files in the given map, it will appropriately + * tabulate non-API and API usage information for class references, package references, and + * plugin references. + * + * @param pluginId2CompXML + */ + private void processUsages(Map pluginId2CompXML) { + // Iterate over all of the source usage.xml files provided by the "src" collection argument + for (Iterator it = getSrc().iterator(); it.hasNext();) { FileInputStream fis = null; - try - { + try { + // Open a file input stream on the current source usage.xml file String file = (String)it.next(); fis = new FileInputStream(file); + // Create a references object to parse the usage.xml file and cache the references References refs = new References(); refs.load(fis); - for (Iterator it2 = refs.getPluginRefs().iterator(); it2.hasNext();) - { + // Iterate through the list of plugins referenced in usage.xml file + for (Iterator it2 = refs.getPluginRefs().iterator(); it2.hasNext();) { PluginRef pluginRef = (PluginRef)it2.next(); String pluginId = pluginRef.getId(); + // Retrieve the corresponding component.xml file for the current plugin referenced ComponentXML compXML = (ComponentXML)pluginId2CompXML.get(pluginId); - ComponentLead compLead = unknown; - for (Iterator it3 = plugin2complead.keySet().iterator(); it3.hasNext();) - { - String regex = (String)it3.next(); - if (pluginId.matches(regex)) - { - compLead = (ComponentLead)plugin2complead.get(regex); - break; - } - } - for (Iterator it3 = pluginRef.getClassRefs().iterator(); it3.hasNext();) - { + // Get the corresponding component team from that plugin id + ComponentTeam compTeam = getComponentTeam(pluginId); + // Iterate through the class references in that plugin reference + for (Iterator it3 = pluginRef.getClassRefs().iterator(); it3.hasNext();) { ClassRef classRef = (ClassRef)it3.next(); - String name = classRef.getName(); - String pkgName = getPackageName(name); - UsageCount usageCount = getUsageCount(compXML, classRef, pkgName, name.substring(pkgName.length() + 1)); - UsageCount classUsageCount = (UsageCount)compLead.class2refCount.get(name); - if (classUsageCount == null) - { - classUsageCount = new UsageCount(); - } - classUsageCount.apiUse += usageCount.apiUse; - classUsageCount.nonAPIUse += usageCount.nonAPIUse; - compLead.class2refCount.put(name, classUsageCount); - UsageCount pkgUsageCount = (UsageCount)compLead.pkg2refCount.get(pkgName); - if (pkgUsageCount == null) - { - pkgUsageCount = new UsageCount(); - } - pkgUsageCount.apiUse += usageCount.apiUse; - pkgUsageCount.nonAPIUse += usageCount.nonAPIUse; - compLead.pkg2refCount.put(pkgName, pkgUsageCount); - UsageCount pluginUsageCount = (UsageCount)compLead.plugin2refCount.get(pluginId); - if (pluginUsageCount == null) - { - pluginUsageCount = new UsageCount(); - } - pluginUsageCount.apiUse += usageCount.apiUse; - pluginUsageCount.nonAPIUse += usageCount.nonAPIUse; - compLead.plugin2refCount.put(pluginId, pluginUsageCount); + // Update the component team's cached reference counts with the current class reference + updateComponentTeamUsageCounts(compTeam,compXML,classRef,pluginId); } } - } - catch (Throwable t) - { + } catch (Throwable t) { throw new RuntimeException(t); - } - finally - { - if (fis != null) - { - try - { + } finally { + // Close the current file input stream + if (fis != null) { + try { fis.close(); - } - catch (IOException ioe) - { - } + } catch (IOException ioe) {} } } } - genCSV(); - genHTML(); + } + + /** + * The execute method drives the combination operation by collecting component.xml files + * from the "api" command line arugment, processing references in usage.xml files from the + * "src" command line argument, and then writing them out in CSV and HTML file format to + * a location specified by the "output" command line argument. + */ + private void execute() { + // Collect the plugin to component.xml file map from the specified collection of + // component.xml files in the "api" command line argument. + Map pluginId2CompXML = collectComponentXMLFiles(); + // Process the usages in the usage.xml files provided by the "src" collection command arg + processUsages(pluginId2CompXML); + // Generate the output files for combined usage in CSV format + generateCSVFiles(); + // Generate the output files for combined usage in HTML format + generateHTMLFiles(); + } + + /** + * Helper method to update the passed in component team's cached usage counts based on the + * given class reference and known API's in the provided component.xml file. + * + * @param compTeam + * @param compXML + * @param classRef + * @param pluginId + */ + private void updateComponentTeamUsageCounts(ComponentTeam compTeam, ComponentXML compXML, ClassRef classRef, String pluginId) { + String name = classRef.getName(); + String pkgName = getPackageName(name); + // Get the usage count object for the current referenced class + UsageCount usageCount = getUsageCount(compXML, classRef, pkgName, name.substring(pkgName.length() + 1)); + // Get the component team's cached class reference usage count + UsageCount classUsageCount = (UsageCount)compTeam.getClassReferenceCounts().get(name); + if (classUsageCount == null) + classUsageCount = new UsageCount(); + // Update the component team's cached class usage count with the current usage count + classUsageCount.apiUse += usageCount.apiUse; + classUsageCount.nonAPIUse += usageCount.nonAPIUse; + // Put the updated class Usage count back into the cache on the component team + compTeam.getClassReferenceCounts().put(name, classUsageCount); + // Get the component team's cached package reference usage count + UsageCount pkgUsageCount = (UsageCount)compTeam.getPackageReferenceCounts().get(pkgName); + if (pkgUsageCount == null) + pkgUsageCount = new UsageCount(); + // Update the component team's cached package reference count with current usage count + pkgUsageCount.apiUse += usageCount.apiUse; + pkgUsageCount.nonAPIUse += usageCount.nonAPIUse; + // Put the updated package usage count back into the cache on the component team + compTeam.getPackageReferenceCounts().put(pkgName, pkgUsageCount); + // Get the component team's cached plugin reference usage count + UsageCount pluginUsageCount = (UsageCount)compTeam.getPluginReferenceCounts().get(pluginId); + if (pluginUsageCount == null) + pluginUsageCount = new UsageCount(); + // Update the component team's cached plugin reference count with current usage count + pluginUsageCount.apiUse += usageCount.apiUse; + pluginUsageCount.nonAPIUse += usageCount.nonAPIUse; + // Put the update plugin usage count back into the cache on the component team + compTeam.getPluginReferenceCounts().put(pluginId, pluginUsageCount); + } + + /** + * Helper method to find the corresponding component team for a given plugin id. If one is + * not found, the unknown component is returned. + * + * @param pluginId + * @return ComponentTeam for specified plugin + */ + private ComponentTeam getComponentTeam(String pluginId) { + // Try and match the proper component team to the referenced plugin from the + // map of plugins to component teams + for (Iterator it3 = getPluginComponentMap().keySet().iterator(); it3.hasNext();) { + String regex = (String)it3.next(); + if (pluginId.matches(regex)) { + return (ComponentTeam)getPluginComponentMap().get(regex); + } + } + // Otherwise return the unknown component + return getUnknownComponent(); } - private UsageCount getUsageCount(ComponentXML compXML, ClassRef classRef, String pkgName, String localName) - { - UsageCount usageCount = new UsageCount(); - int refCount = classRef.getRefCount(); - int subclassCount = classRef.getSubclassCount(); - int implCount = classRef.getImplementCount(); - int instantiateCount = classRef.getInstantiateCount(); - if (compXML == null) - { - usageCount.nonAPIUse = refCount + subclassCount + implCount + instantiateCount; - return usageCount; + /** + * This method will cache and return the usages of the class reference into a UsageCount object. + * It will check the passed in component.xml to see if the reference is using a qualified API + * or if it is an internal usage. The types of references which qualify are class references, + * subclasses, implementers, or instantiators. + * + * @param compXML + * @param classRef + * @param pkgName + * @param localName + * @return UsageCount + */ + private UsageCount getUsageCount(ComponentXML compXML, ClassRef classRef, String pkgName, String localName) { + // Create UsageCount object + UsageCount usageCount = new UsageCount(); + int refCount = classRef.getRefCount(); + int subclassCount = classRef.getSubclassCount(); + int implCount = classRef.getImplementCount(); + int instantiateCount = classRef.getInstantiateCount(); + // If the component.xml is null, or the package referenced does not exist in the component.xml + // we know the reference cannot be an API use, so just add all the references to the non-API + // count and return. + if (compXML==null || compXML.getPackage(pkgName)==null) { + usageCount.nonAPIUse = refCount + subclassCount + implCount + instantiateCount; + return usageCount; + } + // Get the referenced package from the component.xml file + Package pkg = compXML.getPackage(pkgName); + // Get the references type from the references pckage in the component.xml file + Type type = pkg.getType(localName); + if (type == null) { + // If the type is null, but the package is an API package, update the API count + if (pkg.isApi()) { + usageCount.apiUse = refCount + subclassCount + implCount + instantiateCount; + return usageCount; + } + // If the type is null, but the package is not API, update the non-API count + usageCount.nonAPIUse = refCount + subclassCount + implCount + instantiateCount; + return usageCount; + } + // Handle the cases where we have a valid component.xml package and type. + // If the type is not a valid API reference, increment the non-API ref count + if (!type.isReference()) { + usageCount.nonAPIUse += classRef.getRefCount(); + } else { + // Otherwise, increment API ref count + usageCount.apiUse += classRef.getRefCount(); } - Package pkg = compXML.getPackage(pkgName); - if (pkg == null) - { - usageCount.nonAPIUse = refCount + subclassCount + implCount + instantiateCount; - return usageCount; + // If the type is not a valid API subclass, increment the non-API subclass count + if (!type.isSubclass()) { + usageCount.nonAPIUse += classRef.getSubclassCount(); + } else { + // Otherwise, increment API subclass count + usageCount.apiUse += classRef.getSubclassCount(); } - else - { - Type type = pkg.getType(localName); - if (type == null) - { - if (pkg.isApi()) - { - usageCount.apiUse = refCount + subclassCount + implCount + instantiateCount; - return usageCount; - } - else - { - usageCount.nonAPIUse = refCount + subclassCount + implCount + instantiateCount; - return usageCount; - } - } - else - { - if (!type.isReference()) - { - usageCount.nonAPIUse += classRef.getRefCount(); - } - else - { - usageCount.apiUse += classRef.getRefCount(); - } - if (!type.isSubclass()) - { - usageCount.nonAPIUse += classRef.getSubclassCount(); - } - else - { - usageCount.apiUse += classRef.getSubclassCount(); - } - if (!type.isImplement()) - { - usageCount.nonAPIUse += classRef.getImplementCount(); - } - else - { - usageCount.apiUse += classRef.getImplementCount(); - } - if (!type.isInstantiate()) - { - usageCount.nonAPIUse += classRef.getInstantiateCount(); - } - else - { - usageCount.apiUse += classRef.getInstantiateCount(); - } - return usageCount; - } + // If the type is not a valid API implementor, increment the non-API implementor count + if (!type.isImplement()) { + usageCount.nonAPIUse += classRef.getImplementCount(); + } else { + // Otherwise increment API implementor count + usageCount.apiUse += classRef.getImplementCount(); + } + // If the type is not a valid API instantiation, increment the non-API instantiation count + if (!type.isInstantiate()) { + usageCount.nonAPIUse += classRef.getInstantiateCount(); + } else { + // Otherwise increment API instantation count + usageCount.apiUse += classRef.getInstantiateCount(); } + // Return the usage count + return usageCount; } - private String getPackageName(String className) - { + /** + * Given a fully qualified class name, return the package name. + * + * @param a fully qualified className + * @return the package name + */ + private String getPackageName(String className) { int i = className.lastIndexOf('.'); - if (i != -1) - { + if (i != -1) return className.substring(0, i); - } - else - { - return ""; - } + return ""; //$NON-NLS-1$ } - public void genCSV() - { + /** + * Generate CSV format files displaying the combined internal and API usage for classes, + * packages, and plugins. + */ + private void generateCSVFiles() { FileWriter classWriter = null; FileWriter pkgWriter = null; FileWriter pluginWriter = null; - try - { - classWriter = new FileWriter(output + ".class.csv"); - pkgWriter = new FileWriter(output + ".pkg.csv"); - pluginWriter = new FileWriter(output + ".plugin.csv"); - writeCompLeadCSV(chuck, classWriter, pkgWriter, pluginWriter); - writeCompLeadCSV(david, classWriter, pkgWriter, pluginWriter); - writeCompLeadCSV(derping, classWriter, pkgWriter, pluginWriter); - writeCompLeadCSV(chris, classWriter, pkgWriter, pluginWriter); - writeCompLeadCSV(craig, classWriter, pkgWriter, pluginWriter); - writeCompLeadCSV(tim, classWriter, pkgWriter, pluginWriter); - writeCompLeadCSV(unknown, classWriter, pkgWriter, pluginWriter); - } - catch (IOException ioe) - { + try { + // Create the file writers + classWriter = new FileWriter(getOutput() + CLASS_CVS_FILE_EXTENSION); + pkgWriter = new FileWriter(getOutput() + PACKAGE_CVS_FILE_EXTENSION); + pluginWriter = new FileWriter(getOutput() + PLUGIN_CVS_FILE_EXTENSION); + // For each component team, write the CVS file content for the component's usage + for (int i=0; i<getComponentTeams().size(); i++) { + ComponentTeam compTeam = (ComponentTeam) getComponentTeams().get(i); + writeCompTeamCSV(compTeam, classWriter, pkgWriter, pluginWriter); + } + } catch (IOException ioe) { throw new RuntimeException(ioe); - } - finally - { - if (classWriter != null) - { - try - { + } finally { + // Close the class usage file writer + if (classWriter != null) { + try { classWriter.close(); - } - catch (IOException ioe) - { - } + } catch (IOException ioe) {} } - if (pkgWriter != null) - { - try - { + // Close the package usage file writer + if (pkgWriter != null) { + try { pkgWriter.close(); - } - catch (IOException ioe) - { - } + } catch (IOException ioe) {} } - if (pluginWriter != null) - { - try - { + // Close the plugin usage file writer + if (pluginWriter != null) { + try { pluginWriter.close(); - } - catch (IOException ioe) - { - } + } catch (IOException ioe) {} } } } - private void writeCompLeadCSV(ComponentLead compLead, Writer classWriter, Writer pkgWriter, Writer pluginWriter) throws IOException - { - classWriter.write(compLead.leadName); - classWriter.write("\n"); - for (Iterator it = compLead.class2refCount.keySet().iterator(); it.hasNext();) - { - String name = (String)it.next(); - UsageCount usageCount = (UsageCount)compLead.class2refCount.get(name); - classWriter.write(name); - classWriter.write(","); - classWriter.write(String.valueOf(usageCount.nonAPIUse)); - classWriter.write(","); - classWriter.write(String.valueOf(usageCount.apiUse)); - classWriter.write("\n"); - } - classWriter.write("\n"); - - pkgWriter.write(compLead.leadName); - pkgWriter.write("\n"); - for (Iterator it = compLead.pkg2refCount.keySet().iterator(); it.hasNext();) - { - String name = (String)it.next(); - UsageCount usageCount = (UsageCount)compLead.pkg2refCount.get(name); - pkgWriter.write(name); - pkgWriter.write(","); - pkgWriter.write(String.valueOf(usageCount.nonAPIUse)); - pkgWriter.write(","); - pkgWriter.write(String.valueOf(usageCount.apiUse)); - pkgWriter.write("\n"); - } - pkgWriter.write("\n"); - pluginWriter.write(compLead.leadName); - pluginWriter.write("\n"); - for (Iterator it = compLead.plugin2refCount.keySet().iterator(); it.hasNext();) - { - String id = (String)it.next(); - UsageCount usageCount = (UsageCount)compLead.plugin2refCount.get(id); - pluginWriter.write(id); - pluginWriter.write(","); - pluginWriter.write(String.valueOf(usageCount.nonAPIUse)); - pluginWriter.write(","); - pluginWriter.write(String.valueOf(usageCount.apiUse)); - pluginWriter.write("\n"); - } - pluginWriter.write("\n"); + /** + * This method drives the creation of the CSV file contents for the given component team + * and corresponding writers. + * + * @param compTeam + * @param classWriter + * @param pkgWriter + * @param pluginWriter + * @throws IOException + */ + private void writeCompTeamCSV(ComponentTeam compTeam, Writer classWriter, Writer pkgWriter, Writer pluginWriter) throws IOException { + writeCompTeamCSV(compTeam,classWriter,CLASS_USAGE,compTeam.getClassReferenceCounts()); + writeCompTeamCSV(compTeam,pkgWriter,PACKAGE_USAGE,compTeam.getPackageReferenceCounts()); + writeCompTeamCSV(compTeam,pluginWriter,PLUGIN_USAGE,compTeam.getPluginReferenceCounts()); + } + + /** + * This is a helper method to write out the given component team's reference counts in + * a CSV file format. + * + * @param compTeam + * @param writer + * @param usage + * @param referenceCounts + * @throws IOException + */ + private void writeCompTeamCSV(ComponentTeam compTeam, Writer writer, int usage, TreeMap referenceCounts) throws IOException { + // Write the team name + writer.write(compTeam.getTeamName()); + writer.write(LINE_BREAK); + // For each name key, retrieve the corresponding usage count values + for (Iterator it = referenceCounts.keySet().iterator(); it.hasNext();) { + String name = (String)it.next(); + UsageCount usageCount = null; + switch (usage) { + case 0: + usageCount = (UsageCount)compTeam.getClassReferenceCounts().get(name); + break; + case 1: + usageCount = (UsageCount)compTeam.getPackageReferenceCounts().get(name); + break; + case 2: + usageCount = (UsageCount)compTeam.getPluginReferenceCounts().get(name); + break; + } + // Write out the internal and api usages + writer.write(name); + writer.write(COMMA); + writer.write(String.valueOf(usageCount.nonAPIUse)); + writer.write(COMMA); + writer.write(String.valueOf(usageCount.apiUse)); + writer.write(LINE_BREAK); + } + writer.write(LINE_BREAK); } - public void genHTML() - { - try - { + /** + * Generate the HTML files for comibined usage ref counts of classes, packages, and plugins. + */ + private void generateHTMLFiles() { + try { + // Create a new output stream ByteArrayOutputStream baos = new ByteArrayOutputStream(); - baos.write("<root>".getBytes()); - writeCompLead(chuck, baos); - writeCompLead(david, baos); - writeCompLead(derping, baos); - writeCompLead(chris, baos); - writeCompLead(craig, baos); - writeCompLead(tim, baos); - writeCompLead(unknown, baos); - baos.write("</root>".getBytes()); + baos.write(HTML_ROOT_BEGIN.getBytes()); + // For each component team, write out the combined component team specific usage data + for (int i=0; i<getComponentTeams().size(); i++) { + ComponentTeam compTeam = (ComponentTeam) getComponentTeams().get(i); + writeCompTeam(compTeam, baos); + } + // Close the output stream + baos.write(HTML_ROOT_END.getBytes()); baos.close(); + // Create a byte array from the output stream contents byte[] content = baos.toByteArray(); - XSLUtil.transform - ( - ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-plugin2ref.xsl"), + // Write out the byte array to a file output stream for the plugin references + XSLUtil.transform ( + ClassLoader.getSystemResourceAsStream(COMBINE_PLUGINS_FILE), new ByteArrayInputStream(content), - new FileOutputStream(output + ".plugin.html") + new FileOutputStream(getOutput() + PLUGIN_HTML_FILE_EXTENSION) ); - XSLUtil.transform - ( - ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-pkg2ref.xsl"), + // Write out the byte array to a file output stream for the package references + XSLUtil.transform ( + ClassLoader.getSystemResourceAsStream(COMBINE_PACKAGES_FILE), new ByteArrayInputStream(content), - new FileOutputStream(output + ".pkg.html") + new FileOutputStream(getOutput() + PACKAGE_HTML_FILE_EXTENSION) ); - XSLUtil.transform - ( - ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-class2ref.xsl"), + // Write out the byte array to a file output stream for the class references + XSLUtil.transform ( + ClassLoader.getSystemResourceAsStream(COMBINE_CLASSES_FILE), new ByteArrayInputStream(content), - new FileOutputStream(output + ".class.html") + new FileOutputStream(getOutput() + CLASS_HTML_FILE_EXTENSION) ); - } - catch (Throwable t) - { + } catch (Throwable t) { t.printStackTrace(); throw new RuntimeException(t); } } - private void writeCompLead(ComponentLead compLead, ByteArrayOutputStream baos) throws IOException - { - baos.write("<team lead=\"".getBytes()); - baos.write(compLead.leadName.getBytes()); - baos.write("\">".getBytes()); - for (Iterator it = compLead.plugin2refCount.keySet().iterator(); it.hasNext();) - { - String id = (String)it.next(); - UsageCount usageCount = (UsageCount)compLead.plugin2refCount.get(id); - baos.write("<plugin id=\"".getBytes()); - baos.write(id.getBytes()); - baos.write("\" api=\"".getBytes()); - baos.write(String.valueOf(usageCount.apiUse).getBytes()); - baos.write("\" internal=\"".getBytes()); - baos.write(String.valueOf(usageCount.nonAPIUse).getBytes()); - baos.write("\"/>".getBytes()); - } - for (Iterator it = compLead.pkg2refCount.keySet().iterator(); it.hasNext();) - { - String name = (String)it.next(); - UsageCount usageCount = (UsageCount)compLead.pkg2refCount.get(name); - baos.write("<package name=\"".getBytes()); - baos.write(name.getBytes()); - baos.write("\" api=\"".getBytes()); - baos.write(String.valueOf(usageCount.apiUse).getBytes()); - baos.write("\" internal=\"".getBytes()); - baos.write(String.valueOf(usageCount.nonAPIUse).getBytes()); - baos.write("\"/>".getBytes()); - } - for (Iterator it = compLead.class2refCount.keySet().iterator(); it.hasNext();) - { - String name = (String)it.next(); - UsageCount usageCount = (UsageCount)compLead.class2refCount.get(name); - baos.write("<class name=\"".getBytes()); - baos.write(name.getBytes()); - baos.write("\" api=\"".getBytes()); - baos.write(String.valueOf(usageCount.apiUse).getBytes()); - baos.write("\" internal=\"".getBytes()); - baos.write(String.valueOf(usageCount.nonAPIUse).getBytes()); - baos.write("\"/>".getBytes()); - } - baos.write("</team>".getBytes()); + /** + * This method will drive the combined output file html contents for the given component + * team. + * + * @param compTeam + * @param baos + * @throws IOException + */ + private void writeCompTeam(ComponentTeam compTeam, ByteArrayOutputStream baos) throws IOException { + baos.write("<team lead=\"".getBytes()); //$NON-NLS-1$ + baos.write(compTeam.getTeamName().getBytes()); + baos.write("\">".getBytes()); //$NON-NLS-1$ + writeCompTeam(compTeam,baos,CLASS_USAGE,compTeam.getClassReferenceCounts()); + writeCompTeam(compTeam,baos,PACKAGE_USAGE,compTeam.getPackageReferenceCounts()); + writeCompTeam(compTeam,baos,PLUGIN_USAGE,compTeam.getPluginReferenceCounts()); + baos.write("</team>".getBytes()); //$NON-NLS-1$ + } + + /** + * This method will write in html format the combined output for the given component team for + * the non API and API usage for the class usage file, the package usage file, and the plugin + * usage file. + * + * @param compTeam ComponentTeam + * @param baos ByteArrayOutputStream + * @param usage class, package, or plugin? + * @param referenceCounts TreeMap + * @throws IOException + */ + private void writeCompTeam(ComponentTeam compTeam, ByteArrayOutputStream baos, int usage, TreeMap referenceCounts) throws IOException { + // Iterate over the name keys of the references tree map + for (Iterator it = referenceCounts.keySet().iterator(); it.hasNext();) { + String name = (String)it.next(); + UsageCount usageCount = null; + // Retrieve the appropriate UsageCount from the map given the current name key + switch (usage) { + // Class reference + case 0: + usageCount = (UsageCount)compTeam.getClassReferenceCounts().get(name); + baos.write("<class name=\"".getBytes()); //$NON-NLS-1$ + break; + // Package reference + case 1: + usageCount = (UsageCount)compTeam.getPackageReferenceCounts().get(name); + baos.write("<package name=\"".getBytes()); //$NON-NLS-1$ + break; + // Plugin reference + case 2: + usageCount = (UsageCount)compTeam.getPluginReferenceCounts().get(name); + baos.write("<plugin id=\"".getBytes()); //$NON-NLS-1$ + break; + } + // Write the api and internal usage for the current reference + baos.write(name.getBytes()); + baos.write("\" api=\"".getBytes()); //$NON-NLS-1$ + baos.write(String.valueOf(usageCount.apiUse).getBytes()); + baos.write("\" internal=\"".getBytes()); //$NON-NLS-1$ + baos.write(String.valueOf(usageCount.nonAPIUse).getBytes()); + baos.write("\"/>".getBytes()); //$NON-NLS-1$ + } } - public static void main(String[] args) - { + /** + * This is the static main method used for launching this reference usage combination + * application. + * + * @param args + */ + public static void main(String[] args) { + // Use the command option parser to parse the command line arguments CommandOptionParser optionParser = new CommandOptionParser(args); Map options = optionParser.getOptions(); - Collection src = (Collection)options.get("src"); - Collection output = (Collection)options.get("output"); - Collection api = (Collection)options.get("api"); - if (src == null || output == null || src.isEmpty() || output.isEmpty()) - { + Collection src = (Collection)options.get(ARG_SOURCE); + Collection output = (Collection)options.get(ARG_OUTPUT); + Collection api = (Collection)options.get(ARG_API); + // If the usage is improper or arguments are not valid, prompt for proper usage + if (src == null || output == null || src.isEmpty() || output.isEmpty()) { printUsage(); System.exit(-1); } - + // Create a new instance of the class and set the command line argument values CombineClass2Reference class2Ref = new CombineClass2Reference(); class2Ref.setSrc(src); class2Ref.setOutput((String)output.iterator().next()); class2Ref.setApi(api); + // Execute the combination method class2Ref.execute(); } - private static void printUsage() - { - System.out.println("Usage: java org.eclipse.wtp.releng.tools.component.adopters.CombineClass2Reference -src <src> -output <output>"); - System.out.println(""); - System.out.println("\t-src\t\t<src>\t\tlocation of your usage reports"); - System.out.println("\t-output\t<output>\t\tlocation of the output file"); - System.out.println("\t-api\t\t<api>\t\tlocation of your component.xml"); + /** + * This is a helper method to the user to print out an error message of the proper usage of + * the arguments to be passed and the location of the output files. + * See IOutputConstants for messages. + */ + private static void printUsage() { + System.out.println(PRINT_USAGE_COMBINED); + System.out.println(""); //$NON-NLS-1$ + System.out.println(PRINT_SOURCE_LOCATION); + System.out.println(PRINT_OUTPUT_LOCATION); + System.out.println(PRINT_COMPONENT_XML_API_LOCATION); + } + + /** + * @return the list of component teams + */ + private List getComponentTeams() { + if (componentTeams == null) + initializeComponentTeams(); + return componentTeams; + } + + /** + * Initialize the component teams list with the appropriate regular expressions to link plugins + * to the known set of component teams. + */ + private void initializeComponentTeams() { + componentTeams = new ArrayList(); + + // Create the JEE team with associated plugins + ComponentTeam javaEE = new ComponentTeam(COMPONENT_TEAM_JAVA_EE); + getPluginComponentMap().put(PLUGIN_EXPRESSION_J2EE, javaEE); + getPluginComponentMap().put(PLUGIN_EXPRESSION_EJB, javaEE); + getPluginComponentMap().put(PLUGIN_EXPRESSION_SERVLET, javaEE); + getPluginComponentMap().put(PLUGIN_EXPRESSION_WEB, javaEE); + componentTeams.add(javaEE); + + // Create the Common team with associated plugins + ComponentTeam common = new ComponentTeam(COMPONENT_TEAM_COMMON); + getPluginComponentMap().put(PLUGIN_EXPRESSION_COMMAND, common); + getPluginComponentMap().put(PLUGIN_EXPRESSION_COMMON, common); + getPluginComponentMap().put(PLUGIN_EXPRESSION_VALIDATION, common); + componentTeams.add(common); + + // Create the Editors team with associated plugins + ComponentTeam editors = new ComponentTeam(COMPONENT_TEAM_EDITORS); + getPluginComponentMap().put(PLUGIN_EXPRESSION_JSP, editors); + getPluginComponentMap().put(PLUGIN_EXPRESSION_CSS, editors); + getPluginComponentMap().put(PLUGIN_EXPRESSION_DTD, editors); + getPluginComponentMap().put(PLUGIN_EXPRESSION_HTML, editors); + getPluginComponentMap().put(PLUGIN_EXPRESSION_JAVASCRIPT, editors); + getPluginComponentMap().put(PLUGIN_EXPRESSION_SSE, editors); + getPluginComponentMap().put(PLUGIN_EXPRESSION_XML, editors); + getPluginComponentMap().put(PLUGIN_EXPRESSION_XSD, editors); + componentTeams.add(editors); + + // Create the RDB team with associated plugins + ComponentTeam rdb = new ComponentTeam(COMPONENT_TEAM_RDB); + getPluginComponentMap().put(PLUGIN_EXPRESSION_RDB, rdb); + componentTeams.add(rdb); + + // Create the Web Services team with associated plugins + ComponentTeam webServices = new ComponentTeam(COMPONENT_TEAM_WEB_SERVICES); + getPluginComponentMap().put(PLUGIN_EXPRESSION_WS, webServices); + getPluginComponentMap().put(PLUGIN_EXPRESSION_WSDL, webServices); + componentTeams.add(webServices); + + // Create the Server team with associated plugins + ComponentTeam server = new ComponentTeam(COMPONENT_TEAM_SERVER); + getPluginComponentMap().put(PLUGIN_EXPRESSION_SERVER, server); + getPluginComponentMap().put(PLUGIN_EXPRESSION_INTERNET, server); + componentTeams.add(server); + + // Add a "team" for the unknown references + ComponentTeam unknown = new ComponentTeam(COMPONENT_TEAM_UNKNOWN); + componentTeams.add(unknown); + } + + /** + * @return HashMap of plugin keys and associated component team values + */ + private HashMap getPluginComponentMap() { + if (plugin2compTeam==null) { + plugin2compTeam = new HashMap(); + initializeComponentTeams(); + } + return plugin2compTeam; + } + + /** + * @return the unknown component where a plugin's logical component is not known + */ + private ComponentTeam getUnknownComponent() { + for (int i=0; i<getComponentTeams().size(); i++) { + ComponentTeam compTeam = (ComponentTeam) getComponentTeams().get(i); + if (compTeam.getTeamName().equals(COMPONENT_TEAM_UNKNOWN)) + return compTeam; + } + return null; } } \ No newline at end of file diff --git a/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/IComponentConstants.java b/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/IComponentConstants.java new file mode 100644 index 000000000..bb0390c2f --- /dev/null +++ b/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/IComponentConstants.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wtp.releng.tools.component.adopters; + +public interface IComponentConstants { + // ComponentTeam Names + public static final String COMPONENT_TEAM_JAVA_EE = "Java EE"; //$NON-NLS-1$ + public static final String COMPONENT_TEAM_COMMON = "Common"; //$NON-NLS-1$ + public static final String COMPONENT_TEAM_EDITORS = "Editors"; //$NON-NLS-1$ + public static final String COMPONENT_TEAM_RDB = "RDB"; //$NON-NLS-1$ + public static final String COMPONENT_TEAM_WEB_SERVICES = "Web Services"; //$NON-NLS-1$ + public static final String COMPONENT_TEAM_SERVER = "Server"; //$NON-NLS-1$ + public static final String COMPONENT_TEAM_UNKNOWN = "Unknown"; //$NON-NLS-1$ + + // General Expressions for plugin name matching + public static final String PLUGIN_EXPRESSION_J2EE = "org.eclipse..st.j2ee.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_EJB = "org.eclipse..st.ejb.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_SERVLET = "org.eclipse..st.servlet.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_WEB = "org.eclipse..st.web.*"; //$NON-NLS-1$ + + public static final String PLUGIN_EXPRESSION_COMMAND = "org.eclipse..st.command.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_COMMON = "org.eclipse..st.common.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_VALIDATION = "org.eclipse..st.validation.*"; //$NON-NLS-1$ + + public static final String PLUGIN_EXPRESSION_JSP = "org.eclipse..st.jsp.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_CSS = "org.eclipse..st.css.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_DTD = "org.eclipse..st.dtd.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_HTML = "org.eclipse..st.html.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_JAVASCRIPT = "org.eclipse..st.javascript.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_SSE = "org.eclipse..st.sse.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_XML = "org.eclipse..st.xml.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_XSD = "org.eclipse..st.xsd.*"; //$NON-NLS-1$ + + public static final String PLUGIN_EXPRESSION_RDB = "org.eclipse..st.rdb.*"; //$NON-NLS-1$ + + public static final String PLUGIN_EXPRESSION_WS = "org.eclipse..st.ws.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_WSDL = "org.eclipse..st.wsdl.*"; //$NON-NLS-1$ + + public static final String PLUGIN_EXPRESSION_SERVER = "org.eclipse..st.server.*"; //$NON-NLS-1$ + public static final String PLUGIN_EXPRESSION_INTERNET = "org.eclipse..st.internet.*"; //$NON-NLS-1$ + + + +} diff --git a/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/IOutputConstants.java b/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/IOutputConstants.java new file mode 100644 index 000000000..671b59527 --- /dev/null +++ b/archive/releng.builder/tools/apitools/org.eclipse.wtp.releng.tools.component.core/src/org/eclipse/wtp/releng/tools/component/adopters/IOutputConstants.java @@ -0,0 +1,16 @@ +package org.eclipse.wtp.releng.tools.component.adopters; + +public interface IOutputConstants { + + public static final String LINE_BREAK = "\n"; //$NON-NLS-1$ + public static final String COMMA = ","; //$NON-NLS-1$ + + public static final String HTML_ROOT_BEGIN = "<root>"; //$NON-NLS-1$ + public static final String HTML_ROOT_END = "</root>" ; //$NON-NLS-1$ + + public static final String PRINT_SOURCE_LOCATION = "\t-src\t\t<src>\t\tlocation of your usage reports"; //$NON-NLS-1$ + public static final String PRINT_OUTPUT_LOCATION = "\t-output\t<output>\t\tlocation of the output file"; //$NON-NLS-1$ + public static final String PRINT_COMPONENT_XML_API_LOCATION = "\t-api\t\t<api>\t\tlocation of your component.xml"; //$NON-NLS-1$ + public static final String PRINT_USAGE_COMBINED = "Usage: java org.eclipse.wtp.releng.tools.component.adopters.CombineClass2Reference -src <src> -output <output>"; //$NON-NLS-1$ + +} -- GitLab