]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Make the C++0x syntax a lot more concise using initializer lists.
[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         }
20 }
21
22 }  // namespace
23
24 Replace::Replace(const string &str)
25         : str(str) {}
26
27 void Replace::process(xmlNode *node, bool clean) {
28         node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
29         if (clean) {
30                 clean_node(node);
31         }
32 }
33
34 Clone::Clone(const std::vector<Directive *> &subdirectives)
35         : subdirectives(subdirectives) {}
36         
37 Clone::Clone(std::initializer_list<Directive *> subdirectives)
38         : subdirectives(subdirectives) {}
39
40 void Clone::process(xmlNode *node, bool clean) {
41         // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
42         // how to clone elements.
43         vector<xmlNode *> new_nodes;
44
45         for (auto it : subdirectives) {
46                 xmlNode *new_node;
47                 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
48                 it->process(new_node, clean);
49                 for (xmlNode *child = new_node->children; child != NULL; child = child->next) {
50                         new_nodes.push_back(child);
51                 }
52         }
53
54         node->children = NULL;
55         for (auto child : new_nodes) {
56                 xmlAddChild(node, child);
57         }       
58         if (clean) {
59                 clean_node(node);
60         }
61 }
62
63 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
64         : substitution_map(substitution_map) {}
65         
66 Substitute::Substitute(std::initializer_list<std::pair<const std::string, Directive*>> substitution_map)
67         : substitution_map(substitution_map) {}
68
69 void Substitute::process(xmlNode *node, bool clean) {
70         for (xmlNode *child = node->children; child != NULL; child = child->next) {
71                 bool processed = false;
72
73                 if (child->type == XML_ELEMENT_NODE) {
74                         // Find the ID, if any.
75                         string id;
76                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
77                                 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
78                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
79                                         id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
80
81                                         if (clean) {
82                                                 if (attr->prev == NULL) {
83                                                         child->properties = attr->next;
84                                                 } else {
85                                                         attr->prev->next = attr->next;
86                                                 }
87                                         }
88                                 }
89                         }
90
91                         // Check all substitutions to see if we found anything appropriate.
92                         for (auto it : substitution_map) {
93                                 if (it.first == reinterpret_cast<const char *>(child->name) ||
94                                     (!id.empty() && it.first == ("#" + id))) {
95                                         it.second->process(child, clean);
96                                         processed = true;
97                                         break;
98                                 }
99                         }
100                 }
101                 
102                 if (!processed) {
103                         process(child, clean);
104                 }
105         }
106         if (clean) {
107                 clean_node(node);
108         }
109 }
110         
111 void process_file(const string &input_filename,
112                   const string &output_filename,
113                   Directive *root_directive)
114 {
115         LIBXML_TEST_VERSION
116
117         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
118         root_directive->process(xmlDocGetRootElement(doc), true);
119         xmlSaveFile(output_filename.c_str(), doc);
120
121         xmlCleanupParser();
122         xmlMemoryDump();
123 }