X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=c%2B%2B0x%2Fsimple.cpp;h=05c7ea64f6ad4a49338412173c8814c2d65ec290;hb=59996859ced59ac9fdb307b2eb2130b48d4d4f63;hp=69480e9c4abf21bb3ea619a554d7c2e77c6fc40c;hpb=eb5498b74048d0ae51ace918e4d06fcc12264ec4;p=xml-template diff --git a/c++0x/simple.cpp b/c++0x/simple.cpp index 69480e9..05c7ea6 100644 --- a/c++0x/simple.cpp +++ b/c++0x/simple.cpp @@ -2,68 +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) { - // Find the ID, if any. - string id; - for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) { - if (strcmp(reinterpret_cast(attr->ns->href), "http://template.sesse.net/") == 0 && - strcmp(reinterpret_cast(attr->name), "id") == 0) { - 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