001: /*
002: * @(#)ClassRecordIO.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.datastore;
028:
029: import java.io.IOException;
030: import java.io.Reader;
031: import java.io.StringReader;
032: import java.io.StringWriter;
033: import java.io.Writer;
034:
035: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData;
036: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
037:
038: /**
039: * Knows how to read and write a ClassRecord object.
040: *
041: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
042: * @version $Date: 2004/04/15 05:48:26 $
043: * @since December 15, 2002
044: */
045: class ClassRecordIO {
046: public ClassRecordIO() {
047: // do nothing
048: }
049:
050: /**
051: * Write only the class data for a class record.
052: */
053: public void writeClass(ClassRecord cr, Writer out)
054: throws IOException {
055: StringBuffer sb = new StringBuffer();
056:
057: sb.append(cr.getClassName().length()).append(':').append(
058: cr.getClassName()).append(',').append(cr.getClassCRC())
059: .append(',').append(cr.getSourceFileName().length())
060: .append(':').append(cr.getSourceFileName()).append(',');
061:
062: int count = cr.getMethodCount();
063: sb.append(count).append('[');
064: for (short i = 0; i < count; ++i) {
065: sb.append('(');
066: String m = cr.getMethodAt(i);
067: sb.append(m.length()).append(':').append(m);
068: sb.append(')');
069: }
070: sb.append(']');
071:
072: out.write(sb.toString());
073: }
074:
075: /**
076: * Read only the class data for a class record.
077: */
078: public ClassRecord readClass(AnalysisModuleSet ams, Reader in)
079: throws IOException {
080: int count = ReadUtil.toInt(ReadUtil.readTo(in, ':'));
081: String className = ReadUtil.readCount(in, count);
082: ReadUtil.readTo(in, ',');
083: long classCRC = ReadUtil.toLong(ReadUtil.readTo(in, ','));
084: count = ReadUtil.toInt(ReadUtil.readTo(in, ':'));
085: String sourceFileName = ReadUtil.readCount(in, count);
086: ReadUtil.readTo(in, ',');
087:
088: int methodCount = ReadUtil.toInt(ReadUtil.readTo(in, '['));
089: String[] methodSigs = new String[methodCount];
090: for (int i = 0; i < methodCount; ++i) {
091: ReadUtil.readTo(in, '(');
092: count = ReadUtil.toInt(ReadUtil.readTo(in, ':'));
093: methodSigs[i] = ReadUtil.readCount(in, count);
094: ReadUtil.readTo(in, ')');
095: }
096: ReadUtil.readTo(in, ']');
097:
098: return new ClassRecord(className, classCRC, sourceFileName,
099: methodSigs, ams);
100: }
101:
102: //---------------------------------------------------------------------
103:
104: /**
105: * Writes the set of marks for the given analysis module.
106: */
107: public void writeMarksForAnalysisModule(ClassRecord cr,
108: IAnalysisModule am, Writer out) throws IOException {
109: writeMarksForMeasure(cr, am.getMeasureName(), out);
110: }
111:
112: /**
113: * Writes the set of marks for the given analysis module.
114: */
115: public void writeMarksForMeasure(ClassRecord cr,
116: String measureName, Writer out) throws IOException {
117: MarkRecord mr[] = cr.getMarksForAnalysisModule(measureName);
118: StringBuffer sb = new StringBuffer();
119: sb.append(measureName.length()).append(':').append(measureName)
120: .append(',').append(mr.length).append('[');
121: for (int i = 0; i < mr.length; ++i) {
122: StringWriter sw = new StringWriter();
123: writeMark(mr[i], sw);
124:
125: String text = sw.toString();
126: sb.append(text.length()).append('(').append(text).append(
127: ')');
128: }
129: sb.append(']');
130:
131: out.write(sb.toString());
132: }
133:
134: public void readMarks(ClassRecord cr, Reader in) throws IOException {
135: AnalysisModuleSet ams = cr.getAnalysisModuleSet();
136:
137: int count = ReadUtil.toInt(ReadUtil.readTo(in, ':'));
138: String measureName = ReadUtil.readCount(in, count);
139: ReadUtil.readTo(in, ',');
140: int markCount = ReadUtil.toInt(ReadUtil.readTo(in, '['));
141: for (int i = 0; i < markCount; ++i) {
142: count = ReadUtil.toInt(ReadUtil.readTo(in, '('));
143: StringReader sr = new StringReader(ReadUtil.readCount(in,
144: count));
145: MarkRecord mr = readMark(cr, ams, sr);
146: cr.addMark(mr);
147: ReadUtil.readTo(in, ')');
148: }
149: ReadUtil.readTo(in, ']');
150: }
151:
152: //-----------------------------------------------------------------------
153:
154: public void writeMark(MarkRecord mr, Writer out) throws IOException {
155: short amIndex = mr.getAnalysisModuleIndex();
156: short methodIndex = mr.getMethodIndex();
157: short markIndex = mr.getMarkIndex();
158: int lineNumber = mr.getLineNumber();
159: IAnalysisMetaData amd = mr.getAnalysisMetaData();
160:
161: StringBuffer sb = new StringBuffer();
162: sb.append(amIndex).append(',').append(methodIndex).append(',')
163: .append(markIndex).append(',').append(lineNumber)
164: .append(',');
165:
166: StringWriter sw = new StringWriter();
167: AnalysisMetaDataIO amdw = new AnalysisMetaDataIO();
168: amdw.writeAnalysisMetaData(amd, sw);
169: String text = sw.toString();
170: sb.append(text.length()).append(':').append(text).append(',');
171:
172: out.write(sb.toString());
173: }
174:
175: public MarkRecord readMark(ClassRecord cr, AnalysisModuleSet ams,
176: Reader in) throws IOException {
177: short amIndex = (short) ReadUtil
178: .toInt(ReadUtil.readTo(in, ','));
179: short methodIndex = (short) ReadUtil.toInt(ReadUtil.readTo(in,
180: ','));
181: short markIndex = (short) ReadUtil.toInt(ReadUtil.readTo(in,
182: ','));
183: int lineNumber = ReadUtil.toInt(ReadUtil.readTo(in, ','));
184: int count = ReadUtil.toInt(ReadUtil.readTo(in, ':'));
185: StringReader sr = new StringReader(ReadUtil
186: .readCount(in, count));
187: AnalysisMetaDataIO amdr = new AnalysisMetaDataIO();
188: IAnalysisMetaData amd = amdr.readAnalysisMetaData(sr);
189: ReadUtil.readTo(in, ',');
190:
191: return new MarkRecord(amd, ams.getAnalysisModuleAt(amIndex)
192: .getMeasureName(), cr.getMethodAt(methodIndex),
193: markIndex, lineNumber);
194: }
195: }
|