KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ClassRecordIO.java
3  *
4  * Copyright (C) 2002-2003 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.IOException JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.io.StringReader JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import java.io.Writer JavaDoc;
34
35 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData;
36 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
37
38 /**
39  * Knows how to read and write a ClassRecord object.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2004/04/15 05:48:26 $
43  * @since December 15, 2002
44  */

45 class ClassRecordIO
46 {
47     public ClassRecordIO()
48     {
49         // do nothing
50
}
51     
52     
53     /**
54      * Write only the class data for a class record.
55      */

56     public void writeClass( ClassRecord cr, Writer JavaDoc out )
57             throws IOException JavaDoc
58     {
59         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
60         
61         sb.append( cr.getClassName().length() ).
62             append( ':' ).
63             append( cr.getClassName() ).
64             append( ',' ).
65             append( cr.getClassCRC() ).
66             append( ',' ).
67             append( cr.getSourceFileName().length() ).
68             append( ':' ).
69             append( cr.getSourceFileName() ).
70             append( ',' );
71         
72         int count = cr.getMethodCount();
73         sb.append( count ).append( '[' );
74         for (short i = 0; i < count; ++i)
75         {
76             sb.append( '(' );
77             String JavaDoc m = cr.getMethodAt( i );
78             sb.append( m.length() ).
79                 append( ':' ).
80                 append( m );
81             sb.append( ')' );
82         }
83         sb.append( ']' );
84         
85         out.write( sb.toString() );
86     }
87     
88     
89     /**
90      * Read only the class data for a class record.
91      */

92     public ClassRecord readClass( AnalysisModuleSet ams, Reader JavaDoc in )
93             throws IOException JavaDoc
94     {
95         int count = ReadUtil.toInt( ReadUtil.readTo( in, ':' ) );
96         String JavaDoc className = ReadUtil.readCount( in, count );
97         ReadUtil.readTo( in, ',' );
98         long classCRC = ReadUtil.toLong( ReadUtil.readTo( in, ',' ) );
99         count = ReadUtil.toInt( ReadUtil.readTo( in, ':' ) );
100         String JavaDoc sourceFileName = ReadUtil.readCount( in, count );
101         ReadUtil.readTo( in, ',' );
102         
103         int methodCount = ReadUtil.toInt( ReadUtil.readTo( in, '[' ) );
104         String JavaDoc[] methodSigs = new String JavaDoc[ methodCount ];
105         for (int i = 0; i < methodCount; ++i)
106         {
107             ReadUtil.readTo( in, '(' );
108             count = ReadUtil.toInt( ReadUtil.readTo( in, ':' ) );
109             methodSigs[i] = ReadUtil.readCount( in, count );
110             ReadUtil.readTo( in, ')' );
111         }
112         ReadUtil.readTo( in, ']' );
113         
114         return new ClassRecord( className, classCRC, sourceFileName,
115             methodSigs, ams );
116     }
117     
118     
119     //---------------------------------------------------------------------
120

121     
122     /**
123      * Writes the set of marks for the given analysis module.
124      */

125     public void writeMarksForAnalysisModule( ClassRecord cr,
126             IAnalysisModule am, Writer JavaDoc out )
127             throws IOException JavaDoc
128     {
129         writeMarksForMeasure( cr, am.getMeasureName(), out );
130     }
131     
132     
133     /**
134      * Writes the set of marks for the given analysis module.
135      */

136     public void writeMarksForMeasure( ClassRecord cr,
137             String JavaDoc measureName, Writer JavaDoc out )
138             throws IOException JavaDoc
139     {
140         MarkRecord mr[] = cr.getMarksForAnalysisModule( measureName );
141         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
142         sb.append( measureName.length() ).
143             append( ':' ).
144             append( measureName ).
145             append( ',' ).
146             append( mr.length ).
147             append( '[' );
148         for (int i = 0; i < mr.length; ++i)
149         {
150             StringWriter JavaDoc sw = new StringWriter JavaDoc();
151             writeMark( mr[i], sw );
152             
153             String JavaDoc text = sw.toString();
154             sb.append( text.length() ).
155                 append( '(' ).
156                 append( text ).
157                 append( ')' );
158         }
159         sb.append( ']' );
160         
161         out.write( sb.toString() );
162     }
163     
164     
165     public void readMarks( ClassRecord cr, Reader JavaDoc in )
166             throws IOException JavaDoc
167     {
168         AnalysisModuleSet ams = cr.getAnalysisModuleSet();
169         
170         int count = ReadUtil.toInt( ReadUtil.readTo( in, ':' ) );
171         String JavaDoc measureName = ReadUtil.readCount( in, count );
172         ReadUtil.readTo( in, ',' );
173         int markCount = ReadUtil.toInt( ReadUtil.readTo( in, '[' ) );
174         for (int i = 0; i < markCount; ++i)
175         {
176             count = ReadUtil.toInt( ReadUtil.readTo( in, '(' ) );
177             StringReader JavaDoc sr = new StringReader JavaDoc(
178                 ReadUtil.readCount( in, count ) );
179             MarkRecord mr = readMark( cr, ams, sr );
180             cr.addMark( mr );
181             ReadUtil.readTo( in, ')' );
182         }
183         ReadUtil.readTo( in, ']' );
184     }
185     
186     
187     
188     //-----------------------------------------------------------------------
189

190     
191     public void writeMark( MarkRecord mr, Writer JavaDoc out )
192             throws IOException JavaDoc
193     {
194         short amIndex = mr.getAnalysisModuleIndex();
195         short methodIndex = mr.getMethodIndex();
196         short markIndex = mr.getMarkIndex();
197         int lineNumber = mr.getLineNumber();
198         IAnalysisMetaData amd = mr.getAnalysisMetaData();
199         
200         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
201         sb.append( amIndex ).
202             append( ',' ).
203             append( methodIndex ).
204             append( ',' ).
205             append( markIndex ).
206             append( ',' ).
207             append( lineNumber ).
208             append( ',' );
209         
210         StringWriter JavaDoc sw = new StringWriter JavaDoc();
211         AnalysisMetaDataIO amdw = new AnalysisMetaDataIO();
212         amdw.writeAnalysisMetaData( amd, sw );
213         String JavaDoc text = sw.toString();
214         sb.append( text.length() ).
215             append( ':' ).
216             append( text ).
217             append( ',' );
218         
219         out.write( sb.toString() );
220     }
221     
222     
223     public MarkRecord readMark( ClassRecord cr, AnalysisModuleSet ams,
224             Reader JavaDoc in )
225             throws IOException JavaDoc
226     {
227         short amIndex = (short)ReadUtil.toInt( ReadUtil.readTo( in, ',' ) );
228         short methodIndex = (short)ReadUtil.toInt( ReadUtil.readTo( in, ',' ) );
229         short markIndex = (short)ReadUtil.toInt( ReadUtil.readTo( in, ',' ) );
230         int lineNumber = ReadUtil.toInt( ReadUtil.readTo( in, ',' ) );
231         int count = ReadUtil.toInt( ReadUtil.readTo( in, ':' ) );
232         StringReader JavaDoc sr = new StringReader JavaDoc( ReadUtil.readCount( in, count ) );
233         AnalysisMetaDataIO amdr = new AnalysisMetaDataIO();
234         IAnalysisMetaData amd = amdr.readAnalysisMetaData( sr );
235         ReadUtil.readTo( in, ',' );
236         
237         return new MarkRecord( amd,
238             ams.getAnalysisModuleAt( amIndex ).getMeasureName(),
239             cr.getMethodAt( methodIndex ),
240             markIndex, lineNumber );
241     }
242 }
243
244
Popular Tags