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