KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > util > ConvertSingleLog


1 /*
2  * @(#)ConvertSingleLog.java
3  *
4  * Copyright (C) 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.util;
28
29 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger;
30 import java.io.Reader JavaDoc;
31 import java.io.BufferedReader JavaDoc;
32 import java.io.IOException JavaDoc;
33
34
35 /**
36  * A way to convert the single log style (not necessarily a file), and outputs
37  * it to a channel logger. This converter handles multiple channels.
38  * <p>
39  * As of 2004-Jun-03, the format has changed.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2004/07/07 09:39:11 $
43  * @since April 16, 2004
44  */

45 public class ConvertSingleLog
46 {
47     private IChannelLogger channels[];
48     
49     /**
50      * Create a new converter, using the given initialized loggers as the
51      * output channels. The order matters: each logger index refers to
52      * that specific channel index; so ch[1] refers to channel 1.
53      *
54      * @param ch the channels to output the single log to.
55      * @exception IllegalArgumentException if <tt>ch</tt> or any entry inside
56      * <tt>ch</tt> is null, or if the length of <tt>ch</tt> is <tt>0</tt>.
57      */

58     public ConvertSingleLog( IChannelLogger ch[] )
59     {
60         if (ch == null)
61         {
62             throw new IllegalArgumentException JavaDoc("no null args");
63         }
64         
65         int len = ch.length;
66         if (len <= 0)
67         {
68             throw new IllegalArgumentException JavaDoc("must pass at least one channel");
69         }
70         this.channels = new IChannelLogger[ len ];
71         for (int i = 0; i < len; ++i)
72         {
73             if (ch[i] == null)
74             {
75                 throw new IllegalArgumentException JavaDoc("channel "+i+" is null");
76             }
77             this.channels[i] = ch[i];
78         }
79     }
80     
81     
82     /**
83      * Loads all data from the given reader and parses it out to the
84      * channels. The reader is closed on return.
85      *
86      * @exception IOException if there is an I/O error reading from <tt>r</tt>.
87      * @exception IllegalArgumentException if the reader is <tt>null</tt>.
88      */

89     public void read( Reader JavaDoc r, boolean ignoreFormatErrors )
90             throws IOException JavaDoc
91     {
92         if (r == null)
93         {
94             throw new IllegalArgumentException JavaDoc("no null args");
95         }
96         
97         BufferedReader JavaDoc br = new BufferedReader JavaDoc( r );
98         try
99         {
100             String JavaDoc line;
101             while ((line = br.readLine()) != null)
102             {
103 //System.out.println("-- processing line ["+line+"]");
104
processLine( line, ignoreFormatErrors );
105             }
106         }
107         finally
108         {
109             br.close();
110             r.close();
111         }
112     }
113     
114     
115     /**
116      * Process a single line from the single file. Invalid formatted lines
117      * will cause an I/O exception. Blank lines are ignored. Whitespace
118      * is also ignored.
119      *
120      * @param line the input line
121      * @exception IOException if there's a problem with the format of the line,
122      * or if <tt>line</tt> is <tt>null</tt>.
123      */

124     public void processLine( String JavaDoc line, boolean ignoreFormatErrors )
125             throws IOException JavaDoc
126     {
127         if (line == null)
128         {
129             throw new IOException JavaDoc("End of stream: line is null");
130         }
131         line = line.trim();
132         if (line.length() <= 0)
133         {
134             // ignore empty lines.
135
return;
136         }
137         
138         try
139         {
140             int pos[] = { 0, 0 };
141             int channel = (int)nextShort( pos, line );
142             
143             // test the channel
144
if (channel < 0)
145             {
146                 throw new IOException JavaDoc( "Invalid channel: requested "+
147                     channel+", but we're restricted to "+this.channels.length+
148                     " channels." );
149             }
150             if (channel >= this.channels.length)
151             {
152                 if (ignoreFormatErrors)
153                 {
154                     return;
155                 }
156                 // don't ignore the error
157
throw new IOException JavaDoc( "Invalid channel: requested "+
158                     channel+", but we're restricted to "+this.channels.length+
159                     " channels." );
160             }
161             
162             // process the rest of the line
163
String JavaDoc classID = nextElement( pos, line );
164             String JavaDoc s = lastElement( pos, line );
165             HexUtil.TwoShorts ts = new HexUtil.TwoShorts();
166             
167             // method then mark
168
if (!HexUtil.getInstance().parseTwoHex( s, ts, ' ', 0 ))
169             {
170                 throw new IOException JavaDoc( "invalid format for hexidecimal ["+
171                     line+"]" );
172             }
173             
174             // don't cover the line unless there were no format errors
175
//System.out.println("-- Covering channel "+channel+" ["+classID+"]["+ts.a+"]["+ts.b+"]");
176
this.channels[ channel ].cover( classID, ts.a, ts.b );
177         }
178         catch (IOException JavaDoc ioe)
179         {
180             if (!ignoreFormatErrors)
181             {
182                 throw ioe;
183             }
184             // else ignore the errors
185
}
186     }
187     
188     
189     protected String JavaDoc nextElement( int pos[], String JavaDoc line )
190             throws IOException JavaDoc
191     {
192         pos[1] = line.indexOf( ',', pos[0] );
193         if (pos[1] <= pos[0])
194         {
195             throw new IOException JavaDoc( "invalid format ["+line+"]" );
196         }
197         String JavaDoc ret = line.substring( pos[0], pos[1] ).trim();
198         pos[0] = pos[1] + 1;
199         return ret;
200     }
201     
202     
203     protected short nextShort( int pos[], String JavaDoc line )
204             throws IOException JavaDoc
205     {
206         String JavaDoc s = nextElement( pos, line );
207         return parseShort( s, line );
208     }
209     
210     
211     protected String JavaDoc lastElement( int pos[], String JavaDoc line )
212             throws IOException JavaDoc
213     {
214         if (pos[0] >= line.length())
215         {
216             throw new IOException JavaDoc( "invalid format ["+line+"]" );
217         }
218         String JavaDoc s = line.substring( pos[0] );
219         return s;
220     }
221     
222     
223     protected short parseShort( String JavaDoc txt, String JavaDoc line )
224             throws IOException JavaDoc
225     {
226         try
227         {
228             return Short.parseShort( txt );
229         }
230         catch (NumberFormatException JavaDoc e)
231         {
232             throw new IOException JavaDoc( "invalid format for short ["+line+"]" );
233         }
234     }
235 }
236
237
Popular Tags