01: /*
02: * @(#)AnalysisModuleSetIO.java
03: *
04: * Copyright (C) 2002,2003 Matt Albrecht
05: * groboclown@users.sourceforge.net
06: * http://groboutils.sourceforge.net
07: *
08: * Permission is hereby granted, free of charge, to any person obtaining a
09: * 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;
30: import java.io.Reader;
31: import java.io.StringReader;
32: import java.io.StringWriter;
33: import java.io.Writer;
34:
35: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
36:
37: /**
38: * Knows how to read and write an AnalysisModuleSet object.
39: *
40: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41: * @version $Date: 2004/04/15 05:48:26 $
42: * @since December 15, 2002
43: */
44: class AnalysisModuleSetIO {
45: public AnalysisModuleSetIO() {
46: // do nothing
47: }
48:
49: public void writeAnalysisModuleSet(AnalysisModuleSet ams, Writer out)
50: throws IOException {
51: AnalysisModuleIO amw = new AnalysisModuleIO();
52:
53: StringBuffer sb = new StringBuffer();
54:
55: int count = ams.getAnalysisModuleCount();
56: sb.append(count).append('{');
57: for (short i = 0; i < count; ++i) {
58: StringWriter sw = new StringWriter();
59: amw.writeAnalysisModule(ams.getAnalysisModuleAt(i), sw);
60: String text = sw.toString();
61: sb.append('[').append(i).append(';').append(text.length())
62: .append(';').append(text).append(']');
63: }
64: sb.append('}');
65:
66: out.write(sb.toString());
67: }
68:
69: public AnalysisModuleSet readAnalysisModuleSet(Reader in)
70: throws IOException {
71: AnalysisModuleIO amr = new AnalysisModuleIO();
72:
73: int count = ReadUtil.toInt(ReadUtil.readTo(in, '{'));
74: AnalysisModuleSet ams = new AnalysisModuleSet();
75: for (int i = 0; i < count; ++i) {
76: ReadUtil.readTo(in, '[');
77: int index = ReadUtil.toInt(ReadUtil.readTo(in, ';'));
78: if (i != index) {
79: throw new IOException("Expected to find index " + i
80: + ", but found " + index + ".");
81: }
82: int size = ReadUtil.toInt(ReadUtil.readTo(in, ';'));
83: StringReader sr = new StringReader(ReadUtil.readCount(in,
84: size));
85: IAnalysisModule am = amr.readAnalysisModule(sr);
86: ams.addAnalysisModule(am);
87: ReadUtil.readTo(in, ']');
88: }
89: ReadUtil.readTo(in, '}');
90:
91: return ams;
92: }
93: }
|