Mercurial > hg > easaier-soundaccess
diff data/fileio/SparqlResultsReader.cpp @ 15:11e298cdb9e7
add
- EasaierSessionManager
- Easaier menus
- Interval model
author | lbajardsilogic |
---|---|
date | Mon, 14 May 2007 13:10:49 +0000 |
parents | |
children | f3dce7a985cf |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/fileio/SparqlResultsReader.cpp Mon May 14 13:10:49 2007 +0000 @@ -0,0 +1,158 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* Sound Access + EASAIER client application. + Silogic 2007. Laure Bajard. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "SparqlResultsReader.h" + +#include <iostream> + +SparqlResultsReader::SparqlResultsReader(QueryResultsWidget* resultsWidget) : + m_resultsWidget(resultsWidget) +{} + +bool SparqlResultsReader::parse(const QString & filename) +{ + SparqlResultsHandler handler(m_resultsWidget); + QXmlSimpleReader reader; + reader.setContentHandler(&handler); + reader.setErrorHandler(&handler); + + QFile file(filename); + + if (!file.open(QFile::ReadOnly | QFile::Text)) { + return false; + } + + QXmlInputSource xmlInputSource(&file); + if (reader.parse(xmlInputSource)) + { + return true; + } + + return false; +} + +SparqlResultsHandler::SparqlResultsHandler(QueryResultsWidget* resultsWidget) : QXmlDefaultHandler(), + m_resultsWidget(resultsWidget), + m_inBinding(false), + m_curBindingName("") +{} + +bool SparqlResultsHandler::startElement(const QString &namespaceURI, const QString &localName, + const QString &qName, const QXmlAttributes &attributes) +{ + + QString name = qName.toLower(); + + bool ok = false; + + // Valid element names: + // + // sparql + // head + // variable + // results + // result + // binding + + if (name == "sparql") { + + // nothing needed + ok = true; + + } else if (name == "head") { + + // nothing needed + ok = true; + + } else if (name == "variable") { + + // nothing needed + ok = true; + + } else if (name == "results") { + + // nothing needed + ok = true; + + } else if (name == "result") { + + m_resultsWidget->newResult(); + ok = true; + + } else if (name == "binding") { + + m_curBindingName = attributes.value("name"); + ok = true; + + } else if ( (name == "uri") || (name == "literal") ) { + m_inBinding = true; + ok = true; + } + + if (!ok) { + std::cerr << "WARNING: SparqlResultsHandler-XML: Failed to completely process element \"" + << name.toLocal8Bit().data() << "\"" << std::endl; + } + + return true; +} + +bool SparqlResultsHandler::endElement(const QString &namespaceURI, const QString &localName, + const QString &qName) +{ + QString name = qName.toLower(); + + if ( (name == "uri") || (name == "literal") ) + { + m_inBinding = false; + m_curBindingName = ""; + + } else if (name == "result") + { + m_resultsWidget->displayResult(); + } + + return true; +} + +bool SparqlResultsHandler::characters(const QString &str) +{ + if (m_inBinding) + { + m_resultsWidget->addInfo(m_curBindingName, str); + } + + return true; +} + +bool SparqlResultsHandler::error(const QXmlParseException &exception) +{ + QString errorString; + errorString += QString("ERROR: SparqlResultsHandler-XML: %1 at line %2, column %3") + .arg(exception.message()) + .arg(exception.lineNumber()) + .arg(exception.columnNumber()); + std::cerr << errorString.toLocal8Bit().data() << std::endl; + return QXmlDefaultHandler::error(exception); +} + +bool SparqlResultsHandler::fatalError(const QXmlParseException &exception) +{ + QString errorString; + errorString += QString("FATAL ERROR: SparqlResultsHandler-XML: %1 at line %2, column %3") + .arg(exception.message()) + .arg(exception.lineNumber()) + .arg(exception.columnNumber()); + std::cerr << errorString.toLocal8Bit().data() << std::endl; + return QXmlDefaultHandler::fatalError(exception); +}