001    /**
002     * www.jcoverage.com
003     * Copyright (C)2003 jcoverage ltd.
004     *
005     * This file is part of jcoverage.
006     *
007     * jcoverage is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published
009     * by the Free Software Foundation; either version 2 of the License,
010     * or (at your option) any later version.
011     *
012     * jcoverage is distributed in the hope that it will be useful, but
013     * WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with jcoverage; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020     * USA
021     *
022     */
023    package com.jcoverage.coverage.reporting;
024    
025    import gnu.getopt.Getopt;
026    import gnu.getopt.LongOpt;
027    
028    import java.io.BufferedInputStream;
029    import java.io.File;
030    import java.io.FileInputStream;
031    import java.io.FileOutputStream;
032    import java.io.IOException;
033    import java.io.InputStream;
034    import java.io.ObjectInputStream;
035    import java.util.Iterator;
036    import java.util.Map;
037    
038    import org.apache.log4j.Logger;
039    
040    import com.jcoverage.coverage.Instrumentation;
041    
042    public class Main {
043      static final Logger logger=Logger.getLogger(Main.class);
044    
045      static File serializationFile,srcDir,destDir;
046    
047      public static void main(String[] args) throws Exception {
048        LongOpt[] longOpts=new LongOpt[3];
049        longOpts[0]=new LongOpt("instrumentation",LongOpt.REQUIRED_ARGUMENT,null,'i');
050        longOpts[1]=new LongOpt("output",LongOpt.REQUIRED_ARGUMENT,null,'o');
051        longOpts[2]=new LongOpt("source",LongOpt.REQUIRED_ARGUMENT,null,'s');
052    
053        Getopt g=new Getopt(Main.class.getName(),args,":i:o:s:",longOpts);
054    
055        int c;
056    
057        while((c=g.getopt())!=-1) {
058          switch(c) {
059          case 'i':
060            serializationFile=new File(g.getOptarg());
061    
062            if (!serializationFile.exists()) {
063              throw new Exception("Error: serialization file "+serializationFile+" does not exist");
064            }
065            if (serializationFile.isDirectory()) {
066              throw new Exception("Error: serialization file "+serializationFile+" cannot be a directory");
067            }
068            break;
069          case 'o':
070            destDir=new File(g.getOptarg());
071    
072            if (destDir.exists() && destDir.isFile()) {
073              throw new Exception("Error: destination directory "+destDir+" already exists and is a file");
074            }
075            destDir.mkdirs();
076            break;
077          case 's':
078            srcDir=new File(g.getOptarg());
079    
080            if (!srcDir.exists()) {
081              throw new Exception("Error: source directory "+srcDir+" does not exist");
082            }
083            if (srcDir.isFile()) {
084              throw new Exception("Error: source directory "+srcDir+" should be a directory, not a file");
085            }
086            break;
087          }
088        }
089    
090        if(logger.isDebugEnabled()) {
091          logger.debug("serializationFile is "+serializationFile);
092          logger.debug("srcDir is "+srcDir);
093          logger.debug("destDir is "+destDir);
094        }
095    
096        // Copy gifs
097        File imagesDir=new File(destDir,"images");
098        imagesDir.mkdirs();
099        copyResource("red.gif",imagesDir);
100        copyResource("green.gif",imagesDir);
101    
102        ReportDriver driver=new ReportDriver(srcDir);
103    
104        InputStream is=new FileInputStream(serializationFile);
105        ObjectInputStream objects=new ObjectInputStream(is);
106    
107        for(Iterator it=((Map)objects.readObject()).entrySet().iterator();it.hasNext();) {
108          Map.Entry entry=(Map.Entry)it.next();
109          driver.addInstrumentation((String)entry.getKey(),(Instrumentation)entry.getValue());
110        }
111    
112        driver.generate(destDir);
113      }
114    
115      static String toPackage(String clzName) {
116        int i=clzName.lastIndexOf('.');
117        if (i==-1) {
118          return "default";
119        } else {
120          return clzName.substring(0,i);
121        }
122      }
123    
124      static byte[] buf=new byte[2^12];
125    
126      static void copyResource(String resname,File dir) throws IOException {
127        FileOutputStream fos=new FileOutputStream(new File(dir,resname));
128        InputStream in=new BufferedInputStream(Main.class.getResourceAsStream(resname));
129        while(true) {
130          int n=in.read(buf,0,buf.length);
131          if (n==-1) {
132            break;
133          }
134    
135          fos.write(buf,0,n);
136        }
137        in.close();
138        fos.close();
139      }
140    }