]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Fix leak of the temporary clone root elements in the C++0x version.
[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                 while (new_node->children != NULL) {
61                         xmlNode *child = new_node->children;
62                         xmlUnlinkNode(child);
63                         new_nodes.push_back(child);
64                 }
65                 xmlFreeNode(new_node);
66         }
67
68         xmlFreeNodeList(node->children);
69         node->content = NULL;
70         node->children = node->last = NULL;
71
72         for (auto child : new_nodes) {
73                 xmlAddChild(node, child);
74         }       
75         if (clean) {
76                 clean_node(node);
77         }
78 }
79
80 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
81         : substitution_map(substitution_map) {}
82         
83 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
84         : substitution_map(substitution_map) {}
85
86 Substitute::~Substitute()
87 {
88         for (auto it : substitution_map) {
89                 delete it.second;
90         }
91 }
92
93 void Substitute::process(xmlNode *node, bool clean)
94 {
95         for (xmlNode *child = node->children; child != NULL; child = child->next) {
96                 bool processed = false;
97
98                 if (child->type == XML_ELEMENT_NODE) {
99                         // Find the ID, if any.
100                         string id;
101                         xmlAttr *id_attr = NULL;
102                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
103                                 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
104                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
105                                         xmlChar *id_buf = xmlNodeGetContent(attr->children);
106                                         id = reinterpret_cast<const char *>(id_buf);
107                                         xmlFree(id_buf);
108                                         id_attr = attr;
109                                 }
110                         }
111                         if (clean && id_attr != NULL) {
112                                 xmlRemoveProp(id_attr);
113                         }
114
115                         // Check all substitutions to see if we found anything appropriate.
116                         for (auto it : substitution_map) {
117                                 if (it.first == reinterpret_cast<const char *>(child->name) ||
118                                     (!id.empty() && it.first == ("#" + id))) {
119                                         it.second->process(child, clean);
120                                         processed = true;
121                                         break;
122                                 }
123                         }
124                 }
125                 
126                 if (!processed) {
127                         process(child, clean);
128                 }
129         }
130         if (clean) {
131                 clean_node(node);
132         }
133 }
134         
135 void process_file(const string &input_filename,
136                   const string &output_filename,
137                   Directive *root_directive)
138 {
139         LIBXML_TEST_VERSION
140
141         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
142         root_directive->process(xmlDocGetRootElement(doc), true);
143         xmlSaveFile(output_filename.c_str(), doc);
144         xmlFreeDoc(doc);
145
146         xmlCleanupParser();
147         xmlMemoryDump();
148 }