libpappsomspp
Library for mass spectrometry
xmlstreamreaderinterface.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/processing/xml/xmlstreamreaderinterface.cpp
3 * \date 12/11/2021
4 * \author Olivier Langella
5 * \brief common interface to read all XML streams containing convenient
6 * functions
7 */
8
9/*******************************************************************************
10 * Copyright (c) 2021 Olivier Langella <Olivier.Langella@u-psud.fr>.
11 *
12 * This file is part of PAPPSOms-tools.
13 *
14 * PAPPSOms-tools is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * PAPPSOms-tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with PAPPSOms-tools. If not, see <http://www.gnu.org/licenses/>.
26 *
27 ******************************************************************************/
28
29
31#include <QFile>
32#include <QFileInfo>
33#include <QDebug>
34
35using namespace pappso;
36
37XmlStreamReaderInterface::XmlStreamReaderInterface()
38{
39}
40
42{
43}
44
45QString
47{
48 if(m_qxmlStreamReader.errorString().isEmpty())
49 return "";
50 else
51 {
52 QString error = QObject::tr("ERROR at line %1 column %2\n%3")
53 .arg(m_qxmlStreamReader.lineNumber())
54 .arg(m_qxmlStreamReader.columnNumber())
55 .arg(m_qxmlStreamReader.errorString());
56 return error;
57 }
58}
59
60bool
62{
63 QFile file(fileName);
64 if(!file.open(QFile::ReadOnly | QFile::Text))
65 {
66 m_qxmlStreamReader.raiseError(
67 QObject::tr("Cannot read file %1 : %2")
68 .arg(QFileInfo(fileName).absoluteFilePath())
69 .arg(m_qxmlStreamReader.errorString()));
70 return false;
71 }
72
73 if(read(&file))
74 {
75 file.close();
76 return true;
77 }
78 else
79 {
80 file.close();
81 m_qxmlStreamReader.raiseError(
82 QObject::tr("Error reading file %1 : %2")
83 .arg(QFileInfo(fileName).absoluteFilePath())
84 .arg(m_qxmlStreamReader.errorString()));
85 return false;
86 }
87}
88bool
90{
91
92 m_qxmlStreamReader.setDevice(device);
93 m_qxmlStreamReader.setNamespaceProcessing(true);
94 readStream();
95
96 return !m_qxmlStreamReader.error();
97}
98
99bool
100pappso::XmlStreamReaderInterface::read(const QString &xml_content)
101{
102 m_qxmlStreamReader.clear();
103 m_qxmlStreamReader.addData(xml_content);
104 m_qxmlStreamReader.setNamespaceProcessing(true);
105 readStream();
106
107 return !m_qxmlStreamReader.error();
108}
109void
111 QXmlStreamWriter &output) const
112{
113 output.writeStartElement(m_qxmlStreamReader.name().toString());
114
115 for(auto declaration : m_qxmlStreamReader.namespaceDeclarations())
116 {
117 output.writeNamespace(declaration.namespaceUri().toString(),
118 declaration.prefix().toString());
119 }
120 output.writeAttributes(m_qxmlStreamReader.attributes());
121}
122
123void
125{
126 qDebug() << " name=" << m_qxmlStreamReader.name();
127 output.writeStartElement(m_qxmlStreamReader.namespaceUri().toString(),
128 m_qxmlStreamReader.name().toString());
129 output.writeAttributes(m_qxmlStreamReader.attributes());
130
131 qDebug() << m_qxmlStreamReader.name();
132 while(m_qxmlStreamReader.readNext() && !m_qxmlStreamReader.isEndElement())
133 {
134 cloneNode(output);
135 if(output.hasError())
136 {
137 qDebug();
138 m_qxmlStreamReader.raiseError(QObject::tr("Error in output stream"));
139 }
140 }
141 qDebug();
142 output.writeEndElement();
143}
144
145void
147{
148 qDebug();
149 if(m_qxmlStreamReader.isCharacters())
150 {
151 qDebug() << "isCharacters " << m_qxmlStreamReader.text();
152 if((m_qxmlStreamReader.text().toString() == "\n") ||
153 (m_qxmlStreamReader.text().toString() == "\n\t"))
154 {
155 // xml cleaner
156 }
157 else
158 {
159 output.writeCharacters(
160 m_qxmlStreamReader.text().toString().trimmed());
161 }
162 }
163 else if(m_qxmlStreamReader.isEndElement())
164 {
165 qDebug() << "isEndElement";
166 output.writeEndElement();
167 }
168 else if(m_qxmlStreamReader.isStartElement())
169 {
170 qDebug() << "isStartElement";
171 cloneElement(output);
172 }
173 else
174 {
175 }
176 qDebug();
177}
void cloneStartElement(QXmlStreamWriter &output) const
void cloneElement(QXmlStreamWriter &output)
void cloneNode(QXmlStreamWriter &output)
virtual bool readFile(const QString &fileName)
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
common interface to read all XML streams containing convenient functions