Exception

XMLFileErrorHandler

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*
 * =====================================================================================
 *
 *       Filename:  CSCDQM_Exception.h
 *
 *    Description:  Custom Exception
 *
 *        Version:  1.0
 *        Created:  11/14/2008 11:51:31 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Valdas Rapsevicius (VR), valdas.rapsevicius@cern.ch
 *        Company:  CERN, CH
 *
 * =====================================================================================
 */

#ifndef CSCDQM_Exception_H
#define CSCDQM_Exception_H

#include <string>
#include <exception>

#include <xercesc/sax/ErrorHandler.hpp>
#include <xercesc/sax/SAXParseException.hpp>

#include "CSCDQM_Logger.h"

namespace cscdqm {

  /**
   * @class Exception
   * @brief Application level Exception that is used to cut-off application
   * execution in various cases.
   */
  class Exception : public std::exception {
  private:
    std::string message;

  public:
    Exception(const std::string& message) throw() { this->message = message; }

    ~Exception() throw() override {}

    const char* what() const throw() override { return message.c_str(); }
  };

  /**
   * @class XMLFileErrorHandler
   * @brief Takes care of errors and warnings while parsing XML files
   * file in XML format.
   */
  class XMLFileErrorHandler : public XERCES_CPP_NAMESPACE::ErrorHandler {
  public:
    void warning(const XERCES_CPP_NAMESPACE::SAXParseException& exc) override {
      char* message = XERCES_CPP_NAMESPACE::XMLString::transcode(exc.getMessage());
      LOG_WARN << "File: " << message << ". line: " << exc.getLineNumber() << " col: " << exc.getColumnNumber();
      XERCES_CPP_NAMESPACE::XMLString::release(&message);
    }

    void error(const XERCES_CPP_NAMESPACE::SAXParseException& exc) override { this->fatalError(exc); }

    void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exc) override {
      char* message = XERCES_CPP_NAMESPACE::XMLString::transcode(exc.getMessage());
      LOG_COUT << "File: " << message << ". line: " << exc.getLineNumber() << " col: " << exc.getColumnNumber();
      throw Exception(message);
    }

    void resetErrors() override {}
  };

}  // namespace cscdqm

#endif