From be20dd832ffb66e416fce9d6f1dd2f9b5ee4d84f Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Tue, 1 Mar 2016 08:11:35 +0100
Subject: [PATCH] add strgin_from/to_raw

---
 alib2raw/src/RawApi.cpp                     |  1 +
 alib2raw/src/RawApi.hpp                     |  3 +-
 alib2raw/src/string/StringFromRawParser.cpp | 33 ++++++++++++++++
 alib2raw/src/string/StringFromRawParser.h   | 28 +++++++++++++
 alib2raw/src/string/StringToRawComposer.cpp | 26 ++++++++++++
 alib2raw/src/string/StringToRawComposer.h   | 39 ++++++++++++++++++
 alib2raw/src/tree/TreeFromRawParser.cpp     | 26 ++++++------
 alib2raw/src/tree/TreeFromRawParser.h       |  7 ++--
 alib2raw/src/tree/TreeToRawComposer.cpp     |  4 +-
 alib2raw/src/tree/TreeToRawComposer.h       | 10 ++---
 araw2/src/araw.cpp                          | 44 ++++++++++++++++++++-
 11 files changed, 196 insertions(+), 25 deletions(-)
 create mode 100644 alib2raw/src/string/StringFromRawParser.cpp
 create mode 100644 alib2raw/src/string/StringFromRawParser.h
 create mode 100644 alib2raw/src/string/StringToRawComposer.cpp
 create mode 100644 alib2raw/src/string/StringToRawComposer.h

diff --git a/alib2raw/src/RawApi.cpp b/alib2raw/src/RawApi.cpp
index b0abd0f94f..b6b6aaae88 100644
--- a/alib2raw/src/RawApi.cpp
+++ b/alib2raw/src/RawApi.cpp
@@ -10,5 +10,6 @@
 namespace alib {
 
 const tree::TreeFromRawParser FromRawParsers::treeParser;
+const string::StringFromRawParser FromRawParsers::stringParser;
 
 } /* namespace alib */
diff --git a/alib2raw/src/RawApi.hpp b/alib2raw/src/RawApi.hpp
index 4c6b2f15fc..b5389dc9f8 100644
--- a/alib2raw/src/RawApi.hpp
+++ b/alib2raw/src/RawApi.hpp
@@ -9,13 +9,14 @@
 #define RAW_API_HPP_
 
 #include "tree/TreeFromRawParser.h"
