configuration/src/xmlfilesupport.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2011 by Tomassino Ferrauto & Gianluca Massera           *
00003  *   t_ferrauto@yahoo.it - emmegian@yahoo.it                               *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "private/xmlfilesupport.h"
00022 #include "configurationparameters.h"
00023 #include <QtDebug>
00024 #include <QFile>
00025 #include <QDomElement>
00026 
00027 namespace farsa {
00028 
00029 XMLFileLoaderSaver::XMLFileLoaderSaver() :
00030     ParametersFileLoaderSaver()
00031 {
00032 }
00033 
00034 XMLFileLoaderSaver::~XMLFileLoaderSaver()
00035 {
00036 }
00037 
00038 bool XMLFileLoaderSaver::saveParameters(QTextStream &stream, const ConfigurationParameters& configParams)
00039 {
00040     //--- create the XML Document
00041     QDomDocument xmldoc;
00042     //--- create the root node
00043     QDomElement root = xmldoc.createElement( "configurationparameters" );
00044     root.setAttribute( "version", "1.0" );
00045     xmldoc.appendChild( root );
00046     //--- recursively fill up xmldoc starting from root
00047     writeGroupToXMLDoc( xmldoc, root, "", configParams );
00048     //--- write the xmldoc to the stream
00049     //    because the method toString does not allow indentation with tab
00050     //    a substitution of all spaces inserted with tab is done here
00051     QString xmlstr = xmldoc.toString( 4 );
00052     stream << xmlstr.replace( "    ", "\t" );
00053     return true;
00054 }
00055 
00056 void XMLFileLoaderSaver::writeGroupToXMLDoc( QDomDocument& xmldoc, QDomNode& xmlnode, QString groupPath, const ConfigurationParameters& configParams ) {
00057     //--- write parameters first
00058     QStringList paramList = configParams.getParametersList( groupPath );
00059     foreach( QString param, paramList ) {
00060         QString value = configParams.getValue( groupPath + ConfigurationParameters::GroupSeparator() + param );
00061         if ( ! value.isEmpty() ) {
00062             QDomElement xmlparam = xmldoc.createElement( "param" );
00063             xmlparam.setAttribute( "name", param );
00064             xmlparam.appendChild( xmldoc.createTextNode( value ) );
00065             xmlnode.appendChild( xmlparam );
00066         }
00067     }
00068     //--- and then all subgroups recursively
00069     QStringList groupList = configParams.getGroupsList( groupPath );
00070     foreach( QString group, groupList ) {
00071         //--- it create the xmlnode representing the group and call recursively writeGroupToXMLDoc
00072         QDomElement xmlgroup = xmldoc.createElement( "group" );
00073         xmlgroup.setAttribute( "name", group );
00074         xmlnode.appendChild( xmlgroup );
00075         writeGroupToXMLDoc( xmldoc, xmlgroup, groupPath + ConfigurationParameters::GroupSeparator() + group, configParams );
00076     }
00077     return;
00078 }
00079 
00080 bool XMLFileLoaderSaver::loadParameters(QTextStream &stream, ConfigurationParameters& configParams)
00081 {
00082     //--- create the XML Document
00083     QDomDocument xmldoc;
00084     QString parseError;
00085     int line, col;
00086     if ( !xmldoc.setContent( stream.device(), &parseError, &line, &col ) ) {
00087         qWarning() << line << ":" << col << "Error while parsing XML file:" << parseError;
00088         return false;
00089     }
00090     QDomElement root = xmldoc.documentElement().toElement();
00091     if ( root.tagName() != "configurationparameters" ) {
00092         qWarning() << "The root node should be configurationparameters. Parsing this file could generate unexcepted results";
00093     }
00094     if ( root.attribute( "version" ) != "1.0" ) {
00095         qWarning() << "Only version '1.0' of configurationparameters XML syntax is supported. Parsing this file could generate unexcepted results";
00096     }
00097     loadGroupFromXMLDoc( root, "", configParams );
00098     return true;
00099 }
00100 
00101 void XMLFileLoaderSaver::loadGroupFromXMLDoc( const QDomNode& xmlnode, QString groupPath, ConfigurationParameters& configParams ) {
00102     //--- traverse all the child of the current node
00103     QDomNode node = xmlnode.firstChild();
00104     while( !node.isNull() ) {
00105         //--- check that it's an Element
00106         QDomElement e = node.toElement();
00107         if ( e.isNull() ) {
00108             //--- skip this tag
00109             node = node.nextSibling();
00110             continue;
00111         }
00112         //--- check the tagname
00113         if ( e.tagName() == "param" ) {
00114             //--- add the parameter to configParams to the current groupPath
00115             QString name = e.attribute( "name" );
00116             if ( name.isEmpty() ) throw XMLFileMandatoryAttributeMissing( "Tag <param>: attribute 'name' is mandatory" );
00117             configParams.createParameter( groupPath, name );
00118             configParams.setValue( groupPath + ConfigurationParameters::GroupSeparator() + name, e.text().simplified() );
00119         } else if ( e.tagName() == "group" ) {
00120             //--- add the group and call loadGroupFromXMLDoc recursevily
00121             QString name = e.attribute( "name" );
00122             if ( name.isEmpty() ) throw XMLFileMandatoryAttributeMissing( "Tag <group>: attribute 'name' is mandatory" );
00123             QString newgroup = configParams.createSubGroup( groupPath, name );
00124             loadGroupFromXMLDoc( node, newgroup, configParams );
00125         }
00126         node = node.nextSibling();
00127     }
00128 }
00129 
00130 } // end namespace farsa