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