xmlfilesupport.cpp
1 /***************************************************************************
2  * Copyright (C) 2011 by Tomassino Ferrauto & Gianluca Massera *
3  * t_ferrauto@yahoo.it - emmegian@yahoo.it *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include "private/xmlfilesupport.h"
22 #include "configurationparameters.h"
23 #include <QtDebug>
24 #include <QFile>
25 #include <QDomElement>
26 
27 namespace farsa {
28 
29 XMLFileLoaderSaver::XMLFileLoaderSaver() :
30  ParametersFileLoaderSaver()
31 {
32 }
33 
34 XMLFileLoaderSaver::~XMLFileLoaderSaver()
35 {
36 }
37 
38 bool XMLFileLoaderSaver::saveParameters(QTextStream &stream, const ConfigurationParameters& configParams)
39 {
40  //--- create the XML Document
41  QDomDocument xmldoc;
42  //--- create the root node
43  QDomElement root = xmldoc.createElement( "configurationparameters" );
44  root.setAttribute( "version", "1.0" );
45  xmldoc.appendChild( root );
46  //--- recursively fill up xmldoc starting from root
47  writeGroupToXMLDoc( xmldoc, root, "", configParams );
48  //--- write the xmldoc to the stream
49  // because the method toString does not allow indentation with tab
50  // a substitution of all spaces inserted with tab is done here
51  QString xmlstr = xmldoc.toString( 4 );
52  stream << xmlstr.replace( " ", "\t" );
53  return true;
54 }
55 
56 void XMLFileLoaderSaver::writeGroupToXMLDoc( QDomDocument& xmldoc, QDomNode& xmlnode, QString groupPath, const ConfigurationParameters& configParams ) {
57  //--- write parameters first
58  QStringList paramList = configParams.getParametersList( groupPath );
59  foreach( QString param, paramList ) {
60  QString value = configParams.getValue( groupPath + ConfigurationParameters::GroupSeparator() + param );
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 );
66  }
67  }
68  //--- and then all subgroups recursively
69  QStringList groupList = configParams.getGroupsList( groupPath );
70  foreach( QString group, groupList ) {
71  //--- it create the xmlnode representing the group and call recursively writeGroupToXMLDoc
72  QDomElement xmlgroup = xmldoc.createElement( "group" );
73  xmlgroup.setAttribute( "name", group );
74  xmlnode.appendChild( xmlgroup );
75  writeGroupToXMLDoc( xmldoc, xmlgroup, groupPath + ConfigurationParameters::GroupSeparator() + group, configParams );
76  }
77  return;
78 }
79 
80 bool XMLFileLoaderSaver::loadParameters(QTextStream &stream, ConfigurationParameters& configParams)
81 {
82  //--- create the XML Document
83  QDomDocument xmldoc;
84  QString parseError;
85  int line, col;
86  if ( !xmldoc.setContent( stream.device(), &parseError, &line, &col ) ) {
87  qWarning() << line << ":" << col << "Error while parsing XML file:" << parseError;
88  return false;
89  }
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";
93  }
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";
96  }
97  loadGroupFromXMLDoc( root, "", configParams );
98  return true;
99 }
100 
101 void XMLFileLoaderSaver::loadGroupFromXMLDoc( const QDomNode& xmlnode, QString groupPath, ConfigurationParameters& configParams ) {
102  //--- traverse all the child of the current node
103  QDomNode node = xmlnode.firstChild();
104  while( !node.isNull() ) {
105  //--- check that it's an Element
106  QDomElement e = node.toElement();
107  if ( e.isNull() ) {
108  //--- skip this tag
109  node = node.nextSibling();
110  continue;
111  }
112  //--- check the tagname
113  if ( e.tagName() == "param" ) {
114  //--- add the parameter to configParams to the current groupPath
115  QString name = e.attribute( "name" );
116  if ( name.isEmpty() ) throw XMLFileMandatoryAttributeMissing( "Tag <param>: attribute 'name' is mandatory" );
117  configParams.createParameter( groupPath, name );
118  configParams.setValue( groupPath + ConfigurationParameters::GroupSeparator() + name, e.text().simplified() );
119  } else if ( e.tagName() == "group" ) {
120  //--- add the group and call loadGroupFromXMLDoc recursevily
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 );
125  }
126  node = node.nextSibling();
127  }
128 }
129 
130 } // end namespace farsa