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

proposed refactoring of xml parsing and composing

Part 1
parent 36b42841
No related branches found
No related tags found
No related merge requests found
Showing
with 439 additions and 388 deletions
......@@ -955,126 +955,6 @@ void xmlApi<primitive::Primitive>::compose(std::deque<sax::Token>& output, const
ToXMLComposers::primitiveComposer.compose(output, data);
}
 
primitive::String xmlApi<primitive::String>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseString(input);
}
bool xmlApi<primitive::String>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_STRING);
}
void xmlApi<primitive::String>::compose(std::deque<sax::Token>& output, const primitive::String& data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
std::string xmlApi<std::string>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseStringRaw(input);
}
bool xmlApi<std::string>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_STRING);
}
void xmlApi<std::string>::compose(std::deque<sax::Token>& output, const std::string& data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
primitive::Integer xmlApi<primitive::Integer>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseInteger(input);
}
bool xmlApi<primitive::Integer>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_INTEGER);
}
void xmlApi<primitive::Integer>::compose(std::deque<sax::Token>& output, const primitive::Integer& data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
int xmlApi<int>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseIntegerRaw(input);
}
bool xmlApi<int>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_INTEGER);
}
void xmlApi<int>::compose(std::deque<sax::Token>& output, int data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
primitive::Character xmlApi<primitive::Character>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseCharacter(input);
}
bool xmlApi<primitive::Character>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_CHARACTER);
}
void xmlApi<primitive::Character>::compose(std::deque<sax::Token>& output, const primitive::Character& data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
primitive::Unsigned xmlApi<primitive::Unsigned>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseUnsigned(input);
}
bool xmlApi<primitive::Unsigned>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_UNSIGNED);
}
void xmlApi<primitive::Unsigned>::compose(std::deque<sax::Token>& output, const primitive::Unsigned& data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
primitive::Bool xmlApi<primitive::Bool>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseBool(input);
}
bool xmlApi<primitive::Bool>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_BOOL);
}
void xmlApi<primitive::Bool>::compose(std::deque<sax::Token>& output, const primitive::Bool& data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
char xmlApi<char>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseCharacterRaw(input);
}
bool xmlApi<char>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_CHARACTER);
}
void xmlApi<char>::compose(std::deque<sax::Token>& output, char data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
unsigned xmlApi<unsigned>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseCharacterRaw(input);
}
bool xmlApi<unsigned>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_CHARACTER);
}
void xmlApi<unsigned>::compose(std::deque<sax::Token>& output, unsigned data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
bool xmlApi<bool>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::primitiveParser.parseCharacterRaw(input);
}
bool xmlApi<bool>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::PRIMITIVE_CHARACTER);
}
void xmlApi<bool>::compose(std::deque<sax::Token>& output, bool data) {
ToXMLComposers::primitiveComposer.compose(output, data);
}
tree::Tree xmlApi<tree::Tree>::parse(std::deque<sax::Token>::iterator& input) {
return FromXMLParsers::treeParser.parseTree(input);
}
......
......@@ -40,13 +40,6 @@
 
