LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
xmlConfigurationFileParser.cpp
Go to the documentation of this file.
1// Copyright 2002-2022 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
12
13#include "common/LimaCommon.h"
15
19
20#include <QFile>
21#include <iostream>
22#include <memory>
23#include <string>
24
25namespace Lima {
26namespace Common {
27namespace XMLConfigurationFiles {
28
46
48 const XMLConfigurationFileParserPrivate& configuration) :
49 m_configuration(configuration.m_configuration),
50 m_parser(m_configuration),
51 m_configurationFileName(configuration.m_configurationFileName)
52{
53}
54
56 const QString &configurationFileName) :
57 m_configuration(),
58 m_parser(m_configuration),
59 m_configurationFileName(configurationFileName)
60{
62 LINFO << "XMLConfigurationFileParser creating parser for: "
63 << configurationFileName;
64
65 QFile file(m_configurationFileName);
66 if (!file.open(QFile::ReadOnly))
67 {
69 LERROR << "Error opening " << m_configurationFileName;
70 throw std::runtime_error(std::string("XMLConfigurationFileParser Unable to open ")
71 + m_configurationFileName.toUtf8().constData());
72 }
73 if (!m_parser.parse(&file))
74 {
75 LERROR << "Error parsing " << m_configurationFileName;
76 throw XMLException(std::string("XMLConfigurationFileParser Unable to parse ")
77 + m_configurationFileName.toUtf8().constData() + " : "
78 + m_parser.errorString().toUtf8().constData());
79 }
80 {
82 LINFO << "File parsed:" << m_configurationFileName;
83 }
84
85}
86
87// XMLConfigurationFileParser::XMLConfigurationFileParser() :
88// m_d(new XMLConfigurationFileParserPrivate())
89// {
90// }
91
96
97// XMLConfigurationFileParser::XMLConfigurationFileParser(
98// const std::string &configurationFileName) :
99// m_d(std::make_unique<XMLConfigurationFileParserPrivate>(
100// QString::fromUtf8(configurationFileName.c_str())))
101// {
102// }
103
105 const QString &configurationFileName) :
106 m_d(std::make_unique<XMLConfigurationFileParserPrivate>(
107 configurationFileName))
108{
109}
110
114
116{
117 return m_d->m_configuration;
118}
119
121 const std::string& moduleName)
122{
123 auto it = m_d->m_configuration.find(moduleName);
124
125 if (it == m_d->m_configuration.end())
126 {
128 LDEBUG << "XMLConfigurationFileParser::getModuleConfiguration no such module"
129 << moduleName << "in" << m_d->m_configurationFileName;
130 throw NoSuchModule(std::string(m_d->m_configurationFileName.toStdString())+":["+moduleName+"]");
131 }
132 return (*it).second;
133}
134
136 const std::string& moduleName,const std::string& groupName)
137{
138 return (getModuleConfiguration(moduleName).getGroupNamed(groupName));
139}
140
142 const std::string& moduleName,
143 const std::string& groupName,
144 const std::string& key)
145{
147 try
148 {
149 return (getModuleConfiguration(moduleName).getParamValueAtKeyOfGroupNamed(key, groupName));
150 }
151 catch(NoSuchModule& nsm)
152 {
153 //std::cerr << nsm.what() << " "
154 // << m_d->m_configurationFileName.toUtf8().constData() << std::endl;
155 LWARN << nsm.what() << " " << m_d->m_configurationFileName;
156 //not LERROR because user may want the module to be optional -> no error
157 throw NoSuchModule(std::string(m_d->m_configurationFileName.toUtf8().constData())
158 +":["+moduleName
159 +"]["+groupName+"]["+key+"]");
160 }
161 catch(NoSuchGroup& nsg)
162 {
163 //std::cerr << nsg.what() << " "
164 // << m_d->m_configurationFileName.toUtf8().constData() << std::endl;
165 LWARN << nsg.what() << " " << m_d->m_configurationFileName;
166 throw NoSuchGroup(std::string(m_d->m_configurationFileName.toUtf8().constData())
167 +":["+moduleName+"]["
168 +groupName+"]["+key+"]");
169 }
170 catch(NoSuchParam& nsp)
171 {
172 //std::cerr << nsp.what() << " "
173 // << m_d->m_configurationFileName.toUtf8().constData() << std::endl;
174 LWARN << nsp.what() << " " << m_d->m_configurationFileName;
175 throw NoSuchParam(std::string(m_d->m_configurationFileName.toUtf8().constData())+":["
176 +moduleName+"]["+groupName+"]["+key+"]");
177 }
178 catch(...)
179 {
180 throw;
181 }
182}
183
185 const std::string& moduleName,
186 const std::string& groupName,
187 const std::string& key)
188{
189 return (getModuleConfiguration(moduleName).getListValuesAtKeyOfGroupNamed(
190 key, groupName));
191}
192
194{
195 return m_d->m_configurationFileName;
196}
197
198std::ostream& operator<<(std::ostream& os, XMLConfigurationFileParser& parser)
199{
200 return (os << parser.m_d->m_configurationFileName.toUtf8().constData()
201 << ":" << parser.m_d->m_configuration);
202}
203
204} // closing namespace XMLConfigurationFiles
205} // closing namesapce Common
206} // closing namespace Lima
#define FILESREPORTLOGINIT
Definition FileUtils.h:28
#define XMLCFGLOGINIT
Definition LimaCommon.h:217
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
The instances of this class hold configuration informations of the modules Its design is currently ve...
return a message when a 'group' was not found
return a message when a 'module' is not found
return a message when a 'param' was not found
friend std::ostream & operator<<(std::ostream &os, XMLConfigurationFileParser &parser)
Parser class for the lima's xml configuration files.
GroupConfigurationStructure & getModuleGroupConfiguration(const std::string &moduleName, const std::string &groupName)
std::deque< std::string > & getModuleGroupListValues(const std::string &moduleName, const std::string &groupName, const std::string &key)
std::string & getModuleGroupParamValue(const std::string &moduleName, const std::string &groupName, const std::string &key)
ModuleConfigurationStructure & getModuleConfiguration(const std::string &moduleName)
This is a SAX document handler the configuration files.
virtual const char * what() const override
Definition LimaCommon.h:280
Throw this exception when encountering problems with XML files opening or parsing.
Definition LimaCommon.h:470
originally detectConfigurationStructure.h in detectlibraries
std::ostream & operator<<(std::ostream &os, const ConfigurationStructure &conf)
NAUTITIA.
STL namespace.
launch exception related to the configuration file parsing