LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
xmlConfigurationFileHandler.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
15// ---------------------------------------------------------------------------
16// Includes
17// ---------------------------------------------------------------------------
18#include "common/LimaCommon.h"
22
23using namespace std;
24
25namespace Lima
26{
27namespace Common
28{
29namespace XMLConfigurationFiles
30{
31
32// ---------------------------------------------------------------------------
33// XMLConfigurationFileHandler: Constructors and Destructor
34// ---------------------------------------------------------------------------
36 QXmlDefaultHandler(),
37 m_moduleName(), m_groupName(""), m_listName(""), m_configuration(theConfiguration)
38{}
39
42
43
44// ---------------------------------------------------------------------------
45// XMLConfigurationFileHandler: Overrides of the SAX ErrorHandler interface
46// ---------------------------------------------------------------------------
47bool XMLConfigurationFileHandler::error(const QXmlParseException& e)
48{
50 LERROR << "Error at file " << e.systemId()
51 << ", line " << e.lineNumber()
52 << ", char " << e.columnNumber()
53 << " Message: " << e.message();
54 return false;
55}
56
57bool XMLConfigurationFileHandler::fatalError(const QXmlParseException& e)
58{
60 LERROR << "Fatal Error at file " << e.systemId()
61 << ", line " << e.lineNumber()
62 << ", char " << e.columnNumber()
63 << " Message: " << e.message();
64 return false;
65}
66
67bool XMLConfigurationFileHandler::warning(const QXmlParseException& e)
68{
70 LWARN << "Warning at file " << e.systemId()
71 << ", line " << e.lineNumber()
72 << ", char " << e.columnNumber()
73 << " Message: " << e.message();
74 return true;
75}
76
77
78bool XMLConfigurationFileHandler::endElement(const QString & , const QString & eltName, const QString & )
79{
80 string name=toString(eltName);
81 if (name == "Module") {
82 m_moduleName = "";
83 }
84 else if (name == "group") {
85 m_groupName = "";
86 }
87 else if (name == "list") {
88 if (m_firstItem) {
89 // empty list: must be added anyway
90 m_configuration.addListNamedForModuleAndGroup(m_listName, m_moduleName, m_groupName);
91 m_firstItem=false;
92 }
93 m_listName = "";
94 }
95 else if (name == "map") {
96 if (m_firstItem) {
97 // empty map: must be added anyway
98 m_firstItem=false;
99 m_configuration.addMapNamedForModuleAndGroup(m_mapName,m_moduleName,m_groupName);
100 }
101 m_mapName = "";
102 }
103 return true;
104}
105
106
107bool XMLConfigurationFileHandler::startElement( const QString & , const QString & name, const QString & , const QXmlAttributes & attributes)
108{
109 string stringName(toString(name));
110
112 LTRACE << "start element " << stringName;
113
114 // set the current module name and create its entry if necessary
115 if (stringName == std::string("module"))
116 {
117 m_moduleName = attributes.value("name").toUtf8().constData();
118 LTRACE << "XMLConfigurationFileHandler::startElement module name is " << m_moduleName;
119 if ((m_configuration.find(m_moduleName)) == (m_configuration.end()))
120 {
121 m_configuration.insert(make_pair(m_moduleName, ModuleConfigurationStructure(m_moduleName)));
122 }
123 }
124 // set the current group name inside moduleName and creates its entry if necessary
125 else if (stringName == "group")
126 {
127 m_groupName = toString(attributes.value("name"));
128 LTRACE << "group name is " << m_groupName;
129 int32_t indName=attributes.index("name"); // index for the attribute 'name' (not always the first)
130
131 m_configuration.addGroupNamedForModuleNamed(m_groupName, m_moduleName);
132 for (int32_t i=0;i<attributes.length();i++)
133 {
134 if (i==indName) { // if attribute 'name', ignored (already treated)
135 continue;
136 }
137 string key=toString(attributes.localName(i));
138 string value=toString(attributes.value(i));
139 m_configuration.addAttributeForGroupInModule(key,value,m_groupName,m_moduleName);
140 }
141 }
142 // creates a new parameter inside the current module and group
143 else if (stringName == "param")
144 {
145 string key = toString(attributes.value("key"));
146 string value = toString(attributes.value("value"));
147 LTRACE << "param key is " << key;
148 m_configuration.addParamValuePairForModuleAndGroup(key, value, m_moduleName, m_groupName);
149 }
150 else if (stringName == "list")
151 {
152 m_listName = toString(attributes.value("name"));
153 LTRACE << "list name is " << m_listName;
154 }
155 else if (stringName == "item")
156 {
157 uint32_t nbAtt=attributes.length();
158 if (m_firstItem) {
159 LTRACE << "add simple list "<< m_listName;
160 m_configuration.addListNamedForModuleAndGroup(m_listName, m_moduleName, m_groupName);
161 m_firstItem=false;
162 }
163
164 string value = toString(attributes.value("value"));
165 m_configuration.addItemInListNamedForModuleAndGroup(value, m_listName, m_moduleName, m_groupName);
166 }
167 else if (stringName == "map")
168 {
169 m_mapName = toString(attributes.value("name"));
170 LTRACE << "map name is " << m_mapName;
171 }
172 else if (stringName == "entry")
173 {
174 LTRACE << "entry in map";
175
176 uint32_t nbAtt=attributes.length();
177 if (m_firstItem) {
178 // decide if map is simple map or map of entries with attributes
179 LTRACE << "add map list "<< m_mapName.c_str();
180 m_configuration.addMapNamedForModuleAndGroup(m_mapName,m_moduleName,m_groupName);
181 m_firstItem=false;
182 }
183
184 string key = toString(attributes.value("key"));
185 string value = toString(attributes.value("value"));
186 m_configuration.addEntryInMapNamedForModuleAndGroup(key,value,m_mapName,m_moduleName,m_groupName);
187 }
188 return true;
189}
190
191std::string XMLConfigurationFileHandler::toString(const QString& xercesString)
192{
193 return Misc::limastring2utf8stdstring(xercesString);
194}
195
196} // closing namespace XMLConfigurationFiles
197} // closing namespace Common
198} // closing namespace Lima
#define XMLCFGLOGINIT
Definition LimaCommon.h:217
#define LWARN
Definition LimaCommon.h:160
#define LTRACE
Definition LimaCommon.h:156
#define LERROR
Definition LimaCommon.h:161
The instances of this class hold configuration informations of the modules Its design is currently ve...
void addAttributeForGroupInModule(const std::string &attKey, const std::string &attValue, const std::string &groupName, const std::string &moduleName)
void addListNamedForModuleAndGroup(const std::string &listName, const std::string &moduleName, const std::string &group)
void addEntryInMapNamedForModuleAndGroup(const std::string &entryKey, const std::string &entryValue, const std::string &mapName, const std::string &moduleName, const std::string &groupName)
void addMapNamedForModuleAndGroup(const std::string &mapName, const std::string &moduleName, const std::string &group)
void addGroupNamedForModuleNamed(const std::string &groupName, const std::string &moduleName)
void addItemInListNamedForModuleAndGroup(const std::string &item, const std::string &listName, const std::string &moduleName, const std::string &group)
void addParamValuePairForModuleAndGroup(const std::string &param, const std::string &value, const std::string &module, const std::string &group)
bool endElement(const QString &namespaceURI, const QString &name, const QString &qName) override
bool startElement(const QString &namespaceURI, const QString &name, const QString &qName, const QXmlAttributes &attributes) override
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
NAUTITIA.
STL namespace.
launch exception related to the configuration file parsing
originally in detectlibraries