Blame xmlsave.c

Packit 423ecb
/*
Packit 423ecb
 * xmlsave.c: Implemetation of the document serializer
Packit 423ecb
 *
Packit 423ecb
 * See Copyright for the status of this software.
Packit 423ecb
 *
Packit 423ecb
 * daniel@veillard.com
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#define IN_LIBXML
Packit 423ecb
#include "libxml.h"
Packit 423ecb
Packit 423ecb
#include <string.h>
Packit 423ecb
#include <libxml/xmlmemory.h>
Packit 423ecb
#include <libxml/parserInternals.h>
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/xmlsave.h>
Packit 423ecb
Packit 423ecb
#define MAX_INDENT 60
Packit 423ecb
Packit 423ecb
#include <libxml/HTMLtree.h>
Packit 423ecb
Packit 423ecb
#include "buf.h"
Packit 423ecb
#include "enc.h"
Packit 423ecb
#include "save.h"
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			XHTML detection					*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
Packit 423ecb
   "-//W3C//DTD XHTML 1.0 Strict//EN"
Packit 423ecb
#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
Packit 423ecb
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
Packit 423ecb
#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
Packit 423ecb
   "-//W3C//DTD XHTML 1.0 Frameset//EN"
Packit 423ecb
#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
Packit 423ecb
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
Packit 423ecb
#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
Packit 423ecb
   "-//W3C//DTD XHTML 1.0 Transitional//EN"
Packit 423ecb
#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
Packit 423ecb
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
Packit 423ecb
Packit 423ecb
#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
Packit 423ecb
/**
Packit 423ecb
 * xmlIsXHTML:
Packit 423ecb
 * @systemID:  the system identifier
Packit 423ecb
 * @publicID:  the public identifier
Packit 423ecb
 *
Packit 423ecb
 * Try to find if the document correspond to an XHTML DTD
Packit 423ecb
 *
Packit 423ecb
 * Returns 1 if true, 0 if not and -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
Packit 423ecb
    if ((systemID == NULL) && (publicID == NULL))
Packit 423ecb
	return(-1);
Packit 423ecb
    if (publicID != NULL) {
Packit 423ecb
	if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
Packit 423ecb
	if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
Packit 423ecb
	if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
Packit 423ecb
    }
Packit 423ecb
    if (systemID != NULL) {
Packit 423ecb
	if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
Packit 423ecb
	if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
Packit 423ecb
	if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
Packit 423ecb
    }
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
Packit 423ecb
#define TODO								\
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,				\
Packit 423ecb
	    "Unimplemented block at %s:%d\n",				\
Packit 423ecb
            __FILE__, __LINE__);
Packit 423ecb
Packit 423ecb
struct _xmlSaveCtxt {
Packit 423ecb
    void *_private;
Packit 423ecb
    int type;
Packit 423ecb
    int fd;
Packit 423ecb
    const xmlChar *filename;
Packit 423ecb
    const xmlChar *encoding;
Packit 423ecb
    xmlCharEncodingHandlerPtr handler;
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
    int options;
Packit 423ecb
    int level;
Packit 423ecb
    int format;
Packit 423ecb
    char indent[MAX_INDENT + 1];	/* array for indenting output */
Packit 423ecb
    int indent_nr;
Packit 423ecb
    int indent_size;
Packit 423ecb
    xmlCharEncodingOutputFunc escape;	/* used for element content */
Packit 423ecb
    xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */
Packit 423ecb
};
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			Output error handlers				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveErrMemory:
Packit 423ecb
 * @extra:  extra informations
Packit 423ecb
 *
