Go to the documentation of this file.00001 #include <QMap>
00002 #include <QList>
00003 #include <QSharedPointer>
00004 #include <QStringList>
00005 #include <QXmlStreamReader>
00006 #include <QXmlStreamWriter>
00007 #include "utilities/simple_xml_settings.h"
00008
00009 namespace utilities
00010 {
00011 const QSettings::Format SimpleXmlSettings::format = QSettings::registerFormat("xml",
00012 SimpleXmlSettings::read, SimpleXmlSettings::write);
00013
00014 bool SimpleXmlSettings::read(QIODevice& device, QSettings::SettingsMap& map) {
00015 QXmlStreamReader xmlReader(&device);
00016
00017 QStringList groups;
00018
00019 while (!xmlReader.atEnd()) {
00020 xmlReader.readNext();
00021
00022 if (xmlReader.isStartElement())
00023 groups.append(xmlReader.name().toString());
00024 else if (xmlReader.isCharacters() && !xmlReader.isWhitespace())
00025 map[groups.join("/")] = xmlReader.text().toString();
00026 else if (xmlReader.isEndElement())
00027 groups.removeLast();
00028 }
00029
00030 return !xmlReader.hasError();
00031 }
00032
00033 bool SimpleXmlSettings::write(QIODevice& device, const QSettings::SettingsMap& map) {
00034 struct NestedMap;
00035 struct NestedMap : QMap<QString, QSharedPointer<NestedMap> > {};
00036
00037 QSharedPointer<NestedMap> nestedMap(new NestedMap());
00038
00039 for (QSettings::SettingsMap::const_iterator it= map.begin();
00040 it != map.end(); ++it) {
00041 QSharedPointer<NestedMap> currentMap = nestedMap;
00042
00043 QStringList groups = it.key().split("/");
00044
00045 for (QStringList::const_iterator jt = groups.begin();
00046 jt != groups.end(); ++jt) {
00047 NestedMap::iterator kt = currentMap->find(*jt);
00048
00049 if (kt == currentMap->end()) {
00050 kt = currentMap->insert(*jt, QSharedPointer<NestedMap>(
00051 new NestedMap()));
00052 currentMap = kt.value();
00053 }
00054 else
00055 currentMap = kt.value();
00056 }
00057 }
00058
00059 QXmlStreamWriter xmlWriter(&device);
00060
00061 xmlWriter.setAutoFormatting(true);
00062 xmlWriter.writeStartDocument();
00063
00064 QStringList groups;
00065 QList<QSharedPointer<NestedMap> > nestedMaps;
00066 QList<NestedMap::iterator> nestedMapIterators;
00067
00068 nestedMaps.append(nestedMap);
00069 nestedMapIterators.append(nestedMap->begin());
00070
00071 while (!nestedMaps.isEmpty()) {
00072 QSharedPointer<NestedMap> currentMap = nestedMaps.last();
00073 NestedMap::iterator it = nestedMapIterators.last();
00074
00075 if (it != currentMap->end()) {
00076 xmlWriter.writeStartElement(it.key());
00077
00078 groups.append(it.key());
00079 nestedMaps.append(it.value());
00080 nestedMapIterators.append(it.value()->begin());
00081 }
00082 else {
00083 if (currentMap->isEmpty())
00084 xmlWriter.writeCharacters(map[groups.join("/")].toString());
00085
00086 xmlWriter.writeEndElement();
00087
00088 if (!groups.isEmpty())
00089 groups.removeLast();
00090 nestedMaps.removeLast();
00091 nestedMapIterators.removeLast();
00092
00093 if (!nestedMaps.isEmpty())
00094 ++nestedMapIterators.last();
00095 }
00096 }
00097
00098 xmlWriter.writeEndDocument();
00099
00100 return true;
00101 }
00102
00103 }