XMLErrorHandler.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 org.xml.sax.ErrorHandler;
  23. import org.xml.sax.SAXException;
  24. import org.xml.sax.SAXParseException;

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

  33. public class XMLErrorHandler implements ErrorHandler {

  34.     private boolean throwWarningException = true;
  35.     private boolean throwErrorException = true;
  36.     private boolean throwFatalErrorException = true;
  37.     private boolean printStackTraceWarningException = false;
  38.     private boolean printStackTraceErrorException = false;
  39.     private boolean printStackTraceFatalErrorException = false;
  40.    
  41.     private PrintStream warningOutputStream = System.out;
  42.     private PrintStream errorOutputStream = System.err;
  43.     private PrintStream fatalErrorOutputStream = System.err;
  44.    
  45.     XMLErrorHandler(){}
  46.     XMLErrorHandler(boolean throwWarningException,boolean throwErrorException,boolean throwFatalErrorException){
  47.         this(throwWarningException,throwErrorException,throwFatalErrorException,false,false,false);
  48.     }
  49.     XMLErrorHandler(boolean throwWarningException,boolean throwErrorException,boolean throwFatalErrorException,
  50.             boolean printStackTraceWarningException,boolean printStackTraceErrorException, boolean printStackTraceFatalErrorException){
  51.         this.throwWarningException = throwWarningException;
  52.         this.throwErrorException = throwErrorException;
  53.         this.throwFatalErrorException = throwFatalErrorException;
  54.         this.printStackTraceWarningException = printStackTraceWarningException;
  55.         this.printStackTraceErrorException = printStackTraceErrorException;
  56.         this.printStackTraceFatalErrorException = printStackTraceFatalErrorException;
  57.     }
  58.    
  59.     @Override
  60.     public void warning(SAXParseException exception) throws SAXException {
  61.         if(this.printStackTraceWarningException){
  62.             this.warningOutputStream.append("WARNING: "+exception.getMessage());
  63.             exception.printStackTrace(this.warningOutputStream);
  64.         }
  65.         if(this.throwWarningException)
  66.             throw exception;
  67.     }

  68.     @Override
  69.     public void error(SAXParseException exception) throws SAXException {
  70.         if(this.printStackTraceErrorException){
  71.             this.errorOutputStream.append("ERROR: "+exception.getMessage());
  72.             exception.printStackTrace(this.errorOutputStream);
  73.         }
  74.         if(this.throwErrorException)
  75.             throw exception;
  76.     }

  77.     @Override
  78.     public void fatalError(SAXParseException exception) throws SAXException {
  79.         if(this.printStackTraceFatalErrorException){
  80.             this.fatalErrorOutputStream.append("FATAL ERROR: "+exception.getMessage());
  81.             exception.printStackTrace(this.fatalErrorOutputStream);
  82.         }
  83.         if(this.throwFatalErrorException)
  84.             throw exception;
  85.     }

  86. }