00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00041 QDomDocument xmldoc;
00042
00043 QDomElement root = xmldoc.createElement( "configurationparameters" );
00044 root.setAttribute( "version", "1.0" );
00045 xmldoc.appendChild( root );
00046
00047 writeGroupToXMLDoc( xmldoc, root, "", configParams );
00048
00049
00050
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
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
00069 QStringList groupList = configParams.getGroupsList( groupPath );
00070 foreach( QString group, groupList ) {
00071
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
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
00103 QDomNode node = xmlnode.firstChild();
00104 while( !node.isNull() ) {
00105
00106 QDomElement e = node.toElement();
00107 if ( e.isNull() ) {
00108
00109 node = node.nextSibling();
00110 continue;
00111 }
00112
00113 if ( e.tagName() == "param" ) {
00114
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
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 }