namespace alib {
 
template<typename T>
struct xmlApi {
static T parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const T& data);
};
template<>
struct xmlApi<Void> {
static Void parse(std::deque<sax::Token>::iterator& input);
......@@ -607,76 +600,6 @@ struct xmlApi<primitive::Primitive> {
static void compose(std::deque<sax::Token>& output, const primitive::Primitive& data);
};
 
template<>
struct xmlApi<primitive::String> {
static primitive::String parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::String& data);
};
template<>
struct xmlApi<std::string> {
static std::string parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const std::string& data);
};
template<>
struct xmlApi<primitive::Integer> {
static primitive::Integer parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::Integer& data);
};
template<>
struct xmlApi<int> {
static int parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, int data);
};
template<>
struct xmlApi<primitive::Character> {
static primitive::Character parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::Character& data);
};
template<>
struct xmlApi<char> {
static char parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, char data);
};
template<>
struct xmlApi<primitive::Unsigned> {
static primitive::Unsigned parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::Unsigned& data);
};
template<>
struct xmlApi<unsigned> {
static unsigned parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, unsigned data);
};
template<>
struct xmlApi<primitive::Bool> {
static primitive::Bool parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::Bool& data);
};
template<>
struct xmlApi<bool> {
static bool parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, bool data);
};
template<>
struct xmlApi<tree::Tree> {
static tree::Tree parse(std::deque<sax::Token>::iterator& input);
......
/*
* XmlApiBase.hpp
*
* Created on: Apr 1, 2013
* Author: Jan Travnicek
*/
#ifndef XML_API_BASE_H_
#define XML_API_BASE_H_
#include <deque>
#include "sax/Token.h"
namespace alib {
template<typename T>
struct xmlApi {
static T parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const T& data);
};
} /* namespace alib */
#endif /* XML_API_BASE_H_ */
......@@ -73,11 +73,6 @@ const std::string Names::REGEXP_FORMAL_REGEXP = "FormalRegExp";
const std::string Names::STRING_LINEAR_STRING = "LinearString";
const std::string Names::STRING_CYCLIC_STRING = "CyclicString";
const std::string Names::STRING_EPSILON = "Epsilon";
const std::string Names::PRIMITIVE_STRING = "String";
const std::string Names::PRIMITIVE_INTEGER = "Integer";
const std::string Names::PRIMITIVE_CHARACTER = "Character";
const std::string Names::PRIMITIVE_UNSIGNED = "Unsigned";
const std::string Names::PRIMITIVE_BOOL = "Bool";
const std::string Names::TREE_RANKED_TREE = "RankedTree";
const std::string Names::TREE_RANKED_PATTERN = "RankedPattern";
const std::string Names::TREE_PREFIX_RANKED_TREE = "PrefixRankedTree";
......
......@@ -78,11 +78,6 @@ struct Names {
const static std::string STRING_LINEAR_STRING;
const static std::string STRING_CYCLIC_STRING;
const static std::string STRING_EPSILON;
const static std::string PRIMITIVE_STRING;
const static std::string PRIMITIVE_INTEGER;
const static std::string PRIMITIVE_CHARACTER;
const static std::string PRIMITIVE_UNSIGNED;
const static std::string PRIMITIVE_BOOL;
const static std::string TREE_RANKED_TREE;
const static std::string TREE_RANKED_PATTERN;
const static std::string TREE_PREFIX_RANKED_TREE;
......
......@@ -6,6 +6,8 @@
*/
 
#include "Bool.h"
#include "../sax/FromXMLParserHelper.h"
#include "../exception/AlibException.h"
 
namespace primitive {
 
......@@ -41,5 +43,65 @@ Bool::operator std::string() const {
return data ? "true" : "false";
}
 
const std::string Bool::PRIMITIVE_BOOL = "Bool";
Bool Bool::parseBool(std::deque<sax::Token>::iterator& input) {
return Bool(parseBoolRaw(input));
}
bool Bool::parseBoolRaw(std::deque<sax::Token>::iterator& input) {
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, Bool::PRIMITIVE_BOOL);
std::string tmp = sax::FromXMLParserHelper::popTokenData(input, sax::Token::TokenType::CHARACTER);
bool data;
if(tmp == "true")
data = true;
else if(tmp == "false")
data = false;
else
throw exception::AlibException("Invalid boolean value");
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, Bool::PRIMITIVE_BOOL);
return data;
}
void Bool::compose(std::deque<sax::Token>& out) const {
compose(out, getData());
}
void Bool::compose(std::deque<sax::Token>& out, bool primitive) {
out.emplace_back(Bool::PRIMITIVE_BOOL, sax::Token::TokenType::START_ELEMENT);
if(primitive)
out.emplace_back("true", sax::Token::TokenType::CHARACTER);
else
out.emplace_back("false", sax::Token::TokenType::CHARACTER);
out.emplace_back(Bool::PRIMITIVE_BOOL, sax::Token::TokenType::END_ELEMENT);
}
} /* namespace primitive */
 
namespace alib {
primitive::Bool xmlApi<primitive::Bool>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::Bool::parseBool(input);
}
bool xmlApi<primitive::Bool>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::Bool::PRIMITIVE_BOOL);
}
void xmlApi<primitive::Bool>::compose(std::deque<sax::Token>& output, const primitive::Bool& data) {
data.compose(output);
}
bool xmlApi<bool>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::Bool::parseBoolRaw(input);
}
bool xmlApi<bool>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::Bool::PRIMITIVE_BOOL);
}
void xmlApi<bool>::compose(std::deque<sax::Token>& output, bool data) {
primitive::Bool::compose(output, data);
}
} /* namespace alib */
......@@ -9,6 +9,7 @@
#define PRIMITIVE_BOOL_H_
 
