]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Implement attribute handling in C++0x.
[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(initializer_list<Directive *> subdirectives)
54         : subdirectives(subdirectives) {}
55
56 Clone::~Clone()
57 {
58         for (auto it : subdirectives) {
59                 delete it;
60         }
61 }
62
63 void Clone::process(xmlNode *node, bool clean)
64 {
65         // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
66         // how to clone elements.
67         vector<xmlNode *> new_nodes;
68
69         for (auto it : subdirectives) {
70                 xmlNode *new_node;
71                 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
72                 it->process(new_node, clean);
73                 while (new_node->children != NULL) {
74                         xmlNode *child = new_node->children;
75                         xmlUnlinkNode(child);
76                         new_nodes.push_back(child);
77                 }
78                 xmlFreeNode(new_node);
79         }
80
81         xmlFreeNodeList(node->children);
82         node->content = NULL;
83         node->children = node->last = NULL;
84
85         for (auto child : new_nodes) {
86                 xmlAddChild(node, child);
87         }       
88         if (clean) {
89                 clean_node(node);
90         }
91 }
92
93 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
94         : substitution_map(substitution_map) {}
95         
96 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
97         : substitution_map(substitution_map) {}
98
99 Substitute::~Substitute()
100 {
101         for (auto it : substitution_map) {
102                 delete it.second;
103         }
104 }
105
106 void Substitute::process(xmlNode *node, bool clean)
107 {
108         xmlNode *next_child;
109         for (xmlNode *child = node->children; child != NULL; child = next_child) {
110                 next_child = child->next;       
111                 bool processed = false;
112
113                 if (child->type == XML_ELEMENT_NODE) {
114                         // Find the ID, if any.
115                         string id;
116                         xmlAttr *id_attr = NULL;
117                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
118                                 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
119                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
120                                         xmlChar *id_buf = xmlNodeGetContent(attr->children);
121                                         id = reinterpret_cast<const char *>(id_buf);
122                                         xmlFree(id_buf);
123                                         id_attr = attr;
124                                 }
125                         }
126                         if (clean && id_attr != NULL) {
127                                 xmlRemoveProp(id_attr);
128                         }
129
130                         // Check all substitutions to see if we found anything appropriate.
131                         for (auto it : substitution_map) {
132                                 string tag = reinterpret_cast<const char *>(child->name);
133
134                                 // Attribute substitution.
135                                 if (begins_with(it.first, tag + "/")) {
136                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
137                                                 it.first.substr(tag.size() + 1).c_str());
138                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
139                                                 it.second->get_contents().c_str());
140                                         xmlSetProp(child, attr_key, attr_value);
141                                 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
142                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
143                                                 it.first.substr(tag.size() + 2).c_str());
144                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
145                                                 it.second->get_contents().c_str());
146                                         xmlSetProp(child, attr_key, attr_value);
147                                 }
148
149                                 if (processed) {
150                                         continue;
151                                 }
152
153                                 // Regular substitution.
154                                 if (it.first == tag ||
155                                     (!id.empty() && it.first == ("#" + id))) {
156                                         it.second->process(child, clean);
157                                         processed = true;
158                                 }
159                         }
160                 }
161
162                 if (!processed) {
163                         process(child, clean);
164                 }
165         }
166         if (clean) {
167                 clean_node(node);
168         }
169 }
170         
171 void process_file(const string &input_filename,
172                   const string &output_filename,
173                   Directive *root_directive)
174 {
175         LIBXML_TEST_VERSION
176
177         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
178         root_directive->process(xmlDocGetRootElement(doc), true);
179         xmlSaveFile(output_filename.c_str(), doc);
180         xmlFreeDoc(doc);
181
182         xmlCleanupParser();
183         xmlMemoryDump();
184 }