Commit afc9818c authored by Bryan Berthou's avatar Bryan Berthou
Browse files

refs#16

In ElementaryUtils (trunk) : correct XMLParser, it did not create the attribute when its value was empty.
parent 6b944740
......@@ -4,11 +4,8 @@
/**
* @file PropertiesManager.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date 21 April 2015
* @date April 21, 2015
* @version 1.0
*
* @class PropertiesManager
* @brief \<singleton\> Use for handle properties from configuration's file.
*/
#include <string>
......@@ -21,6 +18,10 @@ class Mutex;
namespace ElemUtils {
/**
* @class PropertiesManager
* @brief \<singleton\> Use for handle properties from configuration's file.
*/
class PropertiesManager {
public:
......
......@@ -62,7 +62,15 @@ void LoggerManager::init() {
//TODO ameliorer le parsing du fichier de config et les erreurs
void LoggerManager::parseConfigurationFile(const std::string &filePath) {
IniFileParser iniFileParser;
iniFileParser.parse(filePath);
try {
iniFileParser.parse(filePath);
} catch (std::exception &e) {
throw std::runtime_error(
Formatter()
<< "(LoggerManager::parseConfigurationFile) Cannot read logger.cfg file : "
<< e.what());
}
std::map<std::string, std::string> data = iniFileParser.getValues();
std::map<std::string, std::string>::iterator it;
......@@ -74,7 +82,8 @@ void LoggerManager::parseConfigurationFile(const std::string &filePath) {
} catch (std::exception &e) {
throw std::runtime_error(
"Cannot instantiated LoggerManager wrong value for ["
+ ENABLE_NAME + "] parameter in logger.cfg file");
+ ENABLE_NAME
+ "] parameter in logger.cfg file");
}
}
// retrieve default level value
......
#include "../../../include/ElementaryUtils/parser/IniFileParser.h"
#include <stddef.h>
#include <iostream>
#include <stdexcept>
#include <utility>
#include <vector>
......@@ -37,7 +36,7 @@ void IniFileParser::parse(const std::string & configFilePath) {
} else {
// if the input file is unreadable then throw an exception
throw std::runtime_error(
"[IniFileParser] Unable to open config file: "
"(IniFileParser) Unable to open config file: "
+ configFilePath);
}
}
......@@ -67,9 +66,10 @@ void IniFileParser::analyse(std::string & fileLine, std::string & sectionName,
}
// else the equal character is missing
else {
std::cerr
<< "[IniFileParser::analyse] ERROR in the ini file '=' character is missing, line : "
<< fileLineNumber << std::endl;
throw std::runtime_error(
Formatter()
<< "(IniFileParser::analyse) ERROR in the ini file '=' character is missing, line : "
<< fileLineNumber);
}
}
......@@ -81,9 +81,10 @@ void IniFileParser::analyseSection(const std::string & fileLine,
// if there is no close square bracket character then print an error
if (closeSquareBracketCharPos == std::string::npos) {
std::cerr
<< "[IniFileParser::analyseSection] ERROR in the ini file ']' character is missing, line : "
<< fileLineNumber << std::endl;
throw std::runtime_error(
Formatter()
<< "(IniFileParser::analyseSection) ERROR in the ini file ']' character is missing, line : "
<< fileLineNumber);
}
// if close square bracket character is at the end of the string then retrieve the section name
else if (closeSquareBracketCharPos != fileLine.length() - 2) {
......@@ -91,9 +92,10 @@ void IniFileParser::analyseSection(const std::string & fileLine,
}
// else it's misplaced and print an error
else {
std::cerr
<< "[IniFileParser::analyseSection] ERROR in the ini file ']' character is at wrong place, line : "
<< fileLineNumber << std::endl;
throw std::runtime_error(
Formatter()
<< "(IniFileParser::analyseSection) ERROR in the ini file ']' character is at wrong place, line : "
<< fileLineNumber);
}
}
......@@ -131,7 +133,7 @@ std::string IniFileParser::getString(const std::string & key) {
return (m_it != m_values.end()) ?
m_it->second :
throw std::runtime_error(
"[IniFileParser::getString] enable to find property name = "
"(IniFileParser::getString) enable to find property name = "
+ key);
}
......
......@@ -113,12 +113,11 @@ void XMLParser::parseAttributesFromXMLElement(const std::string& file,
XMLAttributs &attributes, const size_t attributesStartIndex,
const size_t xmlEndTagIndex) {
//TODO this test fail if attributes are separated by a return line
// retrieve first character position of key attribut
size_t keyStringStartIndex = file.find_first_of(
XMLParser::XML_TAG_NAME_ALLOWED_CHARACTERS, attributesStartIndex);
if (file.at(keyStringStartIndex) < xmlEndTagIndex) {
if (keyStringStartIndex < xmlEndTagIndex) {
// try find the equal char separator
size_t equalCharIndex = file.find_first_of(
......@@ -131,23 +130,34 @@ void XMLParser::parseAttributesFromXMLElement(const std::string& file,
size_t valueStringStartIndex = file.find_first_of('"',
equalCharIndex) + 1;
// try to retrieve end index of value
size_t valueStringEndIndex = file.find_first_of('"',
valueStringStartIndex);
if (file.at(valueStringStartIndex) == '"') {
// attribut value is EMPTY
if (valueStringStartIndex != std::string::npos
&& valueStringEndIndex != std::string::npos) {
// if value retrieve then extract key and value to add them to attributs
attributes.add(
file.substr(keyStringStartIndex,
equalCharIndex - keyStringStartIndex),
file.substr(valueStringStartIndex,
valueStringEndIndex - valueStringStartIndex));
// parse remaining attributes recursively
parseAttributesFromXMLElement(file, attributes,
valueStringEndIndex + 1, xmlEndTagIndex);
StringUtils::EMPTY);
} else {
// try to retrieve end index of value
size_t valueStringEndIndex = file.find_first_of('"',
valueStringStartIndex);
if (valueStringStartIndex != std::string::npos
&& valueStringEndIndex != std::string::npos
&& valueStringEndIndex < xmlEndTagIndex) {
// if value retrieve then extract key and value to add them to attributs
attributes.add(
file.substr(keyStringStartIndex,
equalCharIndex - keyStringStartIndex),
file.substr(valueStringStartIndex,
valueStringEndIndex
- valueStringStartIndex));
// parse remaining attributes recursively
parseAttributesFromXMLElement(file, attributes,
valueStringEndIndex + 1, xmlEndTagIndex);
}
}
}
}
......
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