#include "PrimitiveBase.h"
#include "../XmlApiBase.h"
 
namespace primitive {
 
......@@ -53,6 +54,14 @@ public:
virtual int selfTypeId() const {
return typeId(*this);
}
const static std::string PRIMITIVE_BOOL;
static Bool parseBool(std::deque<sax::Token>::iterator& input);
static bool parseBoolRaw(std::deque<sax::Token>::iterator& input);
void compose(std::deque<sax::Token>& out) const;
static void compose(std::deque<sax::Token>& out, bool primitive);
};
 
} /* namespace primitive */
......@@ -68,4 +77,22 @@ struct compare<primitive::Bool> {
 
} /* namespace std */
 
namespace alib {
template<>
struct xmlApi<primitive::Bool> {
static primitive::Bool parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::Bool& data);
};
template<>
struct xmlApi<bool> {
static bool parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, bool data);
};
} /* namespace alib */
#endif /* PRIMITIVE_BOOL_H_ */
......@@ -6,6 +6,7 @@
*/
 
#include "Character.h"
#include "../sax/FromXMLParserHelper.h"
 
namespace primitive {
 
......@@ -41,5 +42,56 @@ Character::operator std::string() const {
return std::string(1, data);
}
 
const std::string Character::PRIMITIVE_CHARACTER = "Character";
Character Character::parseCharacter(std::deque<sax::Token>::iterator& input) {
return Character(parseCharacterRaw(input));
}
char Character::parseCharacterRaw(std::deque<sax::Token>::iterator& input) {
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, Character::PRIMITIVE_CHARACTER);
char data = sax::FromXMLParserHelper::popTokenData(input, sax::Token::TokenType::CHARACTER)[0];
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, Character::PRIMITIVE_CHARACTER);
return data;
}
void Character::compose(std::deque<sax::Token>& out) const {
compose(out, this->getData());
}
void Character::compose(std::deque<sax::Token>& out, char primitive) {
out.emplace_back(Character::PRIMITIVE_CHARACTER, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::string(1, primitive), sax::Token::TokenType::CHARACTER);
out.emplace_back(Character::PRIMITIVE_CHARACTER, sax::Token::TokenType::END_ELEMENT);
}
} /* namespace primitive */
 
namespace alib {
primitive::Character xmlApi<primitive::Character>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::Character::parseCharacter(input);
}
bool xmlApi<primitive::Character>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::Character::PRIMITIVE_CHARACTER);
}
void xmlApi<primitive::Character>::compose(std::deque<sax::Token>& output, const primitive::Character& data) {
data.compose(output);
}
char xmlApi<char>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::Character::parseCharacterRaw(input);
}
bool xmlApi<char>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::Character::PRIMITIVE_CHARACTER);
}
void xmlApi<char>::compose(std::deque<sax::Token>& output, char data) {
primitive::Character::compose(output, data);
}
} /* namespace alib */
......@@ -9,6 +9,7 @@
#define PRIMITIVE_CHARACTER_H_
 
#include "PrimitiveBase.h"
#include "../XmlApiBase.h"
 
namespace primitive {
 
......@@ -53,6 +54,14 @@ public:
virtual int selfTypeId() const {
return typeId(*this);
}
const static std::string PRIMITIVE_CHARACTER;
static Character parseCharacter(std::deque<sax::Token>::iterator& input);
static char parseCharacterRaw(std::deque<sax::Token>::iterator& input);
void compose(std::deque<sax::Token>& out) const;
static void compose(std::deque<sax::Token>& out, char primitive);
};
 
} /* namespace primitive */
......@@ -68,5 +77,23 @@ struct compare<primitive::Character> {
 
} /* namespace std */
 
namespace alib {
template<>
struct xmlApi<primitive::Character> {
static primitive::Character parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::Character& data);
};
template<>
struct xmlApi<char> {
static char parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, char data);
};
} /* namespace alib */
#endif /* PRIMITIVE_CHARACTER_H_ */
 
......@@ -6,6 +6,7 @@
*/
 
#include "Integer.h"
#include "../sax/FromXMLParserHelper.h"
 
