]> git.sesse.net Git - xml-template/blob - php5-swig/xml-template.swig
Implement prettyprinting in the PHP5 SWIG version.
[xml-template] / php5-swig / xml-template.swig
1 %module XML_Template_SWIG
2 %include <std_string.i>
3
4 struct XmlDocPtrWrapper {
5         ~XmlDocPtrWrapper();
6 };
7
8 %{
9
10 #include <memory>
11 #include <libxml/globals.h>
12
13 #include "../c++11/xml-template.h"
14
15 struct XmlDocWrapper {
16         ~XmlDocWrapper() { xmlFreeDoc(ptr); }
17         xmlDocPtr ptr;
18 };
19 typedef std::shared_ptr<XmlDocWrapper> XmlDocPtrWrapper;
20
21 bool is_associative_array(HashTable *ht)
22 {
23         if (ht->nNumOfElements == 0) {
24                 return true;
25         }
26         for (unsigned i = 0; i < ht->nNumOfElements; ++i) {
27                 if (!zend_hash_index_exists(ht, i)) {
28                         return true;
29                 }
30         }
31         return false;
32 }
33
34 Directive* convert_php_objects_to_directive(zval *obj)
35 {
36         switch (Z_TYPE_P(obj)) {
37         case IS_ARRAY: {
38                 HashTable *ht = Z_ARRVAL_P(obj);
39                 if (is_associative_array(ht)) {
40                         std::unordered_map<std::string, Directive *> my_map;
41                         for (zend_hash_internal_pointer_reset(ht); zend_hash_has_more_elements(ht) == SUCCESS; zend_hash_move_forward(ht)) {
42                                 char *str_key;
43                                 ulong num_key;
44                                 zend_hash_get_current_key(ht, &str_key, &num_key, 0);
45
46                                 std::string key;
47                                 if (zend_hash_get_current_key_type(ht) == HASH_KEY_IS_STRING) {
48                                         key = str_key;
49                                 } else {
50                                         char buf[32];
51                                         sprintf(buf, "%lu", num_key);
52                                         key = buf;
53                                 }
54
55                                 zval **data;
56                                 zend_hash_get_current_data(ht, (void **)&data);
57                                 my_map.insert(make_pair(key, convert_php_objects_to_directive(*data)));
58                         }
59                         return new Substitute(my_map);
60                 } else {
61                         std::vector<Directive *> subdirectives;
62                         for (unsigned i = 0; i < ht->nNumOfElements; ++i) {
63                                 zval **data;
64                                 zend_hash_index_find(ht, i, (void **)&data);
65                                 subdirectives.push_back(convert_php_objects_to_directive(*data));
66                         }
67                         return new Clone(subdirectives);
68                 }
69                 break;
70         }
71         case IS_STRING: {
72                 char *str = Z_STRVAL_P(obj);
73                 return new Replace(str);
74         }
75         case IS_RESOURCE: {
76                 XmlDocPtrWrapper *doc;
77                 if (SWIG_ConvertPtr(obj, (void **)&doc, SWIGTYPE_p_XmlDocPtrWrapper, 0) < 0 || doc == NULL) {
78                         return NULL;
79                 }
80                 return new ReplaceInclude(xmlCopyDoc((*doc)->ptr, 1));
81         }
82         case IS_NULL:
83                 return new Replace { "" };
84         default:
85                 printf("WARNING: Unknown type %d!\n", Z_TYPE_P(obj));
86                 break;
87         }
88
89         return NULL;
90 }
91
92 XmlDocPtrWrapper XML_Template_process_file(const std::string &input_filename, Directive *root_directive, bool clean)
93 {
94         xmlDocPtr ret = process_file(input_filename, root_directive, clean);
95         delete root_directive;
96         return XmlDocPtrWrapper(new XmlDocWrapper { ret });
97 }
98
99 void XML_Template_process(XmlDocPtrWrapper doc, Directive *root_directive, bool clean)
100 {
101         root_directive->process(xmlDocGetRootElement(doc->ptr), clean);
102         delete root_directive;
103 }
104
105 namespace {
106
107 int write_to_string(void *context, const char *buffer, int len)
108 {
109         std::string *str = reinterpret_cast<std::string *>(context);
110         str->append(buffer, len);
111         return len;
112 }
113
114 int close_string(void *context)
115 {
116         return 0;
117 }
118
119 }  // namespace
120
121 std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc, bool prettyprint)
122 {
123         xmlIndentTreeOutput = prettyprint;
124         std::string ret;
125         xmlOutputBufferPtr buf = xmlOutputBufferCreateIO(write_to_string, close_string, &ret, NULL);
126         xmlSaveFormatFileTo(buf, doc->ptr, "UTF-8", prettyprint);
127         return ret;
128 }
129
130 namespace {
131
132 // Remove document fragments (ie. move their content up in the parent node)
133 // and combine neighboring text nodes into one.
134 void normalize_node(xmlNodePtr node)
135 {
136         xmlNode *next_child;
137         for (xmlNode *child = node->children; child != NULL; child = next_child) {
138                 next_child = child->next;
139                 if (child->type == XML_DOCUMENT_FRAG_NODE) {
140                         while (child->children != NULL) {
141                                 xmlAddPrevSibling(child, child->children);
142                         }
143
144                         xmlUnlinkNode(child);
145                         xmlFreeNode(child);
146                 }
147         }
148
149         // xmlAddPrevSibling merges adjacent text nodes, but many other things
150         // (including xmlUnlinkNode) do not, so make an extra pass.
151         for (xmlNode *child = node->children; child != NULL; child = child->next) {
152                 while (child->type == XML_TEXT_NODE && (child->next != NULL && child->next->type == XML_TEXT_NODE)) {
153                         xmlNode *next_child = child->next;
154
155                         xmlChar *content = xmlNodeGetContent(next_child);
156                         xmlNodeAddContent(child, content);
157                         xmlFree(content);
158
159                         xmlUnlinkNode(next_child);
160                         xmlFreeNode(next_child);
161                 }
162                 normalize_node(child);
163         }
164 }
165
166 // Clean the page of non-necessary whitespace. Leaves whitespace alone if and
167 // only if xml:space="preserve" on the element. (IOW, it doesn't parse the DTDs,
168 // nor the CSS.)
169 void clean_node(xmlNodePtr node, bool preserve_whitespace, bool aggressive)
170 {
171         if (node->type == XML_TEXT_NODE) {
172                 std::string content = reinterpret_cast<const char *>(xmlNodeGetContent(node));
173                 if (!preserve_whitespace) {
174                         unsigned dstpos = 0;
175                         for (unsigned srcpos = 0; srcpos < content.size(); ++srcpos, ++dstpos) {
176                                 if (content[srcpos] == '\n' ||
177                                     content[srcpos] == '\t' ||
178                                     content[srcpos] == ' ') {
179                                         content[dstpos] = ' ';
180
181                                         // compress double spaces
182                                         if (dstpos > 0 && content[dstpos - 1] == ' ') {
183                                                 --dstpos;
184                                         }
185                                 } else {
186                                         content[dstpos] = content[srcpos];
187                                 }
188                         }
189                         content.resize(dstpos);
190                 }
191                 if (content.empty() || (aggressive && content == " ")) {
192                         xmlUnlinkNode(node);
193                         xmlFreeNode(node);
194                 } else {
195                         xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(content.data()), content.size());
196                 }
197         } else {
198                 if (node->type == XML_ELEMENT_NODE) {
199                         xmlChar *space = xmlGetProp(node, reinterpret_cast<const xmlChar *>("xml:space"));
200                         preserve_whitespace = (space != NULL && strcmp(reinterpret_cast<const char *>(space), "preserve") == 0);
201                 }
202
203                 xmlNode *next_child;
204                 for (xmlNode *child = node->children; child != NULL; child = next_child) {
205                         next_child = child->next;
206                         clean_node(child, preserve_whitespace, aggressive);
207                 }
208
209                 if (node->type == XML_ELEMENT_NODE && node->children == NULL) {
210                         std::string tag = reinterpret_cast<const char *>(node->name);
211
212                         // These are the only elements allowed in XHTML to be EMPTY,
213                         // so insert dummy nodes to prevent the output from using
214                         // the <foo/> syntax where not appropriate.
215                         if (tag != "base" && tag != "meta" && tag != "link" && tag != "hr" &&
216                             tag != "br" && tag != "param" && tag != "img" && tag != "area" &&
217                             tag != "input" && tag != "col") {
218                                 xmlNode *text = xmlNewText(reinterpret_cast<const xmlChar *>(""));
219                                 xmlAddChild(node, text);
220                         }
221                 }
222         }
223 }
224
225 }  // namespace
226
227 void XML_Template_clean_whitespace(XmlDocPtrWrapper doc, bool aggressive)
228 {
229         normalize_node(xmlDocGetRootElement(doc->ptr));
230         clean_node(xmlDocGetRootElement(doc->ptr), false, aggressive);
231 }
232
233 %}
234
235 %typemap(in) Directive* {
236         $1 = convert_php_objects_to_directive(*$input);
237 }
238
239 XmlDocPtrWrapper XML_Template_process_file(const std::string &input_filename, Directive *root_directive, bool clean);
240 void XML_Template_process(XmlDocPtrWrapper doc, Directive *root_directive, bool clean);
241 void XML_Template_clean_whitespace(XmlDocPtrWrapper doc, bool aggressive);
242 std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc, bool prettyprint);
243