Commit 721b87bd authored by Bryan Berthou's avatar Bryan Berthou
Browse files

refs#16

In ElementaryUtils (trunk) :
- Improve XMLParser : remove XMLAttribute class.
parent 629ac4c1
#ifndef ATTRIBUTS_H
#define ATTRIBUTS_H
/**
* @file Attributs.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @author <contributor> Adrien KIELB (Modulo PI - Paris)
* @date 30 January 2014
* @version 1.0
*
* @class Attributs
* @brief
*/
#include <map>
#include <string>
namespace ElemUtils {
class XMLAttributs {
public:
XMLAttributs();
virtual ~XMLAttributs();
void add(const std::string &key, const std::string &value);
std::string getStringValueOf(const std::string &key);
int getIntValueOf(const std::string &key);
double getDoubleValueOf(const std::string &key);
bool getBooleanValueOf(const std::string &key);
bool isInt(std::string);
std::string toString();
bool isAvailable(const std::string &key) const;
private:
std::map<std::string, std::string> m_attributes;
};
} // namespace ElemUtils
#endif /* ATTRIBUTS_H */
......@@ -12,9 +12,7 @@
#include <stddef.h>
#include <string>
namespace ElemUtils {
class XMLAttributs;
} /* namespace ElemUtils */
#include "../parameters/Parameters.h"
namespace ElemUtils {
......@@ -39,20 +37,20 @@ public:
void loop(const std::string& file, size_t startIndex = 0);
virtual void startElement(const std::string &elementName,
XMLAttributs attributes, const std::string &elementData) = 0;
Parameters attributes, const std::string &elementData) = 0;
virtual void emptyStartElement(const std::string &elementName,
XMLAttributs attributes) = 0;
Parameters attributes) = 0;
virtual void endElement(const std::string &elementName) = 0;
private:
static const char TAB_CHARACTER;
static const char ATTRIBUTE_KEY_VALUE_SEPARATOR_CHARACTER;
static const char XML_VERSION_CHARACTER_IDENTIFIER;
static const char XML_COMMENT_CHARACTER_IDENTIFIER;
static const char XML_END_TAG_CHARACTER_IDENTIFIER;
static const std::string ATTRIBUTE_KEY_VALUE_SEPARATOR_CHARACTER;
static const std::string XML_END_TAG_VERSION_IDENTIFIER;
static const std::string XML_END_TAG_COMMENT_IDENTIFIER;
static const std::string XML_TAG_NAME_ALLOWED_CHARACTERS;
......@@ -69,7 +67,7 @@ private:
void parseNode(size_t startIndex);
void parseAttributesFromXMLElement(const std::string& file,
XMLAttributs &attributes, const size_t attributesStartIndex,
Parameters &attributes, const size_t attributesStartIndex,
const size_t xmlEndTagIndex);
};
......
#include "../../../include/ElementaryUtils/parser/XMLAttributs.h"
#include <iostream>
#include <sstream> // needed #include <stdexcept> #include <utility> #include <utility> #include <utility> #include <utility>
#include "../../../include/ElementaryUtils/logger/CustomException.h"
#include "../../../include/ElementaryUtils/string_utils/StringUtils.h"
namespace ElemUtils {
XMLAttributs::XMLAttributs() {
}
XMLAttributs::~XMLAttributs() {
}
std::string XMLAttributs::getStringValueOf(const std::string &key) {
std::map<std::string, std::string>::const_iterator it;
it = m_attributes.find(key);
if (it != m_attributes.end()) {
return it->second;
} else {
throw std::runtime_error(
"[Attributs::getStringValueOf] Enable to find key = " + key);
}
}
//TODO tester le cast
int XMLAttributs::getIntValueOf(const std::string &key) {
int i = 0;
std::istringstream istr(getStringValueOf(key));
istr >> i;
return i;
}
//TODO tester le cast
double XMLAttributs::getDoubleValueOf(const std::string &key) {
double d = 0.;
std::istringstream istr(getStringValueOf(key));
istr >> d;
return d;
}
bool XMLAttributs::isInt(std::string s) {
if (s.find_first_of(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ &�\"'(-�_��)=~#{[|`^@]}/*-+.!:;,?�")
== -1) {
return true;
}
return false;
}
void XMLAttributs::add(const std::string& key, const std::string& value) {
m_attributes.insert(std::make_pair(key, value));
}
std::string XMLAttributs::toString() {
std::string result = "";
for (std::map<std::string, std::string>::const_iterator it =
m_attributes.begin(); it != m_attributes.end(); it++) {
result += (it->first) + " = " + (it->second) + '\n';
}
return result;
}
bool XMLAttributs::isAvailable(const std::string& key) const {
bool result = false;
std::map<std::string, std::string>::const_iterator it = m_attributes.find(
key);
if (it != m_attributes.end()) {
result = true;
}
return result;
}
bool XMLAttributs::getBooleanValueOf(const std::string& key) {
std::stringstream sstream;
sstream << getStringValueOf(key);
bool b = false;
// if conversion failed then print an exception
if ((sstream >> b).fail()) {
if (StringUtils::equalsIgnoreCase(sstream.str(), "true")) {
b = 1;
} else if (!StringUtils::equalsIgnoreCase(sstream.str(), "false")) {
throw CustomException("XMLAttributs", __func__,
"Cast from std::string to bool failed ! ");
}
}
return b;
}
} // namespace ElemUtils
#include "../../../include/ElementaryUtils/parser/XMLParser.h"
#include <stdexcept>
#include "../../../include/ElementaryUtils/logger/LoggerManager.h"
#include "../../../include/ElementaryUtils/parser/XMLAttributs.h"
#include "../../../include/ElementaryUtils/string_utils/Formatter.h"
#include "../../../include/ElementaryUtils/string_utils/StringUtils.h"
namespace ElemUtils {
const char XMLParser::TAB_CHARACTER = '\t';
const char XMLParser::ATTRIBUTE_KEY_VALUE_SEPARATOR_CHARACTER = '=';
const char XMLParser::XML_VERSION_CHARACTER_IDENTIFIER = '?';
const char XMLParser::XML_COMMENT_CHARACTER_IDENTIFIER = '!';
const char XMLParser::XML_END_TAG_CHARACTER_IDENTIFIER = '/';
const std::string XMLParser::ATTRIBUTE_KEY_VALUE_SEPARATOR_CHARACTER = "=";
const std::string XMLParser::XML_END_TAG_VERSION_IDENTIFIER = "?>";
const std::string XMLParser::XML_END_TAG_COMMENT_IDENTIFIER = "-->";
const std::string XMLParser::XML_TAG_NAME_ALLOWED_CHARACTERS =
......@@ -131,7 +128,7 @@ void XMLParser::parseElement(const std::string& file,
== XMLParser::XML_END_TAG_CHARACTER_IDENTIFIER) {
// retrieve its attributes
XMLAttributs attributes;
Parameters attributes;
parseAttributesFromXMLElement(file, attributes, elementNameEndIndex,
xmlEndTagIndex);
......@@ -146,7 +143,7 @@ void XMLParser::parseElement(const std::string& file,
//TODO retrieve data like <node> some data </node>
// retrieve its attributes
XMLAttributs attributes;
Parameters attributes;
parseAttributesFromXMLElement(file, attributes, elementNameEndIndex,
xmlEndTagIndex);
......@@ -161,8 +158,10 @@ void XMLParser::parseElement(const std::string& file,
}
void XMLParser::parseAttributesFromXMLElement(const std::string& file,
XMLAttributs &attributes, const size_t attributesStartIndex,
Parameters &attributes, const size_t attributesStartIndex,
const size_t xmlEndTagIndex) {
std::string attributeName = StringUtils::EMPTY;
std::string attributeValue = StringUtils::EMPTY;
// retrieve first character position of key attribut
size_t keyStringStartIndex = file.find_first_of(
......@@ -172,51 +171,76 @@ void XMLParser::parseAttributesFromXMLElement(const std::string& file,
if (keyStringStartIndex < xmlEndTagIndex) {
// try find the equal char separator
size_t equalCharIndex = file.find_first_of(
size_t equalCharIndex = StringUtils::findFirstJustAfter(file,
XMLParser::ATTRIBUTE_KEY_VALUE_SEPARATOR_CHARACTER,
keyStringStartIndex);
if (equalCharIndex != std::string::npos) {
if (equalCharIndex != m_fileSize && equalCharIndex < xmlEndTagIndex) {
// if found;
// try to retrieve start index of value
size_t valueStringStartIndex = file.find_first_of('"',
equalCharIndex) + 1;
std::string attributeName = StringUtils::EMPTY;
std::string attributeValue = StringUtils::EMPTY;
// if valueStringStartIndex is not a double quote character : attribute value is not empty
if (file.at(valueStringStartIndex) != '"') {
// try to retrieve end index of value
size_t valueStringEndIndex = file.find_first_of('"',
valueStringStartIndex);
// if end of file or end tag element is not reach : value is retrieved
if (valueStringEndIndex != std::string::npos
|| valueStringEndIndex < xmlEndTagIndex) {
attributeValue = file.substr(valueStringStartIndex,
valueStringEndIndex - valueStringStartIndex);
} else {
// else a missing double quote character is missing somewhere
std::runtime_error(
"(XMLParser::parseAttributesFromXMLElement) Bad XML file : missing character");
size_t valueStringStartIndex = StringUtils::findFirstJustAfter(file,
"\"", equalCharIndex);
if (valueStringStartIndex != m_fileSize
&& valueStringStartIndex < xmlEndTagIndex) {
// if valueStringStartIndex is not a double quote character : attribute value is not empty
if (file.at(valueStringStartIndex) != '"') {
// try to retrieve end index of value
size_t valueStringEndIndex = StringUtils::findFirst(file,
"\"", valueStringStartIndex);
// if end of file or end tag element is not reach : value is retrieved
if (valueStringEndIndex != m_fileSize
&& valueStringEndIndex < xmlEndTagIndex) {
attributes.add(
file.substr(keyStringStartIndex,
(equalCharIndex-1) - keyStringStartIndex),
attributeValue = file.substr(
valueStringStartIndex,
valueStringEndIndex
- valueStringStartIndex));
}
// parse remaining attributes recursively
parseAttributesFromXMLElement(file, attributes,
valueStringEndIndex, xmlEndTagIndex);
}
// removed unwanted space characters from attribute name
std::string attributeName = file.substr(keyStringStartIndex,
equalCharIndex - keyStringStartIndex);
StringUtils::replaceAll(attributeName, " ", StringUtils::EMPTY);
// add new attribute to the previous attribute list
attributes.add(attributeName, attributeValue);
// parse remaining attributes recursively
parseAttributesFromXMLElement(file, attributes,
valueStringEndIndex + 1, xmlEndTagIndex);
}
}
}
//
// // if valueStringStartIndex is not a double quote character : attribute value is not empty
// if (file.at(valueStringStartIndex) != '"') {
// // try to retrieve end index of value
// size_t valueStringEndIndex = file.find_first_of('"',
// valueStringStartIndex);
//
// // if end of file or end tag element is not reach : value is retrieved
// if (valueStringEndIndex != std::string::npos
// || valueStringEndIndex < xmlEndTagIndex) {
// attributeValue = file.substr(valueStringStartIndex,
// valueStringEndIndex - valueStringStartIndex);
// } else {
// // else a missing double quote character is missing somewhere
// std::runtime_error(
// "(XMLParser::parseAttributesFromXMLElement) Bad XML file : missing character");
// }
//
// // removed unwanted space characters from attribute name
// std::string attributeName = file.substr(keyStringStartIndex,
// equalCharIndex - keyStringStartIndex);
// StringUtils::replaceAll(attributeName, " ", StringUtils::EMPTY);
//
// // add new attribute to the previous attribute list
// attributes.add(attributeName, attributeValue);
//
// // parse remaining attributes recursively
// parseAttributesFromXMLElement(file, attributes,
// valueStringEndIndex + 1, xmlEndTagIndex);
// }
//
// }
// }
}
} // namespace ElemUtils
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment