XMLErrorListener.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */



  20. package org.openspcoop2.utils.xml;

  21. import java.io.PrintStream;

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

  24. /**
  25.  * Classe utilizzabile per ottenere gli errori generati dal parser
  26.  *
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */

  32. public class XMLErrorListener implements ErrorListener {

  33.     private boolean throwWarningException = true;
  34.     private boolean throwErrorException = true;
  35.     private boolean throwFatalErrorException = true;
  36.     private boolean printStackTraceWarningException = false;
  37.     private boolean printStackTraceErrorException = false;
  38.     private boolean printStackTraceFatalErrorException = false;
  39.    
  40.     private PrintStream warningOutputStream = System.out;
  41.     private PrintStream errorOutputStream = System.err;
  42.     private PrintStream fatalErrorOutputStream = System.err;
  43.    
  44.     XMLErrorListener(){}
  45.     XMLErrorListener(boolean throwWarningException,boolean throwErrorException,boolean throwFatalErrorException){
  46.         this(throwWarningException,throwErrorException,throwFatalErrorException,false,false,false);
  47.     }
  48.     XMLErrorListener(boolean throwWarningException,boolean throwErrorException,boolean throwFatalErrorException,
  49.             boolean printStackTraceWarningException,boolean printStackTraceErrorException, boolean printStackTraceFatalErrorException){
  50.        
  51.     }
  52.    
  53.     @Override
  54.     public void warning(TransformerException exception)
  55.             throws TransformerException {
  56.        
  57.         if(this.printStackTraceWarningException){
  58.             this.warningOutputStream.append("WARNING: "+exception.getMessage());
  59.             exception.printStackTrace(this.warningOutputStream);
  60.         }
  61.         if(this.throwWarningException)
  62.             throw exception;    
  63.        
  64.     }
  65.     @Override
  66.     public void error(TransformerException exception)
  67.             throws TransformerException {
  68.        
  69.         if(this.printStackTraceErrorException){
  70.             this.errorOutputStream.append("ERROR: "+exception.getMessage());
  71.             exception.printStackTrace(this.errorOutputStream);
  72.         }
  73.         if(this.throwErrorException)
  74.             throw exception;
  75.        
  76.     }
  77.     @Override
  78.     public void fatalError(TransformerException exception)
  79.             throws TransformerException {
  80.        
  81.         if(this.printStackTraceFatalErrorException){
  82.             this.fatalErrorOutputStream.append("FATAL ERROR: "+exception.getMessage());
  83.             exception.printStackTrace(this.fatalErrorOutputStream);
  84.         }
  85.         if(this.throwFatalErrorException)
  86.             throw exception;
  87.        
  88.     }

  89. }