All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.conqat.engine.sourcecode.coverage.dotnet.MSCoverageLineCoverageHandler Maven / Gradle / Ivy

The newest version!
package org.conqat.engine.sourcecode.coverage.dotnet;

import org.conqat.engine.sourcecode.coverage.ELineCoverage;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/** Handler for processing coverage data */
public class MSCoverageLineCoverageHandler extends DefaultHandler {

	/** Current text content */
	private final StringBuilder textContent = new StringBuilder();

	/** Value of a line start element */
	private int lineStart = -1;

	/** Value of a line end element */
	private int lineEnd = -1;

	/** Value of a SourceFileID element */
	private int sourceFileID = -1;

	/** Value of a Coverage element */
	private int coverage = -1;

	/** Value of a SourceFileName element */
	private String sourceFileName = null;

	/** Coverage creator. */
	private LookupBasedCoverageCreator coverageCreator;

	/** Constructor. */
	public MSCoverageLineCoverageHandler(LookupBasedCoverageCreator coverageCreator) {
		this.coverageCreator = coverageCreator;
	}

	/** {@inheritDoc} */
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) {
		textContent.setLength(0);
	}

	/** {@inheritDoc} */
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		ECoverageXmlElement element = ECoverageXmlElement.VALUES.valueOf(localName);
		if (element == null) {
			return;
		}

		switch (element) {
		case LnStart:
			lineStart = parseCurrentTextAsInt();
			break;
		case LnEnd:
			lineEnd = parseCurrentTextAsInt();
			break;
		case SourceFileID:
			sourceFileID = parseCurrentTextAsInt();
			break;
		case Coverage:
			coverage = parseCurrentTextAsInt();
			break;
		case Lines:
			endLinesElement();
			break;
		case SourceFileName:
			sourceFileName = currentText();
			break;
		case SourceFileNames:
			coverageCreator.storeFileMapping(sourceFileID, sourceFileName);
			break;
		default:
			// ignore other tags
			break;
		}
	}

	/** Ends a lines element. */
	private void endLinesElement() {
		coverageCreator.addLineCoverage(sourceFileID, lineStart, lineEnd, getLineCoverage(coverage));
	}

	/** Parses an int from the current text. */
	private int parseCurrentTextAsInt() throws SAXException {
		try {
			return Integer.parseInt(currentText());
		} catch (NumberFormatException e) {
			throw new SAXException("Error parsing number: " + e.getMessage());
		}
	}

	/**
	 * Maps the line coverage information in the XML report to the corresponding {@link ELineCoverage}.
	 * The mapping was reverse engineered from coverage reports.
	 */
	private static ELineCoverage getLineCoverage(int coverage) {
		if (coverage == 1) {
			return ELineCoverage.PARTIALLY_COVERED;
		} else if (coverage == 0) {
			return ELineCoverage.FULLY_COVERED;
		}
		return ELineCoverage.NOT_COVERED;
	}

	/** Returns the current text content. */
	private String currentText() {
		return textContent.toString().trim();
	}

	/** {@inheritDoc} */
	@Override
	public void characters(char[] ch, int start, int length) {
		textContent.append(ch, start, length);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy