001    /*
002     * Cobertura - http://cobertura.sourceforge.net/
003     *
004     * Copyright (C) 2003 jcoverage ltd.
005     * Copyright (C) 2005 Mark Doliner
006     * Copyright (C) 2007 Joakim Erdfelt
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.coveragedata;
025    
026    import net.sourceforge.cobertura.util.ConfigurationUtil;
027    
028    import java.io.File;
029    import java.io.FileInputStream;
030    import java.io.FileOutputStream;
031    import java.io.IOException;
032    import java.io.InputStream;
033    import java.io.ObjectInputStream;
034    import java.io.ObjectOutputStream;
035    import java.io.OutputStream;
036    
037    /**
038     * This contains methods used for reading and writing the
039     * "cobertura.ser" file.
040     */
041    public abstract class CoverageDataFileHandler implements HasBeenInstrumented
042    {
043            private static File defaultFile = null;
044    
045            public static File getDefaultDataFile()
046            {
047                    // return cached defaultFile
048                    if (defaultFile != null) 
049                    {
050                            return defaultFile;
051                    }
052    
053                    // load and cache datafile configuration
054                    ConfigurationUtil config = new ConfigurationUtil();
055                    defaultFile = new File(config.getDatafile());
056            
057                    return defaultFile;
058            }
059    
060            public static ProjectData loadCoverageData(File dataFile)
061            {
062                    InputStream is = null;
063    
064                    //System.out.println("Cobertura: Loading coverage data from " + dataFile.getAbsolutePath());
065                    try
066                    {
067                            is = new FileInputStream(dataFile);
068                            return loadCoverageData(is);
069                    }
070                    catch (IOException e)
071                    {
072                            System.err.println("Cobertura: Error reading file "
073                                            + dataFile.getAbsolutePath() + ": "
074                                            + e.getLocalizedMessage());
075                            return null;
076                    }
077                    finally
078                    {
079                            if (is != null)
080                                    try
081                                    {
082                                            is.close();
083                                    }
084                                    catch (IOException e)
085                                    {
086                                            System.err.println("Cobertura: Error closing file "
087                                                            + dataFile.getAbsolutePath() + ": "
088                                                            + e.getLocalizedMessage());
089                                    }
090                    }
091            }
092    
093            private static ProjectData loadCoverageData(InputStream dataFile) throws IOException
094            {
095                    ObjectInputStream objects = null;
096    
097                    try
098                    {
099                            objects = new ObjectInputStream(dataFile);
100                            ProjectData projectData = (ProjectData)objects.readObject();
101                            System.out.println("Cobertura: Loaded information on "
102                                            + projectData.getNumberOfClasses() + " classes.");
103                            return projectData;
104                    }
105                    catch (IOException e) {
106                            throw e;
107                    }
108                    catch (Exception e)
109                    {
110                            System.err.println("Cobertura: Error reading from object stream.");
111                            e.printStackTrace();
112                            return null;
113                    }
114                    finally
115                    {
116                            if (objects != null)
117                            {
118                                    try
119                                    {
120                                            objects.close();
121                                    }
122                                    catch (IOException e)
123                                    {
124                                            System.err
125                                                            .println("Cobertura: Error closing object stream.");
126                                            e.printStackTrace();
127                                    }
128                            }
129                    }
130            }
131    
132            public static void saveCoverageData(ProjectData projectData,
133                            File dataFile)
134            {
135                    FileOutputStream os = null;
136    
137                    //System.out.println("Cobertura: Saving coverage data to " + dataFile.getAbsolutePath());
138                    try
139                    {
140                            File dataDir = dataFile.getParentFile();
141                            if( (dataDir != null) && !dataDir.exists() )
142                            {
143                                    dataDir.mkdirs();
144                            }
145                            os = new FileOutputStream(dataFile);
146                            saveCoverageData(projectData, os);
147                    }
148                    catch (IOException e)
149                    {
150                            System.err.println("Cobertura: Error writing file "
151                                            + dataFile.getAbsolutePath());
152                            e.printStackTrace();
153                    }
154                    finally
155                    {
156                            if (os != null)
157                            {
158                                    try
159                                    {
160                                            os.close();
161                                    }
162                                    catch (IOException e)
163                                    {
164                                            System.err.println("Cobertura: Error closing file "
165                                                            + dataFile.getAbsolutePath());
166                                            e.printStackTrace();
167                                    }
168                            }
169                    }
170            }
171    
172            private static void saveCoverageData(ProjectData projectData,
173                            OutputStream dataFile)
174            {
175                    ObjectOutputStream objects = null;
176            
177                    try
178                    {
179                            objects = new ObjectOutputStream(dataFile);
180                            objects.writeObject(projectData);
181                            System.out.println("Cobertura: Saved information on " + projectData.getNumberOfClasses() + " classes.");
182                    }
183                    catch (IOException e)
184                    {
185                            System.err.println("Cobertura: Error writing to object stream.");
186                            e.printStackTrace();
187                    }
188                    finally
189                    {
190                            if (objects != null)
191                            {
192                                    try
193                                    {
194                                            objects.close();
195                                    }
196                                    catch (IOException e)
197                                    {
198                                            System.err
199                                                            .println("Cobertura: Error closing object stream.");
200                                            e.printStackTrace();
201                                    }
202                            }
203                    }
204            }
205    
206    }