-#include "tree/TreeToRawComposer.h"
+#include "string/StringFromRawParser.h"
 
 namespace alib {
 
 class FromRawParsers {
 public:
 	static const tree::TreeFromRawParser treeParser;
+	static const string::StringFromRawParser stringParser;
 
 };
 
diff --git a/alib2raw/src/string/StringFromRawParser.cpp b/alib2raw/src/string/StringFromRawParser.cpp
new file mode 100644
index 0000000000..f19a86005a
--- /dev/null
+++ b/alib2raw/src/string/StringFromRawParser.cpp
@@ -0,0 +1,33 @@
+/*
+ * StringFromRawParser.cpp
+ *
+ *  Created on: Nov 23, 2013
+ *      Author: Jan Travnicek
+ */
+
+#include <exception/AlibException.h>
+#include <string/String.h>
+#include <string/StringClasses.h>
+#include <alphabet/Symbol.h>
+#include <sax/FromXMLParserHelper.h>
+#include "StringFromRawParser.h"
+
+namespace string {
+
+String StringFromRawParser::parseString(std::istream_iterator<char>& input) const {
+	return parseString(input, std::set<FEATURES>({FEATURES::LINEAR}));
+}
+
+String StringFromRawParser::parseString(std::istream_iterator<char>& input, const std::set<FEATURES>& features) const {
+	std::vector<alphabet::Symbol> data;
+	for(;input != std::istream_iterator<char>(); input++) {
+		data.push_back(alphabet::symbolFrom(*input));
+	}
+	LinearString string { data };
+
+	if(features.count(FEATURES::LINEAR)) return String{string};
+
+	throw exception::AlibException("Invalid input");
+}
+
+} /* namespace string */
diff --git a/alib2raw/src/string/StringFromRawParser.h b/alib2raw/src/string/StringFromRawParser.h
new file mode 100644
index 0000000000..93e9365355
--- /dev/null
+++ b/alib2raw/src/string/StringFromRawParser.h
@@ -0,0 +1,28 @@
+/*
+ * StringFromRawParser.h
+ *
+ *  Created on: Nov 23, 2013
+ *      Author: Jan Travnicek
+ */
+
+#ifndef STRING_FROM_RAW_PARSER
+#define STRING_FROM_RAW_PARSER
+
+#include <iterator>
+#include <set>
+#include <string/StringFeatures.h>
+
+namespace string {
+
+class StringFromRawParser {
+public:
+	StringFromRawParser() {}
+
+	String parseString(std::istream_iterator<char>& input) const;
+	String parseString(std::istream_iterator<char>& input, const std::set<FEATURES>& features) const;
+
+};
+
+} /* namespace string */
+
+#endif /* STRING_FROM_RAW_PARSER */
diff --git a/alib2raw/src/string/StringToRawComposer.cpp b/alib2raw/src/string/StringToRawComposer.cpp
new file mode 100644
index 0000000000..9e69c463c4
--- /dev/null
+++ b/alib2raw/src/string/StringToRawComposer.cpp
@@ -0,0 +1,26 @@
+/*
+ * StringToRawComposer.cpp
+ *
+ *  Created on: Nov 16, 2014
+ *      Author: Jan Travnicek
+ */
+
+#include <string/StringClasses.h>
+#include "StringToRawComposer.h"
+
+namespace string {
+
+void StringToRawComposer::compose(std::ostream& out, const String& string) {
+	getInstance().dispatch(out, string.getData());
+}
+
+void StringToRawComposer::compose(std::ostream& out, const LinearString& string) {
+	for(const alphabet::Symbol& symbol : string.getContent()) {
+		out << (std::string) symbol;
+	}
+}
+
+StringToRawComposer::RegistratorWrapper<void, LinearString> StringToRawComposerLinearString = StringToRawComposer::RegistratorWrapper<void, LinearString>(StringToRawComposer::getInstance(), StringToRawComposer::compose);
+
+} /* namespace automaton */
+
diff --git a/alib2raw/src/string/StringToRawComposer.h b/alib2raw/src/string/StringToRawComposer.h
new file mode 100644
index 0000000000..b47cec5cc5
--- /dev/null
+++ b/alib2raw/src/string/StringToRawComposer.h
@@ -0,0 +1,39 @@
+/*
+ * StringToRawComposer.h
+ *
+ *  Created on: Nov 16, 2014
+ *      Author: Jan Travnicek
+ */
+
+#ifndef STRING_TO_RAW_COMPOSER_H_
+#define STRING_TO_RAW_COMPOSER_H_
+
+#include <deque>
+#include <vector>
+#include <core/multipleDispatch.hpp>
+#include <string/String.h>
+#include <string/StringFeatures.h>
+#include <alphabet/SymbolFeatures.h>
+
+namespace string {
+
+/**
+ * This class contains methods to print XML representation of string to the output stream.
+ */
+class StringToRawComposer : public std::SingleDispatchFirstStaticParam<void, std::ostream&, StringBase> {
+public:
+	static void compose(std::ostream& out, const String& string);
+
+	static void compose(std::ostream& out, const LinearString& string);
+
+public:
+	static StringToRawComposer& getInstance() {
+		static StringToRawComposer res;
+		return res;
+	}
+};
+
+} /* namespace string */
+
+#endif /* STRING_TO_RAW_COMPOSER_H_ */
+
diff --git a/alib2raw/src/tree/TreeFromRawParser.cpp b/alib2raw/src/tree/TreeFromRawParser.cpp
index b93086b8e8..db38219382 100644
--- a/alib2raw/src/tree/TreeFromRawParser.cpp
+++ b/alib2raw/src/tree/TreeFromRawParser.cpp
@@ -5,13 +5,13 @@
  *      Author: Jan Travnicek
  */
 
+#include <sax/FromXMLParserHelper.h>
+#include <exception/AlibException.h>
+#include <tree/Tree.h>
+#include <tree/TreeClasses.h>
+#include <alphabet/Symbol.h>
+#include <alphabet/BarSymbol.h>
 #include "TreeFromRawParser.h"
-#include "tree/Tree.h"
-#include "tree/TreeException.h"
-#include "tree/TreeClasses.h"
-#include "alphabet/LabeledSymbol.h"
-#include "alphabet/BarSymbol.h"
-#include "label/Label.h"
 
 namespace tree {
 
@@ -21,9 +21,9 @@ Tree TreeFromRawParser::parseTree(std::deque<sax::Token>::iterator& input) const
 
 Tree TreeFromRawParser::parseTree(std::deque<sax::Token>::iterator& input, const std::set<FEATURES>& features) const {
 	UnrankedNode* root = NULL;
-	if (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) {
+	if (sax::FromXMLParserHelper::isTokenType(input, sax::Token::TokenType::START_ELEMENT)) {
 		root = this->parseContent(input);
-	} else if(isTokenType(input, sax::Token::TokenType::CHARACTER)) {
+	} else if(sax::FromXMLParserHelper::isTokenType(input, sax::Token::TokenType::CHARACTER)) {
 		root = this->parseContentLeaf(input);
 	} else {
 		throw exception::AlibException("Invalid token stream");
@@ -43,23 +43,23 @@ Tree TreeFromRawParser::parseTree(std::deque<sax::Token>::iterator& input, const
 }
 
 UnrankedNode* TreeFromRawParser::parseContent(std::deque<sax::Token>::iterator& input) const {
-	UnrankedNode* node = new UnrankedNode(alphabet::symbolFrom(popTokenData(input, sax::Token::TokenType::START_ELEMENT)), std::vector<UnrankedNode*> {});
-	while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) {
+	UnrankedNode* node = new UnrankedNode(alphabet::symbolFrom(sax::FromXMLParserHelper::popTokenData(input, sax::Token::TokenType::START_ELEMENT)), std::vector<UnrankedNode*> {});
+	while (sax::FromXMLParserHelper::isTokenType(input, sax::Token::TokenType::START_ELEMENT)) {
 		UnrankedNode* child = this->parseContent(input);
 		node->pushBackChild(std::move(*child));
 		delete child;
 	}
-	if(isTokenType(input, sax::Token::TokenType::CHARACTER)) {
+	if(sax::FromXMLParserHelper::isTokenType(input, sax::Token::TokenType::CHARACTER)) {
 		UnrankedNode* child = this->parseContentLeaf(input);
 		node->pushBackChild(std::move(*child));
 		delete child;
 	}
-	popTokenData(input, sax::Token::TokenType::END_ELEMENT);
+	sax::FromXMLParserHelper::popTokenData(input, sax::Token::TokenType::END_ELEMENT);
 	return node;
 }
 
 UnrankedNode* TreeFromRawParser::parseContentLeaf(std::deque<sax::Token>::iterator& input) const {
-	return new UnrankedNode(alphabet::symbolFrom(popTokenData(input, sax::Token::TokenType::CHARACTER)), std::vector<UnrankedNode*> {});
+	return new UnrankedNode(alphabet::symbolFrom(sax::FromXMLParserHelper::popTokenData(input, sax::Token::TokenType::CHARACTER)), std::vector<UnrankedNode*> {});
 }
 
 } /* namespace tree */
diff --git a/alib2raw/src/tree/TreeFromRawParser.h b/alib2raw/src/tree/TreeFromRawParser.h
index f050b17b13..c7ff68e6a1 100644
--- a/alib2raw/src/tree/TreeFromRawParser.h
+++ b/alib2raw/src/tree/TreeFromRawParser.h
@@ -8,13 +8,14 @@
 #ifndef TREE_FROM_RAW_PARSER
 #define TREE_FROM_RAW_PARSER
 
-#include "sax/FromXMLParserHelper.h"
-#include "tree/TreeFeatures.h"
 #include <set>
+#include <deque>
+#include <sax/Token.h>
+#include <tree/TreeFeatures.h>
 
 namespace tree {
 
-class TreeFromRawParser : public sax::FromXMLParserHelper {
+class TreeFromRawParser {
 public:
 	TreeFromRawParser() {}
 
diff --git a/alib2raw/src/tree/TreeToRawComposer.cpp b/alib2raw/src/tree/TreeToRawComposer.cpp
index 14ead6bc6c..ca85740c3a 100644
--- a/alib2raw/src/tree/TreeToRawComposer.cpp
+++ b/alib2raw/src/tree/TreeToRawComposer.cpp
@@ -5,9 +5,9 @@
  *      Author: Jan Travnicek
  */
 
+#include <tree/TreeException.h>
+#include <tree/TreeClasses.h>
 #include "TreeToRawComposer.h"
-#include "tree/TreeException.h"
-#include "tree/TreeClasses.h"
 
 namespace tree {
 
diff --git a/alib2raw/src/tree/TreeToRawComposer.h b/alib2raw/src/tree/TreeToRawComposer.h
index 940c899454..faef0e79f9 100644
--- a/alib2raw/src/tree/TreeToRawComposer.h
+++ b/alib2raw/src/tree/TreeToRawComposer.h
@@ -9,12 +9,12 @@
 #define TREE_TO_RAW_COMPOSER_H_
 
 #include <deque>
-#include <core/multipleDispatch.hpp>
-#include "tree/Tree.h"
-#include "tree/TreeFeatures.h"
-#include "alphabet/SymbolFeatures.h"
-#include "sax/Token.h"
 #include <vector>
+#include <core/multipleDispatch.hpp>
+#include <tree/Tree.h>
+#include <tree/TreeFeatures.h>
+#include <alphabet/SymbolFeatures.h>
+#include <sax/Token.h>
 
 namespace tree {
 
diff --git a/araw2/src/araw.cpp b/araw2/src/araw.cpp
index 6cf1aae891..2c7d1fc13d 100644
--- a/araw2/src/araw.cpp
+++ b/araw2/src/araw.cpp
@@ -9,12 +9,16 @@
 #include <global/GlobalData.h>
 #include <measure>
 #include <string>
+#include <fstream>
 #include <exception/AlibException.h>
 #include <RawApi.hpp>
 #include <factory/XmlDataFactory.hpp>
 #include <sax/SaxParseInterface.h>
 #include <sax/ParserException.h>
-#include <object/Object.h>
+#include <tree/Tree.h>
+#include <tree/TreeToRawComposer.h>
+#include <string/String.h>
+#include <string/StringToRawComposer.h>
 
 int main(int argc, char** argv) {
 	try {
@@ -96,6 +100,44 @@ int main(int argc, char** argv) {
 			std::deque<sax::Token> tokens2;
 			tree::TreeToRawComposer::compose(tokens2, tree);
 			sax::SaxComposeInterface::printStdout(tokens2);
+		} else if(string_from_raw.getValue()) {
+			std::ifstream inFile;
+			std::istream_iterator<char> iter;
+			if(input.isSet()) {
+				if(input.getValue() == "-") {
+					iter = std::cin;
+				} else {
+					inFile.open(input.getValue());
+					iter = inFile;
+				}
+			} else {
+				iter = std::cin;
+			}
+
+			string::String string = alib::FromRawParsers::stringParser.parseString(iter);
+
+			measurements::end();
+			measurements::start("Output write", measurements::Type::AUXILIARY);
+
+			alib::XmlDataFactory::toStdout(string);
+		} else if(string_to_raw.getValue()) {
+			std::deque<sax::Token> tokens;
+			if(input.isSet()) {
+				if(input.getValue() == "-") {
+					sax::SaxParseInterface::parseStdin(tokens);
+				} else {
+					sax::SaxParseInterface::parseFile(input.getValue(), tokens);
+				}
+			} else {
+				sax::SaxParseInterface::parseStdin(tokens);
+			}
+
+			string::String string = alib::XmlDataFactory::fromTokens<string::String>(tokens);
+
+			measurements::end();
+			measurements::start("Output write", measurements::Type::AUXILIARY);
+
+			string::StringToRawComposer::compose(std::cout, string);
 		} else {
 			throw exception::AlibException("Invalid input format specified");
 		}
-- 
GitLab