Commit af7e4ea8 authored by Pakulin's avatar Pakulin
Browse files

Extended error detection

parent 61646799
Loading
Loading
Loading
Loading
+77 −0
Original line number Diff line number Diff line
package org.etsi.mts.ttcn.part9.xmldiff;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.custommonkey.xmlunit.Difference;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ParserErrorHandler implements ErrorHandler {
public class DiffErrorHandler implements ErrorHandler {
	private List<SAXParseException> errors = new ArrayList<>();
	private List<SAXParseException> warnings = new ArrayList<>();
	private static final String NEWLINE = System.getProperty("line.separator");
	private boolean hasErrors = false;
	
	private StringBuilder description = new StringBuilder();
	
	public String getErrorsText() {
		return description.toString();
	}
	
	public boolean hasErrors() {
		return hasErrors;
	}
	
	public synchronized void reset(boolean clearLog) {
		hasErrors = false;
		if (clearLog) {
			description = new StringBuilder();
		}
	}
	/*************************************************************
	 * 
	 * Parser error handler
	 * 
	 *************************************************************/
	@Override
	public void error(SAXParseException err) throws SAXException {
		errors.add(err);
		throw err;
		appendDescripion(err);
		hasErrors = true;
	}

	@Override
	public void fatalError(SAXParseException err) throws SAXException {
		errors.add(err);
		throw err;
		appendDescripion(err);
		hasErrors = true;
	}

	private synchronized void appendDescripion(Throwable err) {
		StringWriter sw = new StringWriter();
		err.printStackTrace(new PrintWriter(sw));
		description.append(sw).append(NEWLINE);
	}
	@Override
	public void warning(SAXParseException warn) throws SAXException {
		warnings.add(warn);
	}

	public List<SAXParseException> getErrors() {
		return errors;
	/************************************************************
	 * 
	 * Differences
	 * 
	 ************************************************************/
	public void difference(Difference diff) {
		hasErrors = true;
		appendDescripion(diff);
	}
	
	public List<SAXParseException> getWarnings() {
		return warnings;
	private synchronized void appendDescripion(Difference diff) {
		description.append(diff.getDescription()).append(NEWLINE);
	}
	
}
 No newline at end of file
+8 −4
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ public class XmlDiff {
	private List<File> xsdFiles;
	private Schema schemes = null;
	private DocumentBuilderFactory xmlParserFactory;
	private DiffErrorHandler errorHandler = new DiffErrorHandler();

	/** Initialize the diff engine.
	 * 
@@ -165,6 +166,10 @@ public class XmlDiff {
		} catch (IOException e) {
			throw new XmlDiffError("Failed to read XML", e);
		}
		if (errorHandler.hasErrors()) {
			diffDetails.append(errorHandler.getErrorsText());
			return false;
		}
		
		XMLUnit.setIgnoreComments(true);
		XMLUnit.setIgnoreWhitespace(true);
@@ -240,18 +245,17 @@ public class XmlDiff {
	}

	private Document parseXml(Reader inReader, String kind) throws SAXException, IOException, XmlDiffError {
		ParserErrorHandler handler = new ParserErrorHandler();
		DocumentBuilder parser;
		try {
			parser = xmlParserFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			throw new RuntimeException("Internal error: failed to create an XML parser", e);
		}
		parser.setErrorHandler(handler);
		parser.setErrorHandler(errorHandler);
		InputSource input = new InputSource(inReader);
		Document result = parser.parse(input);
		if (handler.getErrors().size() > 0) {
			throw new XmlDiffError("Failed to parse " + kind + ": " + handler.getErrors());
		if (errorHandler.hasErrors()) {
			throw new XmlDiffError("Failed to parse " + kind + ": " + errorHandler.getErrorsText());
		}
		return result;
	}