LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
xmlConfigurationFileReader.cpp
Go to the documentation of this file.
1// Copyright 2002-2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
10// ---------------------------------------------------------------------------
11// Includes
12// ---------------------------------------------------------------------------
13#include "common/LimaCommon.h"
17
18#include <QXmlStreamReader>
19
20namespace Lima
21{
22namespace Common
23{
24namespace XMLConfigurationFiles
25{
26
28{
29public:
30 // -----------------------------------------------------------------------
31 // Constructors
32 // -----------------------------------------------------------------------
35
36 bool parse(QIODevice *device);
37 void readConfiguration();
38 void readModule();
39 void readGroup();
40 void readParam();
41 void readList();
42 void readListItem(const QString& listName);
43 void readMap();
44 void readMapEntry(const QString& mapName);
45
46 static inline QString moduleNameAttribute() { return QStringLiteral("name"); }
47 static inline QString groupNameAttribute() { return QStringLiteral("name"); }
48 static inline QString listNameAttribute() { return QStringLiteral("name"); }
49 static inline QString mapNameAttribute() { return QStringLiteral("name"); }
50 static inline QString paramKeyAttribute() { return QStringLiteral("key"); }
51 static inline QString paramValueAttribute() { return QStringLiteral("value"); }
52 static inline QString listItemValueAttribute() { return QStringLiteral("value"); }
53 static inline QString mapEntryKeyAttribute() { return QStringLiteral("key"); }
54 static inline QString mapEntryValueAttribute() { return QStringLiteral("value"); }
55
56 QXmlStreamReader m_reader;
57 /*
58 * The current module group and, possibly, list
59 */
60 QString m_moduleName;
61 QString m_groupName;
62
63
64 /*
65 * The structure where data will be stored
66 */
68};
69
71 m_reader(),
72 m_moduleName(""), m_groupName(""), m_configuration(theConfiguration)
73{}
74
78
83
84bool XmlConfigurationFileReader::parse(QIODevice *device)
85{
86 return m_d->parse(device);
87}
88
89// <?xml version='1.0' encoding='UTF-8'?>
90// <modulesConfig>
91// …
92// </modulesConfig>
94{
96 LTRACE << "parse";
97 m_reader.setDevice(device);
98 if (m_reader.readNextStartElement())
99 {
100 if (m_reader.name() == QLatin1String("modulesConfig"))
101 {
103 }
104 else
105 {
106 m_reader.raiseError(QObject::tr("The file is not a LIMA XML configuration file."));
107 }
108 }
109 return !m_reader.error();
110}
111
112// <modulesConfig>
113// <module name="common">
114// …
115// </module>
116// …
117// </modulesConfig>
119{
121 LTRACE << "readConfiguration";
122 Q_ASSERT(m_reader.isStartElement() && m_reader.name() == QLatin1String("modulesConfig"));
123
124 while (m_reader.readNextStartElement())
125 {
126 if (m_reader.name() == QLatin1String("module"))
127 readModule();
128 else
129 m_reader.raiseError(QObject::tr("Expected a module but got a %1.").arg(m_reader.name()));
130 }
131}
132
133// <module name="common">
134// <group name="mediaDeclaration">
135// …
136// </group>
137// …
138// </module>
140{
141 m_moduleName = m_reader.attributes().value(moduleNameAttribute()).toString();
143 LTRACE << "XmlConfigurationFileReader::readModule module name is " << m_moduleName;
144 if ((m_configuration.find(m_moduleName.toStdString())) == (m_configuration. end()))
145 {
146 m_configuration.insert(std::make_pair(m_moduleName.toStdString(), ModuleConfigurationStructure(m_moduleName)));
147 }
148 while (m_reader.readNextStartElement())
149 {
150 if (m_reader.name() == QLatin1String("group"))
151 readGroup();
152 else
153 m_reader.raiseError(QObject::tr("Expected a group in module %1 but got a %2.").arg(m_moduleName).arg(m_reader.name()));
154 }
155 m_moduleName = "";
156}
157
158// <group name="mediaDeclaration">
159// <list name="available">
160// …
161// </list>
162// <param key="eng" value="6"/>
163// <map name="conceptTypes">
164// …
165// </map>
166// </group>
168{
169 m_groupName = m_reader.attributes().value(groupNameAttribute()).toString();
171 LTRACE << "XmlConfigurationFileReader::readGroup group name is " << m_groupName;
172
174 for (const auto& attribute: m_reader.attributes())
175 {
176 if (attribute.name() == groupNameAttribute())
177 {
178 continue;
179 }
180 auto key = attribute.name();
181 auto value = attribute.value();
182 m_configuration.addAttributeForGroupInModule(key.toString(), value.toString(), m_groupName, m_moduleName);
183 }
184
185 while (m_reader.readNextStartElement())
186 {
187 if (m_reader.name() == QLatin1String("param"))
188 readParam();
189 else if (m_reader.name() == QLatin1String("list"))
190 readList();
191 else if (m_reader.name() == QLatin1String("map"))
192 readMap();
193 else
194 m_reader.raiseError(QObject::tr("Expected a param, a list or a map in group %1, in module %2 but got a %3.").arg(m_groupName).arg(m_moduleName).arg(m_reader.name()));
195 }
196 m_groupName = "";
197
198}
199
200// <param key="eng" value="6"/>
202{
203 auto key = m_reader.attributes().value(paramKeyAttribute());
204 auto value = m_reader.attributes().value(paramValueAttribute());
206 LTRACE << "XmlConfigurationFileReader::readParam" << key << value;
208 m_reader.skipCurrentElement();
209}
210
211// <list name="available">
212// <item value="eng" />
213// …
214// </list>
216{
217 auto listName = m_reader.attributes().value(listNameAttribute()).toString();
219 LTRACE << "XmlConfigurationFileReader::readList" << listName;
221 while (m_reader.readNextStartElement())
222 {
223 if (m_reader.name() == QLatin1String("item"))
224 readListItem(listName);
225 else
226 m_reader.raiseError(QObject::tr("Expected a list item in list %1 in group %2, in module %3 but got a %4.")
227 .arg(listName).arg(m_groupName).arg(m_moduleName).arg(m_reader.name()));
228 }
229}
230
231// <item value="eng" />
233{
234 auto value = m_reader.attributes().value(listItemValueAttribute());
236 LTRACE << "XmlConfigurationFileReader::readListItem" << value;
238 m_reader.skipCurrentElement();
239}
240
241// <map name="conceptTypes">
242// <entry key="LatticeDown" value="0"/>
243// …
244// </map>
246{
247 auto mapName = m_reader.attributes().value(mapNameAttribute()).toString();
249 LTRACE << " XmlConfigurationFileReaderPrivate::readMap" << mapName;
250 LTRACE << "XmlConfigurationFileReaderPrivate::readMap add empty map"<< mapName;
252 while (m_reader.readNextStartElement())
253 {
254 if (m_reader.name() == QLatin1String("entry"))
255 readMapEntry(mapName);
256 else
257 m_reader.raiseError(QObject::tr("Expected a map entry in map %1 in group %2, in module %3 but got a %4.")
258 .arg(mapName).arg(m_groupName).arg(m_moduleName).arg(m_reader.name()));
259 }
260}
261
262// <entry key="LatticeDown" value="0"/>
264{
266 LTRACE << "XmlConfigurationFileReaderPrivate::readMapEntry";
267
268 auto key = m_reader.attributes().value(mapEntryKeyAttribute());
269 auto value = m_reader.attributes().value(mapEntryValueAttribute());
271 key.toString(), value.toString(), mapName, m_moduleName, m_groupName);
272 m_reader.skipCurrentElement();
273}
274
276{
278 auto errorStr = QObject::tr("%1, Line %2, column %3")
279 .arg(m_d->m_reader.errorString())
280 .arg(m_d->m_reader.lineNumber())
281 .arg(m_d->m_reader.columnNumber());
282 LERROR << errorStr;
283 return errorStr;
284}
285
286} // closing namespace XMLConfigurationFiles
287} // closing namespace Common
288} // closing namespace Lima
#define XMLCFGLOGINIT
Definition LimaCommon.h:217
#define LTRACE
Definition LimaCommon.h:156
#define LIMA_XMLCONFIGURATIONFILES_EXPORT
Definition LimaCommon.h:137
#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)
NAUTITIA.
launch exception related to the configuration file parsing