]> git.sesse.net Git - xml-template/blob - c++11/xml-template.cpp
Microscopic optimization to the C++11 code.
[xml-template] / c++11 / 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 (node->ns != NULL &&
16             strcmp(reinterpret_cast<const char *>(node->ns->href), "http://template.sesse.net/") == 0) {
17                 xmlNode *frag = xmlNewDocFragment(node->doc);
18                 xmlReplaceNode(node, frag);
19                 frag->children = node->children;
20                 frag->last = node->last;
21
22                 node->children = node->last = NULL;
23                 xmlFreeNode(node);
24         }
25 }
26
27 // Does A begin with B?
28 bool begins_with(const string &a, const string &b)
29 {
30         return (a.compare(0, b.size(), b) == 0);
31 }
32
33 }  // namespace
34
35 Directive::~Directive() {}
36         
37 string Directive::get_contents() { return ""; }
38
39 Replace::Replace(const string &str)
40         : str(str) {}
41
42 void Replace::process(xmlNode *node, bool clean) {
43         xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(str.data()), str.size());
44         if (clean) {
45                 clean_node(node);
46         }
47 }
48
49 string Replace::get_contents() { return str; }
50
51 ReplaceInclude::ReplaceInclude(xmlNodePtr included_node)
52         : included_node(included_node),
53           included_doc(NULL) {}
54         
55 ReplaceInclude::ReplaceInclude(xmlDocPtr included_doc)
56         : included_node(xmlDocGetRootElement(included_doc)),
57           included_doc(included_doc) {}
58
59 ReplaceInclude::~ReplaceInclude()
60 {
61         if (included_doc != NULL) {
62                 xmlFreeDoc(included_doc);
63         } else {
64                 xmlFreeNode(included_node);
65         }
66 }
67
68 void ReplaceInclude::process(xmlNode *node, bool clean) {
69         xmlFreeNodeList(node->children);
70         node->content = NULL;
71         node->children = node->last = NULL; 
72
73         xmlNode *new_node;
74         xmlDOMWrapCloneNode(NULL, NULL, included_node, &new_node, node->doc, node, 1, 0);
75         xmlAddChild(node, new_node);
76
77         if (clean) {
78                 Substitute dummy_subs {};
79                 dummy_subs.process(new_node, clean);
80                 clean_node(node);
81         }
82 }
83
84 Clone::Clone(const vector<Directive *> &subdirectives)
85         : subdirectives(subdirectives) {}
86
87 Clone::Clone(const vector<Substitute *> &subdirectives_subs)
88 {
89         for (auto it : subdirectives_subs) {
90                 subdirectives.push_back(static_cast<Directive *>(it));
91         }
92 }
93
94 Clone::Clone(initializer_list<Directive *> subdirectives)
95         : subdirectives(subdirectives) {}
96
97 Clone::~Clone()
98 {
99         for (auto it : subdirectives) {
100                 delete it;
101         }
102 }
103
104 void Clone::process(xmlNode *node, bool clean)
105 {
106         // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
107         // how to clone elements.
108         vector<xmlNode *> new_nodes;
109
110         for (auto it : subdirectives) {
111                 if (it == NULL) {
112                         continue;
113                 }
114                 xmlNode *new_node;
115                 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
116                 it->process(new_node, clean);
117                 while (new_node->children != NULL) {
118                         xmlNode *child = new_node->children;
119                         xmlUnlinkNode(child);
120                         new_nodes.push_back(child);
121                 }
122                 xmlFreeNode(new_node);
123         }
124
125         xmlFreeNodeList(node->children);
126         node->content = NULL;
127         node->children = node->last = NULL;
128
129         for (auto child : new_nodes) {
130                 xmlAddChild(node, child);
131         }       
132         if (clean) {
133                 clean_node(node);
134         }
135 }
136         
137 Alternate::Alternate(const string &attribute,
138                      const vector<Substitute *> &subdirectives_subs,
139                      const vector<string> &alternatives)
140     : Clone(subdirectives_subs)
141 {
142         unsigned jx = 0;
143         for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
144                 if (subdirectives_subs[ix] == NULL) {
145                         continue;
146                 }
147                 string value = alternatives[jx++ % alternatives.size()];
148                 subdirectives_subs[ix]->substitution_map.insert(make_pair(
149                         attribute,
150                         new Replace { value }));
151         } 
152 }
153
154 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
155         : substitution_map(substitution_map) {}
156         
157 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
158         : substitution_map(substitution_map) {}
159
160 Substitute::~Substitute()
161 {
162         for (auto it : substitution_map) {
163                 delete it.second;
164         }
165 }
166
167 void Substitute::process(xmlNode *node, bool clean)
168 {
169         xmlNode *next_child;
170         for (xmlNode *child = node->children; child != NULL; child = next_child) {
171                 next_child = child->next;
172                 Directive *next_processor = this;
173
174                 if (child->type == XML_ELEMENT_NODE) {
175                         // Find the ID, if any.
176                         string id;
177                         xmlAttr *id_attr = NULL;
178                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
179                                 if (attr->ns != NULL &&
180                                     strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
181                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
182                                         xmlChar *id_buf = xmlNodeGetContent(attr->children);
183                                         id = reinterpret_cast<const char *>(id_buf);
184                                         xmlFree(id_buf);
185                                         id_attr = attr;
186                                 }
187                         }
188                         if (clean && id_attr != NULL) {
189                                 xmlRemoveProp(id_attr);
190                         }
191
192                         // Check all substitutions to see if we found anything appropriate.
193                         string tag = reinterpret_cast<const char *>(child->name);
194                         for (auto it : substitution_map) {
195                                 // Attribute substitution.
196                                 if (begins_with(it.first, tag + "/")) {
197                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
198                                                 it.first.c_str() + tag.size() + 1);
199                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
200                                                 it.second->get_contents().c_str());
201                                         xmlSetProp(child, attr_key, attr_value);
202                                 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
203                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
204                                                 it.first.c_str() + tag.size() + 2);
205                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
206                                                 it.second->get_contents().c_str());
207                                         xmlSetProp(child, attr_key, attr_value);
208                                 }
209
210                                 // Regular substitution. (Don't call process() immediately, because
211                                 // that might delete the element, which would cause problems.)
212                                 if (it.first == tag ||
213                                     (!id.empty() && it.first == ("#" + id))) {
214                                         next_processor = it.second;
215                                 }
216                         }
217                 }
218
219                 next_processor->process(child, clean);
220         }
221         if (clean) {
222                 clean_node(node);
223         }
224 }
225         
226 xmlDocPtr process_file(const string &input_filename,
227                        Directive *root_directive,
228                        bool clean)
229 {
230         LIBXML_TEST_VERSION
231
232         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
233         root_directive->process(xmlDocGetRootElement(doc), clean);
234         xmlCleanupParser();
235         xmlMemoryDump();
236         return doc;
237 }
238
239 void output_to_fd_and_free(xmlDocPtr doc, int fd)
240 {
241         xmlOutputBufferPtr buf = xmlOutputBufferCreateFd(fd, NULL);
242         xmlSaveFileTo(buf, doc, NULL);
243         xmlFreeDoc(doc);
244 }