]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Fix a potential bug in the C++0x cleaning.
[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 namespace {
9
10 void clean_node(xmlNode *node)
11 {
12         if (node->type != XML_ELEMENT_NODE) {
13                 return;
14         }
15         if (strcmp(reinterpret_cast<const char *>(node->ns->href), "http://template.sesse.net/") == 0) {
16                 xmlNode *frag = xmlNewDocFragment(node->doc);
17                 xmlReplaceNode(node, frag);
18                 frag->children = node->children;
19                 frag->last = node->last;
20         }
21 }
22
23 }  // namespace
24
25 Directive::~Directive() {}
26
27 Replace::Replace(const string &str)
28         : str(str) {}
29
30 void Replace::process(xmlNode *node, bool clean) {
31         xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(str.data()), str.size());
32         if (clean) {
33                 clean_node(node);
34         }
35 }
36
37 Clone::Clone(const vector<Directive *> &subdirectives)
38         : subdirectives(subdirectives) {}
39         
40 Clone::Clone(initializer_list<Directive *> subdirectives)
41         : subdirectives(subdirectives) {}
42
43 Clone::~Clone()
44 {
45         for (auto it : subdirectives) {
46                 delete it;
47         }
48 }
49
50 void Clone::process(xmlNode *node, bool clean)
51 {
52         // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
53         // how to clone elements.
54         vector<xmlNode *> new_nodes;
55
56         for (auto it : subdirectives) {
57                 xmlNode *new_node;
58                 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
59                 it->process(new_node, clean);
60                 for (xmlNode *child = new_node->children; child != NULL; child = child->next) {
61                         new_nodes.push_back(child);
62                 }
63         }
64
65         xmlFreeNodeList(node->children);
66         node->content = NULL;
67         node->children = node->last = NULL;
68
69         for (auto child : new_nodes) {
70                 xmlAddChild(node, child);
71         }       
72         if (clean) {
73                 clean_node(node);
74         }
75 }
76
77 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
78         : substitution_map(substitution_map) {}
79         
80 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
81         : substitution_map(substitution_map) {}
82
83 Substitute::~Substitute()
84 {
85         for (auto it : substitution_map) {
86                 delete it.second;
87         }
88 }
89
90 void Substitute::process(xmlNode *node, bool clean)
91 {
92         for (xmlNode *child = node->children; child != NULL; child = child->next) {
93                 bool processed = false;
94
95                 if (child->type == XML_ELEMENT_NODE) {
96                         // Find the ID, if any.
97                         string id;
98                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
99                                 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
100                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
101                                         id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
102
103                                         if (clean) {
104                                                 if (attr->prev == NULL) {
105                                                         child->properties = attr->next;
106                                                 } else {
107                                                         attr->prev->next = attr->next;
108                                                 }
109                                         }
110                                 }
111                         }
112
113                         // Check all substitutions to see if we found anything appropriate.
114                         for (auto it : substitution_map) {
115                                 if (it.first == reinterpret_cast<const char *>(child->name) ||
116                                     (!id.empty() && it.first == ("#" + id))) {
117                                         it.second->process(child, clean);
118                                         processed = true;
119                                         break;
120                                 }
121                         }
122                 }
123                 
124                 if (!processed) {
125                         process(child, clean);
126                 }
127         }
128         if (clean) {
129                 clean_node(node);
130         }
131 }
132         
133 void process_file(const string &input_filename,
134                   const string &output_filename,
135                   Directive *root_directive)
136 {
137         LIBXML_TEST_VERSION
138
139         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
140         root_directive->process(xmlDocGetRootElement(doc), true);
141         xmlSaveFile(output_filename.c_str(), doc);
142         xmlFreeDoc(doc);
143
144         xmlCleanupParser();
145         xmlMemoryDump();
146 }