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