diff --git a/alib2/src/automaton/UnknownAutomaton.cpp b/alib2/src/automaton/UnknownAutomaton.cpp index 993fca8204cf601d8dc4b695280decc30aed90ce..9a3d7787d55ef50c3fdf6ef2f815d54ece061161 100644 --- a/alib2/src/automaton/UnknownAutomaton.cpp +++ b/alib2/src/automaton/UnknownAutomaton.cpp @@ -159,6 +159,7 @@ bool UnknownAutomaton::operator==(const UnknownAutomaton& other) { std::ostream& operator<<(std::ostream& out, const UnknownAutomaton& automaton) { out << "(UnknownAutomaton" << " states = " << automaton.states + << " inputAlphabet = " << automaton.inputAlphabet << " initialStates = " << automaton.initialStates << " finalStates = " << automaton.finalStates << " stackAlphabet = " << automaton.stackAlphabet diff --git a/alib2/src/automaton/UnknownTransition.cpp b/alib2/src/automaton/UnknownTransition.cpp index 9a1addb622a96bd51095f1bff0b6ca618b9cd0fb..9b3f63d2fb336ba90f62a2f9f37869584adc1468 100644 --- a/alib2/src/automaton/UnknownTransition.cpp +++ b/alib2/src/automaton/UnknownTransition.cpp @@ -128,18 +128,26 @@ void UnknownTransition::setShift(const Shift& shift) { } bool UnknownTransition::operator <(const UnknownTransition& other) const { - if (*from != *other.from) { - return *from < *other.from; - } else if (*input != *other.input) { - return *input < *other.input; + if (from != other.from && from != NULL && other.from != NULL && *from != *other.from) { + if(from == NULL) return -1; + else if(other.from == NULL) return 1; + else return *from < *other.from; + } else if (input != other.input && input != NULL && other.input != NULL && *input != *other.input) { + if(input == NULL) return -1; + else if(other.input == NULL) return 1; + else return *input < *other.input; } else if (pop != other.pop) { return pop < other.pop; } else if (shift != other.shift) { return shift < other.shift; - } else if (*to != *other.to) { - return *to < *other.to; - } else if (*output != *other.output) { - return *output < *other.output; + } else if (to != other.to && to != NULL && other.to != NULL && *to != *other.to) { + if(to == NULL) return 1; + else if(other.to == NULL) return -1; + else return *to < *other.to; + } else if (output != other.output && output != NULL && other.output != NULL && *output != *other.output) { + if(output == NULL) return 1; + else if(other.output == NULL) return -1; + else return *output < *other.output; } else { return push < other.push; } @@ -147,7 +155,7 @@ bool UnknownTransition::operator <(const UnknownTransition& other) const { bool UnknownTransition::operator ==(const UnknownTransition& other) const { return (from == other.from || (from != NULL && other.from != NULL && *from == *other.from)) - && (input == other.input && (input != NULL && other.input != NULL && *input == *other.input)) + && (input == other.input || (input != NULL && other.input != NULL && *input == *other.input)) && pop == other.pop && shift == other.shift && (to == other.to || (to != NULL && other.to != NULL && *to == *other.to)) diff --git a/alib2/src/sax/ComposerException.cpp b/alib2/src/sax/ComposerException.cpp new file mode 100644 index 0000000000000000000000000000000000000000..261a57693cdee70d4ea759510a33e23274cec636 --- /dev/null +++ b/alib2/src/sax/ComposerException.cpp @@ -0,0 +1,22 @@ +/* + * ComposerException.cpp + * + * Created on: Apr 16, 2013 + * Author: Jan Travnicek + */ + +#include "ComposerException.h" + +namespace sax { + +ComposerException::ComposerException(const Token& expected, const Token& read) : + expected(expected), read(read) { + + cause = "Composer Exception: Expected: " + expected.getData() + " Read: " + read.getData(); +} + +ComposerException::~ComposerException() noexcept { + +} + +} /* namespace sax */ diff --git a/alib2/src/sax/ComposerException.h b/alib2/src/sax/ComposerException.h new file mode 100644 index 0000000000000000000000000000000000000000..fe662196e2f09dd846c0e784e3692d0dbe736667 --- /dev/null +++ b/alib2/src/sax/ComposerException.h @@ -0,0 +1,31 @@ +/* + * ComposerException.h + * + * Created on: Apr 16, 2013 + * Author: Jan Travnicek + */ + +#ifndef COMPOSER_EXCEPTION_H_ +#define COMPOSER_EXCEPTION_H_ + +#include "../AlibException.h" +#include "Token.h" + +namespace sax { + +/** + * Exception thrown by XML parser when is expected different tag than the one which is read. + */ +class ComposerException: public alib::AlibException { +protected: + Token expected; + Token read; +public: + ComposerException(const Token& expected, const Token& read); + ~ComposerException() noexcept; +}; + +} /* namespace sax */ + +#endif /* COMPOSER_EXCEPTION_H_ */ + diff --git a/alib2/src/sax/ParserException.cpp b/alib2/src/sax/ParserException.cpp index 55ff89d8c8417c18a7bcf337f85b4977d4fcded3..f76790197641f7c470c11829d238a3e4a402a31b 100644 --- a/alib2/src/sax/ParserException.cpp +++ b/alib2/src/sax/ParserException.cpp @@ -20,3 +20,4 @@ ParserException::~ParserException() noexcept { } } /* namespace sax */ + diff --git a/alib2/src/sax/ParserException.h b/alib2/src/sax/ParserException.h index 23905d95d132c411abd7852f165fadb53f6c0ba1..af17aa0d8fcf9f38fc8d91b9f91a3433a8218a05 100644 --- a/alib2/src/sax/ParserException.h +++ b/alib2/src/sax/ParserException.h @@ -28,3 +28,4 @@ public: } /* namespace sax */ #endif /* PARSER_EXCEPTION_H_ */ + diff --git a/alib2/src/sax/SaxComposeInterface.cpp b/alib2/src/sax/SaxComposeInterface.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4b28594eb5a554741e1ad38a6023a0bb18ec775 --- /dev/null +++ b/alib2/src/sax/SaxComposeInterface.cpp @@ -0,0 +1,81 @@ +/* + * SaxComposeInterface.cpp + * + * Created on: 8.8.2012 + * Author: Martin Zak + */ + +#include "SaxComposeInterface.h" + +#include <string> +#include <iostream> +#include <fstream> +#include <sstream> +#include <deque> +#include "ComposerException.h" + +namespace sax { + +void SaxComposeInterface::printMemory(std::string& xmlOut, const std::list<Token>& in) { + xmlBufferPtr buf = xmlBufferCreate(); + xmlTextWriterPtr writer = xmlNewTextWriterMemory(buf, 0); + + SaxComposeInterface::xmlSAXUserPrint(writer, in); + + xmlFreeTextWriter(writer); + xmlOut = (const char*) buf->content; + xmlBufferFree(buf); +} + +void SaxComposeInterface::printFile(const std::string& filename, const std::list<Token>& in) { + xmlTextWriterPtr writer = xmlNewTextWriterFilename(filename.c_str(), 0); + + SaxComposeInterface::xmlSAXUserPrint(writer, in); + + xmlFreeTextWriter(writer); +} + +void SaxComposeInterface::printStdout(const std::list<Token>& in) { + SaxComposeInterface::printStream(std::cout, in); +} + +void SaxComposeInterface::printStream(std::ostream& out, const std::list<Token>& in) { + xmlBufferPtr buf = xmlBufferCreate(); + xmlTextWriterPtr writer = xmlNewTextWriterMemory(buf, 0); + + SaxComposeInterface::xmlSAXUserPrint(writer, in); + + xmlFreeTextWriter(writer); + out << (const char*) buf->content; + xmlBufferFree(buf); +} + +void SaxComposeInterface::xmlSAXUserPrint(xmlTextWriterPtr writer, const std::list<Token>& in) { + xmlTextWriterStartDocument(writer, NULL, NULL, NULL); + std::deque<Token> stack; + for(const Token& token : in) { + switch(token.getType()) { + case Token::START_ELEMENT: + xmlTextWriterStartElement(writer, (const xmlChar*) token.getData().c_str()); + stack.push_back(token); + break; + case Token::END_ELEMENT: + if(stack.empty() || stack.back().getData() != token.getData()) { + throw ComposerException(Token(stack.back().getData(), Token::END_ELEMENT), token); + } + stack.pop_back(); + xmlTextWriterEndElement(writer); + break; + case Token::CHARACTER: + xmlTextWriterWriteString(writer, (const xmlChar*) token.getData().c_str()); + break; + case Token::ATTRIBUTE: + //TODO + break; + } + } + xmlTextWriterEndDocument(writer); +} + +} /* namespace sax */ + diff --git a/alib2/src/sax/SaxPrintInterface.h b/alib2/src/sax/SaxComposeInterface.h similarity index 89% rename from alib2/src/sax/SaxPrintInterface.h rename to alib2/src/sax/SaxComposeInterface.h index 21bb98bb4577178d23663f356945d51906f9a466..312eea82aa2b82ac6673aa051a23a85fb40317b2 100644 --- a/alib2/src/sax/SaxPrintInterface.h +++ b/alib2/src/sax/SaxComposeInterface.h @@ -1,12 +1,12 @@ /* - * SaxPrintInterface.h + * SaxComposeInterface.h * * Created on: 8.8.2012 * Author: Martin Zak */ -#ifndef SAX_PRINT_INTERFACE_H_ -#define SAX_PRINT_INTERFACE_H_ +#ifndef SAX_COMPOSE_INTERFACE_H_ +#define SAX_COMPOSE_INTERFACE_H_ #include <libxml/xmlwriter.h> #include <list> @@ -18,7 +18,7 @@ namespace sax { * This class performs parsing of file or string containing XML. Contains callback * methods for libxml SAX parser. */ -class SaxPrintInterface { +class SaxComposeInterface { static void xmlSAXUserPrint(xmlTextWriterPtr writer, const std::list<Token>& in); public: /** @@ -53,4 +53,5 @@ public: } /* namespace sax */ -#endif /* SAX_PRINT_INTERFACE_H_ */ +#endif /* SAX_COMPOSE_INTERFACE_H_ */ + diff --git a/alib2/src/sax/SaxPrintInterface.cpp b/alib2/src/sax/SaxPrintInterface.cpp deleted file mode 100644 index 59c78dca7d9bde6e458bd60e1c399028ffbff701..0000000000000000000000000000000000000000 --- a/alib2/src/sax/SaxPrintInterface.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * SaxPrintInterface.cpp - * - * Created on: 8.8.2012 - * Author: Martin Zak - */ - -#include "SaxPrintInterface.h" - -#include <string> -#include <iostream> -#include <fstream> -#include <sstream> -#include "../AlibException.h" - -namespace sax { - -void SaxPrintInterface::printMemory(std::string& xmlOut, const std::list<Token>& in) { - xmlBufferPtr buf = xmlBufferCreate(); - xmlTextWriterPtr writer = xmlNewTextWriterMemory(buf, 0); - - SaxPrintInterface::xmlSAXUserPrint(writer, in); - - xmlFreeTextWriter(writer); - xmlOut = (const char*) buf->content; - xmlBufferFree(buf); -} - -void SaxPrintInterface::printFile(const std::string& filename, const std::list<Token>& in) { - xmlTextWriterPtr writer = xmlNewTextWriterFilename(filename.c_str(), 0); - - SaxPrintInterface::xmlSAXUserPrint(writer, in); - - xmlFreeTextWriter(writer); -} - -void SaxPrintInterface::printStdout(const std::list<Token>& in) { - SaxPrintInterface::printStream(std::cout, in); -} - -void SaxPrintInterface::printStream(std::ostream& out, const std::list<Token>& in) { - xmlBufferPtr buf = xmlBufferCreate(); - xmlTextWriterPtr writer = xmlNewTextWriterMemory(buf, 0); - - SaxPrintInterface::xmlSAXUserPrint(writer, in); - - xmlFreeTextWriter(writer); - out << (const char*) buf->content; - xmlBufferFree(buf); -} - -void SaxPrintInterface::xmlSAXUserPrint(xmlTextWriterPtr writer, const std::list<Token>& in) { - xmlTextWriterStartDocument(writer, NULL, NULL, NULL); - for(const Token& token : in) { - if(token.getType() == Token::START_ELEMENT) xmlTextWriterStartElement(writer, (const xmlChar*) token.getData().c_str()); - if(token.getType() == Token::END_ELEMENT) xmlTextWriterEndElement(writer); - if(token.getType() == Token::CHARACTER) xmlTextWriterWriteString(writer, (const xmlChar*) token.getData().c_str()); - } - xmlTextWriterEndDocument(writer); -} - -} /* namespace sax */ diff --git a/alib2/test-src/automaton/AutomatonTest.cpp b/alib2/test-src/automaton/AutomatonTest.cpp index a0c8a23e1592f4d2f5fe74ab0cef46ce1fec6a1f..5d5c6587149addf3030e448b07c33ded690c6d5c 100644 --- a/alib2/test-src/automaton/AutomatonTest.cpp +++ b/alib2/test-src/automaton/AutomatonTest.cpp @@ -2,7 +2,7 @@ #include "AutomatonTest.h" #include "sax/SaxParseInterface.h" -#include "sax/SaxPrintInterface.h" +#include "sax/SaxComposeInterface.h" #include "automaton/UnknownAutomaton.h" #include "automaton/AutomatonFromXMLParser.h" @@ -27,15 +27,14 @@ void AutomatonTest::testXMLParser() { automaton::AutomatonToXMLComposer composer; std::list<sax::Token> tokens = composer.compose(automaton); std::string tmp; - sax::SaxPrintInterface::printMemory(tmp, tokens); + sax::SaxComposeInterface::printMemory(tmp, tokens); std::list<sax::Token> tokens2; sax::SaxParseInterface::parseMemory(tmp, tokens2); automaton::AutomatonFromXMLParser parser; automaton::UnknownAutomaton automaton2 = parser.parse(tokens2); - std::cout << std::endl << automaton << std::endl << automaton2 << std::endl; - + CPPUNIT_ASSERT( automaton == automaton ); CPPUNIT_ASSERT( automaton == automaton2 ); } diff --git a/alib2/test-src/regexp/RegExpTest.cpp b/alib2/test-src/regexp/RegExpTest.cpp index dbc69d0286aa6690cbc0d2059d0fcb173aad61b9..a4cdd33d213d27afb923cddfc99def5923553e93 100644 --- a/alib2/test-src/regexp/RegExpTest.cpp +++ b/alib2/test-src/regexp/RegExpTest.cpp @@ -2,7 +2,7 @@ #include "RegExpTest.h" #include "sax/SaxParseInterface.h" -#include "sax/SaxPrintInterface.h" +#include "sax/SaxComposeInterface.h" #include "regexp/RegExp.h" #include "regexp/RegExpFromStringParser.h" @@ -76,7 +76,7 @@ void RegExpTest::testXMLParser() { regexp::RegExpToXMLComposer composer; std::list<sax::Token> tokens = composer.compose(regexp); std::string tmp; - sax::SaxPrintInterface::printMemory(tmp, tokens); + sax::SaxComposeInterface::printMemory(tmp, tokens); std::list<sax::Token> tokens2; sax::SaxParseInterface::parseMemory(tmp, tokens2);