[xml] xmlWriter's indent support: initial patch



Hi

        Here goes a initial patch (against CVS) to implement indent
support to xmlWriter API. Only code creating documents using:

xmlNewTextWriterFilename()
xmlTextWriterWriteComment()
xmlTextWriterStartElement()
xmlTextWriterEndElement()
xmlTextWriterWriteString()

        can be indented by now. I'll continue implementing to others
one.
        This patch have tooked a while 'cause I've implemented in two
other different ways. This one is the third implementation, and looks
much clear for me :)


--

[]'s
Lucas Brasilino
brasilino recife pe gov br
http://www.recife.pe.gov.br
Emprel -        Empresa Municipal de Informatica (pt_BR)
                Municipal Computing Enterprise (en_US)
Recife - Pernambuco - Brasil
Fone: +55-81-34167078
Index: xmlwriter.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/xmlwriter.c,v
retrieving revision 1.8
diff -u -r1.8 xmlwriter.c
--- xmlwriter.c 9 Dec 2003 15:14:26 -0000       1.8
+++ xmlwriter.c 29 Dec 2003 18:26:43 -0000
@@ -61,8 +61,9 @@
     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 */
+    char *ichar;            /* indent character */
     char qchar;             /* character used for quoting attribute values */
 };
 
@@ -129,7 +130,7 @@
     }
 
     ret->out = out;
-    ret->ichar = ' ';
+    ret->ichar = " ";
     ret->qchar = '"';
 
     return ret;
@@ -139,13 +140,14 @@
  * xmlNewTextWriterFilename:
  * @uri:  the URI of the resource for the output
  * @compression:  compress the output?
+ * @indent: indent output?
  *
  * Create a new xmlNewTextWriter structure with @uri as output
  *
  * Returns the new xmlTextWriterPtr or NULL in case of error
  */
 xmlTextWriterPtr
-xmlNewTextWriterFilename(const char *uri, int compression)
+xmlNewTextWriterFilename(const char *uri, int compression, int indent)
 {
     xmlTextWriterPtr ret;
     xmlOutputBufferPtr out;
@@ -165,6 +167,8 @@
         return NULL;
     }
 
+    ret->indent = indent;
+    ret->doindent = indent;
     return ret;
 }
 
@@ -542,11 +546,12 @@
         }
     }
 
+    if (!writer->indent) {
     count = xmlOutputBufferWriteString(writer->out, "\n");
     if(count < 0)
         return -1;
     sum += count;
-
+    }
     return sum;
 }
 
@@ -653,6 +658,11 @@
         }
     }
 
+    if (writer->indent) {
+      count = xmlOutputBufferWriteString (writer->out, "\n");
+      sum += count;
+    }
+    
     count = xmlOutputBufferWriteString(writer->out, "<!--");
     if (count < 0)
         return -1;
@@ -667,6 +677,11 @@
         return -1;
     sum += count;
 
+    if (writer->indent) {
+      count = xmlOutputBufferWriteString (writer->out, "\n");
+      sum += count;
+    }
+
     return sum;
 }
 
@@ -706,6 +721,8 @@
                     if (count < 0)
                         return -1;
                     sum += count;
+                   if (writer->indent)
+                     count = xmlOutputBufferWriteString (writer->out, "\n");
                     p->state = XML_TEXTWRITER_TEXT;
                     break;
                default:
@@ -733,6 +750,11 @@
 
     xmlListPushFront(writer->nodes, p);
 
+    if (writer->indent) { 
+      count = xmlTextWriterWriteIndent (writer);
+      sum += count;
+    }
+
     count = xmlOutputBufferWriteString(writer->out, "<");
     if (count < 0)
         return -1;
@@ -842,6 +864,11 @@
             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 +887,11 @@
             return -1;
     }
 
+    if (writer->indent) {
+      count = xmlOutputBufferWriteString (writer->out, "\n");
+      sum += count;
+    }
+
     xmlListPopFront(writer->nodes);
     return sum;
 }
@@ -1946,6 +1978,7 @@
     if (count == -1)
         return -1;
     sum += count;
+    writer->doindent = 0;
     count = xmlTextWriterEndElement(writer);
     if (count == -1)
         return -1;
@@ -3953,6 +3986,20 @@
             ctxt->myDoc->URL =
                 xmlStrdup((const xmlChar *) ctxt->input->filename);
     }
+}
+
+int
+xmlTextWriterWriteIndent (xmlTextWriterPtr writer)
+{
+  int lksize;
+  int i;
+  
+  lksize = xmlListSize (writer->nodes);
+  for (i = 0; i < (lksize-1); i++) {
+    xmlOutputBufferWriteString (writer->out, writer->ichar);
+  }
+
+  return i;
 }
 
 #endif
Index: doc/examples/testWriter.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/doc/examples/testWriter.c,v
retrieving revision 1.3
diff -u -r1.3 testWriter.c
--- doc/examples/testWriter.c   17 Dec 2003 15:08:51 -0000      1.3
+++ doc/examples/testWriter.c   29 Dec 2003 18:26:46 -0000
@@ -55,7 +55,7 @@
     xmlTextWriterPtr writer;
 
     /* Create a new XmlWriter for uri, with no compression. */
-    writer = xmlNewTextWriterFilename(uri, 0);
+    writer = xmlNewTextWriterFilename(uri, 0, 1);
     if (writer == NULL) {
         printf("testXmlwriterFilename: Error creating the xml writer\n");
         return;
Index: include/libxml/xmlwriter.h
===================================================================
RCS file: /cvs/gnome/gnome-xml/include/libxml/xmlwriter.h,v
retrieving revision 1.5
diff -u -r1.5 xmlwriter.h
--- include/libxml/xmlwriter.h  5 Dec 2003 14:57:41 -0000       1.5
+++ include/libxml/xmlwriter.h  29 Dec 2003 18:26:47 -0000
@@ -27,7 +27,7 @@
 XMLPUBFUN xmlTextWriterPtr XMLCALL
        xmlNewTextWriter                (xmlOutputBufferPtr out);
 XMLPUBFUN xmlTextWriterPtr XMLCALL
-        xmlNewTextWriterFilename(const char *uri, int compression);
+        xmlNewTextWriterFilename(const char *uri, int compression, int indent);
 XMLPUBFUN xmlTextWriterPtr XMLCALL
         xmlNewTextWriterMemory(xmlBufferPtr buf, int compression);
 XMLPUBFUN xmlTextWriterPtr XMLCALL
@@ -410,6 +410,7 @@
  * misc
  */
 XMLPUBFUN int XMLCALL xmlTextWriterFlush(xmlTextWriterPtr writer);
+XMLPUBFUN int XMLCALL xmlTextWriterWriteIndent (xmlTextWriterPtr writer);
 
 #ifdef __cplusplus
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]