Skip to content
Snippets Groups Projects
Commit 590375cf authored by jlanuti's avatar jlanuti
Browse files

Refactored CombineClass2Reference for usability, documentation, and reuse

parent 4888107d
No related branches found
No related tags found
No related merge requests found
...@@ -18,9 +18,11 @@ import java.io.FileOutputStream; ...@@ -18,9 +18,11 @@ import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import org.eclipse.wtp.releng.tools.component.ILocation; import org.eclipse.wtp.releng.tools.component.ILocation;
...@@ -33,510 +35,737 @@ import org.eclipse.wtp.releng.tools.component.model.Type; ...@@ -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.util.CommandOptionParser;
import org.eclipse.wtp.releng.tools.component.xsl.XSLUtil; import org.eclipse.wtp.releng.tools.component.xsl.XSLUtil;
public class CombineClass2Reference /**
{ * This class is used to take the output usage.xml files generated from SimpleClass2Reference
private HashMap plugin2complead; * and combine them into a helpful CSV or HTML file format displaying API and non-API internal
ComponentLead chuck; * usage for each logically grouped component team in WTP. The main method can be invoked with
ComponentLead david; * the following three command line arguments:
ComponentLead derping; *
ComponentLead chris; * "src" = the zip or folder location of the usage.xml files
ComponentLead craig; * "api" = the zip or folder location of the component.xml files defining the API base
ComponentLead tim; * "output" = the location of the output csv and html combined files
ComponentLead unknown; */
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 Collection src;
private String output; private String output;
private Collection api; 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 /**
{ * The ComponentTeam class manages the number of references per logical grouping of plugins at
public String leadName; * either the class, package, or plugin level.
public TreeMap class2refCount; */
public TreeMap pkg2refCount; private class ComponentTeam {
public TreeMap plugin2refCount; private String teamName;
private TreeMap class2refCounts;
public ComponentLead(String leadName) private TreeMap pkg2refCounts;
{ private TreeMap plugin2refCounts;
this.leadName = leadName;
class2refCount = new TreeMap(); /**
pkg2refCount = new TreeMap(); * Simple constructor
plugin2refCount = new TreeMap(); * @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; public int apiUse = 0;
/**
* Internal or other non API usages outside API contract
*/
public int nonAPIUse = 0; public int nonAPIUse = 0;
} }
public CombineClass2Reference() /**
{ * Default Constructor
chuck = new ComponentLead("Chuck"); */
david = new ComponentLead("David"); public CombineClass2Reference() {
derping = new ComponentLead("Der Ping"); super();
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);
} }
public Collection getSrc() /**
{ * @return Collection of source usage.xml files
*/
public Collection getSrc() {
return src; 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; this.src = src;
} }
public String getOutput() /**
{ * @return String location of the output for the generated files
*/
public String getOutput() {
return output; 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; this.output = output;
} }
public Collection getApi() /**
{ * @return Collection of API locations for component.xml files
*/
public Collection getApi() {
return api; 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; this.api = api;
} }
public void execute() /**
{ * This is a helper method to create a map of plugin ids and associated component.xml files.
// Collect component.xml files * These component.xml files are the one specified by the collection in the "api" command
Map pluginId2CompXML = new HashMap(); * line argument.
if (api != null) *
{ * @return Map of plugin ids to component.xml files
for (Iterator i = api.iterator(); i.hasNext();) */
{ private Map collectComponentXMLFiles() {
ILocation apiLocation = Location.createLocation(new File((String)i.next())); Map pluginId2CompXML = new HashMap();
ComponentXMLVisitor compXMLVisitor = new ComponentXMLVisitor(); if (getApi() != null) {
apiLocation.accept(compXMLVisitor); for (Iterator i = getApi().iterator(); i.hasNext();) {
for (Iterator it = compXMLVisitor.getCompXMLs().iterator(); it.hasNext();) // For each API file or location, create a Location object
{ ILocation apiLocation = Location.createLocation(new File((String)i.next()));
ComponentXML compXML = (ComponentXML)it.next(); // Create a visitor to traverse the location and collect all contained component.xml files
for (Iterator it2 = compXML.getPlugins().iterator(); it2.hasNext();) ComponentXMLVisitor compXMLVisitor = new ComponentXMLVisitor();
{ apiLocation.accept(compXMLVisitor);
pluginId2CompXML.put(((Plugin)it2.next()).getId(), compXML); // 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();) {
for (Iterator it = src.iterator(); it.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; FileInputStream fis = null;
try try {
{ // Open a file input stream on the current source usage.xml file
String file = (String)it.next(); String file = (String)it.next();
fis = new FileInputStream(file); fis = new FileInputStream(file);
// Create a references object to parse the usage.xml file and cache the references
References refs = new References(); References refs = new References();
refs.load(fis); 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(); PluginRef pluginRef = (PluginRef)it2.next();
String pluginId = pluginRef.getId(); String pluginId = pluginRef.getId();
// Retrieve the corresponding component.xml file for the current plugin referenced
ComponentXML compXML = (ComponentXML)pluginId2CompXML.get(pluginId); ComponentXML compXML = (ComponentXML)pluginId2CompXML.get(pluginId);
ComponentLead compLead = unknown; // Get the corresponding component team from that plugin id
for (Iterator it3 = plugin2complead.keySet().iterator(); it3.hasNext();) ComponentTeam compTeam = getComponentTeam(pluginId);
{ // Iterate through the class references in that plugin reference
String regex = (String)it3.next(); for (Iterator it3 = pluginRef.getClassRefs().iterator(); it3.hasNext();) {
if (pluginId.matches(regex))
{
compLead = (ComponentLead)plugin2complead.get(regex);
break;
}
}
for (Iterator it3 = pluginRef.getClassRefs().iterator(); it3.hasNext();)
{
ClassRef classRef = (ClassRef)it3.next(); ClassRef classRef = (ClassRef)it3.next();
String name = classRef.getName(); // Update the component team's cached reference counts with the current class reference
String pkgName = getPackageName(name); updateComponentTeamUsageCounts(compTeam,compXML,classRef,pluginId);
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);
} }
} }
} } catch (Throwable t) {
catch (Throwable t)
{
throw new RuntimeException(t); throw new RuntimeException(t);
} } finally {
finally // Close the current file input stream
{ if (fis != null) {
if (fis != null) try {
{
try
{
fis.close(); 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) /**
{ * This method will cache and return the usages of the class reference into a UsageCount object.
UsageCount usageCount = new UsageCount(); * It will check the passed in component.xml to see if the reference is using a qualified API
int refCount = classRef.getRefCount(); * or if it is an internal usage. The types of references which qualify are class references,
int subclassCount = classRef.getSubclassCount(); * subclasses, implementers, or instantiators.
int implCount = classRef.getImplementCount(); *
int instantiateCount = classRef.getInstantiateCount(); * @param compXML
if (compXML == null) * @param classRef
{ * @param pkgName
usageCount.nonAPIUse = refCount + subclassCount + implCount + instantiateCount; * @param localName
return usageCount; * @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 the type is not a valid API subclass, increment the non-API subclass count
if (pkg == null) if (!type.isSubclass()) {
{ usageCount.nonAPIUse += classRef.getSubclassCount();
usageCount.nonAPIUse = refCount + subclassCount + implCount + instantiateCount; } else {
return usageCount; // Otherwise, increment API subclass count
usageCount.apiUse += classRef.getSubclassCount();
} }
else // If the type is not a valid API implementor, increment the non-API implementor count
{ if (!type.isImplement()) {
Type type = pkg.getType(localName); usageCount.nonAPIUse += classRef.getImplementCount();
if (type == null) } else {
{ // Otherwise increment API implementor count
if (pkg.isApi()) usageCount.apiUse += classRef.getImplementCount();
{ }
usageCount.apiUse = refCount + subclassCount + implCount + instantiateCount; // If the type is not a valid API instantiation, increment the non-API instantiation count
return usageCount; if (!type.isInstantiate()) {
} usageCount.nonAPIUse += classRef.getInstantiateCount();
else } else {
{ // Otherwise increment API instantation count
usageCount.nonAPIUse = refCount + subclassCount + implCount + instantiateCount; usageCount.apiUse += classRef.getInstantiateCount();
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;
}
} }
// 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('.'); int i = className.lastIndexOf('.');
if (i != -1) if (i != -1)
{
return className.substring(0, i); return className.substring(0, i);
} return ""; //$NON-NLS-1$
else
{
return "";
}
} }
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 classWriter = null;
FileWriter pkgWriter = null; FileWriter pkgWriter = null;
FileWriter pluginWriter = null; FileWriter pluginWriter = null;
try try {
{ // Create the file writers
classWriter = new FileWriter(output + ".class.csv"); classWriter = new FileWriter(getOutput() + CLASS_CVS_FILE_EXTENSION);
pkgWriter = new FileWriter(output + ".pkg.csv"); pkgWriter = new FileWriter(getOutput() + PACKAGE_CVS_FILE_EXTENSION);
pluginWriter = new FileWriter(output + ".plugin.csv"); pluginWriter = new FileWriter(getOutput() + PLUGIN_CVS_FILE_EXTENSION);
writeCompLeadCSV(chuck, classWriter, pkgWriter, pluginWriter); // For each component team, write the CVS file content for the component's usage
writeCompLeadCSV(david, classWriter, pkgWriter, pluginWriter); for (int i=0; i<getComponentTeams().size(); i++) {
writeCompLeadCSV(derping, classWriter, pkgWriter, pluginWriter); ComponentTeam compTeam = (ComponentTeam) getComponentTeams().get(i);
writeCompLeadCSV(chris, classWriter, pkgWriter, pluginWriter); writeCompTeamCSV(compTeam, classWriter, pkgWriter, pluginWriter);
writeCompLeadCSV(craig, classWriter, pkgWriter, pluginWriter); }
writeCompLeadCSV(tim, classWriter, pkgWriter, pluginWriter); } catch (IOException ioe) {
writeCompLeadCSV(unknown, classWriter, pkgWriter, pluginWriter);
}
catch (IOException ioe)
{
throw new RuntimeException(ioe); throw new RuntimeException(ioe);
} } finally {
finally // Close the class usage file writer
{ if (classWriter != null) {
if (classWriter != null) try {
{
try
{
classWriter.close(); classWriter.close();
} } catch (IOException ioe) {}
catch (IOException ioe)
{
}
} }
if (pkgWriter != null) // Close the package usage file writer
{ if (pkgWriter != null) {
try try {
{
pkgWriter.close(); pkgWriter.close();
} } catch (IOException ioe) {}
catch (IOException ioe)
{
}
} }
if (pluginWriter != null) // Close the plugin usage file writer
{ if (pluginWriter != null) {
try try {
{
pluginWriter.close(); pluginWriter.close();
} } catch (IOException ioe) {}
catch (IOException ioe)
{
}
} }
} }
} }
private void writeCompLeadCSV(ComponentLead compLead, Writer classWriter, Writer pkgWriter, Writer pluginWriter) throws IOException /**
{ * This method drives the creation of the CSV file contents for the given component team
classWriter.write(compLead.leadName); * and corresponding writers.
classWriter.write("\n"); *
for (Iterator it = compLead.class2refCount.keySet().iterator(); it.hasNext();) * @param compTeam
{ * @param classWriter
String name = (String)it.next(); * @param pkgWriter
UsageCount usageCount = (UsageCount)compLead.class2refCount.get(name); * @param pluginWriter
classWriter.write(name); * @throws IOException
classWriter.write(","); */
classWriter.write(String.valueOf(usageCount.nonAPIUse)); private void writeCompTeamCSV(ComponentTeam compTeam, Writer classWriter, Writer pkgWriter, Writer pluginWriter) throws IOException {
classWriter.write(","); writeCompTeamCSV(compTeam,classWriter,CLASS_USAGE,compTeam.getClassReferenceCounts());
classWriter.write(String.valueOf(usageCount.apiUse)); writeCompTeamCSV(compTeam,pkgWriter,PACKAGE_USAGE,compTeam.getPackageReferenceCounts());
classWriter.write("\n"); writeCompTeamCSV(compTeam,pluginWriter,PLUGIN_USAGE,compTeam.getPluginReferenceCounts());
} }
classWriter.write("\n");
/**
pkgWriter.write(compLead.leadName); * This is a helper method to write out the given component team's reference counts in
pkgWriter.write("\n"); * a CSV file format.
for (Iterator it = compLead.pkg2refCount.keySet().iterator(); it.hasNext();) *
{ * @param compTeam
String name = (String)it.next(); * @param writer
UsageCount usageCount = (UsageCount)compLead.pkg2refCount.get(name); * @param usage
pkgWriter.write(name); * @param referenceCounts
pkgWriter.write(","); * @throws IOException
pkgWriter.write(String.valueOf(usageCount.nonAPIUse)); */
pkgWriter.write(","); private void writeCompTeamCSV(ComponentTeam compTeam, Writer writer, int usage, TreeMap referenceCounts) throws IOException {
pkgWriter.write(String.valueOf(usageCount.apiUse)); // Write the team name
pkgWriter.write("\n"); writer.write(compTeam.getTeamName());
} writer.write(LINE_BREAK);
pkgWriter.write("\n"); // For each name key, retrieve the corresponding usage count values
pluginWriter.write(compLead.leadName); for (Iterator it = referenceCounts.keySet().iterator(); it.hasNext();) {
pluginWriter.write("\n"); String name = (String)it.next();
for (Iterator it = compLead.plugin2refCount.keySet().iterator(); it.hasNext();) UsageCount usageCount = null;
{ switch (usage) {
String id = (String)it.next(); case 0:
UsageCount usageCount = (UsageCount)compLead.plugin2refCount.get(id); usageCount = (UsageCount)compTeam.getClassReferenceCounts().get(name);
pluginWriter.write(id); break;
pluginWriter.write(","); case 1:
pluginWriter.write(String.valueOf(usageCount.nonAPIUse)); usageCount = (UsageCount)compTeam.getPackageReferenceCounts().get(name);
pluginWriter.write(","); break;
pluginWriter.write(String.valueOf(usageCount.apiUse)); case 2:
pluginWriter.write("\n"); usageCount = (UsageCount)compTeam.getPluginReferenceCounts().get(name);
} break;
pluginWriter.write("\n"); }
// 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() /**
{ * Generate the HTML files for comibined usage ref counts of classes, packages, and plugins.
try */
{ private void generateHTMLFiles() {
try {
// Create a new output stream
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write("<root>".getBytes()); baos.write(HTML_ROOT_BEGIN.getBytes());
writeCompLead(chuck, baos); // For each component team, write out the combined component team specific usage data
writeCompLead(david, baos); for (int i=0; i<getComponentTeams().size(); i++) {
writeCompLead(derping, baos); ComponentTeam compTeam = (ComponentTeam) getComponentTeams().get(i);
writeCompLead(chris, baos); writeCompTeam(compTeam, baos);
writeCompLead(craig, baos); }
writeCompLead(tim, baos); // Close the output stream
writeCompLead(unknown, baos); baos.write(HTML_ROOT_END.getBytes());
baos.write("</root>".getBytes());
baos.close(); baos.close();
// Create a byte array from the output stream contents
byte[] content = baos.toByteArray(); byte[] content = baos.toByteArray();
XSLUtil.transform // Write out the byte array to a file output stream for the plugin references
( XSLUtil.transform (
ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-plugin2ref.xsl"), ClassLoader.getSystemResourceAsStream(COMBINE_PLUGINS_FILE),
new ByteArrayInputStream(content), new ByteArrayInputStream(content),
new FileOutputStream(output + ".plugin.html") new FileOutputStream(getOutput() + PLUGIN_HTML_FILE_EXTENSION)
); );
XSLUtil.transform // Write out the byte array to a file output stream for the package references
( XSLUtil.transform (
ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-pkg2ref.xsl"), ClassLoader.getSystemResourceAsStream(COMBINE_PACKAGES_FILE),
new ByteArrayInputStream(content), new ByteArrayInputStream(content),
new FileOutputStream(output + ".pkg.html") new FileOutputStream(getOutput() + PACKAGE_HTML_FILE_EXTENSION)
); );
XSLUtil.transform // Write out the byte array to a file output stream for the class references
( XSLUtil.transform (
ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-class2ref.xsl"), ClassLoader.getSystemResourceAsStream(COMBINE_CLASSES_FILE),
new ByteArrayInputStream(content), new ByteArrayInputStream(content),
new FileOutputStream(output + ".class.html") new FileOutputStream(getOutput() + CLASS_HTML_FILE_EXTENSION)
); );
} } catch (Throwable t) {
catch (Throwable t)
{
t.printStackTrace(); t.printStackTrace();
throw new RuntimeException(t); throw new RuntimeException(t);
} }
} }
private void writeCompLead(ComponentLead compLead, ByteArrayOutputStream baos) throws IOException /**
{ * This method will drive the combined output file html contents for the given component
baos.write("<team lead=\"".getBytes()); * team.
baos.write(compLead.leadName.getBytes()); *
baos.write("\">".getBytes()); * @param compTeam
for (Iterator it = compLead.plugin2refCount.keySet().iterator(); it.hasNext();) * @param baos
{ * @throws IOException
String id = (String)it.next(); */
UsageCount usageCount = (UsageCount)compLead.plugin2refCount.get(id); private void writeCompTeam(ComponentTeam compTeam, ByteArrayOutputStream baos) throws IOException {
baos.write("<plugin id=\"".getBytes()); baos.write("<team lead=\"".getBytes()); //$NON-NLS-1$
baos.write(id.getBytes()); baos.write(compTeam.getTeamName().getBytes());
baos.write("\" api=\"".getBytes()); baos.write("\">".getBytes()); //$NON-NLS-1$
baos.write(String.valueOf(usageCount.apiUse).getBytes()); writeCompTeam(compTeam,baos,CLASS_USAGE,compTeam.getClassReferenceCounts());
baos.write("\" internal=\"".getBytes()); writeCompTeam(compTeam,baos,PACKAGE_USAGE,compTeam.getPackageReferenceCounts());
baos.write(String.valueOf(usageCount.nonAPIUse).getBytes()); writeCompTeam(compTeam,baos,PLUGIN_USAGE,compTeam.getPluginReferenceCounts());
baos.write("\"/>".getBytes()); baos.write("</team>".getBytes()); //$NON-NLS-1$
} }
for (Iterator it = compLead.pkg2refCount.keySet().iterator(); it.hasNext();)
{ /**
String name = (String)it.next(); * This method will write in html format the combined output for the given component team for
UsageCount usageCount = (UsageCount)compLead.pkg2refCount.get(name); * the non API and API usage for the class usage file, the package usage file, and the plugin
baos.write("<package name=\"".getBytes()); * usage file.
baos.write(name.getBytes()); *
baos.write("\" api=\"".getBytes()); * @param compTeam ComponentTeam
baos.write(String.valueOf(usageCount.apiUse).getBytes()); * @param baos ByteArrayOutputStream
baos.write("\" internal=\"".getBytes()); * @param usage class, package, or plugin?
baos.write(String.valueOf(usageCount.nonAPIUse).getBytes()); * @param referenceCounts TreeMap
baos.write("\"/>".getBytes()); * @throws IOException
} */
for (Iterator it = compLead.class2refCount.keySet().iterator(); it.hasNext();) private void writeCompTeam(ComponentTeam compTeam, ByteArrayOutputStream baos, int usage, TreeMap referenceCounts) throws IOException {
{ // Iterate over the name keys of the references tree map
String name = (String)it.next(); for (Iterator it = referenceCounts.keySet().iterator(); it.hasNext();) {
UsageCount usageCount = (UsageCount)compLead.class2refCount.get(name); String name = (String)it.next();
baos.write("<class name=\"".getBytes()); UsageCount usageCount = null;
baos.write(name.getBytes()); // Retrieve the appropriate UsageCount from the map given the current name key
baos.write("\" api=\"".getBytes()); switch (usage) {
baos.write(String.valueOf(usageCount.apiUse).getBytes()); // Class reference
baos.write("\" internal=\"".getBytes()); case 0:
baos.write(String.valueOf(usageCount.nonAPIUse).getBytes()); usageCount = (UsageCount)compTeam.getClassReferenceCounts().get(name);
baos.write("\"/>".getBytes()); baos.write("<class name=\"".getBytes()); //$NON-NLS-1$
} break;
baos.write("</team>".getBytes()); // 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); CommandOptionParser optionParser = new CommandOptionParser(args);
Map options = optionParser.getOptions(); Map options = optionParser.getOptions();
Collection src = (Collection)options.get("src"); Collection src = (Collection)options.get(ARG_SOURCE);
Collection output = (Collection)options.get("output"); Collection output = (Collection)options.get(ARG_OUTPUT);
Collection api = (Collection)options.get("api"); Collection api = (Collection)options.get(ARG_API);
if (src == null || output == null || src.isEmpty() || output.isEmpty()) // If the usage is improper or arguments are not valid, prompt for proper usage
{ if (src == null || output == null || src.isEmpty() || output.isEmpty()) {
printUsage(); printUsage();
System.exit(-1); System.exit(-1);
} }
// Create a new instance of the class and set the command line argument values
CombineClass2Reference class2Ref = new CombineClass2Reference(); CombineClass2Reference class2Ref = new CombineClass2Reference();
class2Ref.setSrc(src); class2Ref.setSrc(src);
class2Ref.setOutput((String)output.iterator().next()); class2Ref.setOutput((String)output.iterator().next());
class2Ref.setApi(api); class2Ref.setApi(api);
// Execute the combination method
class2Ref.execute(); class2Ref.execute();
} }
private static void printUsage() /**
{ * This is a helper method to the user to print out an error message of the proper usage of
System.out.println("Usage: java org.eclipse.wtp.releng.tools.component.adopters.CombineClass2Reference -src <src> -output <output>"); * the arguments to be passed and the location of the output files.
System.out.println(""); * See IOutputConstants for messages.
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"); private static void printUsage() {
System.out.println("\t-api\t\t<api>\t\tlocation of your component.xml"); 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
/*******************************************************************************
* 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$
}
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$
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment