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