Skip to content
Snippets Groups Projects
Commit e82a8678 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

Exception xml interface

parent ee2ed9d3
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,7 @@ AlibException::AlibException ( const std::string & cause ) {
this->cause = cause;
}
 
AlibException::~AlibException ( ) {
AlibException::~AlibException ( ) noexcept {
 
}
 
......
......@@ -25,7 +25,7 @@ public:
 
AlibException ( );
AlibException ( const std::string & cause );
virtual ~AlibException ( );
virtual ~AlibException ( ) noexcept;
 
/**
* @return reason why the exception occured
......
/*
* ExceptionFromXMLParser.cpp
*
* Created on: Nov 23, 2013
* Author: Jan Travnicek
*/
#include "ExceptionFromXMLParser.h"
#include "sax/ParserException.h"
#include <string>
namespace alib {
AlibException ExceptionFromXMLParser::parse(std::list<sax::Token>& input) {
popToken(input, sax::Token::START_ELEMENT, "exception");
std::string cause = "";
if (input.front().getType() == sax::Token::CHARACTER) {
cause = input.front().getData();
input.pop_front();
}
popToken(input, sax::Token::END_ELEMENT, "exception");
return AlibException(cause);
}
bool ExceptionFromXMLParser::isToken(std::list<sax::Token>& input, sax::Token::TokenType type, std::string data) {
return input.front().getType() == type && input.front().getData() == data;
}
void ExceptionFromXMLParser::popToken(std::list<sax::Token>& input, sax::Token::TokenType type, std::string data) {
if (isToken(input, type, data)) {
input.pop_front();
} else {
throw sax::ParserException(sax::Token(data, type), input.front());
}
}
} /* namespace alib */
/*
* ExceptionFromXMLParser.h
*
* Created on: Nov 23, 2013
* Author: Jan Travnicek
*/
#ifndef EXCEPTION_FROM_XML_PARSER_H_
#define EXCEPTION_FROM_XML_PARSER_H_
#include <list>
#include "AlibException.h"
#include "sax/Token.h"
namespace alib {
/**
* Parser used to get String from XML parsed into list of tokens.
*/
class ExceptionFromXMLParser {
bool isToken(std::list<sax::Token> &input, sax::Token::TokenType type, std::string data);
void popToken(std::list<sax::Token> &input, sax::Token::TokenType type, std::string data);
public:
/**
* Parses the XML and returns regular expression. The input is destroyed in the process.
* @param input XML represented as list of tokens
* @return AlibException
* @throws ParserException when an error occurs
*/
AlibException parse(std::list<sax::Token>& input);
};
} /* namespace alib */
#endif /* EXCEPTION_FROM_XML_PARSER_H_ */
/*
* ExceptionToXMLComposer.cpp
*
* Created on: Nov 23, 2013
* Author: Jan Travnicek
*/
#include "ExceptionToXMLComposer.h"
namespace alib {
void ExceptionToXMLComposer::Visit(std::list<sax::Token>& out, const AlibException& exception) {
out.push_back(sax::Token("exception", sax::Token::START_ELEMENT));
out.push_back(sax::Token(exception.getCause(), sax::Token::CHARACTER));
out.push_back(sax::Token("exception", sax::Token::END_ELEMENT));
}
std::list<sax::Token> ExceptionToXMLComposer::compose(const AlibException& exception) {
std::list<sax::Token> out;
Visit(out, exception);
return out;
}
} /* namespace alib */
/*
* ExceptionToXMLComposer.h
*
* Created on: Nov 23, 2013
* Author: Jan Travnciek
*/
#ifndef EXCEPTION_TO_XML_COMPOSER_H_
#define EXCEPTION_TO_XML_COMPOSER_H_
#include <list>
#include "AlibException.h"
#include "sax/Token.h"
namespace alib {
/**
* This class contains methods to print XML representation of string to the output stream.
*/
class ExceptionToXMLComposer {
static void Visit(std::list<sax::Token>&, const AlibException& exception);
public:
/**
* Prints XML representation of AlibException to the output stream.
* @param string Exception to print
* @param out output stream to which print the String
*/
std::list<sax::Token> compose(const AlibException& exception);
};
} /* namespace alib */
#endif /* EXCEPTION_TO_XML_COMPOSER_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment