1 %module XML_Template_SWIG
2 %include <std_string.i>
4 struct XmlDocPtrWrapper {
11 #include <libxml/globals.h>
13 #include "../c++11/xml-template.h"
15 struct XmlDocWrapper {
16 ~XmlDocWrapper() { xmlFreeDoc(ptr); }
19 typedef std::shared_ptr<XmlDocWrapper> XmlDocPtrWrapper;
21 bool is_associative_array(HashTable *ht)
23 if (ht->nNumOfElements == 0) {
26 for (unsigned i = 0; i < ht->nNumOfElements; ++i) {
27 if (!zend_hash_index_exists(ht, i)) {
34 Directive* convert_php_objects_to_directive(zval *obj)
36 switch (Z_TYPE_P(obj)) {
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)) {
44 zend_hash_get_current_key(ht, &str_key, &num_key, 0);
47 if (zend_hash_get_current_key_type(ht) == HASH_KEY_IS_STRING) {
51 sprintf(buf, "%lu", num_key);
56 zend_hash_get_current_data(ht, (void **)&data);
57 my_map.insert(make_pair(key, convert_php_objects_to_directive(*data)));
59 return new Substitute(my_map);
61 std::vector<Directive *> subdirectives;
62 for (unsigned i = 0; i < ht->nNumOfElements; ++i) {
64 zend_hash_index_find(ht, i, (void **)&data);
65 subdirectives.push_back(convert_php_objects_to_directive(*data));
67 return new Clone(subdirectives);
72 char *str = Z_STRVAL_P(obj);
73 return new Replace(str);
76 XmlDocPtrWrapper *doc;
77 if (SWIG_ConvertPtr(obj, (void **)&doc, SWIGTYPE_p_XmlDocPtrWrapper, 0) < 0 || doc == NULL) {
80 return new ReplaceInclude(xmlCopyDoc((*doc)->ptr, 1));
83 return new Replace { "" };
85 printf("WARNING: Unknown type %d!\n", Z_TYPE_P(obj));
92 XmlDocPtrWrapper XML_Template_process_file(const std::string &input_filename, Directive *root_directive, bool clean)
94 xmlDocPtr ret = process_file(input_filename, root_directive, clean);
95 delete root_directive;
96 return XmlDocPtrWrapper(new XmlDocWrapper { ret });
99 void XML_Template_process(XmlDocPtrWrapper doc, Directive *root_directive, bool clean)
101 root_directive->process(xmlDocGetRootElement(doc->ptr), clean);
102 delete root_directive;
107 int write_to_string(void *context, const char *buffer, int len)
109 std::string *str = reinterpret_cast<std::string *>(context);
110 str->append(buffer, len);
114 int close_string(void *context)
121 std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc, bool prettyprint)
123 xmlIndentTreeOutput = prettyprint;
125 xmlOutputBufferPtr buf = xmlOutputBufferCreateIO(write_to_string, close_string, &ret, NULL);
126 xmlSaveFormatFileTo(buf, doc->ptr, "UTF-8", prettyprint);
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)
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);
144 xmlUnlinkNode(child);
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;
155 xmlChar *content = xmlNodeGetContent(next_child);
156 xmlNodeAddContent(child, content);
159 xmlUnlinkNode(next_child);
160 xmlFreeNode(next_child);
162 normalize_node(child);
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,
169 void clean_node(xmlNodePtr node, bool preserve_whitespace, bool aggressive)
171 if (node->type == XML_TEXT_NODE) {
172 std::string content = reinterpret_cast<const char *>(xmlNodeGetContent(node));
173 if (!preserve_whitespace) {
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] = ' ';
181 // compress double spaces
182 if (dstpos > 0 && content[dstpos - 1] == ' ') {
186 content[dstpos] = content[srcpos];
189 content.resize(dstpos);
191 if (content.empty() || (aggressive && content == " ")) {
195 xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(content.data()), content.size());
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);
204 for (xmlNode *child = node->children; child != NULL; child = next_child) {
205 next_child = child->next;
206 clean_node(child, preserve_whitespace, aggressive);
209 if (node->type == XML_ELEMENT_NODE && node->children == NULL) {
210 std::string tag = reinterpret_cast<const char *>(node->name);
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);
227 void XML_Template_clean_whitespace(XmlDocPtrWrapper doc, bool aggressive)
229 normalize_node(xmlDocGetRootElement(doc->ptr));
230 clean_node(xmlDocGetRootElement(doc->ptr), false, aggressive);
235 %typemap(in) Directive* {
236 $1 = convert_php_objects_to_directive(*$input);
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);