summaryrefslogtreecommitdiff
path: root/RealtimeSync/xml_proc.cpp
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:11:56 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:11:56 +0200
commit98ecf620f7de377dc8ae9ad7fbd1e3b24477e138 (patch)
treefaadc6d8822c20cd3bc6f50b2a98e6c580585949 /RealtimeSync/xml_proc.cpp
parent3.16 (diff)
downloadFreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.tar.gz
FreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.tar.bz2
FreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.zip
3.17
Diffstat (limited to 'RealtimeSync/xml_proc.cpp')
-rw-r--r--RealtimeSync/xml_proc.cpp114
1 files changed, 67 insertions, 47 deletions
diff --git a/RealtimeSync/xml_proc.cpp b/RealtimeSync/xml_proc.cpp
index 4d7f88e0..4eaf8769 100644
--- a/RealtimeSync/xml_proc.cpp
+++ b/RealtimeSync/xml_proc.cpp
@@ -9,83 +9,103 @@
#include "../shared/i18n.h"
-class RtsXmlParser : public xmlAccess::XmlParser
+using namespace xmlAccess;
+
+class RtsXmlErrorLogger : public xmlAccess::XmlErrorLogger
{
public:
- RtsXmlParser(const TiXmlElement* rootElement) : xmlAccess::XmlParser(rootElement) {}
-
- void readXmlRealConfig(xmlAccess::XmlRealConfig& outputCfg);
+ void readConfig(const TiXmlElement* root, xmlAccess::XmlRealConfig& outputCfg);
};
-
-void readXmlRealConfig(const TiXmlDocument& doc, xmlAccess::XmlRealConfig& outputCfg);
-bool writeXmRealSettings(const xmlAccess::XmlRealConfig& outputCfg, TiXmlDocument& doc);
+//--------------------------------------------------------------------------------
-void xmlAccess::readRealConfig(const wxString& filename, XmlRealConfig& config)
+void RtsXmlErrorLogger::readConfig(const TiXmlElement* root, xmlAccess::XmlRealConfig& outputCfg)
{
- //load XML
- if (!wxFileExists(filename))
- throw XmlError(wxString(_("File does not exist:")) + wxT("\n\"") + filename + wxT("\""));
+ //read directories for monitoring
+ const TiXmlElement* directoriesToWatch = TiXmlHandleConst(root).FirstChild("Directories").ToElement();
- TiXmlDocument doc;
- loadXmlDocument(filename, XML_REAL_CONFIG, doc); //throw (XmlError)
+ readXmlElementLogging("Folder", directoriesToWatch, outputCfg.directories);
- RtsXmlParser parser(doc.RootElement());
- parser.readXmlRealConfig(config); //read GUI layout configuration
+ //commandline to execute
+ readXmlElementLogging("Commandline", root, outputCfg.commandline);
- if (parser.errorsOccurred())
- throw XmlError(wxString(_("Error parsing configuration file:")) + wxT("\n\"") + filename + wxT("\"\n\n") +
- parser.getErrorMessageFormatted(), XmlError::WARNING);
+ //delay
+ readXmlElementLogging("Delay", root, outputCfg.delay);
}
-void xmlAccess::writeRealConfig(const XmlRealConfig& outputCfg, const wxString& filename)
+void writeConfig(const xmlAccess::XmlRealConfig& outputCfg, TiXmlElement& root)
{
- TiXmlDocument doc;
- getDefaultXmlDocument(XML_REAL_CONFIG, doc);
+ //directories to monitor
+ TiXmlElement* directoriesToWatch = new TiXmlElement("Directories");
+ root.LinkEndChild(directoriesToWatch);
+ xmlAccess::addXmlElement("Folder", outputCfg.directories, directoriesToWatch);
- //populate and write XML tree
- if (!writeXmRealSettings(outputCfg, doc)) //add GUI layout configuration settings
- throw XmlError(wxString(_("Error writing file:")) + wxT("\n\"") + filename + wxT("\""));
+ //commandline to execute
+ xmlAccess::addXmlElement("Commandline", outputCfg.commandline, &root);
- saveXmlDocument(filename, doc); //throw (XmlError)
+ //delay
+ xmlAccess::addXmlElement("Delay", outputCfg.delay, &root);
}
-//--------------------------------------------------------------------------------
-
-void RtsXmlParser::readXmlRealConfig(xmlAccess::XmlRealConfig& outputCfg)
+bool isXmlTypeRTS(const TiXmlDocument& doc) //throw()
{
- //read directories for monitoring
- const TiXmlElement* directoriesToWatch = TiXmlHandleConst(getRoot()).FirstChild("Directories").ToElement();
+ const TiXmlElement* root = doc.RootElement();
+ if (root && root->ValueStr() == std::string("RealtimeSync"))
+ {
+ const char* cfgType = root->Attribute("XmlType");
+ if (cfgType)
+ return std::string(cfgType) == "REAL";
+ }
+ return false;
+}
- readXmlElementLogging("Folder", directoriesToWatch, outputCfg.directories);
- //commandline to execute
- readXmlElementLogging("Commandline", getRoot(), outputCfg.commandline);
+void initXmlDocument(TiXmlDocument& doc) //throw()
+{
+ TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", ""); //delete won't be necessary later; ownership passed to TiXmlDocument!
+ doc.LinkEndChild(decl);
- //delay
- readXmlElementLogging("Delay", getRoot(), outputCfg.delay);
+ TiXmlElement* root = new TiXmlElement("RealtimeSync");
+ doc.LinkEndChild(root);
+
+ addXmlAttribute("XmlType", "REAL", doc.RootElement());
}
-bool writeXmRealSettings(const xmlAccess::XmlRealConfig& outputCfg, TiXmlDocument& doc)
+void xmlAccess::readRealConfig(const wxString& filename, XmlRealConfig& config)
{
- TiXmlElement* root = doc.RootElement();
- if (!root) return false;
+ //load XML
+ if (!wxFileExists(filename))
+ throw XmlError(wxString(_("File does not exist:")) + wxT("\n\"") + filename + wxT("\""));
- //directories to monitor
- TiXmlElement* directoriesToWatch = new TiXmlElement("Directories");
- root->LinkEndChild(directoriesToWatch);
- xmlAccess::addXmlElement("Folder", outputCfg.directories, directoriesToWatch);
+ TiXmlDocument doc;
+ loadXmlDocument(filename, doc); //throw (XmlError)
- //commandline to execute
- xmlAccess::addXmlElement("Commandline", outputCfg.commandline, root);
+ if (!isXmlTypeRTS(doc))
+ throw XmlError(wxString(_("Error parsing configuration file:")) + wxT("\n\"") + filename + wxT("\""));
- //delay
- xmlAccess::addXmlElement("Delay", outputCfg.delay, root);
+ RtsXmlErrorLogger parser;
+ parser.readConfig(doc.RootElement(), config); //read GUI layout configuration
- return true;
+ if (parser.errorsOccurred())
+ throw XmlError(wxString(_("Error parsing configuration file:")) + wxT("\n\"") + filename + wxT("\"\n\n") +
+ parser.getErrorMessageFormatted(), XmlError::WARNING);
+}
+
+
+void xmlAccess::writeRealConfig(const XmlRealConfig& outputCfg, const wxString& filename)
+{
+ TiXmlDocument doc;
+ initXmlDocument(doc); //throw()
+
+ if (!doc.RootElement())
+ throw XmlError(wxString(_("Error writing file:")) + wxT("\n\"") + filename + wxT("\""));
+
+ writeConfig(outputCfg, *doc.RootElement()); //add GUI layout configuration settings
+
+ saveXmlDocument(filename, doc); //throw (XmlError)
}
bgstack15