X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=c%2B%2B0x%2Fsimple.cpp;h=0c895426383497494e362f9f8ddadd90d4827989;hb=ad45bc892752571fec46b21564e4683c074f1d46;hp=c95bc437c6bc81fc51d5faf8d613d9485b66683b;hpb=32a15e16a82ca8382ad39076cd942d6eb8242d55;p=xml-template diff --git a/c++0x/simple.cpp b/c++0x/simple.cpp index c95bc43..0c89542 100644 --- a/c++0x/simple.cpp +++ b/c++0x/simple.cpp @@ -1,83 +1,17 @@ #include -#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) { - // Find the ID, if any. - string id; - for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) { - // FIXME: namespace - if (strcmp(reinterpret_cast(attr->name), "id") != 0) { - continue; - } - id = reinterpret_cast(xmlNodeGetContent(attr->children)); - } - - // 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 - - unordered_map master_map; - 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]); - Substitute(master_map).process(xmlDocGetRootElement(doc), false); - xmlSaveFile("out.xml", doc); + Substitute master_directive = { + make_pair("title", new Replace("A very basic example")), + make_pair("#hello", new Replace("Hello world!")), + }; - xmlCleanupParser(); - xmlMemoryDump(); + xmlDocPtr doc = process_file("../xml/simple.xml", &master_directive); + output_to_fd_and_free(doc, 1); return(0); }