aboutsummaryrefslogtreecommitdiffstats
path: root/xmlwriter.c
diff options
context:
space:
mode:
authorDaniel Veillard <veillard@src.gnome.org>2004-01-02 20:04:23 +0000
committerDaniel Veillard <veillard@src.gnome.org>2004-01-02 20:04:23 +0000
commit2cca446b86a77022d5b847b106544aede7207dbc (patch)
treee10ed048cd214c3adbd0b303074a478b09890f3d /xmlwriter.c
parentaae10527fa2f3356a425f1fa42ffed4a9073e6b6 (diff)
downloadandroid_external_libxml2-2cca446b86a77022d5b847b106544aede7207dbc.tar.gz
android_external_libxml2-2cca446b86a77022d5b847b106544aede7207dbc.tar.bz2
android_external_libxml2-2cca446b86a77022d5b847b106544aede7207dbc.zip
applied the patch from Lucas Brasilino to add indentation support to
* include/libxml/xmlwriter.h xmlwriter.c: applied the patch from Lucas Brasilino to add indentation support to xmlWriter Daniel
Diffstat (limited to 'xmlwriter.c')
-rw-r--r--xmlwriter.c113
1 files changed, 109 insertions, 4 deletions
diff --git a/xmlwriter.c b/xmlwriter.c
index 6b2fea28..c75af546 100644
--- a/xmlwriter.c
+++ b/xmlwriter.c
@@ -61,8 +61,9 @@ struct _xmlTextWriter {
xmlListPtr nodes; /* element name stack */
xmlListPtr nsstack; /* name spaces stack */
int level;
- char indent; /* indent amount */
- char ichar; /* indent character */
+ int indent; /* enable indent */
+ int doindent; /* internal indent flag */
+ xmlChar *ichar; /* indent character */
char qchar; /* character used for quoting attribute values */
};
@@ -83,6 +84,7 @@ static xmlChar *xmlTextWriterVSprintf(const char *format, va_list argptr);
static int xmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len,
const unsigned char *data);
static void xmlTextWriterStartDocumentCallback(void *ctx);
+static int xmlTextWriterWriteIndent (xmlTextWriterPtr writer);
/**
* xmlNewTextWriter:
@@ -129,9 +131,11 @@ xmlNewTextWriter(xmlOutputBufferPtr out)
}
ret->out = out;
- ret->ichar = ' ';
+ ret->ichar = xmlStrdup (BAD_CAST " ");
ret->qchar = '"';
+ if (!ret->ichar) return NULL;
+
return ret;
}
@@ -165,6 +169,8 @@ xmlNewTextWriterFilename(const char *uri, int compression)
return NULL;
}
+ ret->indent = 0;
+ ret->doindent = 0;
return ret;
}
@@ -542,11 +548,12 @@ xmlTextWriterEndDocument(xmlTextWriterPtr writer)
}
}
+ if (!writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if(count < 0)
return -1;
sum += count;
-
+ }
return sum;
}
@@ -653,6 +660,11 @@ xmlTextWriterWriteComment(xmlTextWriterPtr writer, const xmlChar * content)
}
}
+ if (writer->indent) {
+ count = xmlOutputBufferWriteString (writer->out, "\n");
+ sum += count;
+ }
+
count = xmlOutputBufferWriteString(writer->out, "<!--");
if (count < 0)
return -1;
@@ -667,6 +679,11 @@ xmlTextWriterWriteComment(xmlTextWriterPtr writer, const xmlChar * content)
return -1;
sum += count;
+ if (writer->indent) {
+ count = xmlOutputBufferWriteString (writer->out, "\n");
+ sum += count;
+ }
+
return sum;
}
@@ -706,6 +723,8 @@ xmlTextWriterStartElement(xmlTextWriterPtr writer, const xmlChar * name)
if (count < 0)
return -1;
sum += count;
+ if (writer->indent)
+ count = xmlOutputBufferWriteString (writer->out, "\n");
p->state = XML_TEXTWRITER_TEXT;
break;
default:
@@ -733,6 +752,11 @@ xmlTextWriterStartElement(xmlTextWriterPtr writer, const xmlChar * name)
xmlListPushFront(writer->nodes, p);
+ if (writer->indent) {
+ count = xmlTextWriterWriteIndent (writer);
+ sum += count;
+ }
+
count = xmlOutputBufferWriteString(writer->out, "<");
if (count < 0)
return -1;
@@ -842,6 +866,11 @@ xmlTextWriterEndElement(xmlTextWriterPtr writer)
sum += count;
break;
case XML_TEXTWRITER_TEXT:
+ if ((writer->indent) && (writer->doindent)) {
+ count = xmlTextWriterWriteIndent (writer);
+ sum += count;
+ writer->doindent = 1;
+ } else writer->doindent = 1;
count = xmlOutputBufferWriteString(writer->out, "</");
if (count < 0)
return -1;
@@ -860,6 +889,11 @@ xmlTextWriterEndElement(xmlTextWriterPtr writer)
return -1;
}
+ if (writer->indent) {
+ count = xmlOutputBufferWriteString (writer->out, "\n");
+ sum += count;
+ }
+
xmlListPopFront(writer->nodes);
return sum;
}
@@ -1946,6 +1980,7 @@ xmlTextWriterWriteElement(xmlTextWriterPtr writer, const xmlChar * name,
if (count == -1)
return -1;
sum += count;
+ writer->doindent = 0;
count = xmlTextWriterEndElement(writer);
if (count == -1)
return -1;
@@ -3955,4 +3990,74 @@ xmlTextWriterStartDocumentCallback(void *ctx)
}
}
+/**
+ * xmlTextWriterSetIndent:
+ * @writer: the xmlTextWriterPtr
+ * @indent: do indentation?
+ *
+ * Set indentation output. indent = 0 do not indentation. indent > 0 do indentation.
+ *
+ * Returns -1 on error or 0 otherwise.
+ */
+int
+xmlTextWriterSetIndent (xmlTextWriterPtr writer, int indent)
+{
+ if (indent < 0)
+ return -1;
+
+ writer->indent = indent;
+ writer->doindent = 1;
+
+ return 0;
+}
+
+/**
+ * xmlTextWriterSetIndentString:
+ * @writer: the xmlTextWriterPtr
+ * @str: the xmlChar string
+ *
+ * Set string indentation.
+ *
+ * Returns -1 on error or 0 otherwise.
+ */
+int
+xmlTextWriterSetIndentString (xmlTextWriterPtr writer, xmlChar *str)
+{
+
+ if (!str)
+ return -1;
+
+ writer->ichar = xmlStrdup (str);
+
+ if (!writer->ichar)
+ return -1;
+ else
+ return 0;
+}
+
+/**
+ * xmlTextWriterWriteIndent:
+ * @writer: the xmlTextWriterPtr
+ *
+ * Write indent string.
+ *
+ * Returns -1 on error or the number of strings written.
+ */
+static int
+xmlTextWriterWriteIndent (xmlTextWriterPtr writer)
+{
+ int lksize;
+ int i;
+ int ret;
+
+ lksize = xmlListSize (writer->nodes);
+ for (i = 0; i < (lksize-1); i++) {
+ ret = xmlOutputBufferWriteString (writer->out, writer->ichar);
+ if (ret == -1)
+ break;
+ }
+
+ return (ret == -1)?ret:i;
+}
+
#endif