]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Add a clone test for C++0x (no cloning logic yet, so test fails).
[xml-template] / c++0x / xml-template.cpp
1 #include "xml-template.h"
2
3 #include <string.h>
4 #include <libxml/parser.h>
5
6 using namespace std;
7
8 Replace::Replace(const string &str)
9         : str(str) {}
10
11 void Replace::process(xmlNode *node, bool clean) {
12         node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
13 }
14
15 Clone::Clone(const std::vector<Directive *> &subdirectives)
16         : subdirectives(subdirectives) {}
17
18 void Clone::process(xmlNode *node, bool clean) {
19 }
20
21 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
22         : substitution_map(substitution_map) {}
23
24 void Substitute::process(xmlNode *node, bool clean) {
25         for (xmlNode *child = node->children; child != NULL; child = child->next) {
26                 bool processed = false;
27
28                 if (child->type == XML_ELEMENT_NODE) {
29                         // Find the ID, if any.
30                         string id;
31                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
32                                 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
33                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
34                                         id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
35
36                                         if (clean) {
37                                                 if (attr->prev == NULL) {
38                                                         child->properties = attr->next;
39                                                 } else {
40                                                         attr->prev->next = attr->next;
41                                                 }
42                                         }
43                                 }
44                         }
45
46                         // Check all substitutions to see if we found anything appropriate.
47                         for (auto it : substitution_map) {
48                                 if (it.first == reinterpret_cast<const char *>(child->name) ||
49                                     (!id.empty() && it.first == ("#" + id))) {
50                                         it.second->process(child, clean);
51                                         processed = true;
52                                         break;
53                                 }
54                         }
55                 }
56                 
57                 if (!processed) {
58                         process(child, clean);
59                 }
60         }
61 }
62         
63 void process_file(const string &input_filename,
64                   const string &output_filename,
65                   Directive *root_directive)
66 {
67         LIBXML_TEST_VERSION
68
69         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
70         root_directive->process(xmlDocGetRootElement(doc), true);
71         xmlSaveFile(output_filename.c_str(), doc);
72
73         xmlCleanupParser();
74         xmlMemoryDump();
75 }