SpikeStream Library  0.2
XMLParameterParser.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "XMLParameterParser.h"
00003 #include "SpikeStreamXMLException.h"
00004 using namespace spikestream;
00005 
00006 //Qt includes
00007 #include <QDebug>
00008 
00009 
00011 XMLParameterParser::XMLParameterParser(){
00012 }
00013 
00014 
00016 XMLParameterParser::~XMLParameterParser(){
00017 }
00018 
00019 
00020 /*----------------------------------------------------------*/
00021 /*-----                  PUBLIC METHODS                -----*/
00022 /*----------------------------------------------------------*/
00023 
00025 QHash<QString, double> XMLParameterParser::getParameterMap(const QString& xmlString){
00026     //Clear parameter map
00027     parameterMap.clear();
00028 
00029     //Return empty map if paramter string is empty
00030     if(xmlString == "")
00031                 return parameterMap;
00032 
00033     //Parse the XML
00034     QXmlSimpleReader xmlReader;
00035     QXmlInputSource xmlInput;
00036     xmlReader.setContentHandler(this);
00037     xmlReader.setErrorHandler(this);
00038     xmlInput.setData(xmlString);
00039     xmlReader.parse(xmlInput);
00040 
00041     return parameterMap;
00042 }
00043 
00044 
00045 /*----------------------------------------------------------*/
00046 /*-----               PROTECTED METHODS                -----*/
00047 /*----------------------------------------------------------*/
00048 
00050 bool XMLParameterParser::characters(const QString& chars){
00051     if(loadingParameter){
00052                 if(currentElement == "name"){
00053                         currentParamName = chars;
00054                 }
00055                 else if(currentElement == "value"){
00056                         bool ok;
00057                         currentParamValue = chars.toDouble(&ok);
00058                         if(!ok)
00059                                 throw SpikeStreamXMLException("Error converting parameter value to double: " + chars);
00060                 }
00061                 else{
00062                         throw SpikeStreamXMLException("Unrecognized element: " + currentElement);
00063                 }
00064     }
00065     else
00066                 throw SpikeStreamXMLException("Unexpected characters encountered: '" + chars + "'");
00067     return true;
00068 }
00069 
00070 
00072 bool XMLParameterParser::endElement( const QString&, const QString&, const QString& elemName){
00073     if(loadingParameter && elemName == "parameter"){
00074                 //Check that parameter name has been set
00075                 if(currentParamName == "")
00076                         throw SpikeStreamXMLException("Parameter is missing description");
00077 
00078                 //Store parameter in map
00079                 parameterMap[currentParamName] = currentParamValue;
00080 
00081                 //Have finished loading the parameter
00082                 loadingParameter = false;
00083     }
00084     return true;
00085 }
00086 
00087 
00089 bool XMLParameterParser::error (const QXmlParseException& parseEx){
00090     throw SpikeStreamXMLException(parseEx.message());
00091     return true;
00092 }
00093 
00094 
00096 QString XMLParameterParser::errorString (){
00097         return QString("Default error string");
00098 
00099 }
00100 
00101 
00103 bool XMLParameterParser::fatalError (const QXmlParseException& parseEx){
00104     throw SpikeStreamXMLException(parseEx.message());
00105     return true;
00106 }
00107 
00108 
00110 bool XMLParameterParser::startDocument(){
00111     loadingParameter = false;
00112     return true;
00113 }
00114 
00115 
00117 bool XMLParameterParser::startElement(const QString&, const QString&, const QString& qName, const QXmlAttributes&){
00118     currentElement = qName;
00119     if(currentElement == "parameter"){
00120                 //Reset information associated with a parameter
00121                 currentParamName = "";
00122                 currentParamValue = 0.0;
00123                 loadingParameter = true;
00124     }
00125     return true;
00126 }
00127 
00128 
00130 bool XMLParameterParser::warning ( const QXmlParseException& ex){
00131     qWarning()<<ex.message();
00132     return true;
00133 }
00134 
00135 
00136 
 All Classes Files Functions Variables Typedefs Defines