namespace primitive {
 
......@@ -41,5 +42,56 @@ Integer::operator std::string() const {
return std::to_string(data);
}
 
const std::string Integer::PRIMITIVE_INTEGER = "Integer";
Integer Integer::parseInteger(std::deque<sax::Token>::iterator& input) {
return Integer(parseIntegerRaw(input));
}
int Integer::parseIntegerRaw(std::deque<sax::Token>::iterator& input) {
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, Integer::PRIMITIVE_INTEGER);
int data = std::stoi(sax::FromXMLParserHelper::popTokenData(input, sax::Token::TokenType::CHARACTER));
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, Integer::PRIMITIVE_INTEGER);
return data;
}
void Integer::compose(std::deque<sax::Token>& out) const {
compose(out, this->getData());
}
void Integer::compose(std::deque<sax::Token>& out, int primitive) {
out.emplace_back(Integer::PRIMITIVE_INTEGER, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::itos(primitive), sax::Token::TokenType::CHARACTER);
out.emplace_back(Integer::PRIMITIVE_INTEGER, sax::Token::TokenType::END_ELEMENT);
}
} /* namespace primitive */
 
namespace alib {
primitive::Integer xmlApi<primitive::Integer>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::Integer::parseInteger(input);
}
bool xmlApi<primitive::Integer>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::Integer::PRIMITIVE_INTEGER);
}
void xmlApi<primitive::Integer>::compose(std::deque<sax::Token>& output, const primitive::Integer& data) {
data.compose(output);
}
int xmlApi<int>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::Integer::parseIntegerRaw(input);
}
bool xmlApi<int>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::Integer::PRIMITIVE_INTEGER);
}
void xmlApi<int>::compose(std::deque<sax::Token>& output, int data) {
primitive::Integer::compose(output, data);
}
} /* namespace alib */
......@@ -9,6 +9,7 @@
#define PRIMITIVE_INTEGER_H_
 
#include "PrimitiveBase.h"
#include "../XmlApiBase.h"
 
namespace primitive {
 
......@@ -53,6 +54,14 @@ public:
virtual int selfTypeId() const {
return typeId(*this);
}
const static std::string PRIMITIVE_INTEGER;
static Integer parseInteger(std::deque<sax::Token>::iterator& input);
static int parseIntegerRaw(std::deque<sax::Token>::iterator& input);
void compose(std::deque<sax::Token>& out) const;
static void compose(std::deque<sax::Token>& out, int primitive);
};
 
} /* namespace primitive */
......@@ -68,4 +77,22 @@ struct compare<primitive::Integer> {
 
} /* namespace std */
 
