]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Fix an attribute leak in the C++0x code.
[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                         xmlAttr *id_attr = NULL;
99                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
100                                 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
101                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
102                                         id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
103                                         id_attr = attr;
104                                 }
105                         }
106                         if (clean && id_attr != NULL) {
107                                 xmlRemoveProp(id_attr);
108                         }
109
110                         // Check all substitutions to see if we found anything appropriate.
111                         for (auto it : substitution_map) {
112                                 if (it.first == reinterpret_cast<const char *>(child->name) ||
113                                     (!id.empty() && it.first == ("#" + id))) {
114                                         it.second->process(child, clean);
115                                         processed = true;
116                                         break;
117                                 }
118                         }
119                 }
120                 
121                 if (!processed) {
122                         process(child, clean);
123                 }
124         }
125         if (clean) {
126                 clean_node(node);
127         }
128 }
129         
130 void process_file(const string &input_filename,
131                   const string &output_filename,
132                   Directive *root_directive)
133 {
134         LIBXML_TEST_VERSION
135
136         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
137         root_directive->process(xmlDocGetRootElement(doc), true);
138         xmlSaveFile(output_filename.c_str(), doc);
139         xmlFreeDoc(doc);
140
141         xmlCleanupParser();
142         xmlMemoryDump();
143 }