Un gestionnaire d'erreurs implémentant l'interface ErrorListener peut être affecté au transformateur XSLT. Dans ce cas, tous les événements liés à des erreurs durant le processus de transformation seront pris en charge par ce gestionnaire.

import javax.xml.transform.ErrorListener;
import javax.xml.transform.TransformerException;

public class GestionnaireErreurs implements ErrorListener {
  public void error(TransformerException exception) {
    System.err.println(
             "ERREUR LORS DU PROCESSUS DE TRANSFORMATION : ");
    exception.printStackTrace();
  }
  public void fatalError(TransformerException exception) {
    System.err.println(
             "ERREUR FATALE LORS DU PROCESSUS DE TRANSFORMATION : ");
    exception.printStackTrace();
  }
  public void warning(TransformerException exception) {
    System.err.println(
             "AVERTISSEMENT LORS DU PROCESSUS DE TRANSFORMATION : ");
    exception.printStackTrace();
  }
}

TransformerFactory fabrique = TransformerFactory.newInstance();
Transformer transformateur = fabrique.newTransformer(sourceXSL);

transformateur.setErrorListener(new GestionnaireErreurs());

L'interface ErrorListener se trouve dans le paquetage javax.xml.transform, à ne pas confondre avec l'interface ErrorHandler du paquetage org.xml.sax.

Le gestionnaire d'erreurs peut être récupéré en sollicitant la méthode getErrorListener().

ErrorHandler gestErreurs = transformeur.getErrorListener();

Les objets TransformerFactory peuvent aussi définir un gestionnaire d'erreurs ErrorListener qui servira pour rapporter les erreurs de traitement des instructions de transformation. Ce gestionnaire ne prendra pas en compte les erreurs émises lors du processus de transformation avec l'objet Transformer.

fabrique.setErrorListener(new GestionnaireErreurs());
public class StyleXSLT {

    public static void main(String[] args) {
        try {
            XMLReader analyseurSax = XMLReaderFactory.createXMLReader();
            File sourceEntreeXSL = new File("logitheque.xsl");
            StreamSource sourceXSL = new StreamSource(sourceEntreeXSL);
            File sourceEntreeXML = new File("logitheque.xml");
            StreamSource sourceXML = new StreamSource(sourceEntreeXML);

            TransformerFactory fabrique = TransformerFactory.newInstance();
            Transformer transformateur = fabrique.newTransformer(sourceXSL);

            transformateur.setErrorListener(new GestionnaireErreurs());

            Properties proprietes = new Properties();
            proprietes.put("method", "html");
            proprietes.put("encoding", "ISO-8859-1");
            proprietes.put("indent", "yes");
            transformateur.setOutputProperties(proprietes);

            transformateur.setParameter("num", "10");

            File oFic = new File("resultat.html");
            FileOutputStream fos = new FileOutputStream(oFic);

            if (fos != null) {
                Result sortie = new StreamResult(fos);
                transformateur.transform(sourceXML, sortie);
            }

            fos.flush();
            fos.close();
        }
        catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (TransformerException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}