Packit 423ecb
 * Handle an out of memory condition
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlSaveErrMemory(const char *extra)
Packit 423ecb
{
Packit 423ecb
    __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveErr:
Packit 423ecb
 * @code:  the error number
Packit 423ecb
 * @node:  the location of the error.
Packit 423ecb
 * @extra:  extra informations
Packit 423ecb
 *
Packit 423ecb
 * Handle an out of memory condition
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlSaveErr(int code, xmlNodePtr node, const char *extra)
Packit 423ecb
{
Packit 423ecb
    const char *msg = NULL;
Packit 423ecb
Packit 423ecb
    switch(code) {
Packit 423ecb
        case XML_SAVE_NOT_UTF8:
Packit 423ecb
	    msg = "string is not in UTF-8\n";
Packit 423ecb
	    break;
Packit 423ecb
	case XML_SAVE_CHAR_INVALID:
Packit 423ecb
	    msg = "invalid character value\n";
Packit 423ecb
	    break;
Packit 423ecb
	case XML_SAVE_UNKNOWN_ENCODING:
Packit 423ecb
	    msg = "unknown encoding %s\n";
Packit 423ecb
	    break;
Packit 423ecb
	case XML_SAVE_NO_DOCTYPE:
Packit 423ecb
	    msg = "document has no DOCTYPE\n";
Packit 423ecb
	    break;
Packit 423ecb
	default:
Packit 423ecb
	    msg = "unexpected error number\n";
Packit 423ecb
    }
Packit 423ecb
    __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			Special escaping routines			*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
static unsigned char *
Packit 423ecb
xmlSerializeHexCharRef(unsigned char *out, int val) {
Packit 423ecb
    unsigned char *ptr;
Packit 423ecb
Packit 423ecb
    *out++ = '&';
Packit 423ecb
    *out++ = '#';
Packit 423ecb
    *out++ = 'x';
Packit 423ecb
    if (val < 0x10) ptr = out;
Packit 423ecb
    else if (val < 0x100) ptr = out + 1;
Packit 423ecb
    else if (val < 0x1000) ptr = out + 2;
Packit 423ecb
    else if (val < 0x10000) ptr = out + 3;
Packit 423ecb
    else if (val < 0x100000) ptr = out + 4;
Packit 423ecb
    else ptr = out + 5;
Packit 423ecb
    out = ptr + 1;
Packit 423ecb
    while (val > 0) {
Packit 423ecb
	switch (val & 0xF) {
Packit 423ecb
	    case 0: *ptr-- = '0'; break;
Packit 423ecb
	    case 1: *ptr-- = '1'; break;
Packit 423ecb
	    case 2: *ptr-- = '2'; break;
Packit 423ecb
	    case 3: *ptr-- = '3'; break;
Packit 423ecb
	    case 4: *ptr-- = '4'; break;
Packit 423ecb
	    case 5: *ptr-- = '5'; break;
Packit 423ecb
	    case 6: *ptr-- = '6'; break;
Packit 423ecb
	    case 7: *ptr-- = '7'; break;
Packit 423ecb
	    case 8: *ptr-- = '8'; break;
Packit 423ecb
	    case 9: *ptr-- = '9'; break;
Packit 423ecb
	    case 0xA: *ptr-- = 'A'; break;
Packit 423ecb
	    case 0xB: *ptr-- = 'B'; break;
Packit 423ecb
	    case 0xC: *ptr-- = 'C'; break;
Packit 423ecb
	    case 0xD: *ptr-- = 'D'; break;
Packit 423ecb
	    case 0xE: *ptr-- = 'E'; break;
Packit 423ecb
	    case 0xF: *ptr-- = 'F'; break;
Packit 423ecb
	    default: *ptr-- = '0'; break;
Packit 423ecb
	}
Packit 423ecb
	val >>= 4;
Packit 423ecb
    }
Packit 423ecb
    *out++ = ';';
Packit 423ecb
    *out = 0;
Packit 423ecb
    return(out);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlEscapeEntities:
Packit 423ecb
 * @out:  a pointer to an array of bytes to store the result
Packit 423ecb
 * @outlen:  the length of @out
Packit 423ecb
 * @in:  a pointer to an array of unescaped UTF-8 bytes
Packit 423ecb
 * @inlen:  the length of @in
Packit 423ecb
 *
Packit 423ecb
 * Take a block of UTF-8 chars in and escape them. Used when there is no
Packit 423ecb
 * encoding specified.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if success, or -1 otherwise
Packit 423ecb
 * The value of @inlen after return is the number of octets consumed
Packit 423ecb
 *     if the return value is positive, else unpredictable.
Packit 423ecb
 * The value of @outlen after return is the number of octets consumed.
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlEscapeEntities(unsigned char* out, int *outlen,
Packit 423ecb
                 const xmlChar* in, int *inlen) {
Packit 423ecb
    unsigned char* outstart = out;
Packit 423ecb
    const unsigned char* base = in;
Packit 423ecb
    unsigned char* outend = out + *outlen;
Packit 423ecb
    const unsigned char* inend;
Packit 423ecb
    int val;
Packit 423ecb
Packit 423ecb
    inend = in + (*inlen);
Packit 423ecb
Packit 423ecb
    while ((in < inend) && (out < outend)) {
Packit 423ecb
	if (*in == '<') {
Packit 423ecb
	    if (outend - out < 4) break;
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'l';
Packit 423ecb
	    *out++ = 't';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	    in++;
Packit 423ecb
	    continue;
Packit 423ecb
	} else if (*in == '>') {
Packit 423ecb
	    if (outend - out < 4) break;
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'g';
Packit 423ecb
	    *out++ = 't';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	    in++;
Packit 423ecb
	    continue;
Packit 423ecb
	} else if (*in == '&') {
Packit 423ecb
	    if (outend - out < 5) break;
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'a';
Packit 423ecb
	    *out++ = 'm';
Packit 423ecb
	    *out++ = 'p';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	    in++;
Packit 423ecb
	    continue;
Packit 423ecb
	} else if (((*in >= 0x20) && (*in < 0x80)) ||
Packit 423ecb
	           (*in == '\n') || (*in == '\t')) {
Packit 423ecb
	    /*
Packit 423ecb
	     * default case, just copy !
Packit 423ecb
	     */
Packit 423ecb
	    *out++ = *in++;
Packit 423ecb
	    continue;
Packit 423ecb
	} else if (*in >= 0x80) {
Packit 423ecb
	    /*
Packit 423ecb
	     * We assume we have UTF-8 input.
Packit 423ecb
	     */
Packit 423ecb
	    if (outend - out < 11) break;
Packit 423ecb
Packit 423ecb
	    if (*in < 0xC0) {
Packit 423ecb
		xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL);
Packit 423ecb
		in++;
Packit 423ecb
		goto error;
Packit 423ecb
	    } else if (*in < 0xE0) {
Packit 423ecb
		if (inend - in < 2) break;
Packit 423ecb
		val = (in[0]) & 0x1F;
Packit 423ecb
		val <<= 6;
Packit 423ecb
		val |= (in[1]) & 0x3F;
Packit 423ecb
		in += 2;
Packit 423ecb
	    } else if (*in < 0xF0) {
Packit 423ecb
		if (inend - in < 3) break;
Packit 423ecb
		val = (in[0]) & 0x0F;
Packit 423ecb
		val <<= 6;
Packit 423ecb
		val |= (in[1]) & 0x3F;
Packit 423ecb
		val <<= 6;
Packit 423ecb
		val |= (in[2]) & 0x3F;
Packit 423ecb
		in += 3;
Packit 423ecb
	    } else if (*in < 0xF8) {
Packit 423ecb
		if (inend - in < 4) break;
Packit 423ecb
		val = (in[0]) & 0x07;
Packit 423ecb
		val <<= 6;
Packit 423ecb
		val |= (in[1]) & 0x3F;
Packit 423ecb
		val <<= 6;
Packit 423ecb
		val |= (in[2]) & 0x3F;
Packit 423ecb
		val <<= 6;
Packit 423ecb
		val |= (in[3]) & 0x3F;
Packit 423ecb
		in += 4;
Packit 423ecb
	    } else {
Packit 423ecb
		xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Packit 423ecb
		in++;
Packit 423ecb
		goto error;
Packit 423ecb
	    }
Packit 423ecb
	    if (!IS_CHAR(val)) {
Packit 423ecb
		xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Packit 423ecb
		in++;
Packit 423ecb
		goto error;
Packit 423ecb
	    }
Packit 423ecb
Packit 423ecb
	    /*
Packit 423ecb
	     * We could do multiple things here. Just save as a char ref
Packit 423ecb
	     */
Packit 423ecb
	    out = xmlSerializeHexCharRef(out, val);
Packit 423ecb
	} else if (IS_BYTE_CHAR(*in)) {
Packit 423ecb
	    if (outend - out < 6) break;
Packit 423ecb
	    out = xmlSerializeHexCharRef(out, *in++);
Packit 423ecb
	} else {
Packit 423ecb
	    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlEscapeEntities : char out of range\n");
Packit 423ecb
	    in++;
Packit 423ecb
	    goto error;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    *outlen = out - outstart;
Packit 423ecb
    *inlen = in - base;
Packit 423ecb
    return(0);
Packit 423ecb
error:
Packit 423ecb
    *outlen = out - outstart;
Packit 423ecb
    *inlen = in - base;
Packit 423ecb
    return(-1);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			Allocation and deallocation			*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveCtxtInit:
Packit 423ecb
 * @ctxt: the saving context
Packit 423ecb
 *
Packit 423ecb
 * Initialize a saving context
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    int i;
Packit 423ecb
    int len;
Packit 423ecb
Packit 423ecb
    if (ctxt == NULL) return;
Packit 423ecb
    if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
Packit 423ecb
        ctxt->escape = xmlEscapeEntities;
Packit 423ecb
    len = xmlStrlen((xmlChar *)xmlTreeIndentString);
Packit 423ecb
    if ((xmlTreeIndentString == NULL) || (len == 0)) {
Packit 423ecb
        memset(&ctxt->indent[0], 0, MAX_INDENT + 1);
Packit 423ecb
    } else {
Packit 423ecb
	ctxt->indent_size = len;
Packit 423ecb
	ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
Packit 423ecb
	for (i = 0;i < ctxt->indent_nr;i++)
Packit 423ecb
	    memcpy(&ctxt->indent[i * ctxt->indent_size], xmlTreeIndentString,
Packit 423ecb
		   ctxt->indent_size);
Packit 423ecb
        ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if (xmlSaveNoEmptyTags) {
Packit 423ecb
	ctxt->options |= XML_SAVE_NO_EMPTY;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlFreeSaveCtxt:
Packit 423ecb
 *
Packit 423ecb
 * Free a saving context, destroying the ouptut in any remaining buffer
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    if (ctxt == NULL) return;
Packit 423ecb
    if (ctxt->encoding != NULL)
Packit 423ecb
        xmlFree((char *) ctxt->encoding);
Packit 423ecb
    if (ctxt->buf != NULL)
Packit 423ecb
        xmlOutputBufferClose(ctxt->buf);
Packit 423ecb
    xmlFree(ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNewSaveCtxt:
Packit 423ecb
 *
Packit 423ecb
 * Create a new saving context
Packit 423ecb
 *
Packit 423ecb
 * Returns the new structure or NULL in case of error
Packit 423ecb
 */
Packit 423ecb
static xmlSaveCtxtPtr
Packit 423ecb
xmlNewSaveCtxt(const char *encoding, int options)
Packit 423ecb
{
Packit 423ecb
    xmlSaveCtxtPtr ret;
Packit 423ecb
Packit 423ecb
    ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
Packit 423ecb
    if (ret == NULL) {
Packit 423ecb
	xmlSaveErrMemory("creating saving context");
Packit 423ecb
	return ( NULL );
Packit 423ecb
    }
Packit 423ecb
    memset(ret, 0, sizeof(xmlSaveCtxt));
Packit 423ecb
Packit 423ecb
    if (encoding != NULL) {
Packit 423ecb
        ret->handler = xmlFindCharEncodingHandler(encoding);
Packit 423ecb
	if (ret->handler == NULL) {
Packit 423ecb
	    xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
Packit 423ecb
            xmlFreeSaveCtxt(ret);
Packit 423ecb
	    return(NULL);
Packit 423ecb
	}
Packit 423ecb
        ret->encoding = xmlStrdup((const xmlChar *)encoding);
Packit 423ecb
	ret->escape = NULL;
Packit 423ecb
    }
Packit 423ecb
    xmlSaveCtxtInit(ret);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Use the options
Packit 423ecb
     */
Packit 423ecb
Packit 423ecb
    /* Re-check this option as it may already have been set */
Packit 423ecb
    if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) {
Packit 423ecb
	options |= XML_SAVE_NO_EMPTY;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    ret->options = options;
Packit 423ecb
    if (options & XML_SAVE_FORMAT)
Packit 423ecb
        ret->format = 1;
Packit 423ecb
    else if (options & XML_SAVE_WSNONSIG)
Packit 423ecb
        ret->format = 2;
Packit 423ecb
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Dumping XML tree content to a simple buffer		*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
/**
Packit 423ecb
 * xmlAttrSerializeContent:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @attr:  the attribute pointer
Packit 423ecb
 *
Packit 423ecb
 * Serialize the attribute in the buffer
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
Packit 423ecb
{
Packit 423ecb
    xmlNodePtr children;
Packit 423ecb
Packit 423ecb
    children = attr->children;
Packit 423ecb
    while (children != NULL) {
Packit 423ecb
        switch (children->type) {
Packit 423ecb
            case XML_TEXT_NODE:
Packit 423ecb
	        xmlBufAttrSerializeTxtContent(buf->buffer, attr->doc,
Packit 423ecb
		                              attr, children->content);
Packit 423ecb
		break;
Packit 423ecb
            case XML_ENTITY_REF_NODE:
Packit 423ecb
                xmlBufAdd(buf->buffer, BAD_CAST "&", 1);
Packit 423ecb
                xmlBufAdd(buf->buffer, children->name,
Packit 423ecb
                             xmlStrlen(children->name));
Packit 423ecb
                xmlBufAdd(buf->buffer, BAD_CAST ";", 1);
Packit 423ecb
                break;
Packit 423ecb
            default:
Packit 423ecb
                /* should not happen unless we have a badly built tree */
Packit 423ecb
                break;
Packit 423ecb
        }
Packit 423ecb
        children = children->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufDumpNotationTable:
Packit 423ecb
 * @buf:  an xmlBufPtr output
Packit 423ecb
 * @table:  A notation table
Packit 423ecb
 *
Packit 423ecb
 * This will dump the content of the notation table as an XML DTD definition
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlBufDumpNotationTable(xmlBufPtr buf, xmlNotationTablePtr table) {
Packit 423ecb
    xmlBufferPtr buffer;
Packit 423ecb
Packit 423ecb
    buffer = xmlBufferCreate();
Packit 423ecb
    if (buffer == NULL) {
Packit 423ecb
        /*
Packit 423ecb
         * TODO set the error in buf
Packit 423ecb
         */
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    xmlDumpNotationTable(buffer, table);
Packit 423ecb
    xmlBufMergeBuffer(buf, buffer);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufDumpElementDecl:
Packit 423ecb
 * @buf:  an xmlBufPtr output
Packit 423ecb
 * @elem:  An element table
Packit 423ecb
 *
Packit 423ecb
 * This will dump the content of the element declaration as an XML
Packit 423ecb
 * DTD definition
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlBufDumpElementDecl(xmlBufPtr buf, xmlElementPtr elem) {
Packit 423ecb
    xmlBufferPtr buffer;
Packit 423ecb
Packit 423ecb
    buffer = xmlBufferCreate();
Packit 423ecb
    if (buffer == NULL) {
Packit 423ecb
        /*
Packit 423ecb
         * TODO set the error in buf
Packit 423ecb
         */
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    xmlDumpElementDecl(buffer, elem);
Packit 423ecb
    xmlBufMergeBuffer(buf, buffer);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufDumpAttributeDecl:
Packit 423ecb
 * @buf:  an xmlBufPtr output
Packit 423ecb
 * @attr:  An attribute declaration
Packit 423ecb
 *
Packit 423ecb
 * This will dump the content of the attribute declaration as an XML
Packit 423ecb
 * DTD definition
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlBufDumpAttributeDecl(xmlBufPtr buf, xmlAttributePtr attr) {
Packit 423ecb
    xmlBufferPtr buffer;
Packit 423ecb
Packit 423ecb
    buffer = xmlBufferCreate();
Packit 423ecb
    if (buffer == NULL) {
Packit 423ecb
        /*
Packit 423ecb
         * TODO set the error in buf
Packit 423ecb
         */
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    xmlDumpAttributeDecl(buffer, attr);
Packit 423ecb
    xmlBufMergeBuffer(buf, buffer);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufDumpEntityDecl:
Packit 423ecb
 * @buf:  an xmlBufPtr output
Packit 423ecb
 * @ent:  An entity table
Packit 423ecb
 *
Packit 423ecb
 * This will dump the content of the entity table as an XML DTD definition
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlBufDumpEntityDecl(xmlBufPtr buf, xmlEntityPtr ent) {
Packit 423ecb
    xmlBufferPtr buffer;
Packit 423ecb
Packit 423ecb
    buffer = xmlBufferCreate();
Packit 423ecb
    if (buffer == NULL) {
Packit 423ecb
        /*
Packit 423ecb
         * TODO set the error in buf
Packit 423ecb
         */
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    xmlDumpEntityDecl(buffer, ent);
Packit 423ecb
    xmlBufMergeBuffer(buf, buffer);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Dumping XML tree content to an I/O output buffer	*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
static int xmlSaveSwitchEncoding(xmlSaveCtxtPtr ctxt, const char *encoding) {
Packit 423ecb
    xmlOutputBufferPtr buf = ctxt->buf;
Packit 423ecb
Packit 423ecb
    if ((encoding != NULL) && (buf->encoder == NULL) && (buf->conv == NULL)) {
Packit 423ecb
	buf->encoder = xmlFindCharEncodingHandler((const char *)encoding);
Packit 423ecb
	if (buf->encoder == NULL) {
Packit 423ecb
	    xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL,
Packit 423ecb
		       (const char *)encoding);
Packit 423ecb
	    return(-1);
Packit 423ecb
	}
Packit 423ecb
	buf->conv = xmlBufCreate();
Packit 423ecb
	if (buf->conv == NULL) {
Packit 423ecb
	    xmlCharEncCloseFunc(buf->encoder);
Packit 423ecb
	    xmlSaveErrMemory("creating encoding buffer");
Packit 423ecb
	    return(-1);
Packit 423ecb
	}
Packit 423ecb
	/*
Packit 423ecb
	 * initialize the state, e.g. if outputting a BOM
Packit 423ecb
	 */
Packit 423ecb
        xmlCharEncOutput(buf, 1);
Packit 423ecb
    }
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static int xmlSaveClearEncoding(xmlSaveCtxtPtr ctxt) {
Packit 423ecb
    xmlOutputBufferPtr buf = ctxt->buf;
Packit 423ecb
    xmlOutputBufferFlush(buf);
Packit 423ecb
    xmlCharEncCloseFunc(buf->encoder);
Packit 423ecb
    xmlBufFree(buf->conv);
Packit 423ecb
    buf->encoder = NULL;
Packit 423ecb
    buf->conv = NULL;
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
static void
Packit 423ecb
xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Packit 423ecb
#endif
Packit 423ecb
static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Packit 423ecb
static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Packit 423ecb
void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
Packit 423ecb
static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlOutputBufferWriteWSNonSig:
Packit 423ecb
 * @ctxt:  The save context
Packit 423ecb
 * @extra: Number of extra indents to apply to ctxt->level
Packit 423ecb
 *
Packit 423ecb
 * Write out formatting for non-significant whitespace output.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlOutputBufferWriteWSNonSig(xmlSaveCtxtPtr ctxt, int extra)
Packit 423ecb
{
Packit 423ecb
    int i;
Packit 423ecb
    if ((ctxt == NULL) || (ctxt->buf == NULL))
Packit 423ecb
        return;
Packit 423ecb
    xmlOutputBufferWrite(ctxt->buf, 1, "\n");
Packit 423ecb
    for (i = 0; i < (ctxt->level + extra); i += ctxt->indent_nr) {
Packit 423ecb
        xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size *
Packit 423ecb
                ((ctxt->level + extra - i) > ctxt->indent_nr ?
Packit 423ecb
                 ctxt->indent_nr : (ctxt->level + extra - i)),
Packit 423ecb
                ctxt->indent);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNsDumpOutput:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @cur:  a namespace
Packit 423ecb
 * @ctxt: the output save context. Optional.
Packit 423ecb
 *
Packit 423ecb
 * Dump a local Namespace definition.
Packit 423ecb
 * Should be called in the context of attributes dumps.
Packit 423ecb
 * If @ctxt is supplied, @buf should be its buffer.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur, xmlSaveCtxtPtr ctxt) {
Packit 423ecb
    if ((cur == NULL) || (buf == NULL)) return;
Packit 423ecb
    if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
Packit 423ecb
	if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
Packit 423ecb
	    return;
Packit 423ecb
Packit 423ecb
	if (ctxt != NULL && ctxt->format == 2)
Packit 423ecb
	    xmlOutputBufferWriteWSNonSig(ctxt, 2);
Packit 423ecb
	else
Packit 423ecb
	    xmlOutputBufferWrite(buf, 1, " ");
Packit 423ecb
Packit 423ecb
        /* Within the context of an element attributes */
Packit 423ecb
	if (cur->prefix != NULL) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 6, "xmlns:");
Packit 423ecb
	    xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
Packit 423ecb
	} else
Packit 423ecb
	    xmlOutputBufferWrite(buf, 5, "xmlns");
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, "=");
Packit 423ecb
	xmlBufWriteQuotedString(buf->buffer, cur->href);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNsDumpOutputCtxt
Packit 423ecb
 * @ctxt: the save context
Packit 423ecb
 * @cur:  a namespace
Packit 423ecb
 *
Packit 423ecb
 * Dump a local Namespace definition to a save context.
Packit 423ecb
 * Should be called in the context of attribute dumps.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlNsDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
Packit 423ecb
    xmlNsDumpOutput(ctxt->buf, cur, ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNsListDumpOutputCtxt
Packit 423ecb
 * @ctxt: the save context
Packit 423ecb
 * @cur:  the first namespace
Packit 423ecb
 *
Packit 423ecb
 * Dump a list of local namespace definitions to a save context.
Packit 423ecb
 * Should be called in the context of attribute dumps.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlNsListDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
Packit 423ecb
    while (cur != NULL) {
Packit 423ecb
        xmlNsDumpOutput(ctxt->buf, cur, ctxt);
Packit 423ecb
	cur = cur->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNsListDumpOutput:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @cur:  the first namespace
Packit 423ecb
 *
Packit 423ecb
 * Dump a list of local Namespace definitions.
Packit 423ecb
 * Should be called in the context of attributes dumps.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
Packit 423ecb
    while (cur != NULL) {
Packit 423ecb
        xmlNsDumpOutput(buf, cur, NULL);
Packit 423ecb
	cur = cur->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDtdDumpOutput:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @dtd:  the pointer to the DTD
Packit 423ecb
 *
Packit 423ecb
 * Dump the XML document DTD, if any.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
    int format, level;
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
Packit 423ecb
    if (dtd == NULL) return;
Packit 423ecb
    if ((ctxt == NULL) || (ctxt->buf == NULL))
Packit 423ecb
        return;
Packit 423ecb
    buf = ctxt->buf;
Packit 423ecb
    xmlOutputBufferWrite(buf, 10, "
Packit 423ecb
    xmlOutputBufferWriteString(buf, (const char *)dtd->name);
Packit 423ecb
    if (dtd->ExternalID != NULL) {
Packit 423ecb
	xmlOutputBufferWrite(buf, 8, " PUBLIC ");
Packit 423ecb
	xmlBufWriteQuotedString(buf->buffer, dtd->ExternalID);
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, " ");
Packit 423ecb
	xmlBufWriteQuotedString(buf->buffer, dtd->SystemID);
Packit 423ecb
    }  else if (dtd->SystemID != NULL) {
Packit 423ecb
	xmlOutputBufferWrite(buf, 8, " SYSTEM ");
Packit 423ecb
	xmlBufWriteQuotedString(buf->buffer, dtd->SystemID);
Packit 423ecb
    }
Packit 423ecb
    if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
Packit 423ecb
        (dtd->attributes == NULL) && (dtd->notations == NULL) &&
Packit 423ecb
	(dtd->pentities == NULL)) {
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, ">");
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    xmlOutputBufferWrite(buf, 3, " [\n");
Packit 423ecb
    /*
Packit 423ecb
     * Dump the notations first they are not in the DTD children list
Packit 423ecb
     * Do this only on a standalone DTD or on the internal subset though.
Packit 423ecb
     */
Packit 423ecb
    if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
Packit 423ecb
        (dtd->doc->intSubset == dtd))) {
Packit 423ecb
        xmlBufDumpNotationTable(buf->buffer,
Packit 423ecb
                                (xmlNotationTablePtr) dtd->notations);
Packit 423ecb
    }
Packit 423ecb
    format = ctxt->format;
Packit 423ecb
    level = ctxt->level;
Packit 423ecb
    doc = ctxt->doc;
Packit 423ecb
    ctxt->format = 0;
Packit 423ecb
    ctxt->level = -1;
Packit 423ecb
    ctxt->doc = dtd->doc;
Packit 423ecb
    xmlNodeListDumpOutput(ctxt, dtd->children);
Packit 423ecb
    ctxt->format = format;
Packit 423ecb
    ctxt->level = level;
Packit 423ecb
    ctxt->doc = doc;
Packit 423ecb
    xmlOutputBufferWrite(buf, 2, "]>");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlAttrDumpOutput:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @cur:  the attribute pointer
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML attribute
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
Packit 423ecb
    if (cur == NULL) return;
Packit 423ecb
    buf = ctxt->buf;
Packit 423ecb
    if (buf == NULL) return;
Packit 423ecb
    if (ctxt->format == 2)
Packit 423ecb
        xmlOutputBufferWriteWSNonSig(ctxt, 2);
Packit 423ecb
    else
Packit 423ecb
        xmlOutputBufferWrite(buf, 1, " ");
Packit 423ecb
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Packit 423ecb
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, ":");
Packit 423ecb
    }
Packit 423ecb
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
    xmlOutputBufferWrite(buf, 2, "=\"");
Packit 423ecb
    xmlAttrSerializeContent(buf, cur);
Packit 423ecb
    xmlOutputBufferWrite(buf, 1, "\"");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlAttrListDumpOutput:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @cur:  the first attribute pointer
Packit 423ecb
 * @encoding:  an optional encoding string
Packit 423ecb
 *
Packit 423ecb
 * Dump a list of XML attributes
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Packit 423ecb
    if (cur == NULL) return;
Packit 423ecb
    while (cur != NULL) {
Packit 423ecb
        xmlAttrDumpOutput(ctxt, cur);
Packit 423ecb
	cur = cur->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNodeListDumpOutput:
Packit 423ecb
 * @cur:  the first node
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML node list, recursive behaviour, children are printed too.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
Packit 423ecb
    if (cur == NULL) return;
Packit 423ecb
    buf = ctxt->buf;
Packit 423ecb
    while (cur != NULL) {
Packit 423ecb
	if ((ctxt->format == 1) && (xmlIndentTreeOutput) &&
Packit 423ecb
	    ((cur->type == XML_ELEMENT_NODE) ||
Packit 423ecb
	     (cur->type == XML_COMMENT_NODE) ||
Packit 423ecb
	     (cur->type == XML_PI_NODE)))
Packit 423ecb
	    xmlOutputBufferWrite(buf, ctxt->indent_size *
Packit 423ecb
	                         (ctxt->level > ctxt->indent_nr ?
Packit 423ecb
				  ctxt->indent_nr : ctxt->level),
Packit 423ecb
				 ctxt->indent);
Packit 423ecb
        xmlNodeDumpOutputInternal(ctxt, cur);
Packit 423ecb
	if (ctxt->format == 1) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 1, "\n");
Packit 423ecb
	}
Packit 423ecb
	cur = cur->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlNodeDumpOutputInternal:
Packit 423ecb
 * @cur:  the current node
Packit 423ecb
 *
Packit 423ecb
 * Dump an HTML node, recursive behaviour, children are printed too.
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Packit 423ecb
    const xmlChar *oldenc = NULL;
Packit 423ecb
    const xmlChar *oldctxtenc = ctxt->encoding;
Packit 423ecb
    const xmlChar *encoding = ctxt->encoding;
Packit 423ecb
    xmlOutputBufferPtr buf = ctxt->buf;
Packit 423ecb
    int switched_encoding = 0;
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
Packit 423ecb
    xmlInitParser();
Packit 423ecb
Packit 423ecb
    doc = cur->doc;
Packit 423ecb
    if (doc != NULL) {
Packit 423ecb
        oldenc = doc->encoding;
Packit 423ecb
	if (ctxt->encoding != NULL) {
Packit 423ecb
	    doc->encoding = BAD_CAST ctxt->encoding;
Packit 423ecb
	} else if (doc->encoding != NULL) {
Packit 423ecb
	    encoding = doc->encoding;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if ((encoding != NULL) && (doc != NULL))
Packit 423ecb
	htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
Packit 423ecb
    if ((encoding == NULL) && (doc != NULL))
Packit 423ecb
	encoding = htmlGetMetaEncoding(doc);
Packit 423ecb
    if (encoding == NULL)
Packit 423ecb
	encoding = BAD_CAST "HTML";
Packit 423ecb
    if ((encoding != NULL) && (oldctxtenc == NULL) &&
Packit 423ecb
	(buf->encoder == NULL) && (buf->conv == NULL)) {
Packit 423ecb
	if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
Packit 423ecb
	    doc->encoding = oldenc;
Packit 423ecb
	    return(-1);
Packit 423ecb
	}
Packit 423ecb
	switched_encoding = 1;
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->options & XML_SAVE_FORMAT)
Packit 423ecb
	htmlNodeDumpFormatOutput(buf, doc, cur,
Packit 423ecb
				       (const char *)encoding, 1);
Packit 423ecb
    else
Packit 423ecb
	htmlNodeDumpFormatOutput(buf, doc, cur,
Packit 423ecb
				       (const char *)encoding, 0);
Packit 423ecb
    /*
Packit 423ecb
     * Restore the state of the saving context at the end of the document
Packit 423ecb
     */
Packit 423ecb
    if ((switched_encoding) && (oldctxtenc == NULL)) {
Packit 423ecb
	xmlSaveClearEncoding(ctxt);
Packit 423ecb
    }
Packit 423ecb
    if (doc != NULL)
Packit 423ecb
	doc->encoding = oldenc;
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNodeDumpOutputInternal:
Packit 423ecb
 * @cur:  the current node
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML node, recursive behaviour, children are printed too.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Packit 423ecb
    int format;
Packit 423ecb
    xmlNodePtr tmp;
Packit 423ecb
    xmlChar *start, *end;
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
Packit 423ecb
    if (cur == NULL) return;
Packit 423ecb
    buf = ctxt->buf;
Packit 423ecb
    if (cur->type == XML_XINCLUDE_START)
Packit 423ecb
	return;
Packit 423ecb
    if (cur->type == XML_XINCLUDE_END)
Packit 423ecb
	return;
Packit 423ecb
    if ((cur->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
        (cur->type == XML_HTML_DOCUMENT_NODE)) {
Packit 423ecb
	xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
    if (ctxt->options & XML_SAVE_XHTML) {
Packit 423ecb
        xhtmlNodeDumpOutput(ctxt, cur);
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL) &&
Packit 423ecb
         (cur->doc->type == XML_HTML_DOCUMENT_NODE) &&
Packit 423ecb
         ((ctxt->options & XML_SAVE_AS_XML) == 0)) ||
Packit 423ecb
        (ctxt->options & XML_SAVE_AS_HTML)) {
Packit 423ecb
	htmlNodeDumpOutputInternal(ctxt, cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
#endif
Packit 423ecb
    if (cur->type == XML_DTD_NODE) {
Packit 423ecb
        xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_DOCUMENT_FRAG_NODE) {
Packit 423ecb
        xmlNodeListDumpOutput(ctxt, cur->children);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ELEMENT_DECL) {
Packit 423ecb
        xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ATTRIBUTE_DECL) {
Packit 423ecb
        xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ENTITY_DECL) {
Packit 423ecb
        xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_TEXT_NODE) {
Packit 423ecb
	if (cur->content != NULL) {
Packit 423ecb
	    if (cur->name != xmlStringTextNoenc) {
Packit 423ecb
                xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Packit 423ecb
	    } else {
Packit 423ecb
		/*
Packit 423ecb
		 * Disable escaping, needed for XSLT
Packit 423ecb
		 */
Packit 423ecb
		xmlOutputBufferWriteString(buf, (const char *) cur->content);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_PI_NODE) {
Packit 423ecb
	if (cur->content != NULL) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "
Packit 423ecb
	    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
	    if (cur->content != NULL) {
Packit 423ecb
	        if (ctxt->format == 2)
Packit 423ecb
	            xmlOutputBufferWriteWSNonSig(ctxt, 0);
Packit 423ecb
	        else
Packit 423ecb
	            xmlOutputBufferWrite(buf, 1, " ");
Packit 423ecb
		xmlOutputBufferWriteString(buf, (const char *)cur->content);
Packit 423ecb
	    }
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "?>");
Packit 423ecb
	} else {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "
Packit 423ecb
	    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
	    if (ctxt->format == 2)
Packit 423ecb
	        xmlOutputBufferWriteWSNonSig(ctxt, 0);
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "?>");
Packit 423ecb
	}
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_COMMENT_NODE) {
Packit 423ecb
	if (cur->content != NULL) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 4, "
Packit 423ecb
	    xmlOutputBufferWriteString(buf, (const char *)cur->content);
Packit 423ecb
	    xmlOutputBufferWrite(buf, 3, "-->");
Packit 423ecb
	}
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ENTITY_REF_NODE) {
Packit 423ecb
        xmlOutputBufferWrite(buf, 1, "&";;
Packit 423ecb
	xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
        xmlOutputBufferWrite(buf, 1, ";");
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_CDATA_SECTION_NODE) {
Packit 423ecb
	if (cur->content == NULL || *cur->content == '\0') {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 12, "");
Packit 423ecb
	} else {
Packit 423ecb
	    start = end = cur->content;
Packit 423ecb
	    while (*end != '\0') {
Packit 423ecb
		if ((*end == ']') && (*(end + 1) == ']') &&
Packit 423ecb
		    (*(end + 2) == '>')) {
Packit 423ecb
		    end = end + 2;
Packit 423ecb
		    xmlOutputBufferWrite(buf, 9, "
Packit 423ecb
		    xmlOutputBufferWrite(buf, end - start, (const char *)start);
Packit 423ecb
		    xmlOutputBufferWrite(buf, 3, "]]>");
Packit 423ecb
		    start = end;
Packit 423ecb
		}
Packit 423ecb
		end++;
Packit 423ecb
	    }
Packit 423ecb
	    if (start != end) {
Packit 423ecb
		xmlOutputBufferWrite(buf, 9, "
Packit 423ecb
		xmlOutputBufferWriteString(buf, (const char *)start);
Packit 423ecb
		xmlOutputBufferWrite(buf, 3, "]]>");
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ATTRIBUTE_NODE) {
Packit 423ecb
	xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_NAMESPACE_DECL) {
Packit 423ecb
	xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    format = ctxt->format;
Packit 423ecb
    if (format == 1) {
Packit 423ecb
	tmp = cur->children;
Packit 423ecb
	while (tmp != NULL) {
Packit 423ecb
	    if ((tmp->type == XML_TEXT_NODE) ||
Packit 423ecb
		(tmp->type == XML_CDATA_SECTION_NODE) ||
Packit 423ecb
		(tmp->type == XML_ENTITY_REF_NODE)) {
Packit 423ecb
		ctxt->format = 0;
Packit 423ecb
		break;
Packit 423ecb
	    }
Packit 423ecb
	    tmp = tmp->next;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    xmlOutputBufferWrite(buf, 1, "<");
Packit 423ecb
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Packit 423ecb
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, ":");
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
    if (cur->nsDef)
Packit 423ecb
        xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
Packit 423ecb
    if (cur->properties != NULL)
Packit 423ecb
        xmlAttrListDumpOutput(ctxt, cur->properties);
Packit 423ecb
Packit 423ecb
    if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
Packit 423ecb
	(cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) {
Packit 423ecb
        if (ctxt->format == 2)
Packit 423ecb
            xmlOutputBufferWriteWSNonSig(ctxt, 0);
Packit 423ecb
        xmlOutputBufferWrite(buf, 2, "/>");
Packit 423ecb
	ctxt->format = format;
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->format == 2)
Packit 423ecb
        xmlOutputBufferWriteWSNonSig(ctxt, 1);
Packit 423ecb
    xmlOutputBufferWrite(buf, 1, ">");
Packit 423ecb
    if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Packit 423ecb
	xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Packit 423ecb
    }
Packit 423ecb
    if (cur->children != NULL) {
Packit 423ecb
	if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
Packit 423ecb
	if (ctxt->level >= 0) ctxt->level++;
Packit 423ecb
	xmlNodeListDumpOutput(ctxt, cur->children);
Packit 423ecb
	if (ctxt->level > 0) ctxt->level--;
Packit 423ecb
	if ((xmlIndentTreeOutput) && (ctxt->format == 1))
Packit 423ecb
	    xmlOutputBufferWrite(buf, ctxt->indent_size *
Packit 423ecb
	                         (ctxt->level > ctxt->indent_nr ?
Packit 423ecb
				  ctxt->indent_nr : ctxt->level),
Packit 423ecb
				 ctxt->indent);
Packit 423ecb
    }
Packit 423ecb
    xmlOutputBufferWrite(buf, 2, "</");
Packit 423ecb
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Packit 423ecb
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, ":");
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
    if (ctxt->format == 2)
Packit 423ecb
        xmlOutputBufferWriteWSNonSig(ctxt, 0);
Packit 423ecb
    xmlOutputBufferWrite(buf, 1, ">");
Packit 423ecb
    ctxt->format = format;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDocContentDumpOutput:
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document.
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
    xmlDtdPtr dtd;
Packit 423ecb
    int is_xhtml = 0;
Packit 423ecb
#endif
Packit 423ecb
    const xmlChar *oldenc = cur->encoding;
Packit 423ecb
    const xmlChar *oldctxtenc = ctxt->encoding;
Packit 423ecb
    const xmlChar *encoding = ctxt->encoding;
Packit 423ecb
    xmlCharEncodingOutputFunc oldescape = ctxt->escape;
Packit 423ecb
    xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr;
Packit 423ecb
    xmlOutputBufferPtr buf = ctxt->buf;
Packit 423ecb
    xmlCharEncoding enc;
Packit 423ecb
    int switched_encoding = 0;
Packit 423ecb
Packit 423ecb
    xmlInitParser();
Packit 423ecb
Packit 423ecb
    if ((cur->type != XML_HTML_DOCUMENT_NODE) &&
Packit 423ecb
        (cur->type != XML_DOCUMENT_NODE))
Packit 423ecb
	 return(-1);
Packit 423ecb
Packit 423ecb
    if (ctxt->encoding != NULL) {
Packit 423ecb
        cur->encoding = BAD_CAST ctxt->encoding;
Packit 423ecb
    } else if (cur->encoding != NULL) {
Packit 423ecb
	encoding = cur->encoding;
Packit 423ecb
    } else if (cur->charset != XML_CHAR_ENCODING_UTF8) {
Packit 423ecb
	encoding = (const xmlChar *)
Packit 423ecb
		     xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if (((cur->type == XML_HTML_DOCUMENT_NODE) &&
Packit 423ecb
         ((ctxt->options & XML_SAVE_AS_XML) == 0) &&
Packit 423ecb
         ((ctxt->options & XML_SAVE_XHTML) == 0)) ||
Packit 423ecb
        (ctxt->options & XML_SAVE_AS_HTML)) {
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
        if (encoding != NULL)
Packit 423ecb
	    htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
Packit 423ecb
        if (encoding == NULL)
Packit 423ecb
	    encoding = htmlGetMetaEncoding(cur);
Packit 423ecb
        if (encoding == NULL)
Packit 423ecb
	    encoding = BAD_CAST "HTML";
Packit 423ecb
	if ((encoding != NULL) && (oldctxtenc == NULL) &&
Packit 423ecb
	    (buf->encoder == NULL) && (buf->conv == NULL)) {
Packit 423ecb
	    if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
Packit 423ecb
		cur->encoding = oldenc;
Packit 423ecb
		return(-1);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
        if (ctxt->options & XML_SAVE_FORMAT)
Packit 423ecb
	    htmlDocContentDumpFormatOutput(buf, cur,
Packit 423ecb
	                                   (const char *)encoding, 1);
Packit 423ecb
	else
Packit 423ecb
	    htmlDocContentDumpFormatOutput(buf, cur,
Packit 423ecb
	                                   (const char *)encoding, 0);
Packit 423ecb
	if (ctxt->encoding != NULL)
Packit 423ecb
	    cur->encoding = oldenc;
Packit 423ecb
	return(0);
Packit 423ecb
#else
Packit 423ecb
        return(-1);
Packit 423ecb
#endif
Packit 423ecb
    } else if ((cur->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
               (ctxt->options & XML_SAVE_AS_XML) ||
Packit 423ecb
               (ctxt->options & XML_SAVE_XHTML)) {
Packit 423ecb
	enc = xmlParseCharEncoding((const char*) encoding);
Packit 423ecb
	if ((encoding != NULL) && (oldctxtenc == NULL) &&
Packit 423ecb
	    (buf->encoder == NULL) && (buf->conv == NULL) &&
Packit 423ecb
	    ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
Packit 423ecb
	    if ((enc != XML_CHAR_ENCODING_UTF8) &&
Packit 423ecb
		(enc != XML_CHAR_ENCODING_NONE) &&
Packit 423ecb
		(enc != XML_CHAR_ENCODING_ASCII)) {
Packit 423ecb
		/*
Packit 423ecb
		 * we need to switch to this encoding but just for this
Packit 423ecb
		 * document since we output the XMLDecl the conversion
Packit 423ecb
		 * must be done to not generate not well formed documents.
Packit 423ecb
		 */
Packit 423ecb
		if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
Packit 423ecb
		    cur->encoding = oldenc;
Packit 423ecb
		    return(-1);
Packit 423ecb
		}
Packit 423ecb
		switched_encoding = 1;
Packit 423ecb
	    }
Packit 423ecb
	    if (ctxt->escape == xmlEscapeEntities)
Packit 423ecb
		ctxt->escape = NULL;
Packit 423ecb
	    if (ctxt->escapeAttr == xmlEscapeEntities)
Packit 423ecb
		ctxt->escapeAttr = NULL;
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
Packit 423ecb
	/*
Packit 423ecb
	 * Save the XML declaration
Packit 423ecb
	 */
Packit 423ecb
	if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 14, "
Packit 423ecb
	    if (cur->version != NULL)
Packit 423ecb
		xmlBufWriteQuotedString(buf->buffer, cur->version);
Packit 423ecb
	    else
Packit 423ecb
		xmlOutputBufferWrite(buf, 5, "\"1.0\"");
Packit 423ecb
	    if (encoding != NULL) {
Packit 423ecb
		xmlOutputBufferWrite(buf, 10, " encoding=");
Packit 423ecb
		xmlBufWriteQuotedString(buf->buffer, (xmlChar *) encoding);
Packit 423ecb
	    }
Packit 423ecb
	    switch (cur->standalone) {
Packit 423ecb
		case 0:
Packit 423ecb
		    xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
Packit 423ecb
		    break;
Packit 423ecb
		case 1:
Packit 423ecb
		    xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
Packit 423ecb
		    break;
Packit 423ecb
	    }
Packit 423ecb
	    xmlOutputBufferWrite(buf, 3, "?>\n");
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
        if (ctxt->options & XML_SAVE_XHTML)
Packit 423ecb
            is_xhtml = 1;
Packit 423ecb
	if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
Packit 423ecb
	    dtd = xmlGetIntSubset(cur);
Packit 423ecb
	    if (dtd != NULL) {
Packit 423ecb
		is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
Packit 423ecb
		if (is_xhtml < 0) is_xhtml = 0;
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
#endif
Packit 423ecb
	if (cur->children != NULL) {
Packit 423ecb
	    xmlNodePtr child = cur->children;
Packit 423ecb
Packit 423ecb
	    while (child != NULL) {
Packit 423ecb
		ctxt->level = 0;
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
		if (is_xhtml)
Packit 423ecb
		    xhtmlNodeDumpOutput(ctxt, child);
Packit 423ecb
		else
Packit 423ecb
#endif
Packit 423ecb
		    xmlNodeDumpOutputInternal(ctxt, child);
Packit 423ecb
		xmlOutputBufferWrite(buf, 1, "\n");
Packit 423ecb
		child = child->next;
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Restore the state of the saving context at the end of the document
Packit 423ecb
     */
Packit 423ecb
    if ((switched_encoding) && (oldctxtenc == NULL)) {
Packit 423ecb
	xmlSaveClearEncoding(ctxt);
Packit 423ecb
	ctxt->escape = oldescape;
Packit 423ecb
	ctxt->escapeAttr = oldescapeAttr;
Packit 423ecb
    }
Packit 423ecb
    cur->encoding = oldenc;
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Functions specific to XHTML serialization		*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xhtmlIsEmpty:
Packit 423ecb
 * @node:  the node
Packit 423ecb
 *
Packit 423ecb
 * Check if a node is an empty xhtml node
Packit 423ecb
 *
Packit 423ecb
 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xhtmlIsEmpty(xmlNodePtr node) {
Packit 423ecb
    if (node == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
    if (node->type != XML_ELEMENT_NODE)
Packit 423ecb
	return(0);
Packit 423ecb
    if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
Packit 423ecb
	return(0);
Packit 423ecb
    if (node->children != NULL)
Packit 423ecb
	return(0);
Packit 423ecb
    switch (node->name[0]) {
Packit 423ecb
	case 'a':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "area"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
	case 'b':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "br"))
Packit 423ecb
		return(1);
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "base"))
Packit 423ecb
		return(1);
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "basefont"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
	case 'c':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "col"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
	case 'f':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "frame"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
	case 'h':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "hr"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
	case 'i':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "img"))
Packit 423ecb
		return(1);
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "input"))
Packit 423ecb
		return(1);
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "isindex"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
	case 'l':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "link"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
	case 'm':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "meta"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
	case 'p':
Packit 423ecb
	    if (xmlStrEqual(node->name, BAD_CAST "param"))
Packit 423ecb
		return(1);
Packit 423ecb
	    return(0);
Packit 423ecb
    }
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xhtmlAttrListDumpOutput:
Packit 423ecb
 * @cur:  the first attribute pointer
Packit 423ecb
 *
Packit 423ecb
 * Dump a list of XML attributes
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Packit 423ecb
    xmlAttrPtr xml_lang = NULL;
Packit 423ecb
    xmlAttrPtr lang = NULL;
Packit 423ecb
    xmlAttrPtr name = NULL;
Packit 423ecb
    xmlAttrPtr id = NULL;
Packit 423ecb
    xmlNodePtr parent;
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
Packit 423ecb
    if (cur == NULL) return;
Packit 423ecb
    buf = ctxt->buf;
Packit 423ecb
    parent = cur->parent;
Packit 423ecb
    while (cur != NULL) {
Packit 423ecb
	if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
Packit 423ecb
	    id = cur;
Packit 423ecb
	else
Packit 423ecb
	if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
Packit 423ecb
	    name = cur;
Packit 423ecb
	else
Packit 423ecb
	if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
Packit 423ecb
	    lang = cur;
Packit 423ecb
	else
Packit 423ecb
	if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
Packit 423ecb
	    (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
Packit 423ecb
	    xml_lang = cur;
Packit 423ecb
	else if ((cur->ns == NULL) &&
Packit 423ecb
		 ((cur->children == NULL) ||
Packit 423ecb
		  (cur->children->content == NULL) ||
Packit 423ecb
		  (cur->children->content[0] == 0)) &&
Packit 423ecb
		 (htmlIsBooleanAttr(cur->name))) {
Packit 423ecb
	    if (cur->children != NULL)
Packit 423ecb
		xmlFreeNode(cur->children);
Packit 423ecb
	    cur->children = xmlNewText(cur->name);
Packit 423ecb
	    if (cur->children != NULL)
Packit 423ecb
		cur->children->parent = (xmlNodePtr) cur;
Packit 423ecb
	}
Packit 423ecb
        xmlAttrDumpOutput(ctxt, cur);
Packit 423ecb
	cur = cur->next;
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * C.8
Packit 423ecb
     */
Packit 423ecb
    if ((name != NULL) && (id == NULL)) {
Packit 423ecb
	if ((parent != NULL) && (parent->name != NULL) &&
Packit 423ecb
	    ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
Packit 423ecb
	     (xmlStrEqual(parent->name, BAD_CAST "p")) ||
Packit 423ecb
	     (xmlStrEqual(parent->name, BAD_CAST "div")) ||
Packit 423ecb
	     (xmlStrEqual(parent->name, BAD_CAST "img")) ||
Packit 423ecb
	     (xmlStrEqual(parent->name, BAD_CAST "map")) ||
Packit 423ecb
	     (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
Packit 423ecb
	     (xmlStrEqual(parent->name, BAD_CAST "form")) ||
Packit 423ecb
	     (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
Packit 423ecb
	     (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 5, " id=\"");
Packit 423ecb
	    xmlAttrSerializeContent(buf, name);
Packit 423ecb
	    xmlOutputBufferWrite(buf, 1, "\"");
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * C.7.
Packit 423ecb
     */
Packit 423ecb
    if ((lang != NULL) && (xml_lang == NULL)) {
Packit 423ecb
	xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
Packit 423ecb
	xmlAttrSerializeContent(buf, lang);
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, "\"");
Packit 423ecb
    } else
Packit 423ecb
    if ((xml_lang != NULL) && (lang == NULL)) {
Packit 423ecb
	xmlOutputBufferWrite(buf, 7, " lang=\"");
Packit 423ecb
	xmlAttrSerializeContent(buf, xml_lang);
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, "\"");
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xhtmlNodeListDumpOutput:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @doc:  the XHTML document
Packit 423ecb
 * @cur:  the first node
Packit 423ecb
 * @level: the imbrication level for indenting
Packit 423ecb
 * @format: is formatting allowed
Packit 423ecb
 * @encoding:  an optional encoding string
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML node list, recursive behaviour, children are printed too.
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
Packit 423ecb
    if (cur == NULL) return;
Packit 423ecb
    buf = ctxt->buf;
Packit 423ecb
    while (cur != NULL) {
Packit 423ecb
	if ((ctxt->format == 1) && (xmlIndentTreeOutput) &&
Packit 423ecb
	    (cur->type == XML_ELEMENT_NODE))
Packit 423ecb
	    xmlOutputBufferWrite(buf, ctxt->indent_size *
Packit 423ecb
	                         (ctxt->level > ctxt->indent_nr ?
Packit 423ecb
				  ctxt->indent_nr : ctxt->level),
Packit 423ecb
				 ctxt->indent);
Packit 423ecb
        xhtmlNodeDumpOutput(ctxt, cur);
Packit 423ecb
	if (ctxt->format == 1) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 1, "\n");
Packit 423ecb
	}
Packit 423ecb
	cur = cur->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xhtmlNodeDumpOutput:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @doc:  the XHTML document
Packit 423ecb
 * @cur:  the current node
Packit 423ecb
 * @level: the imbrication level for indenting
Packit 423ecb
 * @format: is formatting allowed
Packit 423ecb
 * @encoding:  an optional encoding string
Packit 423ecb
 *
Packit 423ecb
 * Dump an XHTML node, recursive behaviour, children are printed too.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Packit 423ecb
    int format, addmeta = 0;
Packit 423ecb
    xmlNodePtr tmp;
Packit 423ecb
    xmlChar *start, *end;
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
Packit 423ecb
    if (cur == NULL) return;
Packit 423ecb
    if ((cur->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
        (cur->type == XML_HTML_DOCUMENT_NODE)) {
Packit 423ecb
        xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_XINCLUDE_START)
Packit 423ecb
	return;
Packit 423ecb
    if (cur->type == XML_XINCLUDE_END)
Packit 423ecb
	return;
Packit 423ecb
    if (cur->type == XML_NAMESPACE_DECL) {
Packit 423ecb
	xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_DTD_NODE) {
Packit 423ecb
        xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_DOCUMENT_FRAG_NODE) {
Packit 423ecb
        xhtmlNodeListDumpOutput(ctxt, cur->children);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    buf = ctxt->buf;
Packit 423ecb
    if (cur->type == XML_ELEMENT_DECL) {
Packit 423ecb
        xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ATTRIBUTE_DECL) {
Packit 423ecb
        xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ENTITY_DECL) {
Packit 423ecb
        xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_TEXT_NODE) {
Packit 423ecb
	if (cur->content != NULL) {
Packit 423ecb
	    if ((cur->name == xmlStringText) ||
Packit 423ecb
		(cur->name != xmlStringTextNoenc)) {
Packit 423ecb
                xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Packit 423ecb
	    } else {
Packit 423ecb
		/*
Packit 423ecb
		 * Disable escaping, needed for XSLT
Packit 423ecb
		 */
Packit 423ecb
		xmlOutputBufferWriteString(buf, (const char *) cur->content);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_PI_NODE) {
Packit 423ecb
	if (cur->content != NULL) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "
Packit 423ecb
	    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
	    if (cur->content != NULL) {
Packit 423ecb
		xmlOutputBufferWrite(buf, 1, " ");
Packit 423ecb
		xmlOutputBufferWriteString(buf, (const char *)cur->content);
Packit 423ecb
	    }
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "?>");
Packit 423ecb
	} else {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "
Packit 423ecb
	    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "?>");
Packit 423ecb
	}
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_COMMENT_NODE) {
Packit 423ecb
	if (cur->content != NULL) {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 4, "
Packit 423ecb
	    xmlOutputBufferWriteString(buf, (const char *)cur->content);
Packit 423ecb
	    xmlOutputBufferWrite(buf, 3, "-->");
Packit 423ecb
	}
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ENTITY_REF_NODE) {
Packit 423ecb
        xmlOutputBufferWrite(buf, 1, "&";;
Packit 423ecb
	xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
        xmlOutputBufferWrite(buf, 1, ";");
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_CDATA_SECTION_NODE) {
Packit 423ecb
	if (cur->content == NULL || *cur->content == '\0') {
Packit 423ecb
	    xmlOutputBufferWrite(buf, 12, "");
Packit 423ecb
	} else {
Packit 423ecb
	    start = end = cur->content;
Packit 423ecb
	    while (*end != '\0') {
Packit 423ecb
		if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
Packit 423ecb
		    end = end + 2;
Packit 423ecb
		    xmlOutputBufferWrite(buf, 9, "
Packit 423ecb
		    xmlOutputBufferWrite(buf, end - start, (const char *)start);
Packit 423ecb
		    xmlOutputBufferWrite(buf, 3, "]]>");
Packit 423ecb
		    start = end;
Packit 423ecb
		}
Packit 423ecb
		end++;
Packit 423ecb
	    }
Packit 423ecb
	    if (start != end) {
Packit 423ecb
		xmlOutputBufferWrite(buf, 9, "
Packit 423ecb
		xmlOutputBufferWriteString(buf, (const char *)start);
Packit 423ecb
		xmlOutputBufferWrite(buf, 3, "]]>");
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    if (cur->type == XML_ATTRIBUTE_NODE) {
Packit 423ecb
        xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    format = ctxt->format;
Packit 423ecb
    if (format == 1) {
Packit 423ecb
	tmp = cur->children;
Packit 423ecb
	while (tmp != NULL) {
Packit 423ecb
	    if ((tmp->type == XML_TEXT_NODE) ||
Packit 423ecb
		(tmp->type == XML_ENTITY_REF_NODE)) {
Packit 423ecb
		format = 0;
Packit 423ecb
		break;
Packit 423ecb
	    }
Packit 423ecb
	    tmp = tmp->next;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    xmlOutputBufferWrite(buf, 1, "<");
Packit 423ecb
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Packit 423ecb
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, ":");
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
    if (cur->nsDef)
Packit 423ecb
        xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
Packit 423ecb
    if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
Packit 423ecb
	(cur->ns == NULL) && (cur->nsDef == NULL))) {
Packit 423ecb
	/*
Packit 423ecb
	 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
Packit 423ecb
	 */
Packit 423ecb
	xmlOutputBufferWriteString(buf,
Packit 423ecb
		" xmlns=\"http://www.w3.org/1999/xhtml\"");
Packit 423ecb
    }
Packit 423ecb
    if (cur->properties != NULL)
Packit 423ecb
        xhtmlAttrListDumpOutput(ctxt, cur->properties);
Packit 423ecb
Packit 423ecb
	if ((cur->type == XML_ELEMENT_NODE) &&
Packit 423ecb
		(cur->parent != NULL) &&
Packit 423ecb
		(cur->parent->parent == (xmlNodePtr) cur->doc) &&
Packit 423ecb
		xmlStrEqual(cur->name, BAD_CAST"head") &&
Packit 423ecb
		xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
Packit 423ecb
Packit 423ecb
		tmp = cur->children;
Packit 423ecb
		while (tmp != NULL) {
Packit 423ecb
			if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
Packit 423ecb
				xmlChar *httpequiv;
Packit 423ecb
Packit 423ecb
				httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
Packit 423ecb
				if (httpequiv != NULL) {
Packit 423ecb
					if (xmlStrcasecmp(httpequiv, BAD_CAST"Content-Type") == 0) {
Packit 423ecb
						xmlFree(httpequiv);
Packit 423ecb
						break;
Packit 423ecb
					}
Packit 423ecb
					xmlFree(httpequiv);
Packit 423ecb
				}
Packit 423ecb
			}
Packit 423ecb
			tmp = tmp->next;
Packit 423ecb
		}
Packit 423ecb
		if (tmp == NULL)
Packit 423ecb
			addmeta = 1;
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
    if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
Packit 423ecb
	if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
Packit 423ecb
	    ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
Packit 423ecb
	    /*
Packit 423ecb
	     * C.2. Empty Elements
Packit 423ecb
	     */
Packit 423ecb
	    xmlOutputBufferWrite(buf, 3, " />");
Packit 423ecb
	} else {
Packit 423ecb
		if (addmeta == 1) {
Packit 423ecb
			xmlOutputBufferWrite(buf, 1, ">");
Packit 423ecb
			if (ctxt->format == 1) {
Packit 423ecb
				xmlOutputBufferWrite(buf, 1, "\n");
Packit 423ecb
				if (xmlIndentTreeOutput)
Packit 423ecb
					xmlOutputBufferWrite(buf, ctxt->indent_size *
Packit 423ecb
					(ctxt->level + 1 > ctxt->indent_nr ?
Packit 423ecb
					ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
Packit 423ecb
			}
Packit 423ecb
			xmlOutputBufferWriteString(buf,
Packit 423ecb
				"
Packit 423ecb
			if (ctxt->encoding) {
Packit 423ecb
				xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
Packit 423ecb
			} else {
Packit 423ecb
				xmlOutputBufferWrite(buf, 5, "UTF-8");
Packit 423ecb
			}
Packit 423ecb
			xmlOutputBufferWrite(buf, 4, "\" />");
Packit 423ecb
			if (ctxt->format == 1)
Packit 423ecb
				xmlOutputBufferWrite(buf, 1, "\n");
Packit 423ecb
		} else {
Packit 423ecb
			xmlOutputBufferWrite(buf, 1, ">");
Packit 423ecb
		}
Packit 423ecb
	    /*
Packit 423ecb
	     * C.3. Element Minimization and Empty Element Content
Packit 423ecb
	     */
Packit 423ecb
	    xmlOutputBufferWrite(buf, 2, "</");
Packit 423ecb
	    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Packit 423ecb
		xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Packit 423ecb
		xmlOutputBufferWrite(buf, 1, ":");
Packit 423ecb
	    }
Packit 423ecb
	    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
	    xmlOutputBufferWrite(buf, 1, ">");
Packit 423ecb
	}
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    xmlOutputBufferWrite(buf, 1, ">");
Packit 423ecb
	if (addmeta == 1) {
Packit 423ecb
		if (ctxt->format == 1) {
Packit 423ecb
			xmlOutputBufferWrite(buf, 1, "\n");
Packit 423ecb
			if (xmlIndentTreeOutput)
Packit 423ecb
				xmlOutputBufferWrite(buf, ctxt->indent_size *
Packit 423ecb
				(ctxt->level + 1 > ctxt->indent_nr ?
Packit 423ecb
				ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
Packit 423ecb
		}
Packit 423ecb
		xmlOutputBufferWriteString(buf,
Packit 423ecb
			"
Packit 423ecb
		if (ctxt->encoding) {
Packit 423ecb
			xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
Packit 423ecb
		} else {
Packit 423ecb
			xmlOutputBufferWrite(buf, 5, "UTF-8");
Packit 423ecb
		}
Packit 423ecb
		xmlOutputBufferWrite(buf, 4, "\" />");
Packit 423ecb
	}
Packit 423ecb
    if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Packit 423ecb
	xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
#if 0
Packit 423ecb
    /*
Packit 423ecb
    * This was removed due to problems with HTML processors.
Packit 423ecb
    * See bug #345147.
Packit 423ecb
    */
Packit 423ecb
    /*
Packit 423ecb
     * 4.8. Script and Style elements
Packit 423ecb
     */
Packit 423ecb
    if ((cur->type == XML_ELEMENT_NODE) &&
Packit 423ecb
	((xmlStrEqual(cur->name, BAD_CAST "script")) ||
Packit 423ecb
	 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
Packit 423ecb
	((cur->ns == NULL) ||
Packit 423ecb
	 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
Packit 423ecb
	xmlNodePtr child = cur->children;
Packit 423ecb
Packit 423ecb
	while (child != NULL) {
Packit 423ecb
	    if (child->type == XML_TEXT_NODE) {
Packit 423ecb
		if ((xmlStrchr(child->content, '<') == NULL) &&
Packit 423ecb
		    (xmlStrchr(child->content, '&') == NULL) &&
Packit 423ecb
		    (xmlStrstr(child->content, BAD_CAST "]]>") == NULL)) {
Packit 423ecb
		    /* Nothing to escape, so just output as is... */
Packit 423ecb
		    /* FIXME: Should we do something about "--" also? */
Packit 423ecb
		    int level = ctxt->level;
Packit 423ecb
		    int indent = ctxt->format;
Packit 423ecb
Packit 423ecb
		    ctxt->level = 0;
Packit 423ecb
		    ctxt->format = 0;
Packit 423ecb
		    xmlOutputBufferWriteString(buf, (const char *) child->content);
Packit 423ecb
		    /* (We cannot use xhtmlNodeDumpOutput() here because
Packit 423ecb
		     * we wish to leave '>' unescaped!) */
Packit 423ecb
		    ctxt->level = level;
Packit 423ecb
		    ctxt->format = indent;
Packit 423ecb
		} else {
Packit 423ecb
		    /* We must use a CDATA section.  Unfortunately,
Packit 423ecb
		     * this will break CSS and JavaScript when read by
Packit 423ecb
		     * a browser in HTML4-compliant mode. :-( */
Packit 423ecb
		    start = end = child->content;
Packit 423ecb
		    while (*end != '\0') {
Packit 423ecb
			if (*end == ']' &&
Packit 423ecb
			    *(end + 1) == ']' &&
Packit 423ecb
			    *(end + 2) == '>') {
Packit 423ecb
			    end = end + 2;
Packit 423ecb
			    xmlOutputBufferWrite(buf, 9, "
Packit 423ecb
			    xmlOutputBufferWrite(buf, end - start,
Packit 423ecb
						 (const char *)start);
Packit 423ecb
			    xmlOutputBufferWrite(buf, 3, "]]>");
Packit 423ecb
			    start = end;
Packit 423ecb
			}
Packit 423ecb
			end++;
Packit 423ecb
		    }
Packit 423ecb
		    if (start != end) {
Packit 423ecb
			xmlOutputBufferWrite(buf, 9, "
Packit 423ecb
			xmlOutputBufferWrite(buf, end - start,
Packit 423ecb
			                     (const char *)start);
Packit 423ecb
			xmlOutputBufferWrite(buf, 3, "]]>");
Packit 423ecb
		    }
Packit 423ecb
		}
Packit 423ecb
	    } else {
Packit 423ecb
		int level = ctxt->level;
Packit 423ecb
		int indent = ctxt->format;
Packit 423ecb
Packit 423ecb
		ctxt->level = 0;
Packit 423ecb
		ctxt->format = 0;
Packit 423ecb
		xhtmlNodeDumpOutput(ctxt, child);
Packit 423ecb
		ctxt->level = level;
Packit 423ecb
		ctxt->format = indent;
Packit 423ecb
	    }
Packit 423ecb
	    child = child->next;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    if (cur->children != NULL) {
Packit 423ecb
	int indent = ctxt->format;
Packit 423ecb
Packit 423ecb
	if (format == 1) xmlOutputBufferWrite(buf, 1, "\n");
Packit 423ecb
	if (ctxt->level >= 0) ctxt->level++;
Packit 423ecb
	ctxt->format = format;
Packit 423ecb
	xhtmlNodeListDumpOutput(ctxt, cur->children);
Packit 423ecb
	if (ctxt->level > 0) ctxt->level--;
Packit 423ecb
	ctxt->format = indent;
Packit 423ecb
	if ((xmlIndentTreeOutput) && (format == 1))
Packit 423ecb
	    xmlOutputBufferWrite(buf, ctxt->indent_size *
Packit 423ecb
	                         (ctxt->level > ctxt->indent_nr ?
Packit 423ecb
				  ctxt->indent_nr : ctxt->level),
Packit 423ecb
				 ctxt->indent);
Packit 423ecb
    }
Packit 423ecb
    xmlOutputBufferWrite(buf, 2, "</");
Packit 423ecb
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Packit 423ecb
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Packit 423ecb
	xmlOutputBufferWrite(buf, 1, ":");
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
Packit 423ecb
    xmlOutputBufferWrite(buf, 1, ">");
Packit 423ecb
}
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			Public entry points				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveToFd:
Packit 423ecb
 * @fd:  a file descriptor number
Packit 423ecb
 * @encoding:  the encoding name to use or NULL
Packit 423ecb
 * @options:  a set of xmlSaveOptions
Packit 423ecb
 *
Packit 423ecb
 * Create a document saving context serializing to a file descriptor
Packit 423ecb
 * with the encoding and the options given.
Packit 423ecb
 *
Packit 423ecb
 * Returns a new serialization context or NULL in case of error.
Packit 423ecb
 */
Packit 423ecb
xmlSaveCtxtPtr
Packit 423ecb
xmlSaveToFd(int fd, const char *encoding, int options)
Packit 423ecb
{
Packit 423ecb
    xmlSaveCtxtPtr ret;
Packit 423ecb
Packit 423ecb
    ret = xmlNewSaveCtxt(encoding, options);
Packit 423ecb
    if (ret == NULL) return(NULL);
Packit 423ecb
    ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
Packit 423ecb
    if (ret->buf == NULL) {
Packit 423ecb
	xmlFreeSaveCtxt(ret);
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveToFilename:
Packit 423ecb
 * @filename:  a file name or an URL
Packit 423ecb
 * @encoding:  the encoding name to use or NULL
Packit 423ecb
 * @options:  a set of xmlSaveOptions
Packit 423ecb
 *
Packit 423ecb
 * Create a document saving context serializing to a filename or possibly
Packit 423ecb
 * to an URL (but this is less reliable) with the encoding and the options
Packit 423ecb
 * given.
Packit 423ecb
 *
Packit 423ecb
 * Returns a new serialization context or NULL in case of error.
Packit 423ecb
 */
Packit 423ecb
xmlSaveCtxtPtr
Packit 423ecb
xmlSaveToFilename(const char *filename, const char *encoding, int options)
Packit 423ecb
{
Packit 423ecb
    xmlSaveCtxtPtr ret;
Packit 423ecb
    int compression = 0; /* TODO handle compression option */
Packit 423ecb
Packit 423ecb
    ret = xmlNewSaveCtxt(encoding, options);
Packit 423ecb
    if (ret == NULL) return(NULL);
Packit 423ecb
    ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
Packit 423ecb
                                             compression);
Packit 423ecb
    if (ret->buf == NULL) {
Packit 423ecb
	xmlFreeSaveCtxt(ret);
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveToBuffer:
Packit 423ecb
 * @buffer:  a buffer
Packit 423ecb
 * @encoding:  the encoding name to use or NULL
Packit 423ecb
 * @options:  a set of xmlSaveOptions
Packit 423ecb
 *
Packit 423ecb
 * Create a document saving context serializing to a buffer
Packit 423ecb
 * with the encoding and the options given
Packit 423ecb
 *
Packit 423ecb
 * Returns a new serialization context or NULL in case of error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
xmlSaveCtxtPtr
Packit 423ecb
xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
Packit 423ecb
{
Packit 423ecb
    xmlSaveCtxtPtr ret;
Packit 423ecb
    xmlOutputBufferPtr out_buff;
Packit 423ecb
    xmlCharEncodingHandlerPtr handler;
Packit 423ecb
Packit 423ecb
    ret = xmlNewSaveCtxt(encoding, options);
Packit 423ecb
    if (ret == NULL) return(NULL);
Packit 423ecb
Packit 423ecb
    if (encoding != NULL) {
Packit 423ecb
        handler = xmlFindCharEncodingHandler(encoding);
Packit 423ecb
        if (handler == NULL) {
Packit 423ecb
            xmlFree(ret);
Packit 423ecb
            return(NULL);
Packit 423ecb
        }
Packit 423ecb
    } else
Packit 423ecb
        handler = NULL;
Packit 423ecb
    out_buff = xmlOutputBufferCreateBuffer(buffer, handler);
Packit 423ecb
    if (out_buff == NULL) {
Packit 423ecb
        xmlFree(ret);
Packit 423ecb
        if (handler) xmlCharEncCloseFunc(handler);
Packit 423ecb
        return(NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    ret->buf = out_buff;
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveToIO:
Packit 423ecb
 * @iowrite:  an I/O write function
Packit 423ecb
 * @ioclose:  an I/O close function
Packit 423ecb
 * @ioctx:  an I/O handler
Packit 423ecb
 * @encoding:  the encoding name to use or NULL
Packit 423ecb
 * @options:  a set of xmlSaveOptions
Packit 423ecb
 *
Packit 423ecb
 * Create a document saving context serializing to a file descriptor
Packit 423ecb
 * with the encoding and the options given
Packit 423ecb
 *
Packit 423ecb
 * Returns a new serialization context or NULL in case of error.
Packit 423ecb
 */
Packit 423ecb
xmlSaveCtxtPtr
Packit 423ecb
xmlSaveToIO(xmlOutputWriteCallback iowrite,
Packit 423ecb
            xmlOutputCloseCallback ioclose,
Packit 423ecb
            void *ioctx, const char *encoding, int options)
Packit 423ecb
{
Packit 423ecb
    xmlSaveCtxtPtr ret;
Packit 423ecb
Packit 423ecb
    ret = xmlNewSaveCtxt(encoding, options);
Packit 423ecb
    if (ret == NULL) return(NULL);
Packit 423ecb
    ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
Packit 423ecb
    if (ret->buf == NULL) {
Packit 423ecb
	xmlFreeSaveCtxt(ret);
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveDoc:
Packit 423ecb
 * @ctxt:  a document saving context
Packit 423ecb
 * @doc:  a document
Packit 423ecb
 *
Packit 423ecb
 * Save a full document to a saving context
Packit 423ecb
 * TODO: The function is not fully implemented yet as it does not return the
Packit 423ecb
 * byte count but 0 instead
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of byte written or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
long
Packit 423ecb
xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    long ret = 0;
Packit 423ecb
Packit 423ecb
    if ((ctxt == NULL) || (doc == NULL)) return(-1);
Packit 423ecb
    if (xmlDocContentDumpOutput(ctxt, doc) < 0)
Packit 423ecb
        return(-1);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveTree:
Packit 423ecb
 * @ctxt:  a document saving context
Packit 423ecb
 * @node:  the top node of the subtree to save
Packit 423ecb
 *
Packit 423ecb
 * Save a subtree starting at the node parameter to a saving context
Packit 423ecb
 * TODO: The function is not fully implemented yet as it does not return the
Packit 423ecb
 * byte count but 0 instead
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of byte written or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
long
Packit 423ecb
xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
Packit 423ecb
{
Packit 423ecb
    long ret = 0;
Packit 423ecb
Packit 423ecb
    if ((ctxt == NULL) || (node == NULL)) return(-1);
Packit 423ecb
    xmlNodeDumpOutputInternal(ctxt, node);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveFlush:
Packit 423ecb
 * @ctxt:  a document saving context
Packit 423ecb
 *
Packit 423ecb
 * Flush a document saving context, i.e. make sure that all bytes have
Packit 423ecb
 * been output.
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of byte written or -1 in case of error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveFlush(xmlSaveCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    if (ctxt == NULL) return(-1);
Packit 423ecb
    if (ctxt->buf == NULL) return(-1);
Packit 423ecb
    return(xmlOutputBufferFlush(ctxt->buf));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveClose:
Packit 423ecb
 * @ctxt:  a document saving context
Packit 423ecb
 *
Packit 423ecb
 * Close a document saving context, i.e. make sure that all bytes have
Packit 423ecb
 * been output and free the associated data.
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of byte written or -1 in case of error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveClose(xmlSaveCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    if (ctxt == NULL) return(-1);
Packit 423ecb
    ret = xmlSaveFlush(ctxt);
Packit 423ecb
    xmlFreeSaveCtxt(ctxt);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveSetEscape:
Packit 423ecb
 * @ctxt:  a document saving context
Packit 423ecb
 * @escape:  the escaping function
Packit 423ecb
 *
Packit 423ecb
 * Set a custom escaping function to be used for text in element content
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if successful or -1 in case of error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
Packit 423ecb
{
Packit 423ecb
    if (ctxt == NULL) return(-1);
Packit 423ecb
    ctxt->escape = escape;
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveSetAttrEscape:
Packit 423ecb
 * @ctxt:  a document saving context
Packit 423ecb
 * @escape:  the escaping function
Packit 423ecb
 *
Packit 423ecb
 * Set a custom escaping function to be used for text in attribute content
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if successful or -1 in case of error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
Packit 423ecb
{
Packit 423ecb
    if (ctxt == NULL) return(-1);
Packit 423ecb
    ctxt->escapeAttr = escape;
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Public entry points based on buffers			*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufAttrSerializeTxtContent:
Packit 423ecb
 * @buf:  and xmlBufPtr output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @attr: the attribute node
Packit 423ecb
 * @string: the text content
Packit 423ecb
 *
Packit 423ecb
 * Serialize text attribute values to an xmlBufPtr
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlBufAttrSerializeTxtContent(xmlBufPtr buf, xmlDocPtr doc,
Packit 423ecb
                              xmlAttrPtr attr, const xmlChar * string)
Packit 423ecb
{
Packit 423ecb
    xmlChar *base, *cur;
Packit 423ecb
Packit 423ecb
    if (string == NULL)
Packit 423ecb
        return;
Packit 423ecb
    base = cur = (xmlChar *) string;
Packit 423ecb
    while (*cur != 0) {
Packit 423ecb
        if (*cur == '\n') {
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
            xmlBufAdd(buf, BAD_CAST "
", 5);
Packit 423ecb
            cur++;
Packit 423ecb
            base = cur;
Packit 423ecb
        } else if (*cur == '\r') {
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
            xmlBufAdd(buf, BAD_CAST "
", 5);
Packit 423ecb
            cur++;
Packit 423ecb
            base = cur;
Packit 423ecb
        } else if (*cur == '\t') {
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
            xmlBufAdd(buf, BAD_CAST "	", 4);
Packit 423ecb
            cur++;
Packit 423ecb
            base = cur;
Packit 423ecb
        } else if (*cur == '"') {
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
            xmlBufAdd(buf, BAD_CAST """, 6);
Packit 423ecb
            cur++;
Packit 423ecb
            base = cur;
Packit 423ecb
        } else if (*cur == '<') {
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
            xmlBufAdd(buf, BAD_CAST "<", 4);
Packit 423ecb
            cur++;
Packit 423ecb
            base = cur;
Packit 423ecb
        } else if (*cur == '>') {
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
            xmlBufAdd(buf, BAD_CAST ">", 4);
Packit 423ecb
            cur++;
Packit 423ecb
            base = cur;
Packit 423ecb
        } else if (*cur == '&') {
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
            xmlBufAdd(buf, BAD_CAST "&", 5);
Packit 423ecb
            cur++;
Packit 423ecb
            base = cur;
Packit 423ecb
        } else if ((*cur >= 0x80) && (cur[1] != 0) &&
Packit 423ecb
	           ((doc == NULL) || (doc->encoding == NULL))) {
Packit 423ecb
            /*
Packit 423ecb
             * We assume we have UTF-8 content.
Packit 423ecb
             */
Packit 423ecb
            unsigned char tmp[12];
Packit 423ecb
            int val = 0, l = 1;
Packit 423ecb
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
            if (*cur < 0xC0) {
Packit 423ecb
                xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
Packit 423ecb
		xmlSerializeHexCharRef(tmp, *cur);
Packit 423ecb
                xmlBufAdd(buf, (xmlChar *) tmp, -1);
Packit 423ecb
                cur++;
Packit 423ecb
                base = cur;
Packit 423ecb
                continue;
Packit 423ecb
            } else if (*cur < 0xE0) {
Packit 423ecb
                val = (cur[0]) & 0x1F;
Packit 423ecb
                val <<= 6;
Packit 423ecb
                val |= (cur[1]) & 0x3F;
Packit 423ecb
                l = 2;
Packit 423ecb
            } else if ((*cur < 0xF0) && (cur [2] != 0)) {
Packit 423ecb
                val = (cur[0]) & 0x0F;
Packit 423ecb
                val <<= 6;
Packit 423ecb
                val |= (cur[1]) & 0x3F;
Packit 423ecb
                val <<= 6;
Packit 423ecb
                val |= (cur[2]) & 0x3F;
Packit 423ecb
                l = 3;
Packit 423ecb
            } else if ((*cur < 0xF8) && (cur [2] != 0) && (cur[3] != 0)) {
Packit 423ecb
                val = (cur[0]) & 0x07;
Packit 423ecb
                val <<= 6;
Packit 423ecb
                val |= (cur[1]) & 0x3F;
Packit 423ecb
                val <<= 6;
Packit 423ecb
                val |= (cur[2]) & 0x3F;
Packit 423ecb
                val <<= 6;
Packit 423ecb
                val |= (cur[3]) & 0x3F;
Packit 423ecb
                l = 4;
Packit 423ecb
            }
Packit 423ecb
            if ((l == 1) || (!IS_CHAR(val))) {
Packit 423ecb
                xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
Packit 423ecb
		xmlSerializeHexCharRef(tmp, *cur);
Packit 423ecb
                xmlBufAdd(buf, (xmlChar *) tmp, -1);
Packit 423ecb
                cur++;
Packit 423ecb
                base = cur;
Packit 423ecb
                continue;
Packit 423ecb
            }
Packit 423ecb
            /*
Packit 423ecb
             * We could do multiple things here. Just save
Packit 423ecb
             * as a char ref
Packit 423ecb
             */
Packit 423ecb
	    xmlSerializeHexCharRef(tmp, val);
Packit 423ecb
            xmlBufAdd(buf, (xmlChar *) tmp, -1);
Packit 423ecb
            cur += l;
Packit 423ecb
            base = cur;
Packit 423ecb
        } else {
Packit 423ecb
            cur++;
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
    if (base != cur)
Packit 423ecb
        xmlBufAdd(buf, base, cur - base);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlAttrSerializeTxtContent:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @attr: the attribute node
Packit 423ecb
 * @string: the text content
Packit 423ecb
 *
Packit 423ecb
 * Serialize text attribute values to an xml simple buffer
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
Packit 423ecb
                           xmlAttrPtr attr, const xmlChar * string)
Packit 423ecb
{
Packit 423ecb
    xmlBufPtr buffer;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (string == NULL))
Packit 423ecb
        return;
Packit 423ecb
    buffer = xmlBufFromBuffer(buf);
Packit 423ecb
    if (buffer == NULL)
Packit 423ecb
        return;
Packit 423ecb
    xmlBufAttrSerializeTxtContent(buffer, doc, attr, string);
Packit 423ecb
    xmlBufBackToBuffer(buffer);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNodeDump:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @cur:  the current node
Packit 423ecb
 * @level: the imbrication level for indenting
Packit 423ecb
 * @format: is formatting allowed
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML node, recursive behaviour,children are printed too.
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 * Since this is using xmlBuffer structures it is limited to 2GB and somehow
Packit 423ecb
 * deprecated, use xmlBufNodeDump() instead.
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of bytes written to the buffer or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
Packit 423ecb
            int format)
Packit 423ecb
{
Packit 423ecb
    xmlBufPtr buffer;
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (cur == NULL))
Packit 423ecb
        return(-1);
Packit 423ecb
    buffer = xmlBufFromBuffer(buf);
Packit 423ecb
    if (buffer == NULL)
Packit 423ecb
        return(-1);
Packit 423ecb
    ret = xmlBufNodeDump(buffer, doc, cur, level, format);
Packit 423ecb
    xmlBufBackToBuffer(buffer);
Packit 423ecb
    if (ret > INT_MAX)
Packit 423ecb
        return(-1);
Packit 423ecb
    return((int) ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufNodeDump:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @cur:  the current node
Packit 423ecb
 * @level: the imbrication level for indenting
Packit 423ecb
 * @format: is formatting allowed
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML node, recursive behaviour,children are printed too.
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of bytes written to the buffer, in case of error 0
Packit 423ecb
 *     is returned or @buf stores the error
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
size_t
Packit 423ecb
xmlBufNodeDump(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
Packit 423ecb
            int format)
Packit 423ecb
{
Packit 423ecb
    size_t use;
Packit 423ecb
    int ret;
Packit 423ecb
    xmlOutputBufferPtr outbuf;
Packit 423ecb
    int oldalloc;
Packit 423ecb
Packit 423ecb
    xmlInitParser();
Packit 423ecb
Packit 423ecb
    if (cur == NULL) {
Packit 423ecb
#ifdef DEBUG_TREE
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                        "xmlNodeDump : node == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
        return (-1);
Packit 423ecb
    }
Packit 423ecb
    if (buf == NULL) {
Packit 423ecb
#ifdef DEBUG_TREE
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                        "xmlNodeDump : buf == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
        return (-1);
Packit 423ecb
    }
Packit 423ecb
    outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
Packit 423ecb
    if (outbuf == NULL) {
Packit 423ecb
        xmlSaveErrMemory("creating buffer");
Packit 423ecb
        return (-1);
Packit 423ecb
    }
Packit 423ecb
    memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
Packit 423ecb
    outbuf->buffer = buf;
Packit 423ecb
    outbuf->encoder = NULL;
Packit 423ecb
    outbuf->writecallback = NULL;
Packit 423ecb
    outbuf->closecallback = NULL;
Packit 423ecb
    outbuf->context = NULL;
Packit 423ecb
    outbuf->written = 0;
Packit 423ecb
Packit 423ecb
    use = xmlBufUse(buf);
Packit 423ecb
    oldalloc = xmlBufGetAllocationScheme(buf);
Packit 423ecb
    xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT);
Packit 423ecb
    xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
Packit 423ecb
    xmlBufSetAllocationScheme(buf, oldalloc);
Packit 423ecb
    xmlFree(outbuf);
Packit 423ecb
    ret = xmlBufUse(buf) - use;
Packit 423ecb
    return (ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlElemDump:
Packit 423ecb
 * @f:  the FILE * for the output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @cur:  the current node
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML/HTML node, recursive behaviour, children are printed too.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
Packit 423ecb
{
Packit 423ecb
    xmlOutputBufferPtr outbuf;
Packit 423ecb
Packit 423ecb
    xmlInitParser();
Packit 423ecb
Packit 423ecb
    if (cur == NULL) {
Packit 423ecb
#ifdef DEBUG_TREE
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                        "xmlElemDump : cur == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
#ifdef DEBUG_TREE
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                        "xmlElemDump : doc == NULL\n");
Packit 423ecb
    }
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    outbuf = xmlOutputBufferCreateFile(f, NULL);
Packit 423ecb
    if (outbuf == NULL)
Packit 423ecb
        return;
Packit 423ecb
    if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
        htmlNodeDumpOutput(outbuf, doc, cur, NULL);
Packit 423ecb
#else
Packit 423ecb
	xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
Packit 423ecb
#endif /* LIBXML_HTML_ENABLED */
Packit 423ecb
    } else
Packit 423ecb
        xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
Packit 423ecb
    xmlOutputBufferClose(outbuf);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Saving functions front-ends				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNodeDumpOutput:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @cur:  the current node
Packit 423ecb
 * @level: the imbrication level for indenting
Packit 423ecb
 * @format: is formatting allowed
Packit 423ecb
 * @encoding:  an optional encoding string
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML node, recursive behaviour, children are printed too.
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
Packit 423ecb
                  int level, int format, const char *encoding)
Packit 423ecb
{
Packit 423ecb
    xmlSaveCtxt ctxt;
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
    xmlDtdPtr dtd;
Packit 423ecb
    int is_xhtml = 0;
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    xmlInitParser();
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (cur == NULL)) return;
Packit 423ecb
Packit 423ecb
    if (encoding == NULL)
Packit 423ecb
        encoding = "UTF-8";
Packit 423ecb
Packit 423ecb
    memset(&ctxt, 0, sizeof(ctxt));
Packit 423ecb
    ctxt.doc = doc;
Packit 423ecb
    ctxt.buf = buf;
Packit 423ecb
    ctxt.level = level;
Packit 423ecb
    ctxt.format = format ? 1 : 0;
Packit 423ecb
    ctxt.encoding = (const xmlChar *) encoding;
Packit 423ecb
    xmlSaveCtxtInit(&ctxt);
Packit 423ecb
    ctxt.options |= XML_SAVE_AS_XML;
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
    dtd = xmlGetIntSubset(doc);
Packit 423ecb
    if (dtd != NULL) {
Packit 423ecb
	is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
Packit 423ecb
	if (is_xhtml < 0)
Packit 423ecb
	    is_xhtml = 0;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if (is_xhtml)
Packit 423ecb
        xhtmlNodeDumpOutput(&ctxt, cur);
Packit 423ecb
    else
Packit 423ecb
#endif
Packit 423ecb
        xmlNodeDumpOutputInternal(&ctxt, cur);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDocDumpFormatMemoryEnc:
Packit 423ecb
 * @out_doc:  Document to generate XML text from
Packit 423ecb
 * @doc_txt_ptr:  Memory pointer for allocated XML text
Packit 423ecb
 * @doc_txt_len:  Length of the generated XML text
Packit 423ecb
 * @txt_encoding:  Character encoding to use when generating XML text
Packit 423ecb
 * @format:  should formatting spaces been added
Packit 423ecb
 *
Packit 423ecb
 * Dump the current DOM tree into memory using the character encoding specified
Packit 423ecb
 * by the caller.  Note it is up to the caller of this function to free the
Packit 423ecb
 * allocated memory with xmlFree().
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Packit 423ecb
		int * doc_txt_len, const char * txt_encoding,
Packit 423ecb
		int format) {
Packit 423ecb
    xmlSaveCtxt ctxt;
Packit 423ecb
    int                         dummy = 0;
Packit 423ecb
    xmlOutputBufferPtr          out_buff = NULL;
Packit 423ecb
    xmlCharEncodingHandlerPtr   conv_hdlr = NULL;
Packit 423ecb
Packit 423ecb
    if (doc_txt_len == NULL) {
Packit 423ecb
        doc_txt_len = &dummy;   /*  Continue, caller just won't get length */
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if (doc_txt_ptr == NULL) {
Packit 423ecb
        *doc_txt_len = 0;
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    *doc_txt_ptr = NULL;
Packit 423ecb
    *doc_txt_len = 0;
Packit 423ecb
Packit 423ecb
    if (out_doc == NULL) {
Packit 423ecb
        /*  No document, no output  */
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     *  Validate the encoding value, if provided.
Packit 423ecb
     *  This logic is copied from xmlSaveFileEnc.
Packit 423ecb
     */
Packit 423ecb
Packit 423ecb
    if (txt_encoding == NULL)
Packit 423ecb
	txt_encoding = (const char *) out_doc->encoding;
Packit 423ecb
    if (txt_encoding != NULL) {
Packit 423ecb
	conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
Packit 423ecb
	if ( conv_hdlr == NULL ) {
Packit 423ecb
	    xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
Packit 423ecb
		       txt_encoding);
Packit 423ecb
	    return;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
Packit 423ecb
        xmlSaveErrMemory("creating buffer");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    memset(&ctxt, 0, sizeof(ctxt));
Packit 423ecb
    ctxt.doc = out_doc;
Packit 423ecb
    ctxt.buf = out_buff;
Packit 423ecb
    ctxt.level = 0;
Packit 423ecb
    ctxt.format = format ? 1 : 0;
Packit 423ecb
    ctxt.encoding = (const xmlChar *) txt_encoding;
Packit 423ecb
    xmlSaveCtxtInit(&ctxt);
Packit 423ecb
    ctxt.options |= XML_SAVE_AS_XML;
Packit 423ecb
    xmlDocContentDumpOutput(&ctxt, out_doc);
Packit 423ecb
    xmlOutputBufferFlush(out_buff);
Packit 423ecb
    if (out_buff->conv != NULL) {
Packit 423ecb
	*doc_txt_len = xmlBufUse(out_buff->conv);
Packit 423ecb
	*doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->conv), *doc_txt_len);
Packit 423ecb
    } else {
Packit 423ecb
	*doc_txt_len = xmlBufUse(out_buff->buffer);
Packit 423ecb
	*doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->buffer),*doc_txt_len);
Packit 423ecb
    }
Packit 423ecb
    (void)xmlOutputBufferClose(out_buff);
Packit 423ecb
Packit 423ecb
    if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
Packit 423ecb
        *doc_txt_len = 0;
Packit 423ecb
        xmlSaveErrMemory("creating output");
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    return;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDocDumpMemory:
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 * @mem:  OUT: the memory pointer
Packit 423ecb
 * @size:  OUT: the memory length
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document in memory and return the #xmlChar * and it's size
Packit 423ecb
 * in bytes. It's up to the caller to free the memory with xmlFree().
Packit 423ecb
 * The resulting byte array is zero terminated, though the last 0 is not
Packit 423ecb
 * included in the returned size.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
Packit 423ecb
    xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDocDumpFormatMemory:
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 * @mem:  OUT: the memory pointer
Packit 423ecb
 * @size:  OUT: the memory length
Packit 423ecb
 * @format:  should formatting spaces been added
Packit 423ecb
 *
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document in memory and return the #xmlChar * and it's size.
Packit 423ecb
 * It's up to the caller to free the memory with xmlFree().
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
Packit 423ecb
    xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDocDumpMemoryEnc:
Packit 423ecb
 * @out_doc:  Document to generate XML text from
Packit 423ecb
 * @doc_txt_ptr:  Memory pointer for allocated XML text
Packit 423ecb
 * @doc_txt_len:  Length of the generated XML text
Packit 423ecb
 * @txt_encoding:  Character encoding to use when generating XML text
Packit 423ecb
 *
Packit 423ecb
 * Dump the current DOM tree into memory using the character encoding specified
Packit 423ecb
 * by the caller.  Note it is up to the caller of this function to free the
Packit 423ecb
 * allocated memory with xmlFree().
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Packit 423ecb
	            int * doc_txt_len, const char * txt_encoding) {
Packit 423ecb
    xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Packit 423ecb
	                      txt_encoding, 0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDocFormatDump:
Packit 423ecb
 * @f:  the FILE*
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 * @format: should formatting spaces been added
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document to an open FILE.
Packit 423ecb
 *
Packit 423ecb
 * returns: the number of bytes written or -1 in case of failure.
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Packit 423ecb
    xmlSaveCtxt ctxt;
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
    const char * encoding;
Packit 423ecb
    xmlCharEncodingHandlerPtr handler = NULL;
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    if (cur == NULL) {
Packit 423ecb
#ifdef DEBUG_TREE
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlDocDump : document == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    encoding = (const char *) cur->encoding;
Packit 423ecb
Packit 423ecb
    if (encoding != NULL) {
Packit 423ecb
	handler = xmlFindCharEncodingHandler(encoding);
Packit 423ecb
	if (handler == NULL) {
Packit 423ecb
	    xmlFree((char *) cur->encoding);
Packit 423ecb
	    cur->encoding = NULL;
Packit 423ecb
	    encoding = NULL;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    buf = xmlOutputBufferCreateFile(f, handler);
Packit 423ecb
    if (buf == NULL) return(-1);
Packit 423ecb
    memset(&ctxt, 0, sizeof(ctxt));
Packit 423ecb
    ctxt.doc = cur;
Packit 423ecb
    ctxt.buf = buf;
Packit 423ecb
    ctxt.level = 0;
Packit 423ecb
    ctxt.format = format ? 1 : 0;
Packit 423ecb
    ctxt.encoding = (const xmlChar *) encoding;
Packit 423ecb
    xmlSaveCtxtInit(&ctxt);
Packit 423ecb
    ctxt.options |= XML_SAVE_AS_XML;
Packit 423ecb
    xmlDocContentDumpOutput(&ctxt, cur);
Packit 423ecb
Packit 423ecb
    ret = xmlOutputBufferClose(buf);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDocDump:
Packit 423ecb
 * @f:  the FILE*
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document to an open FILE.
Packit 423ecb
 *
Packit 423ecb
 * returns: the number of bytes written or -1 in case of failure.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlDocDump(FILE *f, xmlDocPtr cur) {
Packit 423ecb
    return(xmlDocFormatDump (f, cur, 0));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveFileTo:
Packit 423ecb
 * @buf:  an output I/O buffer
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 * @encoding:  the encoding if any assuming the I/O layer handles the trancoding
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document to an I/O buffer.
Packit 423ecb
 * Warning ! This call xmlOutputBufferClose() on buf which is not available
Packit 423ecb
 * after this call.
Packit 423ecb
 *
Packit 423ecb
 * returns: the number of bytes written or -1 in case of failure.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Packit 423ecb
    xmlSaveCtxt ctxt;
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    if (buf == NULL) return(-1);
Packit 423ecb
    if (cur == NULL) {
Packit 423ecb
        xmlOutputBufferClose(buf);
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    memset(&ctxt, 0, sizeof(ctxt));
Packit 423ecb
    ctxt.doc = cur;
Packit 423ecb
    ctxt.buf = buf;
Packit 423ecb
    ctxt.level = 0;
Packit 423ecb
    ctxt.format = 0;
Packit 423ecb
    ctxt.encoding = (const xmlChar *) encoding;
Packit 423ecb
    xmlSaveCtxtInit(&ctxt);
Packit 423ecb
    ctxt.options |= XML_SAVE_AS_XML;
Packit 423ecb
    xmlDocContentDumpOutput(&ctxt, cur);
Packit 423ecb
    ret = xmlOutputBufferClose(buf);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveFormatFileTo:
Packit 423ecb
 * @buf:  an output I/O buffer
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 * @encoding:  the encoding if any assuming the I/O layer handles the trancoding
Packit 423ecb
 * @format: should formatting spaces been added
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document to an I/O buffer.
Packit 423ecb
 * Warning ! This call xmlOutputBufferClose() on buf which is not available
Packit 423ecb
 * after this call.
Packit 423ecb
 *
Packit 423ecb
 * returns: the number of bytes written or -1 in case of failure.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
Packit 423ecb
                    const char *encoding, int format)
Packit 423ecb
{
Packit 423ecb
    xmlSaveCtxt ctxt;
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    if (buf == NULL) return(-1);
Packit 423ecb
    if ((cur == NULL) ||
Packit 423ecb
        ((cur->type != XML_DOCUMENT_NODE) &&
Packit 423ecb
	 (cur->type != XML_HTML_DOCUMENT_NODE))) {
Packit 423ecb
        xmlOutputBufferClose(buf);
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    memset(&ctxt, 0, sizeof(ctxt));
Packit 423ecb
    ctxt.doc = cur;
Packit 423ecb
    ctxt.buf = buf;
Packit 423ecb
    ctxt.level = 0;
Packit 423ecb
    ctxt.format = format ? 1 : 0;
Packit 423ecb
    ctxt.encoding = (const xmlChar *) encoding;
Packit 423ecb
    xmlSaveCtxtInit(&ctxt);
Packit 423ecb
    ctxt.options |= XML_SAVE_AS_XML;
Packit 423ecb
    xmlDocContentDumpOutput(&ctxt, cur);
Packit 423ecb
    ret = xmlOutputBufferClose(buf);
Packit 423ecb
    return (ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveFormatFileEnc:
Packit 423ecb
 * @filename:  the filename or URL to output
Packit 423ecb
 * @cur:  the document being saved
Packit 423ecb
 * @encoding:  the name of the encoding to use or NULL.
Packit 423ecb
 * @format:  should formatting spaces be added.
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document to a file or an URL.
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of bytes written or -1 in case of error.
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
Packit 423ecb
			const char * encoding, int format ) {
Packit 423ecb
    xmlSaveCtxt ctxt;
Packit 423ecb
    xmlOutputBufferPtr buf;
Packit 423ecb
    xmlCharEncodingHandlerPtr handler = NULL;
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    if (cur == NULL)
Packit 423ecb
	return(-1);
Packit 423ecb
Packit 423ecb
    if (encoding == NULL)
Packit 423ecb
	encoding = (const char *) cur->encoding;
Packit 423ecb
Packit 423ecb
    if (encoding != NULL) {
Packit 423ecb
Packit 423ecb
	    handler = xmlFindCharEncodingHandler(encoding);
Packit 423ecb
	    if (handler == NULL)
Packit 423ecb
		return(-1);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
#ifdef HAVE_ZLIB_H
Packit 423ecb
    if (cur->compression < 0) cur->compression = xmlGetCompressMode();
Packit 423ecb
#endif
Packit 423ecb
    /*
Packit 423ecb
     * save the content to a temp buffer.
Packit 423ecb
     */
Packit 423ecb
    buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
Packit 423ecb
    if (buf == NULL) return(-1);
Packit 423ecb
    memset(&ctxt, 0, sizeof(ctxt));
Packit 423ecb
    ctxt.doc = cur;
Packit 423ecb
    ctxt.buf = buf;
Packit 423ecb
    ctxt.level = 0;
Packit 423ecb
    ctxt.format = format ? 1 : 0;
Packit 423ecb
    ctxt.encoding = (const xmlChar *) encoding;
Packit 423ecb
    xmlSaveCtxtInit(&ctxt);
Packit 423ecb
    ctxt.options |= XML_SAVE_AS_XML;
Packit 423ecb
Packit 423ecb
    xmlDocContentDumpOutput(&ctxt, cur);
Packit 423ecb
Packit 423ecb
    ret = xmlOutputBufferClose(buf);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveFileEnc:
Packit 423ecb
 * @filename:  the filename (or URL)
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 * @encoding:  the name of an encoding (or NULL)
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document, converting it to the given encoding
Packit 423ecb
 *
Packit 423ecb
 * returns: the number of bytes written or -1 in case of failure.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
Packit 423ecb
    return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveFormatFile:
Packit 423ecb
 * @filename:  the filename (or URL)
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 * @format:  should formatting spaces been added
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document to a file. Will use compression if
Packit 423ecb
 * compiled in and enabled. If @filename is "-" the stdout file is
Packit 423ecb
 * used. If @format is set then the document will be indented on output.
Packit 423ecb
 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Packit 423ecb
 * or xmlKeepBlanksDefault(0) was called
Packit 423ecb
 *
Packit 423ecb
 * returns: the number of bytes written or -1 in case of failure.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Packit 423ecb
    return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSaveFile:
Packit 423ecb
 * @filename:  the filename (or URL)
Packit 423ecb
 * @cur:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dump an XML document to a file. Will use compression if
Packit 423ecb
 * compiled in and enabled. If @filename is "-" the stdout file is
Packit 423ecb
 * used.
Packit 423ecb
 * returns: the number of bytes written or -1 in case of failure.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSaveFile(const char *filename, xmlDocPtr cur) {
Packit 423ecb
    return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
Packit 423ecb
#define bottom_xmlsave
Packit 423ecb
#include "elfgcchack.h"