From 4142d87687bbe253892238fc2ff001b32288f548 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sat, 5 Jul 2014 10:29:35 +0200 Subject: [PATCH] parse exceptions in all parsers --- alib2/src/FromXMLParser.cpp | 31 +++++++++ alib2/src/FromXMLParser.hpp | 49 +++++++++----- alib2/src/factory/AutomatonFactory.cpp | 14 ++-- alib2/src/factory/AutomatonFactory.h | 6 +- alib2/src/factory/ExceptionFactory.cpp | 67 +++++++++++++++++++ alib2/src/factory/ExceptionFactory.h | 92 ++++++++++++++++++++++++++ alib2/src/factory/LabelFactory.cpp | 14 ++-- alib2/src/factory/LabelFactory.h | 6 +- alib2/src/factory/RegExpFactory.cpp | 14 ++-- alib2/src/factory/RegExpFactory.h | 6 +- alib2/src/factory/StringFactory.cpp | 14 ++-- alib2/src/factory/StringFactory.h | 6 +- alib2/src/factory/SymbolFactory.cpp | 14 ++-- alib2/src/factory/SymbolFactory.h | 6 +- 14 files changed, 267 insertions(+), 72 deletions(-) create mode 100644 alib2/src/FromXMLParser.cpp create mode 100644 alib2/src/factory/ExceptionFactory.cpp create mode 100644 alib2/src/factory/ExceptionFactory.h diff --git a/alib2/src/FromXMLParser.cpp b/alib2/src/FromXMLParser.cpp new file mode 100644 index 0000000000..1eafd5d8c4 --- /dev/null +++ b/alib2/src/FromXMLParser.cpp @@ -0,0 +1,31 @@ +/* + * FromXMLParsers.cpp + * + * Created on: Apr 1, 2013 + * Author: Jan Travnicek + */ + +#include "FromXMLParser.hpp" +#include "FromXMLParsers.h" + +namespace alib { + +// NOTE: do not remove this explicit specialisation of parseValue for Exceptions unless you refactor ExceptionFromXMLParser. The implicit specialisation causes inifite recursion +template<> +exception::AlibException FromXMLParser<exception::AlibException, exception::FEATURES>::parseValue(std::list<sax::Token>& input) const { + exception::AlibException res = parse(input); + + if(input.size() == 0) { + return res; + } else { + throw exception::AlibException(); + } +} + +void tryParseAndThrowException(std::list<sax::Token>& input) { + if(FromXMLParsers::exceptionParser.first(input)) { + throw FromXMLParsers::exceptionParser.parseValue(input); + } +} + +} /* namespace alib */ diff --git a/alib2/src/FromXMLParser.hpp b/alib2/src/FromXMLParser.hpp index f772cfcaad..0e6d22260e 100644 --- a/alib2/src/FromXMLParser.hpp +++ b/alib2/src/FromXMLParser.hpp @@ -12,7 +12,6 @@ #include <list> #include <set> #include "sax/Token.h" -#include "exception/AlibException.h" namespace alib { @@ -33,13 +32,7 @@ public: * @return Symbol* the parsed symbol or NULL when by first token it is not a symbol * @throws ParserException when tokens do not represent Symbol but first token seemd as a symbol */ - T* parsePointer(std::list<sax::Token>& input) const { - if(first(input)) { - return new T(parse(input)); - } else { - return NULL; - } - } + T* parsePointer(std::list<sax::Token>& input) const; /** * Parses the XML tokens and returns symbol. The input is destroyed in the process. @@ -47,18 +40,40 @@ public: * @return Symbol the parsed symbol * @throws ParserException when tokens do not represent Symbol */ - T parseValue(std::list<sax::Token>& input) const { - T res = parse(input); - - if(input.size() == 0) { - return res; - } else { - throw exception::AlibException(); - } - } + T parseValue(std::list<sax::Token>& input) const; }; +void tryParseAndThrowException(std::list<sax::Token>& input); + +} /* namespace alib */ + +#include "exception/AlibException.h" +#include "exception/ExceptionFeatures.h" + +namespace alib { + +template<class T, class F> +T* FromXMLParser<T, F>::parsePointer(std::list<sax::Token>& input) const { + if(first(input)) { + return new T(parse(input)); + } else { + return NULL; + } +} + +template<class T, class F> +T FromXMLParser<T, F>::parseValue(std::list<sax::Token>& input) const { + tryParseAndThrowException(input); + T res = parse(input); + + if(input.size() == 0) { + return res; + } else { + throw exception::AlibException(); + } +} + } /* namespace alib */ #endif /* FROM_XML_PARSER_HELPER_H_ */ diff --git a/alib2/src/factory/AutomatonFactory.cpp b/alib2/src/factory/AutomatonFactory.cpp index 5c9db6d2f9..44315d746b 100644 --- a/alib2/src/factory/AutomatonFactory.cpp +++ b/alib2/src/factory/AutomatonFactory.cpp @@ -17,13 +17,13 @@ namespace automaton { Automaton AutomatonFactory::fromFile(const std::string& filename) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseFile(filename, tokens); - return parse(tokens); + return fromTokens(tokens); } Automaton AutomatonFactory::fromString(const std::string& str) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseMemory(str, tokens); - return parse(tokens); + return fromTokens(tokens); } Automaton AutomatonFactory::fromStdin() { @@ -35,7 +35,7 @@ Automaton AutomatonFactory::fromStream(std::istream& in) { return AutomatonFactory::fromString(input); } -Automaton AutomatonFactory::parse(std::list<sax::Token> tokens) { +Automaton AutomatonFactory::fromTokens(std::list<sax::Token> tokens) { return alib::FromXMLParsers::automatonParser.parseValue(tokens); } @@ -56,12 +56,12 @@ void AutomatonFactory::toStream(const Automaton& automaton, std::ostream& out) { } void AutomatonFactory::toFile(const AutomatonBase& automaton, const std::string& filename) { - std::list<sax::Token> tokens = compose(automaton); + std::list<sax::Token> tokens = toTokens(automaton); sax::SaxComposeInterface::printFile(filename, tokens); } std::string AutomatonFactory::toString(const AutomatonBase& automaton) { - std::list<sax::Token> tokens = compose(automaton); + std::list<sax::Token> tokens = toTokens(automaton); std::string str; sax::SaxComposeInterface::printMemory(str, tokens); return str; @@ -72,11 +72,11 @@ void AutomatonFactory::toStdout(const AutomatonBase& automaton) { } void AutomatonFactory::toStream(const AutomatonBase& automaton, std::ostream& out) { - std::list<sax::Token> tokens = compose(automaton); + std::list<sax::Token> tokens = toTokens(automaton); sax::SaxComposeInterface::printStream(out, tokens); } -std::list<sax::Token> AutomatonFactory::compose(const AutomatonBase& automaton) { +std::list<sax::Token> AutomatonFactory::toTokens(const AutomatonBase& automaton) { return alib::ToXMLComposers::automatonComposer.compose(automaton); } diff --git a/alib2/src/factory/AutomatonFactory.h b/alib2/src/factory/AutomatonFactory.h index 74808c7896..e175fc2a2e 100644 --- a/alib2/src/factory/AutomatonFactory.h +++ b/alib2/src/factory/AutomatonFactory.h @@ -98,21 +98,19 @@ public: */ static void toStream(const AutomatonBase&, std::ostream& out); -protected: - /** * Parses the Automaton* from the list of tokens. * @param tokens XML represented as list of tokens * @return parsed Automaton* */ - static Automaton parse(std::list<sax::Token> tokens); + static Automaton fromTokens(std::list<sax::Token> tokens); /** * Parses the RegExp from list of tokens. * @param tokens XML represented as list of tokens * @return parsed RegExp */ - static std::list<sax::Token> compose(const AutomatonBase&); + static std::list<sax::Token> toTokens(const AutomatonBase&); }; } /* namespace automaton */ diff --git a/alib2/src/factory/ExceptionFactory.cpp b/alib2/src/factory/ExceptionFactory.cpp new file mode 100644 index 0000000000..c72f09762b --- /dev/null +++ b/alib2/src/factory/ExceptionFactory.cpp @@ -0,0 +1,67 @@ +/* + * ExceptionFactory.cpp + * + * Created on: Jan 1, 2014 + * Author: Jan Travnicek + */ + +#include "ExceptionFactory.h" + +#include "../sax/SaxParseInterface.h" +#include "../sax/SaxComposeInterface.h" +#include "../FromXMLParsers.h" +#include "../ToXMLComposers.h" + +namespace exception { + +AlibException ExceptionFactory::fromFile(const std::string& filename) { + std::list<sax::Token> tokens; + sax::SaxParseInterface::parseFile(filename, tokens); + return fromTokens(tokens); +} + +AlibException ExceptionFactory::fromString(const std::string& str) { + std::list<sax::Token> tokens; + sax::SaxParseInterface::parseMemory(str, tokens); + return fromTokens(tokens); +} + +AlibException ExceptionFactory::fromStdin() { + return ExceptionFactory::fromStream(std::cin); +} + +AlibException ExceptionFactory::fromStream(std::istream& in) { + std::string input(std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>())); + return ExceptionFactory::fromString(input); +} + +AlibException ExceptionFactory::fromTokens(std::list<sax::Token> tokens) { + return alib::FromXMLParsers::exceptionParser.parseValue(tokens); +} + +void ExceptionFactory::toFile(const AlibException& exception, const std::string& filename) { + std::list<sax::Token> tokens = toTokens(exception); + sax::SaxComposeInterface::printFile(filename, tokens); +} + +std::string ExceptionFactory::toString(const AlibException& exception) { + std::list<sax::Token> tokens = toTokens(exception); + std::string str; + sax::SaxComposeInterface::printMemory(str, tokens); + return str; +} + +void ExceptionFactory::toStdout(const AlibException& exception) { + return ExceptionFactory::toStream(exception, std::cout); +} + +void ExceptionFactory::toStream(const AlibException& exception, std::ostream& out) { + std::list<sax::Token> tokens = toTokens(exception); + sax::SaxComposeInterface::printStream(out, tokens); +} + +std::list<sax::Token> ExceptionFactory::toTokens(const AlibException& exception) { + return alib::ToXMLComposers::exceptionComposer.compose(exception); +} + +} /* namespace exception */ diff --git a/alib2/src/factory/ExceptionFactory.h b/alib2/src/factory/ExceptionFactory.h new file mode 100644 index 0000000000..bf7eb89533 --- /dev/null +++ b/alib2/src/factory/ExceptionFactory.h @@ -0,0 +1,92 @@ +/* + * AlibExceptionFactory.h + * + * Created on: Jan 1, 2014 + * Author: Jan Travnicek + */ + +#ifndef EXCEPTION_FACTORY_H_ +#define EXCEPTION_FACTORY_H_ + +#include <string> +#include <list> +#include "../sax/Token.h" +#include "../exception/AlibException.h" + +namespace exception { + +/** + * String builder. + */ +class ExceptionFactory { +public: + /** + * Parses the XML in file and returns the String. + * @param filename path to the file + * @return String + */ + static AlibException fromFile(const std::string& filename); + + /** + * Parses the XML and returns the String. + * @param str string containing the XML + * @return String + */ + static AlibException fromString(const std::string& str); + + /** + * Parses the XML from stdin and returns the String. + * @return String + */ + static AlibException fromStdin(); + + /** + * Parses the XML from stream and returns the String. + * @return String + */ + static AlibException fromStream(std::istream& in); + + /** + * Parses the XML in file and returns the String. + * @param filename path to the file + * @return String + */ + static void toFile(const AlibException&, const std::string& filename); + + /** + * Parses the XML and returns the String. + * @param str string containing the XML + * @return String + */ + static std::string toString(const AlibException&); + + /** + * Parses the XML from stdin and returns the String. + * @return String + */ + static void toStdout(const AlibException&); + + /** + * Parses the XML from stream and returns the String. + * @return String + */ + static void toStream(const AlibException&, std::ostream& out); + + /** + * Parses the String from list of tokens. + * @param tokens XML represented as list of tokens + * @return parsed String + */ + static AlibException fromTokens(std::list<sax::Token> tokens); + + /** + * Parses the String from list of tokens. + * @param tokens XML represented as list of tokens + * @return parsed String + */ + static std::list<sax::Token> toTokens(const AlibException&); +}; + +} /* namespace exception */ + +#endif /* EXCEPTION_FACTORY_H_ */ diff --git a/alib2/src/factory/LabelFactory.cpp b/alib2/src/factory/LabelFactory.cpp index 806f3c8092..3ba4f012c1 100644 --- a/alib2/src/factory/LabelFactory.cpp +++ b/alib2/src/factory/LabelFactory.cpp @@ -17,13 +17,13 @@ namespace label { Label LabelFactory::fromFile(const std::string& filename) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseFile(filename, tokens); - return parse(tokens); + return fromTokens(tokens); } Label LabelFactory::fromString(const std::string& str) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseMemory(str, tokens); - return parse(tokens); + return fromTokens(tokens); } Label LabelFactory::fromStdin() { @@ -35,17 +35,17 @@ Label LabelFactory::fromStream(std::istream& in) { return LabelFactory::fromString(input); } -Label LabelFactory::parse(std::list<sax::Token> tokens) { +Label LabelFactory::fromTokens(std::list<sax::Token> tokens) { return alib::FromXMLParsers::labelParser.parseValue(tokens); } void LabelFactory::toFile(const Label& symbol, const std::string& filename) { - std::list<sax::Token> tokens = compose(symbol); + std::list<sax::Token> tokens = toTokens(symbol); sax::SaxComposeInterface::printFile(filename, tokens); } std::string LabelFactory::toString(const Label& symbol) { - std::list<sax::Token> tokens = compose(symbol); + std::list<sax::Token> tokens = toTokens(symbol); std::string str; sax::SaxComposeInterface::printMemory(str, tokens); return str; @@ -56,11 +56,11 @@ void LabelFactory::toStdout(const Label& symbol) { } void LabelFactory::toStream(const Label& symbol, std::ostream& out) { - std::list<sax::Token> tokens = compose(symbol); + std::list<sax::Token> tokens = toTokens(symbol); sax::SaxComposeInterface::printStream(out, tokens); } -std::list<sax::Token> LabelFactory::compose(const Label& symbol) { +std::list<sax::Token> LabelFactory::toTokens(const Label& symbol) { return alib::ToXMLComposers::labelComposer.compose(symbol); } diff --git a/alib2/src/factory/LabelFactory.h b/alib2/src/factory/LabelFactory.h index 7f35790d23..a8e5e79f48 100644 --- a/alib2/src/factory/LabelFactory.h +++ b/alib2/src/factory/LabelFactory.h @@ -72,21 +72,19 @@ public: */ static void toStream(const Label&, std::ostream& out); - -protected: /** * Parses the String from list of tokens. * @param tokens XML represented as list of tokens * @return parsed String */ - static Label parse(std::list<sax::Token> tokens); + static Label fromTokens(std::list<sax::Token> tokens); /** * Parses the String from list of tokens. * @param tokens XML represented as list of tokens * @return parsed String */ - static std::list<sax::Token> compose(const Label&); + static std::list<sax::Token> toTokens(const Label&); }; } /* namespace label */ diff --git a/alib2/src/factory/RegExpFactory.cpp b/alib2/src/factory/RegExpFactory.cpp index 26ff3043f5..f47d09f0b7 100644 --- a/alib2/src/factory/RegExpFactory.cpp +++ b/alib2/src/factory/RegExpFactory.cpp @@ -17,13 +17,13 @@ namespace regexp { RegExp RegExpFactory::fromFile(const std::string& filename) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseFile(filename, tokens); - return parse(tokens); + return fromTokens(tokens); } RegExp RegExpFactory::fromString(const std::string& str) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseMemory(str, tokens); - return parse(tokens); + return fromTokens(tokens); } RegExp RegExpFactory::fromStdin() { @@ -35,17 +35,17 @@ RegExp RegExpFactory::fromStream(std::istream& in) { return RegExpFactory::fromString(input); } -RegExp RegExpFactory::parse(std::list<sax::Token> tokens) { +RegExp RegExpFactory::fromTokens(std::list<sax::Token> tokens) { return alib::FromXMLParsers::regexpParser.parseValue(tokens); } void RegExpFactory::toFile(const RegExp& regexp, const std::string& filename) { - std::list<sax::Token> tokens = compose(regexp); + std::list<sax::Token> tokens = toTokens(regexp); sax::SaxComposeInterface::printFile(filename, tokens); } std::string RegExpFactory::toString(const RegExp& regexp) { - std::list<sax::Token> tokens = compose(regexp); + std::list<sax::Token> tokens = toTokens(regexp); std::string str; sax::SaxComposeInterface::printMemory(str, tokens); return str; @@ -56,11 +56,11 @@ void RegExpFactory::toStdout(const RegExp& regexp) { } void RegExpFactory::toStream(const RegExp& regexp, std::ostream& out) { - std::list<sax::Token> tokens = compose(regexp); + std::list<sax::Token> tokens = toTokens(regexp); sax::SaxComposeInterface::printStream(out, tokens); } -std::list<sax::Token> RegExpFactory::compose(const RegExp& regexp) { +std::list<sax::Token> RegExpFactory::toTokens(const RegExp& regexp) { return alib::ToXMLComposers::regexpComposer.compose(regexp); } diff --git a/alib2/src/factory/RegExpFactory.h b/alib2/src/factory/RegExpFactory.h index a96abfd84b..aba31574dc 100644 --- a/alib2/src/factory/RegExpFactory.h +++ b/alib2/src/factory/RegExpFactory.h @@ -72,21 +72,19 @@ public: */ static void toStream(const RegExp&, std::ostream& out); - -protected: /** * Parses the RegExp from list of tokens. * @param tokens XML represented as list of tokens * @return parsed RegExp */ - static RegExp parse(std::list<sax::Token> tokens); + static RegExp fromTokens(std::list<sax::Token> tokens); /** * Parses the RegExp from list of tokens. * @param tokens XML represented as list of tokens * @return parsed RegExp */ - static std::list<sax::Token> compose(const RegExp&); + static std::list<sax::Token> toTokens(const RegExp&); }; } /* namespace regexp */ diff --git a/alib2/src/factory/StringFactory.cpp b/alib2/src/factory/StringFactory.cpp index 008d49552c..97fdc3949d 100644 --- a/alib2/src/factory/StringFactory.cpp +++ b/alib2/src/factory/StringFactory.cpp @@ -17,13 +17,13 @@ namespace string { String StringFactory::fromFile(const std::string& filename) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseFile(filename, tokens); - return parse(tokens); + return fromTokens(tokens); } String StringFactory::fromString(const std::string& str) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseMemory(str, tokens); - return parse(tokens); + return fromTokens(tokens); } String StringFactory::fromStdin() { @@ -35,17 +35,17 @@ String StringFactory::fromStream(std::istream& in) { return StringFactory::fromString(input); } -String StringFactory::parse(std::list<sax::Token> tokens) { +String StringFactory::fromTokens(std::list<sax::Token> tokens) { return alib::FromXMLParsers::stringParser.parseValue(tokens); } void StringFactory::toFile(const String& string, const std::string& filename) { - std::list<sax::Token> tokens = compose(string); + std::list<sax::Token> tokens = toTokens(string); sax::SaxComposeInterface::printFile(filename, tokens); } std::string StringFactory::toString(const String& string) { - std::list<sax::Token> tokens = compose(string); + std::list<sax::Token> tokens = toTokens(string); std::string str; sax::SaxComposeInterface::printMemory(str, tokens); return str; @@ -56,11 +56,11 @@ void StringFactory::toStdout(const String& string) { } void StringFactory::toStream(const String& string, std::ostream& out) { - std::list<sax::Token> tokens = compose(string); + std::list<sax::Token> tokens = toTokens(string); sax::SaxComposeInterface::printStream(out, tokens); } -std::list<sax::Token> StringFactory::compose(const String& string) { +std::list<sax::Token> StringFactory::toTokens(const String& string) { return alib::ToXMLComposers::stringComposer.compose(string); } diff --git a/alib2/src/factory/StringFactory.h b/alib2/src/factory/StringFactory.h index 8f3e3e9e75..501d654f96 100644 --- a/alib2/src/factory/StringFactory.h +++ b/alib2/src/factory/StringFactory.h @@ -72,21 +72,19 @@ public: */ static void toStream(const String&, std::ostream& out); - -protected: /** * Parses the String from list of tokens. * @param tokens XML represented as list of tokens * @return parsed String */ - static String parse(std::list<sax::Token> tokens); + static String fromTokens(std::list<sax::Token> tokens); /** * Parses the String from list of tokens. * @param tokens XML represented as list of tokens * @return parsed String */ - static std::list<sax::Token> compose(const String&); + static std::list<sax::Token> toTokens(const String&); }; } /* namespace string */ diff --git a/alib2/src/factory/SymbolFactory.cpp b/alib2/src/factory/SymbolFactory.cpp index cd80ca0a3d..ddfe3732b9 100644 --- a/alib2/src/factory/SymbolFactory.cpp +++ b/alib2/src/factory/SymbolFactory.cpp @@ -17,13 +17,13 @@ namespace alphabet { Symbol SymbolFactory::fromFile(const std::string& filename) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseFile(filename, tokens); - return parse(tokens); + return fromTokens(tokens); } Symbol SymbolFactory::fromString(const std::string& str) { std::list<sax::Token> tokens; sax::SaxParseInterface::parseMemory(str, tokens); - return parse(tokens); + return fromTokens(tokens); } Symbol SymbolFactory::fromStdin() { @@ -35,17 +35,17 @@ Symbol SymbolFactory::fromStream(std::istream& in) { return SymbolFactory::fromString(input); } -Symbol SymbolFactory::parse(std::list<sax::Token> tokens) { +Symbol SymbolFactory::fromTokens(std::list<sax::Token> tokens) { return alib::FromXMLParsers::symbolParser.parseValue(tokens); } void SymbolFactory::toFile(const Symbol& symbol, const std::string& filename) { - std::list<sax::Token> tokens = compose(symbol); + std::list<sax::Token> tokens = toTokens(symbol); sax::SaxComposeInterface::printFile(filename, tokens); } std::string SymbolFactory::toString(const Symbol& symbol) { - std::list<sax::Token> tokens = compose(symbol); + std::list<sax::Token> tokens = toTokens(symbol); std::string str; sax::SaxComposeInterface::printMemory(str, tokens); return str; @@ -56,11 +56,11 @@ void SymbolFactory::toStdout(const Symbol& symbol) { } void SymbolFactory::toStream(const Symbol& symbol, std::ostream& out) { - std::list<sax::Token> tokens = compose(symbol); + std::list<sax::Token> tokens = toTokens(symbol); sax::SaxComposeInterface::printStream(out, tokens); } -std::list<sax::Token> SymbolFactory::compose(const Symbol& symbol) { +std::list<sax::Token> SymbolFactory::toTokens(const Symbol& symbol) { return alib::ToXMLComposers::symbolComposer.compose(symbol); } diff --git a/alib2/src/factory/SymbolFactory.h b/alib2/src/factory/SymbolFactory.h index b90b4ec921..7194233052 100644 --- a/alib2/src/factory/SymbolFactory.h +++ b/alib2/src/factory/SymbolFactory.h @@ -72,21 +72,19 @@ public: */ static void toStream(const Symbol&, std::ostream& out); - -protected: /** * Parses the String from list of tokens. * @param tokens XML represented as list of tokens * @return parsed String */ - static Symbol parse(std::list<sax::Token> tokens); + static Symbol fromTokens(std::list<sax::Token> tokens); /** * Parses the String from list of tokens. * @param tokens XML represented as list of tokens * @return parsed String */ - static std::list<sax::Token> compose(const Symbol&); + static std::list<sax::Token> toTokens(const Symbol&); }; } /* namespace alphabet */ -- GitLab