21 #include "private/xmlfilesupport.h"
22 #include "configurationparameters.h"
25 #include <QDomElement>
29 XMLFileLoaderSaver::XMLFileLoaderSaver() :
30 ParametersFileLoaderSaver()
34 XMLFileLoaderSaver::~XMLFileLoaderSaver()
38 bool XMLFileLoaderSaver::saveParameters(QTextStream &stream,
const ConfigurationParameters& configParams)
43 QDomElement root = xmldoc.createElement(
"configurationparameters" );
44 root.setAttribute(
"version",
"1.0" );
45 xmldoc.appendChild( root );
47 writeGroupToXMLDoc( xmldoc, root,
"", configParams );
51 QString xmlstr = xmldoc.toString( 4 );
52 stream << xmlstr.replace(
" ",
"\t" );
56 void XMLFileLoaderSaver::writeGroupToXMLDoc( QDomDocument& xmldoc, QDomNode& xmlnode, QString groupPath,
const ConfigurationParameters& configParams ) {
58 QStringList paramList = configParams.getParametersList( groupPath );
59 foreach( QString param, paramList ) {
61 if ( ! value.isEmpty() ) {
62 QDomElement xmlparam = xmldoc.createElement(
"param" );
63 xmlparam.setAttribute(
"name", param );
64 xmlparam.appendChild( xmldoc.createTextNode( value ) );
65 xmlnode.appendChild( xmlparam );
69 QStringList groupList = configParams.getGroupsList( groupPath );
70 foreach( QString group, groupList ) {
72 QDomElement xmlgroup = xmldoc.createElement(
"group" );
73 xmlgroup.setAttribute(
"name", group );
74 xmlnode.appendChild( xmlgroup );
80 bool XMLFileLoaderSaver::loadParameters(QTextStream &stream, ConfigurationParameters& configParams)
86 if ( !xmldoc.setContent( stream.device(), &parseError, &line, &col ) ) {
87 qWarning() << line <<
":" << col <<
"Error while parsing XML file:" << parseError;
90 QDomElement root = xmldoc.documentElement().toElement();
91 if ( root.tagName() !=
"configurationparameters" ) {
92 qWarning() <<
"The root node should be configurationparameters. Parsing this file could generate unexcepted results";
94 if ( root.attribute(
"version" ) !=
"1.0" ) {
95 qWarning() <<
"Only version '1.0' of configurationparameters XML syntax is supported. Parsing this file could generate unexcepted results";
97 loadGroupFromXMLDoc( root,
"", configParams );
101 void XMLFileLoaderSaver::loadGroupFromXMLDoc(
const QDomNode& xmlnode, QString groupPath, ConfigurationParameters& configParams ) {
103 QDomNode node = xmlnode.firstChild();
104 while( !node.isNull() ) {
106 QDomElement e = node.toElement();
109 node = node.nextSibling();
113 if ( e.tagName() ==
"param" ) {
115 QString name = e.attribute(
"name" );
116 if ( name.isEmpty() )
throw XMLFileMandatoryAttributeMissing(
"Tag <param>: attribute 'name' is mandatory" );
117 configParams.createParameter( groupPath, name );
119 }
else if ( e.tagName() ==
"group" ) {
121 QString name = e.attribute(
"name" );
122 if ( name.isEmpty() )
throw XMLFileMandatoryAttributeMissing(
"Tag <group>: attribute 'name' is mandatory" );
123 QString newgroup = configParams.createSubGroup( groupPath, name );
124 loadGroupFromXMLDoc( node, newgroup, configParams );
126 node = node.nextSibling();