X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=c%2B%2B0x%2Fsimple.cpp;h=cfc504181d25584d906fb61b922918ca15152bc1;hb=463ec2a071522ca887d1548bc1a5f01ed0263b01;hp=a5e7894ccfe0d66cc82d61dfe97b68bd884b96a9;hpb=8dd31132b249ab02168c61875dadd7eb18300566;p=xml-template diff --git a/c++0x/simple.cpp b/c++0x/simple.cpp index a5e7894..cfc5041 100644 --- a/c++0x/simple.cpp +++ b/c++0x/simple.cpp @@ -2,73 +2,10 @@ #include #include -#include -#include -#include +#include "xml-template.h" using namespace std; -class Directive { - public: - virtual void process(xmlNode *node, bool clean) = 0; -}; - -class Replace : public Directive { - public: - Replace(const string &str) : str(str) {} - virtual void process(xmlNode *node, bool clean) { - node->children = xmlNewTextLen(reinterpret_cast(str.data()), str.size()); - } - - private: - const string str; -}; - -class Substitute : public Directive { - public: - Substitute(const unordered_map &substitution_map) - : substitution_map(substitution_map) {} - - virtual void process(xmlNode *node, bool clean) { - for (xmlNode *child = node->children; child != NULL; child = child->next) { - bool processed = false; - - if (child->type == XML_ELEMENT_NODE) { - xmlElement *elem = reinterpret_cast(child); - - // Find the ID, if any. - string id; -#if 0 - for (xmlAttribute *attr = elem->attributes; attr != NULL; attr = attr->nexth) { - // FIXME: namespace - if (strcmp(reinterpret_cast(attr->name), "id") != 0) { - continue; - } - id = xmlNodeGetContent(attr->content); - } -#endif - - // Check all substitutions to see if we found anything appropriate. - for (auto it : substitution_map) { - if (it.first == reinterpret_cast(child->name) || - (!id.empty() && ("#" + it.first) == id)) { - it.second->process(child, clean); - processed = true; - break; - } - } - } - - if (!processed) { - process(child, clean); - } - } - } - - private: - const unordered_map &substitution_map; -}; - int main(int argc, char **argv) { LIBXML_TEST_VERSION @@ -77,9 +14,9 @@ int main(int argc, char **argv) master_map.insert(make_pair("title", new Replace("A very basic example"))); master_map.insert(make_pair("#hello", new Replace("Hello world!"))); - xmlDocPtr doc = xmlParseFile(argv[1]); + xmlDocPtr doc = xmlParseFile("../xml/simple.xml"); Substitute(master_map).process(xmlDocGetRootElement(doc), false); - xmlSaveFile("out.xml", doc); + xmlSaveFile(argv[1], doc); xmlCleanupParser(); xmlMemoryDump();