KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > datastore > DirMetaDataIO


1 /*
2  * @(#)DirMetaDataIO.java
3  *
4  * Copyright (C) 2002-2004 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.datastore;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.BufferedWriter JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileReader JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
37 import net.sourceforge.groboutils.util.io.v1.ExtensionFilenameFilter;
38
39 /**
40  * Accesses the meta-data at the I/O level in a directory structure.
41  *
42  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
43  * @version $Date: 2004/04/15 05:48:26 $
44  * @since December 15, 2002
45  * @see ZipMetaDataReader
46  */

47 class DirMetaDataIO
48 {
49     private static final org.apache.log4j.Logger LOG =
50         org.apache.log4j.Logger.getLogger( DirMetaDataIO.class );
51     
52     private static final String JavaDoc MODULE_SET_FILENAME = "moduleset.txt";
53     private static final String JavaDoc CLASS_FILE_EXT = ".classdata.txt";
54     private static final String JavaDoc MARK_FILE_EXT = ".classmarks.txt";
55     
56     
57     private File JavaDoc basedir;
58     private static final ExtensionFilenameFilter CLASS_FILE_FILTER =
59         new ExtensionFilenameFilter( CLASS_FILE_EXT, true );
60     static {
61         CLASS_FILE_FILTER.allowsDirectories( false );
62     }
63     
64     public DirMetaDataIO( File JavaDoc dir )
65             throws IOException JavaDoc
66     {
67         if (dir == null)
68         {
69             throw new IllegalArgumentException JavaDoc( "No null args." );
70         }
71         if (dir.exists() && !dir.isDirectory())
72         {
73             throw new IllegalArgumentException JavaDoc(
74                 "File "+dir+" is not a directory." );
75         }
76         if (!dir.exists())
77         {
78             dir.mkdir();
79         }
80         this.basedir = dir;
81     }
82     
83     
84     /**
85      *
86      */

87     public AnalysisModuleSet getAnalysisModuleSet()
88             throws IOException JavaDoc
89     {
90         checkClosed();
91         AnalysisModuleSetIO amsr = new AnalysisModuleSetIO();
92         AnalysisModuleSet ams;
93         File JavaDoc amsf = getAnalysisModuleSetFile();
94         if (!amsf.exists())
95         {
96             ams = new AnalysisModuleSet();
97         }
98         else
99         {
100             BufferedReader JavaDoc fr = new BufferedReader JavaDoc( new FileReader JavaDoc( amsf ) );
101             try
102             {
103                 ams = amsr.readAnalysisModuleSet( fr );
104             }
105             finally
106             {
107                 fr.close();
108             }
109         }
110         return ams;
111     }
112     
113     
114     /**
115      *
116      */

117     public void putAnalysisModuleSet( AnalysisModuleSet ams )
118             throws IOException JavaDoc
119     {
120         if (ams == null)
121         {
122             throw new IllegalArgumentException JavaDoc("no null args");
123         }
124         checkClosed();
125         AnalysisModuleSetIO amsw = new AnalysisModuleSetIO();
126         File JavaDoc amsf = getAnalysisModuleSetFile();
127         BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc( new FileWriter JavaDoc( amsf ) );
128         try
129         {
130             amsw.writeAnalysisModuleSet( ams, bw );
131         }
132         finally
133         {
134             bw.close();
135         }
136     }
137     
138     
139     /**
140      *
141      */

142     public String JavaDoc[] getClassSigsForAnalysisModule( IAnalysisModule am )
143             throws IOException JavaDoc
144     {
145         if (am == null)
146         {
147             throw new IllegalArgumentException JavaDoc("no null args");
148         }
149         checkClosed();
150         File JavaDoc amDir = getAnalysisModuleDir( am );
151         String JavaDoc names[] = amDir.list( CLASS_FILE_FILTER );
152         for (int i = 0; i < names.length; ++i)
153         {
154             names[i] = names[i].substring( 0, names[i].length() -
155                 CLASS_FILE_EXT.length() );
156         }
157         return names;
158     }
159     
160     
161     /**
162      *
163      */

164     public ClassRecord getClassRecord( IAnalysisModule am, String JavaDoc classSig )
165             throws IOException JavaDoc
166     {
167         return getClassRecord( getAnalysisModuleSet(), am, classSig );
168     }
169     
170     
171     /**
172      * If the class record does not exist, then <tt>null</tt> is returned.
173      */

174     public ClassRecord getClassRecord( AnalysisModuleSet ams,
175             IAnalysisModule am, String JavaDoc classSig )
176             throws IOException JavaDoc
177     {
178         if (am == null || classSig == null || ams == null)
179         {
180             throw new IllegalArgumentException JavaDoc("no null args");
181         }
182         checkClosed();
183         File JavaDoc cf = getClassDataFileForModule( am, classSig );
184         ClassRecordIO crr = new ClassRecordIO();
185         BufferedReader JavaDoc fr;
186         try
187         {
188             fr = new BufferedReader JavaDoc( new FileReader JavaDoc( cf ) );
189         }
190         catch (java.io.FileNotFoundException JavaDoc fnfe)
191         {
192             LOG.warn( "Couldn't find the file '"+cf+
193                 "' containing the class details." );
194             return null;
195         }
196         
197         ClassRecord cr;
198         try
199         {
200             cr = crr.readClass( ams, fr );
201         }
202         finally
203         {
204             fr.close();
205         }
206         
207         // read marks
208
File JavaDoc mcf = getClassMarkFileForModule( am, cr.getClassSignature() );
209         try
210         {
211             fr = new BufferedReader JavaDoc( new FileReader JavaDoc( mcf ) );
212         }
213         catch (java.io.FileNotFoundException JavaDoc fnfe)
214         {
215             LOG.warn( "Could not find file '"+mcf+"' containing marks." );
216             return cr;
217         }
218         
219         try
220         {
221             crr.readMarks( cr, fr );
222         }
223         finally
224         {
225             fr.close();
226         }
227         
228         return cr;
229     }
230     
231     
232     /**
233      *
234      */

235     public void putClassRecord( IAnalysisModule am, ClassRecord cr )
236             throws IOException JavaDoc
237     {
238         if (am == null || cr == null)
239         {
240             throw new IllegalArgumentException JavaDoc("no null args");
241         }
242         checkClosed();
243         File JavaDoc cf = getClassDataFileForModule( am, cr.getClassSignature() );
244         ClassRecordIO crw = new ClassRecordIO();
245         BufferedWriter JavaDoc fw = new BufferedWriter JavaDoc( new FileWriter JavaDoc( cf ) );
246         try
247         {
248             crw.writeClass( cr, fw );
249         }
250         finally
251         {
252             fw.close();
253         }
254         
255         // write marks
256
File JavaDoc mcf = getClassMarkFileForModule( am, cr.getClassSignature() );
257         fw = new BufferedWriter JavaDoc( new FileWriter JavaDoc( mcf ) );
258         try
259         {
260             crw.writeMarksForAnalysisModule( cr, am, fw );
261         }
262         finally
263         {
264             fw.close();
265         }
266     }
267     
268     
269     /**
270      *
271      */

272     public void deleteClassRecord( IAnalysisModule am, ClassRecord cr )
273             throws IOException JavaDoc
274     {
275         if (am == null || cr == null)
276         {
277             throw new IllegalArgumentException JavaDoc("no null args");
278         }
279         checkClosed();
280         File JavaDoc cf = getClassDataFileForModule( am, cr.getClassSignature() );
281         if (cf.exists())
282         {
283             cf.delete();
284         }
285     }
286     
287     
288     /**
289      *
290      */

291     private File JavaDoc getAnalysisModuleSetFile()
292     {
293         File JavaDoc moduleSetFile = new File JavaDoc( this.basedir, MODULE_SET_FILENAME );
294         return moduleSetFile;
295     }
296     
297     
298     /**
299      *
300      */

301     private File JavaDoc getAnalysisModuleDir( IAnalysisModule am )
302             throws IOException JavaDoc
303     {
304         File JavaDoc newDir = new File JavaDoc( this.basedir, am.getMeasureName() );
305         if (newDir.exists() && !newDir.isDirectory())
306         {
307             throw new IOException JavaDoc( "Expected measure directory '"+newDir+
308                 "', but it isn't a directory." );
309         }
310         if (!newDir.exists())
311         {
312             newDir.mkdir();
313         }
314         return newDir;
315     }
316     
317     
318     /**
319      *
320      */

321     private File JavaDoc getClassDataFileForModule( IAnalysisModule am,
322             String JavaDoc classSig )
323             throws IOException JavaDoc
324     {
325         File JavaDoc mf = getAnalysisModuleDir( am );
326         File JavaDoc cf = new File JavaDoc( mf, classSig + CLASS_FILE_EXT );
327         return cf;
328     }
329     
330     
331     /**
332      *
333      */

334     private File JavaDoc getClassMarkFileForModule( IAnalysisModule am,
335             String JavaDoc classSig )
336             throws IOException JavaDoc
337     {
338         File JavaDoc mf = getAnalysisModuleDir( am );
339         File JavaDoc cf = new File JavaDoc( mf, classSig + MARK_FILE_EXT );
340         return cf;
341     }
342     
343     
344     
345     /**
346      * This can be called multiple times without error.
347      */

348     public void close()
349             throws IOException JavaDoc
350     {
351         LOG.debug( "Closing DirMetaDataIO." );
352         this.basedir = null;
353     }
354     
355     
356     /**
357      *
358      */

359     private void checkClosed()
360             throws IOException JavaDoc
361     {
362         if (isClosed())
363         {
364             throw new IOException JavaDoc( "Directory storage has been closed." );
365         }
366     }
367     
368     
369     /**
370      *
371      */

372     boolean isClosed()
373     {
374         return (this.basedir == null);
375     }
376     
377     
378     protected void finalize() throws Throwable JavaDoc
379     {
380         Exception JavaDoc ex = null;
381         if (!isClosed())
382         {
383             ex = new IllegalStateException JavaDoc("Did not close writer.");
384             close();
385         }
386         
387         super.finalize();
388         
389         // class-based post condition
390
if (ex != null)
391         {
392             throw ex;
393         }
394     }
395 }
396
397
Popular Tags