/*
 * This file (ValidateXML.java) is part of DebGen-LabelPrintLib  developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 *  Copyright ASTRID Research, Hungary, 2010.
 *
 *  ASTRID Research
 *  Headquaters: Csapó utca 42. Debrecen H-4029 HUNGARY
 *  Phone: +36-52-533-173        Fax: +36-52-533-174
 *  Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DEBGEN-Biobank-Project.
 * File last modified at 2010.01.12 14:39
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.astrid.debgen.labelprint.util;

import hu.astrid.debgen.labelprint.exception.LabelPrintException;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author inagy
 */
public class ValidateXML {
 static final String XSD_SCHEMA = "/patient.xsd";
    /**
     * Default constructor.
     */
    public ValidateXML() {
    }

     /**
     * Validate XML document.
     * @param doc XML document.
     */
    public void validateXML(Document doc) throws LabelPrintException{
        try {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Source schemaFile = new StreamSource(getClass().getResource(XSD_SCHEMA).openStream());
            Schema schema = factory.newSchema(schemaFile);
            Validator validator = schema.newValidator();
            validator.validate(new DOMSource(doc));
        } catch (SAXException saxe) {
            throw new LabelPrintException(saxe);
        } catch (IOException ioe) {
            throw new LabelPrintException(ioe);
        }
    }
}
