Loading...
Searching...
No Matches
odsdocreader.cpp
Go to the documentation of this file.
/*
libodsstream is a library to read and write ODS documents as streams
Copyright (C) 2013 Olivier Langella <Olivier.Langella@moulon.inra.fr>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "odsdocreader.h"
#include "odsexception.h"
#include <QDebug>
#include <QFileInfo>
#include "saxreader/qxmlstreamreadercontentxml.h"
//#include "./saxreader/saxhandlercontentxml.h"
#include <quazipfile.h>
OdsDocReader::OdsDocReader(OdsDocHandlerInterface &handler) : _handler(handler)
{
}
OdsDocReader::~OdsDocReader()
{
qDebug();
if(_p_quaZip != nullptr)
{
if(_p_quaZip->isOpen())
{
_p_quaZip->close();
delete _p_quaZip;
_p_quaZip = nullptr;
}
}
qDebug();
}
void
OdsDocReader::parse(QFile &odsFile)
{
qDebug() << odsFile.fileName();
QFileInfo ods_file_info(odsFile.fileName());
if(!ods_file_info.exists())
{
throw OdsException(QObject::tr("ODS file %1 does not exists")
.arg(ods_file_info.absoluteFilePath()));
}
if(!ods_file_info.isReadable())
{
throw OdsException(QObject::tr("ODS file %1 is not readable")
.arg(ods_file_info.absoluteFilePath()));
}
if(!odsFile.open(QIODevice::ReadOnly))
{
throw OdsException(QObject::tr("Unable to read ODS file %1")
.arg(ods_file_info.absoluteFilePath()));
}
qDebug() << ods_file_info.absoluteFilePath();
parse(&odsFile);
odsFile.close();
}
void
OdsDocReader::parse(QIODevice *p_inputstream)
{
qDebug();
_p_quaZip = new QuaZip(p_inputstream);
_p_quaZip->open(QuaZip::mdUnzip);
qDebug();
for(bool more = _p_quaZip->goToFirstFile(); more;
more = _p_quaZip->goToNextFile())
{
qDebug() << _p_quaZip->getCurrentFileName();
if(_p_quaZip->getCurrentFileName() == "content.xml")
{
QuaZipFile zip_file(_p_quaZip);
QXmlStreamReaderContentXml content_reader(*this);
zip_file.open(QIODevice::ReadOnly);
content_reader.setDevice(&zip_file);
content_reader.setNamespaceProcessing(true);
content_reader.read();
if(content_reader.error())
{
zip_file.close();
qDebug() << " error " << content_reader.errorString();
throw OdsException(QObject::tr("error reading ODS input file :\n")
.append(content_reader.errorString()));
}
qDebug() << " contentXml : DONE on file '"
<< _p_quaZip->getCurrentFileName() << "'";
zip_file.close();
}
}
if(_p_quaZip != nullptr)
{
if(_p_quaZip->isOpen())
{
_p_quaZip->close();
delete _p_quaZip;
_p_quaZip = nullptr;
}
}
this->endDocument();
}
void
OdsDocReader::startInsideLine()
{
_column_number = 0;
startLine();
}
void
OdsDocReader::setInsideCell(const OdsCell &cell)
{
_column_number++;
qDebug() << cell.getOfficeValueType() << " " << cell.toString();
if(cell.isDate())
{
qDebug() << " date " << cell.getDateTimeValue().toString(Qt::ISODate)
<< " " << cell.getDateTimeValue().date().day() << " "
<< cell.getDateTimeValue().date().month();
}
else if(cell.isString())
{
qDebug() << " string " << cell.getStringValue();
}
else if(cell.isDouble())
{
qDebug() << " double " << cell.getDoubleValue();
}
setCell(cell);
}
void
OdsDocReader::startSheet(const QString &sheet_name)
{
_handler.startSheet(sheet_name);
}
void
OdsDocReader::endSheet()
{
_handler.endSheet();
}
void
OdsDocReader::startLine()
{
_handler.startLine();
}
void
OdsDocReader::endLine()
{
_handler.endLine();
}
void
OdsDocReader::setCell(const OdsCell &cell)
{
_handler.setCell(cell);
}
void
OdsDocReader::endDocument()
{
_handler.endDocument();
}
OdsCell
Definition odscell.h:29
OdsCell::getDateTimeValue
const QDateTime & getDateTimeValue() const
Definition odscell.cpp:97
OdsCell::toString
const QString & toString() const
Definition odscell.cpp:83
OdsCell::getDoubleValue
double getDoubleValue() const
Definition odscell.cpp:123
OdsCell::isDate
bool isDate() const
Definition odscell.cpp:154
OdsCell::isString
bool isString() const
Definition odscell.cpp:168
OdsCell::isDouble
bool isDouble() const
Definition odscell.cpp:161
OdsCell::getStringValue
const QString & getStringValue() const
Definition odscell.cpp:110
OdsCell::getOfficeValueType
const QString & getOfficeValueType() const
Definition odscell.cpp:91
OdsDocHandlerInterface
Definition odsdochandlerinterface.h:43
OdsDocReader::_handler
OdsDocHandlerInterface & _handler
Definition odsdocreader.h:69
OdsDocReader::endLine
void endLine()
Definition odsdocreader.cpp:171
OdsDocReader::_p_quaZip
QuaZip * _p_quaZip
Definition odsdocreader.h:65
OdsDocReader::startInsideLine
void startInsideLine()
Definition odsdocreader.cpp:123
OdsDocReader::startSheet
void startSheet(const QString &sheet_name)
Definition odsdocreader.cpp:153
OdsDocReader::setCell
void setCell(const OdsCell &cell)
Definition odsdocreader.cpp:177
OdsDocReader::~OdsDocReader
virtual ~OdsDocReader()
Definition odsdocreader.cpp:32
OdsDocReader::parse
void parse(QFile &odsFile)
Definition odsdocreader.cpp:49
OdsDocReader::endDocument
void endDocument()
Definition odsdocreader.cpp:182
OdsDocReader::_column_number
uint _column_number
Definition odsdocreader.h:67
OdsDocReader::endSheet
void endSheet()
Definition odsdocreader.cpp:159
OdsDocReader::setInsideCell
void setInsideCell(const OdsCell &cell)
Definition odsdocreader.cpp:130
OdsDocReader::OdsDocReader
OdsDocReader(OdsDocHandlerInterface &handler)
Definition odsdocreader.cpp:28
OdsDocReader::QXmlStreamReaderContentXml
friend QXmlStreamReaderContentXml
Definition odsdocreader.h:31
OdsDocReader::startLine
void startLine()
Definition odsdocreader.cpp:165
OdsException
Definition odsexception.h:27
QXmlStreamReaderContentXml::read
bool read()
Definition qxmlstreamreadercontentxml.cpp:41
odsdocreader.h
odsexception.h
qxmlstreamreadercontentxml.h