]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Implement t:id cleaning in C++0x. simple 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 Replace::Replace(const string &str)
9         : str(str) {}
10
11 void Replace::process(xmlNode *node, bool clean) {
12         node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
13 }
14
15 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
16         : substitution_map(substitution_map) {}
17
18 void Substitute::process(xmlNode *node, bool clean) {
19         for (xmlNode *child = node->children; child != NULL; child = child->next) {
20                 bool processed = false;
21
22                 if (child->type == XML_ELEMENT_NODE) {
23                         // Find the ID, if any.
24                         string id;
25                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
26                                 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
27                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
28                                         id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
29
30                                         if (clean) {
31                                                 if (attr->prev == NULL) {
32                                                         child->properties = attr->next;
33                                                 } else {
34                                                         attr->prev->next = attr->next;
35                                                 }
36                                         }
37                                 }
38                         }
39
40                         // Check all substitutions to see if we found anything appropriate.
41                         for (auto it : substitution_map) {
42                                 if (it.first == reinterpret_cast<const char *>(child->name) ||
43                                     (!id.empty() && it.first == ("#" + id))) {
44                                         it.second->process(child, clean);
45                                         processed = true;
46                                         break;
47                                 }
48                         }
49                 }
50                 
51                 if (!processed) {
52                         process(child, clean);
53                 }
54         }
55 }
56         
57 void process_file(const string &input_filename,
58                   const string &output_filename,
59                   Directive *root_directive)
60 {
61         LIBXML_TEST_VERSION
62
63         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
64         root_directive->process(xmlDocGetRootElement(doc), true);
65         xmlSaveFile(output_filename.c_str(), doc);
66
67         xmlCleanupParser();
68         xmlMemoryDump();
69 }