/*
 * 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.xml;

/**
 *
 * @author themi
 */
import cz.csu.sdmx.validation.model.ValidationResult;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;

public class XmlValidator {
    
    public static Document validate(String filename, InputStream is, ValidationResult result){
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
            //set filename to document as user data
            doc.setUserData(XmlHandler.FILENAME_KEY_NAME, filename, null);
            
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
                    
            SAXParser parser = factory.newSAXParser();
            parser.parse(is, new XmlHandler(doc, result));
            return doc;
        } catch (Exception ex) {
            return null;
        }
    }
    
}
