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