view data/fileio/AudioSourceInfoReader.cpp @ 90:87495ac7710a

support audio source info from boca
author lbajardsilogic
date Thu, 28 Jun 2007 16:57:29 +0000
parents 11e298cdb9e7
children ec2ca3fbd957
line wrap: on
line source
/* -*- 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 "AudioSourceInfoReader.h"


#include <iostream>

AudioSourceInfoReader::AudioSourceInfoReader(AudioSourceInfoModel* model)
{
	m_model = model;
}

bool AudioSourceInfoReader::parse(const QString & filename)
{
	AudioSourceInfoHandler handler(m_model);
    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;
}

AudioSourceInfoHandler::AudioSourceInfoHandler(AudioSourceInfoModel* model) : QXmlDefaultHandler(),
	m_model(model),
	m_inBinding(false),
	m_curBindingName("")
{}

bool AudioSourceInfoHandler::startElement(const QString &namespaceURI, const QString &localName,
						  const QString &qName, const QXmlAttributes &attributes)
{
	/*QString name = qName.toLower();
    bool ok = false;

    // Valid element names:
    // easaierResource
    // dc:identifier
    // dc:title
    // dc:creator...

    if (name == "easaierresource") {
		// nothing needed
		ok = true;

    } else {
		ok = true;
		QString id = attributes.value("id");
		name.append(id);
		m_model->addInfo(name, attributes.value("value"));

    } 

    if (!ok) {
	std::cerr << "WARNING: connexion config-XML: Failed to completely process element \""
		  << name.toLocal8Bit().data() << "\"" << std::endl;
    }

    return true;*/
	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") {
	
		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 AudioSourceInfoHandler::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_model->addInfo(m_property, m_value);
	}

	return true;
}

bool AudioSourceInfoHandler::characters(const QString &str)
{
	if (m_inBinding)
	{
		if (m_curBindingName == "value")
		{
			m_value = str;
		}
		else if (m_curBindingName == "property")
		{
			m_property = str;
		}
		else 
		{
			m_model->addInfo(m_curBindingName, str);
		}
	}

    return true;
}

bool AudioSourceInfoHandler::error(const QXmlParseException &exception)
{
	QString errorString;
	errorString += QString("ERROR: connexion config-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 AudioSourceInfoHandler::fatalError(const QXmlParseException &exception)
{
	QString errorString;
	errorString += QString("FATAL ERROR: connexion config-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);
}