libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
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
40
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(QObject::tr("Cannot read file %1 : %2")
67 .arg(QFileInfo(fileName).absoluteFilePath())
68 .arg(m_qxmlStreamReader.errorString()));
69 return false;
70 }
71
72 if(read(&file))
73 {
74 file.close();
75 return true;
76 }
77 else
78 {
79 file.close();
80 m_qxmlStreamReader.raiseError(QObject::tr("Error reading file %1 : %2")
81 .arg(QFileInfo(fileName).absoluteFilePath())
82 .arg(m_qxmlStreamReader.errorString()));
83 return false;
84 }
85}
86bool
88{
89
90 m_qxmlStreamReader.setDevice(device);
91 m_qxmlStreamReader.setNamespaceProcessing(true);
92 readStream();
93
94 return !m_qxmlStreamReader.error();
95}
96
97bool
98pappso::XmlStreamReaderInterface::read(const QString &xml_content)
99{
100 m_qxmlStreamReader.clear();
101 m_qxmlStreamReader.addData(xml_content);
102 m_qxmlStreamReader.setNamespaceProcessing(true);
103 readStream();
104
105 return !m_qxmlStreamReader.error();
106}
107void
109{
110 output.writeStartElement(m_qxmlStreamReader.name().toString());
111
112 for(auto declaration : m_qxmlStreamReader.namespaceDeclarations())
113 {
114 output.writeNamespace(declaration.namespaceUri().toString(), declaration.prefix().toString());
115 }
116 output.writeAttributes(m_qxmlStreamReader.attributes());
117}
118
119void
121{
122 qDebug() << " name=" << m_qxmlStreamReader.name();
123 output.writeStartElement(m_qxmlStreamReader.namespaceUri().toString(),
124 m_qxmlStreamReader.name().toString());
125 output.writeAttributes(m_qxmlStreamReader.attributes());
126
127 qDebug() << m_qxmlStreamReader.name();
128 while(m_qxmlStreamReader.readNext() && !m_qxmlStreamReader.isEndElement())
129 {
130 cloneNode(output);
131 if(output.hasError())
132 {
133 qDebug();
134 m_qxmlStreamReader.raiseError(QObject::tr("Error in output stream"));
135 }
136 }
137 qDebug();
138 output.writeEndElement();
139}
140
141void
143{
144 qDebug();
145 if(m_qxmlStreamReader.isCharacters())
146 {
147 qDebug() << "isCharacters " << m_qxmlStreamReader.text();
148 if((m_qxmlStreamReader.text().toString() == "\n") ||
149 (m_qxmlStreamReader.text().toString() == "\n\t"))
150 {
151 // xml cleaner
152 }
153 else
154 {
155 output.writeCharacters(m_qxmlStreamReader.text().toString().trimmed());
156 }
157 }
158 else if(m_qxmlStreamReader.isEndElement())
159 {
160 qDebug() << "isEndElement";
161 output.writeEndElement();
162 }
163 else if(m_qxmlStreamReader.isStartElement())
164 {
165 qDebug() << "isStartElement";
166 cloneElement(output);
167 }
168 else
169 {
170 }
171 qDebug();
172}
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