LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
documentReader.cpp
Go to the documentation of this file.
1// Copyright (C) 2003 by CEA LIST (LVIC)
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#include "documentReader.h"
10
12
13// Pour la lecture des fichiers de configuration
17
18
21
22#include <QtCore/QXmlStreamReader>
23#include <QBuffer>
24#include <QFile>
25
26
27
28using namespace std;
30
31namespace Lima {
32namespace DocumentsReader {
33
35{
36public:
38
39 virtual ~DocumentReaderPrivate();
40
41 QXmlStreamReader* m_reader;
42 std::shared_ptr<StructuredDocumentXMLParser> m_parser;
43
44 std::shared_ptr<ShiftFrom> m_shiftFrom;
45
46 //private functions
47 //void init(Common::XMLConfigurationFiles::XMLConfigurationFileParser& configParser)
49 /* OME
50 * void
51 * readFieldTags(Common::XMLConfigurationFiles::XMLConfigurationFileParser& configParser,
52 * const FieldType type,
53 * std::vector<std::pair<std::string,FieldType> >& fields);
54 */
55};
56
57//***********************************************************************
58// constructors
59//***********************************************************************
61 m_reader(0),
62 m_parser(0)
63{
64 init ( conf );
65}
66
68{
69 delete m_reader;
70// delete m_parser;
71}
72
74{
75 // to avoid unwanted encoding of text
76 setlocale ( LC_ALL,"" );
77
79#ifdef DEBUG_LP
80 LINFO << "DocumentReader::init";
81#endif
82
83 // init factory fo XmlDocumentParser and DocumentProperties
84 try
85 {
86#ifdef DEBUG_LP
88 LDEBUG << "DocumentReader::init: DocumentsReaderResources::changeable().init(conf)";
89#endif
90 // init manager of documentsXMLParser
92#ifdef DEBUG_LP
93 LDEBUG << "DocumentReader::init: DocumentsReaderResources::changeable().init(conf) done";
94#endif
95 }
96 catch ( NoSuchModule& e )
97 {
98 LERROR << "no such module lp-xmlreaderclient";
99 }
100 // Create and configure the XMLParser
101// LDEBUG << "XMLReaderFactory::createXMLReader()...";
102// m_reader = new QXmlSimpleReader();
103//
104// if ( !m_reader )
105// {
106// LERROR << "can't create _sax2XmlReader !!!";
107// return;
108// }
109
110 // Create automate to store parsing context and to handle SAX event
111// LDEBUG << "DocumentReader:;init(): create DocumentXMLParser...";
112 m_parser = DocumentsReaderResources::single().getXmlParser ( "documentXMLParser" );
113// m_parser = new StructuredDocumentXMLParser();
114
115// LDEBUG << "DocumentReader::init(): DocumentParser created";
116
117// m_reader->setContentHandler ( m_parser );
118// m_reader->setErrorHandler ( m_parser );
119
120}
121
125
126
127//***********************************************************************
128// destructor
129//***********************************************************************
131{
132 delete m_d;
133}
134
135void DocumentReader::setShiftFrom(std::shared_ptr<const ShiftFrom> shiftFrom)
136{
137 m_d->m_parser->setShiftFrom(shiftFrom);
138}
139
141{
142 m_d->m_parser->setLinguisticXMLDocHandler ( handler );
143}
144
145//***********************************************************************
146// member functions
147//***********************************************************************
148bool DocumentReader::initFile ( const QString& filename )
149{
150#ifdef DEBUG_LP
151 DRLOGINIT;
152 LDEBUG << "DocumentReader::initFile: " << filename;
153#endif
154 QFile file(filename);
155 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
156 LIMA_EXCEPTION_SELECT_LOGINIT(DRLOGINIT, "Cannot open" << filename,
158 if (m_d->m_reader != 0)
159 delete m_d->m_reader;
160 auto content = QString::fromUtf8(file.readAll());
161 return initWithString(content);
162}
163
164
165bool DocumentReader::initWithString ( const QString& text )
166{
167#ifdef DEBUG_LP
168 DRLOGINIT;
169 LDEBUG << "DocumentReader::initWithString: " << text.left(100) << (text.size()>100?"(...)":"");
170#endif
171 if (m_d->m_reader != 0)
172 delete m_d->m_reader;
173 m_d->m_shiftFrom = std::make_shared<ShiftFrom>(text);
174 m_d->m_reader = new QXmlStreamReader(m_d->m_shiftFrom->xml_noent());
175 m_d->m_parser->startDocument(getCurrentOffsetFromXmlReader());
176 return true;
177
178#ifdef DEBUG_LP
179 LDEBUG << "DocumentReader: initWithString NOT OK";
180#endif
181 return false;
182}
183
185{
186// DRLOGINIT;
187// LDEBUG << "DocumentReader::getCurrentOffsetFromXmlReader:" << m_d->m_reader->characterOffset() ;
188 // take into account the fact that QXmlStreamReader reports a position shifted by 1 to the right
189 // when reading character sequencies
190 if (m_d->m_reader->tokenType() == QXmlStreamReader::Characters)
191 return ( m_d->m_reader->characterOffset() -1 );
192 else
193 return ( m_d->m_reader->characterOffset() );
194}
195
196
198{
199#ifdef DEBUG_LP
200 DRLOGINIT;
201 LDEBUG << "DocumentReader::readXMLDocument";
202#endif
203 // extract an element
204
205 while (!m_d->m_reader->atEnd())
206 {
207#ifdef DEBUG_LP
208 LDEBUG <<"DocumentReader::readXMLDocument:"<<getCurrentOffsetFromXmlReader()
209 << "; tokenType:" << m_d->m_reader->tokenType() << "; tokenString:"
210 << m_d->m_reader->tokenString() << "; name:" << m_d->m_reader->name();
211#endif
212 if (m_d->m_reader->isStartElement())
213 {
214 m_d->m_parser->startElement(m_d->m_reader->namespaceUri().toString(),
215 m_d->m_reader->name().toString(),
216 m_d->m_reader->qualifiedName().toString(),
217 m_d->m_reader->attributes(),
219 }
220 else if (m_d->m_reader->isEndElement())
221 {
222 m_d->m_parser->endElement(m_d->m_reader->namespaceUri().toString(),
223 m_d->m_reader->name().toString(),
224 m_d->m_reader->qualifiedName().toString(),
226 }
227 else if (m_d->m_reader->isCharacters())
228 {
229 m_d->m_parser->characters(m_d->m_reader->text().toString(),
231 }
232 m_d->m_reader->readNext();
233 // do processing
234 }
235 m_d->m_parser->endDocument();
236 if (m_d->m_reader->hasError())
237 {
238 // do error handling
239 DRLOGINIT;
240 LERROR <<"DocumentReader: readXMLDocument: parse error:" << m_d->m_reader->errorString();
241 return false;
242 }
243
244 return true;
245}
246
247} // namespace DocumentsReader
248} // end namespace
#define LIMA_EXCEPTION_SELECT_LOGINIT(X, Y, Z)
This macro writes the message Y to the error stream configured by its first parameter X,...
Definition LimaCommon.h:332
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
#define DRLOGINIT
return a message when a 'module' is not found
DocumentReaderPrivate(ModuleConfigurationStructure &conf)
void init(Lima::Common::XMLConfigurationFiles::ModuleConfigurationStructure &conf)
std::shared_ptr< StructuredDocumentXMLParser > m_parser
DocumentReader(Lima::Common::XMLConfigurationFiles::ModuleConfigurationStructure &conf)
bool initFile(const QString &filename)
initialize the reader with a file containing documents in XML format
void setShiftFrom(std::shared_ptr< const ShiftFrom > shiftFrom)
void setLinguisticXMLDocHandler(StructuredXmlDocumentHandler *handler)
uint64_t getCurrentOffsetFromXmlReader()
Returns the reader's current character offset, starting with 0.
bool readXMLDocument()
read the next element in the current file (initFile must have been
bool initWithString(const QString &text)
static const DocumentsReaderResources & single()
const singleton accessor
Definition Singleton.h:51
static DocumentsReaderResources & changeable()
singleton accessor
Definition Singleton.h:71
Handler pour les elements de parsing de document structure.
Throw this exception when encountering problems with XML files opening or parsing.
Definition LimaCommon.h:470
originally detectModuleConfigurationStructure.h in detectlibraries
NAUTITIA.
STL namespace.
launch exception related to the configuration file parsing