]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Add Alternate support to C++0x version, and corresponding attribute2 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 // 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                 xmlNode *new_node;
78                 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
79                 it->process(new_node, clean);
80                 while (new_node->children != NULL) {
81                         xmlNode *child = new_node->children;
82                         xmlUnlinkNode(child);
83                         new_nodes.push_back(child);
84                 }
85                 xmlFreeNode(new_node);
86         }
87
88         xmlFreeNodeList(node->children);
89         node->content = NULL;
90         node->children = node->last = NULL;
91
92         for (auto child : new_nodes) {
93                 xmlAddChild(node, child);
94         }       
95         if (clean) {
96                 clean_node(node);
97         }
98 }
99         
100 Alternate::Alternate(const string &attribute,
101                      const vector<Substitute *> &subdirectives_subs,
102                      const vector<string> &alternatives)
103     : Clone(subdirectives_subs)
104 {
105         for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
106                 string value = alternatives[ix % alternatives.size()];
107                 subdirectives_subs[ix]->substitution_map.insert(make_pair(
108                         attribute,
109                         new Replace { value }));
110         } 
111 }
112
113 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
114         : substitution_map(substitution_map) {}
115         
116 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
117         : substitution_map(substitution_map) {}
118
119 Substitute::~Substitute()
120 {
121         for (auto it : substitution_map) {
122                 delete it.second;
123         }
124 }
125
126 void Substitute::process(xmlNode *node, bool clean)
127 {
128         xmlNode *next_child;
129         for (xmlNode *child = node->children; child != NULL; child = next_child) {
130                 next_child = child->next;       
131                 bool processed = false;
132
133                 if (child->type == XML_ELEMENT_NODE) {
134                         // Find the ID, if any.
135                         string id;
136                         xmlAttr *id_attr = NULL;
137                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
138                                 if (attr->ns != NULL &&
139                                     strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
140                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
141                                         xmlChar *id_buf = xmlNodeGetContent(attr->children);
142                                         id = reinterpret_cast<const char *>(id_buf);
143                                         xmlFree(id_buf);
144                                         id_attr = attr;
145                                 }
146                         }
147                         if (clean && id_attr != NULL) {
148                                 xmlRemoveProp(id_attr);
149                         }
150
151                         // Check all substitutions to see if we found anything appropriate.
152                         for (auto it : substitution_map) {
153                                 string tag = reinterpret_cast<const char *>(child->name);
154
155                                 // Attribute substitution.
156                                 if (begins_with(it.first, tag + "/")) {
157                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
158                                                 it.first.substr(tag.size() + 1).c_str());
159                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
160                                                 it.second->get_contents().c_str());
161                                         xmlSetProp(child, attr_key, attr_value);
162                                 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
163                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
164                                                 it.first.substr(tag.size() + 2).c_str());
165                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
166                                                 it.second->get_contents().c_str());
167                                         xmlSetProp(child, attr_key, attr_value);
168                                 }
169
170                                 if (processed) {
171                                         continue;
172                                 }
173
174                                 // Regular substitution.
175                                 if (it.first == tag ||
176                                     (!id.empty() && it.first == ("#" + id))) {
177                                         it.second->process(child, clean);
178                                         processed = true;
179                                 }
180                         }
181                 }
182
183                 if (!processed) {
184                         process(child, clean);
185                 }
186         }
187         if (clean) {
188                 clean_node(node);
189         }
190 }
191         
192 void process_file(const string &input_filename,
193                   const string &output_filename,
194                   Directive *root_directive)
195 {
196         LIBXML_TEST_VERSION
197
198         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
199         root_directive->process(xmlDocGetRootElement(doc), true);
200         xmlSaveFile(output_filename.c_str(), doc);
201         xmlFreeDoc(doc);
202
203         xmlCleanupParser();
204         xmlMemoryDump();
205 }