namespace alib{
template<>
struct xmlApi<primitive::Integer> {
static primitive::Integer parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::Integer& data);
};
template<>
struct xmlApi<int> {
static int parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, int data);
};
} /* namespace alib */
#endif /* PRIMITIVE_INTEGER_H_ */
......@@ -21,19 +21,19 @@ Primitive PrimitiveFromXMLParser::parsePrimitive(std::deque<sax::Token>::iterato
Primitive PrimitiveFromXMLParser::parsePrimitive(std::deque<sax::Token>::iterator& input, const std::set<FEATURES>& features) const {
if(alib::xmlApi<Integer>::first(input)) {
if(!features.count(FEATURES::INTEGER)) throw exception::AlibException();
return Primitive(parseInteger(input));
return Primitive(Integer::parseInteger(input));
} else if(alib::xmlApi<String>::first(input)) {
if(!features.count(FEATURES::STRING)) throw exception::AlibException();
return Primitive(parseString(input));
return Primitive(String::parseString(input));
} else if(alib::xmlApi<Character>::first(input)) {
if(!features.count(FEATURES::CHAR)) throw exception::AlibException();
return Primitive(parseCharacter(input));
return Primitive(Character::parseCharacter(input));
} else if(alib::xmlApi<Unsigned>::first(input)) {
if(!features.count(FEATURES::UNSIGNED)) throw exception::AlibException();
return Primitive(parseUnsigned(input));
return Primitive(Unsigned::parseUnsigned(input));
} else if(alib::xmlApi<Bool>::first(input)) {
if(!features.count(FEATURES::BOOL)) throw exception::AlibException();
return Primitive(parseBool(input));
return Primitive(Bool::parseBool(input));
} else {
throw sax::ParserException(sax::Token("Integer, String, Character, Unsigned, Bool", sax::Token::TokenType::START_ELEMENT), *input);
}
......@@ -47,90 +47,4 @@ bool PrimitiveFromXMLParser::first(const std::deque<sax::Token>::const_iterator&
}
}
 
Integer PrimitiveFromXMLParser::parseInteger(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_INTEGER);
Integer data(std::stoi(popTokenData(input, sax::Token::TokenType::CHARACTER)));
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_INTEGER);
return data;
}
Character PrimitiveFromXMLParser::parseCharacter(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_CHARACTER);
Character data(popTokenData(input, sax::Token::TokenType::CHARACTER)[0]);
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_CHARACTER);
return data;
}
String PrimitiveFromXMLParser::parseString(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_STRING);
std::string data = "";
if( isTokenType(input, sax::Token::TokenType::CHARACTER))
data = popTokenData(input, sax::Token::TokenType::CHARACTER);
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_STRING);
return String(std::move(data));
}
Unsigned PrimitiveFromXMLParser::parseUnsigned(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_UNSIGNED);
Unsigned data(std::stou(popTokenData(input, sax::Token::TokenType::CHARACTER)));
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_UNSIGNED);
return data;
}
Bool PrimitiveFromXMLParser::parseBool(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_BOOL);
std::string tmp = popTokenData(input, sax::Token::TokenType::CHARACTER);
bool data;
if(tmp == "true")
data = true;
else if(tmp == "false")
data = false;
else
throw exception::AlibException("Invalid boolean value");
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_BOOL);
return Bool(data);
}
int PrimitiveFromXMLParser::parseIntegerRaw(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_INTEGER);
int data = std::stoi(popTokenData(input, sax::Token::TokenType::CHARACTER));
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_INTEGER);
return data;
}
char PrimitiveFromXMLParser::parseCharacterRaw(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_CHARACTER);
char data = popTokenData(input, sax::Token::TokenType::CHARACTER)[0];
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_CHARACTER);
return data;
}
std::string PrimitiveFromXMLParser::parseStringRaw(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_STRING);
std::string data = popTokenData(input, sax::Token::TokenType::CHARACTER);
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_STRING);
return data;
}
unsigned PrimitiveFromXMLParser::parseUnsignedRaw(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_UNSIGNED);
unsigned data = std::stou(popTokenData(input, sax::Token::TokenType::CHARACTER));
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_UNSIGNED);
return data;
}
bool PrimitiveFromXMLParser::parseBoolRaw(std::deque<sax::Token>::iterator& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_BOOL);
std::string tmp = popTokenData(input, sax::Token::TokenType::CHARACTER);
bool data;
if(tmp == "true")
data = true;
else if(tmp == "false")
data = false;
else
throw exception::AlibException("Invalid boolean value");
popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_BOOL);
return data;
}
} /* namespace primitive */
......@@ -32,18 +32,6 @@ private:
Primitive parsePrimitive(std::deque<sax::Token>::iterator& input, const std::set<FEATURES>&) const;
Primitive parsePrimitive(std::deque<sax::Token>::iterator& input) const;
 
Integer parseInteger(std::deque<sax::Token>::iterator& input) const;
Character parseCharacter(std::deque<sax::Token>::iterator& input) const;
String parseString(std::deque<sax::Token>::iterator& input) const;
Unsigned parseUnsigned(std::deque<sax::Token>::iterator& input) const;
Bool parseBool(std::deque<sax::Token>::iterator& input) const;
int parseIntegerRaw(std::deque<sax::Token>::iterator& input) const;
char parseCharacterRaw(std::deque<sax::Token>::iterator& input) const;
std::string parseStringRaw(std::deque<sax::Token>::iterator& input) const;
unsigned parseUnsignedRaw(std::deque<sax::Token>::iterator& input) const;
bool parseBoolRaw(std::deque<sax::Token>::iterator& input) const;
template<typename T> friend struct alib::xmlApi;
public:
bool first(const std::deque<sax::Token>::const_iterator& input) const;
......
......@@ -22,70 +22,4 @@ void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, const Primitiv
primitive.Accept((void*) &out, alib::ToXMLComposers::toXMLComposers);
}
 
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, const Integer& primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_INTEGER, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::itos(primitive.getData()), sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_INTEGER, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, const Character& primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_CHARACTER, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::string(1, primitive.getData()), sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_CHARACTER, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, const Unsigned& primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_UNSIGNED, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::utos(primitive.getData()), sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_UNSIGNED, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, const Bool& primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_BOOL, sax::Token::TokenType::START_ELEMENT);
if(primitive.getData())
out.emplace_back("true", sax::Token::TokenType::CHARACTER);
else
out.emplace_back("false", sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_BOOL, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, const String& primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_STRING, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(primitive.getData(), sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_STRING, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, int primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_INTEGER, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::itos(primitive), sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_INTEGER, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, char primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_CHARACTER, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::string(1, primitive), sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_CHARACTER, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, const std::string& primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_STRING, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(primitive, sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_STRING, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, unsigned primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_UNSIGNED, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::utos(primitive), sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_UNSIGNED, sax::Token::TokenType::END_ELEMENT);
}
void PrimitiveToXMLComposer::compose(std::deque<sax::Token>& out, bool primitive) const {
out.emplace_back(alib::Names::PRIMITIVE_BOOL, sax::Token::TokenType::START_ELEMENT);
if(primitive)
out.emplace_back("true", sax::Token::TokenType::CHARACTER);
else
out.emplace_back("false", sax::Token::TokenType::CHARACTER);
out.emplace_back(alib::Names::PRIMITIVE_BOOL, sax::Token::TokenType::END_ELEMENT);
}
} /* namespace primitive */
......@@ -43,18 +43,6 @@ private:
*/
void compose(std::deque<sax::Token>& out, const Primitive& primitive) const;
 
