/** * htmlBufNodeDumpFormat: * @buf: the xmlBufPtr output * @doc: the document * @cur: the current node * @format: should formatting spaces been added * * Dump an HTML node, recursive behaviour,children are printed too. * * Returns the number of byte written or -1 in case of error */ static size_t htmlBufNodeDumpFormat(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int format) { size_t use; int ret; xmlOutputBufferPtr outbuf; if (cur == NULL) { return (-1); } if (buf == NULL) { return (-1); } outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); if (outbuf == NULL) { htmlSaveErrMemory("allocating HTML output buffer"); return (-1); } memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer)); outbuf->buffer = buf; outbuf->encoder = NULL; outbuf->writecallback = NULL; outbuf->closecallback = NULL; outbuf->context = NULL; outbuf->written = 0; use = xmlBufUse(buf); htmlNodeDumpFormatOutput(outbuf, doc, cur, NULL, format); xmlFree(outbuf); ret = xmlBufUse(buf) - use; return (ret); }
int xsltSaveResultToString(xmlChar **doc_txt_ptr, int * doc_txt_len, xmlDocPtr result, xsltStylesheetPtr style) { xmlOutputBufferPtr buf; *doc_txt_ptr = NULL; *doc_txt_len = 0; if (result->children == NULL) return(0); buf = xmlAllocOutputBuffer(NULL); if (buf == NULL) return(-1); xsltSaveResultTo(buf, result, style); if (buf->conv != NULL) { *doc_txt_len = xmlBufUse (buf->conv); *doc_txt_ptr = xmlStrndup (xmlBufContent (buf->conv), *doc_txt_len); } else { *doc_txt_len = xmlBufUse (buf->buffer); *doc_txt_ptr = xmlStrndup (xmlBufContent (buf->buffer), *doc_txt_len); } (void)xmlOutputBufferClose(buf); return 0; }
String::C xdoc2buf(Request& r, VXdoc& vdoc, XDocOutputOptions& oo, const String* file_spec, bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) { Charset* render=0; Charset* header=0; if(use_source_charset_to_render_and_client_charset_to_write_to_header) { render=&r.charsets.source(); header=&r.charsets.client(); } else { header=render=&charsets.get(oo.encoding->change_case(r.charsets.source(), String::CC_UPPER)); } const char* render_encoding=render->NAME_CSTR(); const char* header_encoding=header->NAME_CSTR(); xmlCharEncodingHandler *renderer=xmlFindCharEncodingHandler(render_encoding); // UTF-8 renderer contains empty input/output converters, // which is wrong for xmlOutputBufferCreateIO // while zero renderer goes perfectly if(render->isUTF8()) renderer=0; xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer)); xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet()); if(!stylesheet.get()) throw Exception(0, 0, "xsltNewStylesheet failed"); #define OOSTRING2STYLE(name) \ stylesheet->name=oo.name?BAD_CAST xmlMemStrdup((const char*)r.transcode(*oo.name)):0 #define OOBOOL2STYLE(name) \ if(oo.name>=0) stylesheet->name=oo.name OOSTRING2STYLE(method); OOSTRING2STYLE(encoding); OOSTRING2STYLE(mediaType); // OOSTRING2STYLE(doctypeSystem); // OOSTRING2STYLE(doctypePublic); OOBOOL2STYLE(indent); OOSTRING2STYLE(version); OOBOOL2STYLE(standalone); OOBOOL2STYLE(omitXmlDeclaration); xmlDoc& xmldoc=vdoc.get_xmldoc(); xmldoc.encoding=BAD_CAST xmlMemStrdup(render_encoding); if(header_encoding) stylesheet->encoding=BAD_CAST xmlMemStrdup(header_encoding); if(xsltSaveResultTo(outputBuffer.get(), &xmldoc, stylesheet.get())<0 || xmlHaveGenericErrors()) throw XmlException(0, r); // write out result char *gnome_str; size_t gnome_length; #ifdef LIBXML2_NEW_BUFFER if(outputBuffer->conv) { gnome_length=xmlBufUse(outputBuffer->conv); gnome_str=(char *)xmlBufContent(outputBuffer->conv); } else { gnome_length=xmlOutputBufferGetSize(&(*outputBuffer)); gnome_str=(char *)xmlOutputBufferGetContent(&(*outputBuffer)); } #else if(outputBuffer->conv) { gnome_length=outputBuffer->conv->use; gnome_str=(char *)outputBuffer->conv->content; } else { gnome_length=outputBuffer->buffer->use; gnome_str=(char *)outputBuffer->buffer->content; } #endif if(file_spec){ file_write(r.charsets, *file_spec, gnome_str, gnome_length, true/*as_text*/); return String::C(); // actually, we don't need this output at all } else return String::C(gnome_length ? pa_strdup(gnome_str, gnome_length) : 0, gnome_length); }
/** * htmlDocDumpMemoryFormat: * @cur: the document * @mem: OUT: the memory pointer * @size: OUT: the memory length * @format: should formatting spaces been added * * Dump an HTML document in memory and return the xmlChar * and it's size. * It's up to the caller to free the memory. */ void htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) { xmlOutputBufferPtr buf; xmlCharEncodingHandlerPtr handler = NULL; const char *encoding; xmlInitParser(); if ((mem == NULL) || (size == NULL)) return; if (cur == NULL) { *mem = NULL; *size = 0; return; } encoding = (const char *) htmlGetMetaEncoding(cur); if (encoding != NULL) { xmlCharEncoding enc; enc = xmlParseCharEncoding(encoding); if (enc != cur->charset) { if (cur->charset != XML_CHAR_ENCODING_UTF8) { /* * Not supported yet */ *mem = NULL; *size = 0; return; } handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding); } else { handler = xmlFindCharEncodingHandler(encoding); } } /* * Fallback to HTML or ASCII when the encoding is unspecified */ if (handler == NULL) handler = xmlFindCharEncodingHandler("HTML"); if (handler == NULL) handler = xmlFindCharEncodingHandler("ascii"); buf = xmlAllocOutputBufferInternal(handler); if (buf == NULL) { *mem = NULL; *size = 0; return; } htmlDocContentDumpFormatOutput(buf, cur, NULL, format); xmlOutputBufferFlush(buf); if (buf->conv != NULL) { *size = xmlBufUse(buf->conv); *mem = xmlStrndup(xmlBufContent(buf->conv), *size); } else { *size = xmlBufUse(buf->buffer); *mem = xmlStrndup(xmlBufContent(buf->buffer), *size); } (void)xmlOutputBufferClose(buf); }