LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SymbolicCodeXmlReader.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
5
7#include "common/LimaCommon.h"
9
10#include <QXmlStreamReader>
11
12using namespace Lima;
13using namespace Lima::Common::PropertyCode;
14
16{
18public:
19 // -----------------------------------------------------------------------
20 // Constructors
21 // -----------------------------------------------------------------------
23 std::map<std::string,Lima::LinguisticCode>& outputMap);
25
26 bool parse(QIODevice *device);
27 void readCodes();
28 void readCode();
29 void readField();
30
31 QXmlStreamReader m_reader;
33 std::map<std::string, std::string> m_currentPropertyValues;
34 std::map<std::string,Lima::LinguisticCode>& m_outputMap;
36};
37
39 std::map<std::string,LinguisticCode>& outputMap) :
40 m_currentCode(),
41 m_currentPropertyValues(),
42 m_outputMap(outputMap),
43 m_propCodeManager(pcm)
44{}
45
47 std::map<std::string,LinguisticCode>& outputMap) :
48 m_d(new SymbolicCodeXmlReaderPrivate(pcm, outputMap))
49{}
50
55
56bool SymbolicCodeXmlReader::parse(QIODevice *device)
57{
58 return m_d->parse(device);
59}
60
61// <?xml version='1.0' encoding='UTF-8'?>
62// <codes>
63// <code key="">
64// <field key="MICRO" value=""/>
65// </code>
66// </codes>
68{
70 LDEBUG << "SymbolicCodeXmlReaderPrivate::parse";
71 m_reader.setDevice(device);
72 if (m_reader.readNextStartElement()) {
73 if (m_reader.name() == QLatin1String("codes"))
74 {
75 readCodes();
76 }
77 else
78 {
79 m_reader.raiseError(QObject::tr("The file is not a LIMA XML symbolic codes file."));
80 }
81 }
82 return !m_reader.error();
83}
84
85// <?xml version='1.0' encoding='UTF-8'?>
86// <codes>
87// …
88// </codes>
90{
92 LTRACE << "SymbolicCodeXmlReader::readCodes" << m_reader.name();
93 Q_ASSERT(m_reader.isStartElement() && m_reader.name() == QLatin1String("codes"));
94
95 while (m_reader.readNextStartElement()) {
96 if (m_reader.name() == QLatin1String("code"))
97 readCode();
98 else
99 m_reader.raiseError(QObject::tr("Expected a code but got a %1.").arg(m_reader.name()));
100 }
101}
102
103// <code key="">
104// <field key="MICRO" value=""/>
105// …
106// </code>
108{
110 Q_ASSERT(m_reader.isStartElement() && m_reader.name() == QLatin1String("code"));
111
112 m_currentCode = m_reader.attributes().value("key").toString();
114 LTRACE << "SymbolicCodeXmlReader::readCode" << m_reader.name() << m_currentCode;
115 while (m_reader.readNextStartElement()) {
116 if (m_reader.name() == QLatin1String("field"))
117 readField();
118 else
119 m_reader.raiseError(QObject::tr("Expected a field but got a %1.").arg(m_reader.name()));
120 }
121 if (m_outputMap.find(m_currentCode.toStdString()) != m_outputMap.end())
122 {
124 LWARN << m_currentCode << " already exists! overwrite it";
125 }
127}
128
129// <field key="MICRO" value=""/>
131{
132#ifdef DEBUG_LP
134 LTRACE << "SymbolicCodeXmlReader::readField" << m_reader.name()
135 << m_reader.attributes().value("key")
136 << m_reader.attributes().value("value");
137#endif
138 Q_ASSERT(m_reader.isStartElement() && m_reader.name() == QLatin1String("field"));
139 auto key = m_reader.attributes().value("key").toString();
140 auto value = m_reader.attributes().value("value").toString();
141 if (value != "")
142 {
143 m_currentPropertyValues[key.toStdString()] = value.toStdString();
144 }
145 m_reader.skipCurrentElement();
146}
147
149{
151 auto errorStr = QObject::tr("%1, Line %2, column %3")
152 .arg(m_d->m_reader.errorString())
153 .arg(m_d->m_reader.lineNumber())
154 .arg(m_d->m_reader.columnNumber());
155 LERROR << errorStr;
156 return errorStr;
157}
#define LWARN
Definition LimaCommon.h:160
#define LTRACE
Definition LimaCommon.h:156
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define PROPERTYCODELOGINIT
Definition LimaCommon.h:213
#define LIMA_PROPERTYCODE_EXPORT
Provide tools to parse a property file, and deal with the property coding system.
LinguisticCode encode(const std::map< std::string, std::string > &propValues) const
Compute the code corresponding to given the properties/values.
SymbolicCodeXmlReaderPrivate(const Lima::Common::PropertyCode::PropertyCodeManager &pcm, std::map< std::string, Lima::LinguisticCode > &outputMap)
std::map< std::string, Lima::LinguisticCode > & m_outputMap
const Lima::Common::PropertyCode::PropertyCodeManager & m_propCodeManager
std::map< std::string, std::string > m_currentPropertyValues
SymbolicCodeXmlReader(const Lima::Common::PropertyCode::PropertyCodeManager &pcm, std::map< std::string, Lima::LinguisticCode > &outputMap)
bool parse(QIODevice *device)
NAUTITIA.