void compose(std::deque<sax::Token>& out, const String& primitive) const;
void compose(std::deque<sax::Token>& out, const Integer& primitive) const;
void compose(std::deque<sax::Token>& out, const Character& primitive) const;
void compose(std::deque<sax::Token>& out, const Unsigned& primitive) const;
void compose(std::deque<sax::Token>& out, const Bool& primitive) const;
void compose(std::deque<sax::Token>& out, const std::string& primitive) const;
void compose(std::deque<sax::Token>& out, int primitive) const;
void compose(std::deque<sax::Token>& out, char primitive) const;
void compose(std::deque<sax::Token>& out, unsigned primitive) const;
void compose(std::deque<sax::Token>& out, bool primitive) const;
template<typename T> friend struct alib::xmlApi;
};
 
......
......@@ -6,6 +6,7 @@
*/
 
#include "String.h"
#include "../sax/FromXMLParserHelper.h"
 
namespace primitive {
 
......@@ -45,5 +46,59 @@ String::operator std::string() const {
return data;
}
 
const std::string String::PRIMITIVE_STRING = "String";
String String::parseString(std::deque<sax::Token>::iterator& input) {
return String(parseStringRaw(input));
}
std::string String::parseStringRaw(std::deque<sax::Token>::iterator& input) {
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, primitive::String::PRIMITIVE_STRING);
std::string data = "";
if( sax::FromXMLParserHelper::isTokenType(input, sax::Token::TokenType::CHARACTER))
data = sax::FromXMLParserHelper::popTokenData(input, sax::Token::TokenType::CHARACTER);
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, primitive::String::PRIMITIVE_STRING);
return data;
}
void String::compose(std::deque<sax::Token>& out) const {
compose(out, this->getData());
}
void String::compose(std::deque<sax::Token>& out, std::string primitive) {
out.emplace_back(primitive::String::PRIMITIVE_STRING, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(primitive, sax::Token::TokenType::CHARACTER);
out.emplace_back(primitive::String::PRIMITIVE_STRING, sax::Token::TokenType::END_ELEMENT);
}
} /* namespace primitive */
 
namespace alib {
primitive::String xmlApi<primitive::String>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::String::parseString(input);
}
bool xmlApi<primitive::String>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::String::PRIMITIVE_STRING);
}
void xmlApi<primitive::String>::compose(std::deque<sax::Token>& output, const primitive::String& data) {
data.compose(output);
}
std::string xmlApi<std::string>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::String::parseStringRaw(input);
}
bool xmlApi<std::string>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::String::PRIMITIVE_STRING);
}
void xmlApi<std::string>::compose(std::deque<sax::Token>& output, std::string data) {
primitive::String::compose(output, data);
}
} /* namespace alib */
......@@ -10,6 +10,7 @@
 
#include <string>
#include "PrimitiveBase.h"
#include "../XmlApiBase.h"
 
namespace primitive {
 
......@@ -56,6 +57,14 @@ public:
virtual int selfTypeId() const {
return typeId(*this);
}
const static std::string PRIMITIVE_STRING;
static String parseString(std::deque<sax::Token>::iterator& input);
static std::string parseStringRaw(std::deque<sax::Token>::iterator& input);
void compose(std::deque<sax::Token>& out) const;
static void compose(std::deque<sax::Token>& out, std::string primitive);
};
 
} /* namespace primitive */
......@@ -71,5 +80,23 @@ struct compare<primitive::String> {
 
} /* namespace std */
 
