]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Tweak C++0x API a bit, to prepare for include. Also, output to stdout instead of...
[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 // 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 Clone::Clone(const vector<Directive *> &subdirectives)
51         : subdirectives(subdirectives) {}
52
53 Clone::Clone(const vector<Substitute *> &subdirectives_subs)
54 {
55         for (auto it : subdirectives_subs) {
56                 subdirectives.push_back(static_cast<Directive *>(it));
57         }
58 }
59
60 Clone::Clone(initializer_list<Directive *> subdirectives)
61         : subdirectives(subdirectives) {}
62
63 Clone::~Clone()
64 {
65         for (auto it : subdirectives) {
66                 delete it;
67         }
68 }
69
70 void Clone::process(xmlNode *node, bool clean)
71 {
72         // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
73         // how to clone elements.
74         vector<xmlNode *> new_nodes;
75
76         for (auto it : subdirectives) {
77                 if (it == NULL) {
78                         continue;
79                 }
80                 xmlNode *new_node;
81                 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
82                 it->process(new_node, clean);
83                 while (new_node->children != NULL) {
84                         xmlNode *child = new_node->children;
85                         xmlUnlinkNode(child);
86                         new_nodes.push_back(child);
87                 }
88                 xmlFreeNode(new_node);
89         }
90
91         xmlFreeNodeList(node->children);
92         node->content = NULL;
93         node->children = node->last = NULL;
94
95         for (auto child : new_nodes) {
96                 xmlAddChild(node, child);
97         }       
98         if (clean) {
99                 clean_node(node);
100         }
101 }
102         
103 Alternate::Alternate(const string &attribute,
104                      const vector<Substitute *> &subdirectives_subs,
105                      const vector<string> &alternatives)
106     : Clone(subdirectives_subs)
107 {
108         unsigned jx = 0;
109         for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
110                 if (subdirectives_subs[ix] == NULL) {
111                         continue;
112                 }
113                 string value = alternatives[jx++ % alternatives.size()];
114                 subdirectives_subs[ix]->substitution_map.insert(make_pair(
115                         attribute,
116                         new Replace { value }));
117         } 
118 }
119
120 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
121         : substitution_map(substitution_map) {}
122         
123 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
124         : substitution_map(substitution_map) {}
125
126 Substitute::~Substitute()
127 {
128         for (auto it : substitution_map) {
129                 delete it.second;
130         }
131 }
132
133 void Substitute::process(xmlNode *node, bool clean)
134 {
135         xmlNode *next_child;
136         for (xmlNode *child = node->children; child != NULL; child = next_child) {
137                 next_child = child->next;       
138                 bool processed = false;
139
140                 if (child->type == XML_ELEMENT_NODE) {
141                         // Find the ID, if any.
142                         string id;
143                         xmlAttr *id_attr = NULL;
144                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
145                                 if (attr->ns != NULL &&
146                                     strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
147                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
148                                         xmlChar *id_buf = xmlNodeGetContent(attr->children);
149                                         id = reinterpret_cast<const char *>(id_buf);
150                                         xmlFree(id_buf);
151                                         id_attr = attr;
152                                 }
153                         }
154                         if (clean && id_attr != NULL) {
155                                 xmlRemoveProp(id_attr);
156                         }
157
158                         // Check all substitutions to see if we found anything appropriate.
159                         for (auto it : substitution_map) {
160                                 string tag = reinterpret_cast<const char *>(child->name);
161
162                                 // Attribute substitution.
163                                 if (begins_with(it.first, tag + "/")) {
164                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
165                                                 it.first.substr(tag.size() + 1).c_str());
166                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
167                                                 it.second->get_contents().c_str());
168                                         xmlSetProp(child, attr_key, attr_value);
169                                 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
170                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
171                                                 it.first.substr(tag.size() + 2).c_str());
172                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
173                                                 it.second->get_contents().c_str());
174                                         xmlSetProp(child, attr_key, attr_value);
175                                 }
176
177                                 if (processed) {
178                                         continue;
179                                 }
180
181                                 // Regular substitution.
182                                 if (it.first == tag ||
183                                     (!id.empty() && it.first == ("#" + id))) {
184                                         it.second->process(child, clean);
185                                         processed = true;
186                                 }
187                         }
188                 }
189
190                 if (!processed) {
191                         process(child, clean);
192                 }
193         }
194         if (clean) {
195                 clean_node(node);
196         }
197 }
198         
199 xmlDocPtr process_file(const string &input_filename,
200                        Directive *root_directive)
201 {
202         LIBXML_TEST_VERSION
203
204         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
205         root_directive->process(xmlDocGetRootElement(doc), true);
206         xmlCleanupParser();
207         xmlMemoryDump();
208         return doc;
209 }
210
211 void output_to_fd_and_free(xmlDocPtr doc, int fd)
212 {
213         xmlOutputBufferPtr buf = xmlOutputBufferCreateFd(fd, NULL);
214         xmlSaveFileTo(buf, doc, NULL);
215         xmlFreeDoc(doc);
216 }