/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.csu.sdmx.validation;

import cz.csu.sdmx.validation.model.ValidationResult;
import cz.csu.sdmx.validation.model.ValidationError;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 *
 * @author themi
 */
public class SimpleErrorHandler implements ErrorHandler{
    private ValidationResult result;
    private String filename;
    
    public SimpleErrorHandler(String filename, ValidationResult result) {
        this.filename = filename;
        this.result = result;
    }
    
    @Override
    public void warning(SAXParseException e) throws SAXException {
        result.addError(createError(ValidationError.WARNING, e));
    }

    @Override
    public void error(SAXParseException e) throws SAXException {
        result.addError(createError(ValidationError.ERROR, e));
    }

    @Override
    public void fatalError(SAXParseException e) throws SAXException {
        result.addError(createError(ValidationError.FATAL, e));
    }
    
    private ValidationError createError(String type, SAXParseException e){
        ValidationError error = new ValidationError();
        error.setFilename(filename);
        error.setText(e.getLocalizedMessage());
        error.setLineNumber(String.valueOf(e.getLineNumber()));
        error.setType(type);
        
        return error;
    }
    
}