namespace alib{
template<>
struct xmlApi<primitive::String> {
static primitive::String parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::String& data);
};
template<>
struct xmlApi<std::string> {
static std::string parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, std::string data);
};
} /* namespace alib */
#endif /* PRIMITIVE_STRING_H_ */
 
......@@ -6,6 +6,7 @@
*/
 
#include "Unsigned.h"
#include "../sax/FromXMLParserHelper.h"
 
namespace primitive {
 
......@@ -41,5 +42,56 @@ Unsigned::operator std::string() const {
return std::to_string(data);
}
 
const std::string Unsigned::PRIMITIVE_UNSIGNED = "Unsigned";
Unsigned Unsigned::parseUnsigned(std::deque<sax::Token>::iterator& input) {
return Unsigned(parseUnsignedRaw(input));
}
unsigned Unsigned::parseUnsignedRaw(std::deque<sax::Token>::iterator& input) {
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, Unsigned::PRIMITIVE_UNSIGNED);
unsigned data = std::stoi(sax::FromXMLParserHelper::popTokenData(input, sax::Token::TokenType::CHARACTER));
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, Unsigned::PRIMITIVE_UNSIGNED);
return data;
}
void Unsigned::compose(std::deque<sax::Token>& out) const {
compose(out, this->getData());
}
void Unsigned::compose(std::deque<sax::Token>& out, unsigned primitive) {
out.emplace_back(Unsigned::PRIMITIVE_UNSIGNED, sax::Token::TokenType::START_ELEMENT);
out.emplace_back(std::itos(primitive), sax::Token::TokenType::CHARACTER);
out.emplace_back(Unsigned::PRIMITIVE_UNSIGNED, sax::Token::TokenType::END_ELEMENT);
}
} /* namespace primitive */
 
namespace alib {
primitive::Unsigned xmlApi<primitive::Unsigned>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::Unsigned::parseUnsigned(input);
}
bool xmlApi<primitive::Unsigned>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::Unsigned::PRIMITIVE_UNSIGNED);
}
void xmlApi<primitive::Unsigned>::compose(std::deque<sax::Token>& output, const primitive::Unsigned& data) {
data.compose(output);
}
unsigned xmlApi<unsigned>::parse(std::deque<sax::Token>::iterator& input) {
return primitive::Unsigned::parseUnsignedRaw(input);
}
bool xmlApi<unsigned>::first(const std::deque<sax::Token>::const_iterator& input) {
return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, primitive::Unsigned::PRIMITIVE_UNSIGNED);
}
void xmlApi<unsigned>::compose(std::deque<sax::Token>& output, unsigned data) {
primitive::Unsigned::compose(output, data);
}
} /* namespace alib */
......@@ -9,6 +9,7 @@
#define PRIMITIVE_UNSIGNED_H_
 
#include "PrimitiveBase.h"
#include "../XmlApiBase.h"
 
namespace primitive {
 
......@@ -53,6 +54,14 @@ public:
virtual int selfTypeId() const {
return typeId(*this);
}
const static std::string PRIMITIVE_UNSIGNED;
static Unsigned parseUnsigned(std::deque<sax::Token>::iterator& input);
static unsigned parseUnsignedRaw(std::deque<sax::Token>::iterator& input);
void compose(std::deque<sax::Token>& out) const;
static void compose(std::deque<sax::Token>& out, unsigned primitive);
};
 
} /* namespace primitive */
......@@ -68,4 +77,22 @@ struct compare<primitive::Unsigned> {
 
} /* namespace std */
 
namespace alib{
template<>
struct xmlApi<primitive::Unsigned> {
static primitive::Unsigned parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, const primitive::Unsigned& data);
};
template<>
struct xmlApi<unsigned> {
static unsigned parse(std::deque<sax::Token>::iterator& input);
static bool first(const std::deque<sax::Token>::const_iterator& input);
static void compose(std::deque<sax::Token>& output, unsigned data);
};
} /* namespace alib */
#endif /* PRIMITIVE_UNSIGNED_H_ */
......@@ -11,6 +11,7 @@
#include <factory/XmlDataFactory.hpp>
#include <exception/AlibException.h>
#include <string/String.h>
#include <primitive/Bool.h>
#include <object/Object.h>
#include <tree/ranked/RankedTree.h>
#include <automaton/Automaton.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