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);
77 snprintf(str, sizeof(str), "%ld", Z_LVAL_P(obj));
78 return new Replace(str);
82 snprintf(str, sizeof(str), "%f", Z_DVAL_P(obj));
83 return new Replace(str);
86 XmlDocPtrWrapper *doc;
87 if (SWIG_ConvertPtr(obj, (void **)&doc, SWIGTYPE_p_XmlDocPtrWrapper, 0) < 0 || doc == NULL) {
90 return new ReplaceInclude(xmlCopyDoc((*doc)->ptr, 1));
93 return new Replace { "" };
95 printf("WARNING: Unknown type %d!\n", Z_TYPE_P(obj));
102 XmlDocPtrWrapper XML_Template_process_file(const std::string &input_filename, Directive *root_directive, bool clean)
104 xmlDocPtr ret = process_file(input_filename, root_directive, clean);
105 delete root_directive;
106 return XmlDocPtrWrapper(new XmlDocWrapper { ret });
109 void XML_Template_process(XmlDocPtrWrapper doc, Directive *root_directive, bool clean)
111 root_directive->process(xmlDocGetRootElement(doc->ptr), clean);
112 delete root_directive;
117 int write_to_string(void *context, const char *buffer, int len)
119 std::string *str = reinterpret_cast<std::string *>(context);
120 str->append(buffer, len);
124 int close_string(void *context)
131 std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc, bool prettyprint)
133 xmlIndentTreeOutput = prettyprint;
135 xmlOutputBufferPtr buf = xmlOutputBufferCreateIO(write_to_string, close_string, &ret, NULL);
136 xmlSaveFormatFileTo(buf, doc->ptr, "UTF-8", prettyprint);
142 // Remove document fragments (ie. move their content up in the parent node)
143 // and combine neighboring text nodes into one.
144 void normalize_node(xmlNodePtr node)
147 for (xmlNode *child = node->children; child != NULL; child = next_child) {
148 next_child = child->next;
149 if (child->type == XML_DOCUMENT_FRAG_NODE) {
150 while (child->children != NULL) {
151 xmlAddPrevSibling(child, child->children);
154 xmlUnlinkNode(child);
159 // xmlAddPrevSibling merges adjacent text nodes, but many other things
160 // (including xmlUnlinkNode) do not, so make an extra pass.
161 for (xmlNode *child = node->children; child != NULL; child = child->next) {
162 while (child->type == XML_TEXT_NODE && (child->next != NULL && child->next->type == XML_TEXT_NODE)) {
163 xmlNode *next_child = child->next;
165 xmlChar *content = xmlNodeGetContent(next_child);
166 xmlNodeAddContent(child, content);
169 xmlUnlinkNode(next_child);
170 xmlFreeNode(next_child);
172 normalize_node(child);
176 // Clean the page of non-necessary whitespace. Leaves whitespace alone if and
177 // only if xml:space="preserve" on the element. (IOW, it doesn't parse the DTDs,
179 void clean_node(xmlNodePtr node, bool preserve_whitespace, bool aggressive)
181 if (node->type == XML_TEXT_NODE) {
182 std::string content = reinterpret_cast<const char *>(xmlNodeGetContent(node));
183 if (!preserve_whitespace) {
185 for (unsigned srcpos = 0; srcpos < content.size(); ++srcpos, ++dstpos) {
186 if (content[srcpos] == '\n' ||
187 content[srcpos] == '\t' ||
188 content[srcpos] == ' ') {
189 content[dstpos] = ' ';
191 // compress double spaces
192 if (dstpos > 0 && content[dstpos - 1] == ' ') {
196 content[dstpos] = content[srcpos];
199 content.resize(dstpos);
201 if (content.empty() || (aggressive && content == " ")) {
205 xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(content.data()), content.size());
208 if (node->type == XML_ELEMENT_NODE) {
209 xmlChar *space = xmlGetProp(node, reinterpret_cast<const xmlChar *>("xml:space"));
210 preserve_whitespace = (space != NULL && strcmp(reinterpret_cast<const char *>(space), "preserve") == 0);
214 for (xmlNode *child = node->children; child != NULL; child = next_child) {
215 next_child = child->next;
216 clean_node(child, preserve_whitespace, aggressive);
219 if (node->type == XML_ELEMENT_NODE && node->children == NULL) {
220 std::string tag = reinterpret_cast<const char *>(node->name);
222 // These are the only elements allowed in XHTML to be EMPTY,
223 // so insert dummy nodes to prevent the output from using
224 // the <foo/> syntax where not appropriate.
225 if (tag != "base" && tag != "meta" && tag != "link" && tag != "hr" &&
226 tag != "br" && tag != "param" && tag != "img" && tag != "area" &&
227 tag != "input" && tag != "col") {
228 xmlNode *text = xmlNewText(reinterpret_cast<const xmlChar *>(""));
229 xmlAddChild(node, text);
237 void XML_Template_clean_whitespace(XmlDocPtrWrapper doc, bool aggressive)
239 normalize_node(xmlDocGetRootElement(doc->ptr));
240 clean_node(xmlDocGetRootElement(doc->ptr), false, aggressive);
245 %typemap(in) Directive* {
246 $1 = convert_php_objects_to_directive(*$input);
249 XmlDocPtrWrapper XML_Template_process_file(const std::string &input_filename, Directive *root_directive, bool clean);
250 void XML_Template_process(XmlDocPtrWrapper doc, Directive *root_directive, bool clean);
251 void XML_Template_clean_whitespace(XmlDocPtrWrapper doc, bool aggressive);
252 std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc, bool prettyprint);