KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > coveragedata > CoverageDataFileHandler


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2003 jcoverage ltd.
5  * Copyright (C) 2005 Mark Doliner
6  *
7  * Cobertura is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License,
10  * or (at your option) any later version.
11  *
12  * Cobertura is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Cobertura; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22
23 package net.sourceforge.cobertura.coveragedata;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33
34 /**
35  * This contains methods used for reading and writing the
36  * "cobertura.ser" file.
37  */

38 public abstract class CoverageDataFileHandler implements HasBeenInstrumented
39 {
40
41     /**
42      * Default file name used to write instrumentation information.
43      */

44     public static final String JavaDoc FILE_NAME = "cobertura.ser";
45
46     public static File JavaDoc getDefaultDataFile()
47     {
48         String JavaDoc systemProperty = System
49                 .getProperty("net.sourceforge.cobertura.datafile");
50         if (systemProperty != null)
51             return new File JavaDoc(systemProperty);
52         return new File JavaDoc(FILE_NAME);
53     }
54
55     public static ProjectData loadCoverageData(File JavaDoc dataFile)
56     {
57         InputStream JavaDoc is = null;
58
59         //System.out.println("Cobertura: Loading coverage data from " + dataFile.getAbsolutePath());
60
try
61         {
62             is = new FileInputStream JavaDoc(dataFile);
63             return loadCoverageData(is);
64         }
65         catch (IOException JavaDoc e)
66         {
67             System.err.println("Cobertura: Error reading file "
68                     + dataFile.getAbsolutePath() + ": "
69                     + e.getLocalizedMessage());
70             return null;
71         }
72         finally
73         {
74             if (is != null)
75                 try
76                 {
77                     is.close();
78                 }
79                 catch (IOException JavaDoc e)
80                 {
81                     System.err.println("Cobertura: Error closing file "
82                             + dataFile.getAbsolutePath() + ": "
83                             + e.getLocalizedMessage());
84                 }
85         }
86     }
87
88     private static ProjectData loadCoverageData(InputStream JavaDoc dataFile) throws IOException JavaDoc
89     {
90         ObjectInputStream JavaDoc objects = null;
91
92         try
93         {
94             objects = new ObjectInputStream JavaDoc(dataFile);
95             ProjectData projectData = (ProjectData)objects.readObject();
96             System.out.println("Cobertura: Loaded information on "
97                     + projectData.getNumberOfClasses() + " classes.");
98             return projectData;
99         }
100         catch (IOException JavaDoc e) {
101             throw e;
102         }
103         catch (Exception JavaDoc e)
104         {
105             System.err.println("Cobertura: Error reading from object stream.");
106             e.printStackTrace();
107             return null;
108         }
109         finally
110         {
111             if (objects != null)
112             {
113                 try
114                 {
115                     objects.close();
116                 }
117                 catch (IOException JavaDoc e)
118                 {
119                     System.err
120                             .println("Cobertura: Error closing object stream.");
121                     e.printStackTrace();
122                 }
123             }
124         }
125     }
126
127     public static void saveCoverageData(ProjectData projectData,
128             File JavaDoc dataFile)
129     {
130         FileOutputStream JavaDoc os = null;
131
132         //System.out.println("Cobertura: Saving coverage data to " + dataFile.getAbsolutePath());
133
try
134         {
135             os = new FileOutputStream JavaDoc(dataFile);
136             saveCoverageData(projectData, os);
137         }
138         catch (IOException JavaDoc e)
139         {
140             System.err.println("Cobertura: Error writing file "
141                     + dataFile.getAbsolutePath());
142             e.printStackTrace();
143         }
144         finally
145         {
146             if (os != null)
147             {
148                 try
149                 {
150                     os.close();
151                 }
152                 catch (IOException JavaDoc e)
153                 {
154                     System.err.println("Cobertura: Error closing file "
155                             + dataFile.getAbsolutePath());
156                     e.printStackTrace();
157                 }
158             }
159         }
160     }
161
162     private static void saveCoverageData(ProjectData projectData,
163             OutputStream JavaDoc dataFile)
164     {
165         ObjectOutputStream JavaDoc objects = null;
166
167         try
168         {
169             objects = new ObjectOutputStream JavaDoc(dataFile);
170             objects.writeObject(projectData);
171             System.out.println("Cobertura: Saved information on " + projectData.getNumberOfClasses() + " classes.");
172         }
173         catch (IOException JavaDoc e)
174         {
175             System.err.println("Cobertura: Error writing to object stream.");
176             e.printStackTrace();
177         }
178         finally
179         {
180             if (objects != null)
181             {
182                 try
183                 {
184                     objects.close();
185                 }
186                 catch (IOException JavaDoc e)
187                 {
188                     System.err
189                             .println("Cobertura: Error closing object stream.");
190                     e.printStackTrace();
191                 }
192             }
193         }
194     }
195
196 }
197
Popular Tags