| /******************************************************************************* |
| * Copyright (c) 2005 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; |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.io.FileWriter; |
| import java.io.IOException; |
| import java.util.Collection; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.Map; |
| 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; |
| private Collection src; |
| private String output; |
| |
| private class ComponentLead |
| { |
| public String leadName; |
| public HashMap class2refCount; |
| public HashMap pkg2refCount; |
| public HashMap plugin2refCount; |
| |
| public ComponentLead(String leadName) |
| { |
| this.leadName = leadName; |
| class2refCount = new HashMap(); |
| pkg2refCount = new HashMap(); |
| plugin2refCount = new HashMap(); |
| } |
| } |
| |
| 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.*", chuck); |
| plugin2complead.put("org.eclipse..st.j2ee.*", 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.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.*", craig); |
| plugin2complead.put("org.eclipse..st.xsd.*", craig); |
| } |
| |
| public Collection getSrc() |
| { |
| return src; |
| } |
| |
| public void setSrc(Collection src) |
| { |
| this.src = src; |
| } |
| |
| public String getOutput() |
| { |
| return output; |
| } |
| |
| public void setOutput(String output) |
| { |
| this.output = output; |
| } |
| |
| public void execute() |
| { |
| for (Iterator it = src.iterator(); it.hasNext();) |
| { |
| FileInputStream fis = null; |
| try |
| { |
| fis = new FileInputStream((String)it.next()); |
| References refs = new References(); |
| refs.load(fis); |
| for (Iterator it2 = refs.getPluginRefs().iterator(); it2.hasNext();) |
| { |
| int pluginRefTotal = 0; |
| PluginRef pluginRef = (PluginRef)it2.next(); |
| String pluginId = pluginRef.getId(); |
| 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();) |
| { |
| ClassRef classRef = (ClassRef)it3.next(); |
| String name = classRef.getName(); |
| int refCount = classRef.getRefCount(); |
| pluginRefTotal += refCount; |
| Integer refTotal = (Integer)compLead.class2refCount.get(name); |
| if (refTotal == null) |
| { |
| refTotal = new Integer(0); |
| } |
| refTotal = new Integer(refCount + refTotal.intValue()); |
| compLead.class2refCount.put(name, refTotal); |
| String pkgName = getPackageName(name); |
| Integer pkgRefTotal = (Integer)compLead.pkg2refCount.get(pkgName); |
| if (pkgRefTotal == null) |
| { |
| pkgRefTotal = new Integer(0); |
| } |
| compLead.pkg2refCount.put(pkgName, new Integer(refCount + pkgRefTotal.intValue())); |
| } |
| compLead.plugin2refCount.put(pluginId, new Integer(pluginRefTotal)); |
| } |
| } |
| catch (Throwable t) |
| { |
| throw new RuntimeException(t); |
| } |
| finally |
| { |
| if (fis != null) |
| { |
| try |
| { |
| fis.close(); |
| } |
| catch (IOException ioe) |
| { |
| } |
| } |
| } |
| } |
| //genCSV(); |
| genHTML(); |
| } |
| |
| private String getPackageName(String className) |
| { |
| int i = className.lastIndexOf('.'); |
| if (i != -1) |
| { |
| return className.substring(0, i); |
| } |
| else |
| { |
| return ""; |
| } |
| } |
| |
| /* |
| public void genCSV() |
| { |
| FileWriter fw = null; |
| try |
| { |
| fw = new FileWriter(output); |
| for (Iterator it = class2refCount.keySet().iterator(); it.hasNext();) |
| { |
| String name = (String)it.next(); |
| fw.write(name); |
| fw.write(","); |
| fw.write(((Integer)class2refCount.get(name)).toString()); |
| fw.write("\n"); |
| } |
| } |
| catch (IOException ioe) |
| { |
| throw new RuntimeException(ioe); |
| } |
| finally |
| { |
| if (fw != null) |
| { |
| try |
| { |
| fw.close(); |
| } |
| catch (IOException ioe) |
| { |
| } |
| } |
| } |
| } |
| */ |
| |
| public void genHTML() |
| { |
| try |
| { |
| 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.close(); |
| byte[] content = baos.toByteArray(); |
| XSLUtil.transform |
| ( |
| ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-plugin2ref.xsl"), |
| new ByteArrayInputStream(content), |
| new FileOutputStream(output + ".plugin.html") |
| ); |
| XSLUtil.transform |
| ( |
| ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-pkg2ref.xsl"), |
| new ByteArrayInputStream(content), |
| new FileOutputStream(output + ".pkg.html") |
| ); |
| XSLUtil.transform |
| ( |
| ClassLoader.getSystemResourceAsStream("org/eclipse/wtp/releng/tools/component/xsl/combine-class2ref.xsl"), |
| new ByteArrayInputStream(content), |
| new FileOutputStream(output + ".class.html") |
| ); |
| } |
| 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(); |
| baos.write("<plugin id=\"".getBytes()); |
| baos.write(id.getBytes()); |
| baos.write("\" ref=\"".getBytes()); |
| baos.write(((Integer)compLead.plugin2refCount.get(id)).toString().getBytes()); |
| baos.write("\"/>".getBytes()); |
| } |
| for (Iterator it = compLead.pkg2refCount.keySet().iterator(); it.hasNext();) |
| { |
| String name = (String)it.next(); |
| baos.write("<package name=\"".getBytes()); |
| baos.write(name.getBytes()); |
| baos.write("\" ref=\"".getBytes()); |
| baos.write(((Integer)compLead.pkg2refCount.get(name)).toString().getBytes()); |
| baos.write("\"/>".getBytes()); |
| } |
| for (Iterator it = compLead.class2refCount.keySet().iterator(); it.hasNext();) |
| { |
| String name = (String)it.next(); |
| baos.write("<class name=\"".getBytes()); |
| baos.write(name.getBytes()); |
| baos.write("\" ref=\"".getBytes()); |
| baos.write(((Integer)compLead.class2refCount.get(name)).toString().getBytes()); |
| baos.write("\"/>".getBytes()); |
| } |
| baos.write("</team>".getBytes()); |
| } |
| |
| public static void main(String[] args) |
| { |
| CommandOptionParser optionParser = new CommandOptionParser(args); |
| Map options = optionParser.getOptions(); |
| Collection src = (Collection)options.get("src"); |
| Collection output = (Collection)options.get("output"); |
| if (src == null || output == null || src.isEmpty() || output.isEmpty()) |
| { |
| printUsage(); |
| System.exit(-1); |
| } |
| |
| CombineClass2Reference class2Ref = new CombineClass2Reference(); |
| class2Ref.setSrc(src); |
| class2Ref.setOutput((String)output.iterator().next()); |
| 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"); |
| } |
| } |