001    /**
002     * Cobertura - http://cobertura.sourceforge.net/
003     *
004     * Copyright (C) 2003 jcoverage ltd.
005     * Copyright (C) 2005 Mark Doliner <thekingant@users.sourceforge.net>
006     * Copyright (C) 2005 Joakim Erdfelt <joakim@erdfelt.net>
007     *
008     * Cobertura is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License as published
010     * by the Free Software Foundation; either version 2 of the License,
011     * or (at your option) any later version.
012     *
013     * Cobertura is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     * General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with Cobertura; if not, write to the Free Software
020     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
021     * USA
022     */
023    
024    package net.sourceforge.cobertura.merge;
025    
026    import gnu.getopt.Getopt;
027    import gnu.getopt.LongOpt;
028    
029    import java.io.BufferedReader;
030    import java.io.File;
031    import java.io.FileInputStream;
032    import java.io.FileNotFoundException;
033    import java.io.FileReader;
034    import java.io.IOException;
035    import java.util.ArrayList;
036    import java.util.List;
037    
038    import net.sourceforge.cobertura.coverage.InstrumentationPersistence;
039    import net.sourceforge.cobertura.util.Copyright;
040    
041    import org.apache.log4j.Logger;
042    
043    public class Main extends InstrumentationPersistence
044    {
045    
046            private static final Logger logger = Logger.getLogger(Main.class);
047    
048            public Main(String[] args)
049            {
050                    LongOpt[] longOpts = new LongOpt[2];
051                    longOpts[0] = new LongOpt("instrumentation",
052                                    LongOpt.REQUIRED_ARGUMENT, null, 'i');
053                    longOpts[1] = new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null,
054                                    'o');
055    
056                    Getopt g = new Getopt(getClass().getName(), args, ":i:o:", longOpts);
057                    int c;
058    
059                    File destDir = new File(System.getProperty("user.dir"));
060    
061                    while ((c = g.getopt()) != -1)
062                    {
063                            switch (c)
064                            {
065                                    case 'i':
066                                            System.out.println("cobertura loading: " + g.getOptarg());
067                                            try
068                                            {
069                                                    merge(loadInstrumentation(new FileInputStream(g
070                                                                    .getOptarg())));
071                                            }
072                                            catch (FileNotFoundException ex)
073                                            {
074                                                    logger.warn(ex);
075                                            }
076                                            break;
077    
078                                    case 'o':
079                                            destDir = new File(g.getOptarg());
080                                            destDir.mkdirs();
081                                            break;
082                            }
083                    }
084    
085                    saveInstrumentation(destDir);
086            }
087    
088            public static void main(String[] args)
089            {
090                    Copyright.print(System.out);
091                    System.out.println("Cobertura instrumentation merge tool");
092    
093                    boolean hasCommandsFile = false;
094                    String commandsFileName = null;
095                    for (int i = 0; i < args.length; i++)
096                    {
097                            if (args[i].equals("-commandsfile"))
098                            {
099                                    hasCommandsFile = true;
100                                    commandsFileName = args[++i];
101                            }
102                    }
103    
104                    if (hasCommandsFile)
105                    {
106                            List arglist = new ArrayList();
107                            BufferedReader bufreader = null;
108    
109                            try
110                            {
111                                    bufreader = new BufferedReader(new FileReader(
112                                                    commandsFileName));
113                                    String line;
114    
115                                    while ((line = bufreader.readLine()) != null)
116                                    {
117                                            arglist.add(line);
118                                    }
119    
120                            }
121                            catch (IOException e)
122                            {
123                                    logger.fatal("Unable to read temporary commands file "
124                                                    + commandsFileName + ".");
125                                    logger.info(e);
126                            }
127                            finally
128                            {
129                                    if (bufreader != null)
130                                    {
131                                            try
132                                            {
133                                                    bufreader.close();
134                                            }
135                                            catch (IOException e)
136                                            {
137                                            }
138                                    }
139                            }
140                            args = (String[])arglist.toArray(new String[arglist.size()]);
141                    }
142    
143                    new Main(args);
144            }
145    }