import java.io.*;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import java.nio.file.Files;
import java.nio.file.Paths;


class ValidationXsl {

	public static void main(String[] args) throws FileNotFoundException, IOException, TransformerConfigurationException {
		System.out.println("- - ValidationXsl - -"); 

		// Constantes
		boolean debbug = true;

		// Arguments
		if (debbug) System.out.println("- Paramètres -"); 
		String fichierXslPath = args[0];
		System.out.println("fichierXsl : "+fichierXslPath);
		
		String fichierXmlPath = args[1];
		System.out.println("fichierXml : "+fichierXmlPath);

		String fichierXmlSortiePath = args[2];
		System.out.println("fichierXmlSortiePath : "+fichierXmlSortiePath);
		
		// Fichiers
		if (debbug) System.out.println("- Ouverture Fichiers -"); 
		File fichierXsl = new File(fichierXslPath);  
		File fichierXml = new File(fichierXmlPath);  
		
		FileOutputStream fichierXmlOutputStream = new  FileOutputStream(fichierXmlSortiePath);
		Result fichierXmlOutputResult = new StreamResult(fichierXmlOutputStream);

		// Application xsl sur xml
		if (debbug) System.out.println("- Application xsl sur xml -"); 
		try {
			if (debbug) System.out.println(" debbug - 0010 - xsl - StreamSource"); 
			Source xslSource = new StreamSource( fichierXsl );

			if (debbug) System.out.println(" debbug - 0020 - xml - StreamSource"); 
			Source xmlSource = new StreamSource( fichierXml );
			
			TransformerFactory xslFactory = TransformerFactory.newInstance();
			if (debbug) System.out.println(" debbug - 0110 - xsl - newTemplates fichierXsl"); 
			// Peut nécessiter la désactivation d'une limite défaut de java :
			// java -Djdk.xml.xpathTotalOpLimit=0 ValidationXsl ...
			
			System.out.println("- - - - - -");
			System.out.println("En cas d'erreur de type :");
			System.out.println("ERROR:  'JAXP0801003: the compiler encountered XPath expressions with an accumulated '10,001' ...");
			System.out.println("Tentez la désactivation de la limite par défaut :");
			System.out.println("> java -Djdk.xml.xpathTotalOpLimit=0 ValidationXsl ...");
			System.out.println("- - - - - -");
			
			Templates xslTemplate = xslFactory.newTemplates(xslSource);
			
			if (debbug) System.out.println(" debbug - 0120 - xsl - newTransformer"); 
			Transformer xslTransformer = xslTemplate.newTransformer();
			
			if (debbug) System.out.println(" debbug - 0140 - xml - transform"); 
			xslTransformer.transform(xmlSource, fichierXmlOutputResult);
			
			System.out.println("Résultat de la transformation dispo. dans  : "+fichierXmlSortiePath);

        } catch (TransformerConfigurationException e) {
            // An error occurred in the XSL file
            throw e;
		} catch (TransformerException e) {
            // An error occurred while applying the XSL file
            // Get location of error in input file
            SourceLocator locator = e.getLocator();
            int col = locator.getColumnNumber();
            int line = locator.getLineNumber();
            String publicId = locator.getPublicId();
            String systemId = locator.getSystemId();
        }

	}
}
