abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r595579 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/util/ core/src/main/java/org/apache/abdera/writer/ examples/src/main/java/org/apache/abdera/examples/simple/ parser/src/main/java/org/apache/abdera/parser/stax/
Date Fri, 16 Nov 2007 06:54:40 GMT
Author: jmsnell
Date: Thu Nov 15 22:54:39 2007
New Revision: 595579

URL: http://svn.apache.org/viewvc?rev=595579&view=rev
Log:
Add an option to enable automatic indenting and formatting of the StreamWriter output

e.g.

 abdera.newStreamWriter()
   .setAutoIndent(true)
   .startDocument()
   .startFeed()
   .writeTitle("foo")
   .endFeed()
   .endDocument();
   

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/StreamWriterExample.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java?rev=595579&r1=595578&r2=595579&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java
Thu Nov 15 22:54:39 2007
@@ -40,6 +40,7 @@
   protected final String name;
   protected boolean autoflush = false;
   protected boolean autoclose = false;
+  protected boolean autoindent = false;
   
   protected AbstractStreamWriter(String name) {
     this.name = name;
@@ -638,4 +639,8 @@
       return startElement(name,namespace,null);
   }
   
+  public StreamWriter setAutoIndent(boolean indent) {
+    this.autoindent = indent;
+    return this;
+  }
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java?rev=595579&r1=595578&r2=595579&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
Thu Nov 15 22:54:39 2007
@@ -727,5 +727,15 @@
    */
   StreamWriter writeAttribute(QName qname, String value);
   
+  /**
+   * Write a new line and indent
+   */
+  StreamWriter indent();
+  
+  /**
+   * True to enable automatic indenting
+   */
+  StreamWriter setAutoIndent(boolean indent);
+  
 }
 

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/StreamWriterExample.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/StreamWriterExample.java?rev=595579&r1=595578&r2=595579&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/StreamWriterExample.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/StreamWriterExample.java
Thu Nov 15 22:54:39 2007
@@ -37,6 +37,7 @@
       abdera.newStreamWriter()
         .setOutputStream(System.out,"UTF-8")
         .setAutoflush(false)
+        .setAutoIndent(true)
         .startDocument()
           .startFeed()
             .writeId("http://example.org")
@@ -67,6 +68,9 @@
           .startElement(new QName("a","b","c"))
             .startElement(new QName("x","y","z"))
               .writeElementText("This is a test")
+              .startElement("a")
+              .writeElementText("foo")
+              .endElement()
             .endElement()
           .endElement()
         .endContent()

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java?rev=595579&r1=595578&r2=595579&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
Thu Nov 15 22:54:39 2007
@@ -19,6 +19,7 @@
 
 import java.io.OutputStream;
 import java.io.Writer;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Stack;
@@ -37,6 +38,8 @@
   private static final String NAME = "default";
   
   private XMLStreamWriter writer;
+  private int depth = 0;
+  private int textwritten = 0;
   
   public StaxStreamWriter() {
     super(NAME);
@@ -116,7 +119,10 @@
  
   public StreamWriter endElement() {
     try {
-      pop();
+      if (autoindent && textwritten == 0) {
+        pop();
+        indent();
+      } else pop();
       writer.writeEndElement();
       if (autoflush) writer.flush();
     } catch(XMLStreamException e) {
@@ -147,11 +153,21 @@
     String namespace, 
     String prefix) {
     try {
+      if (autoindent && textwritten == 0) indent();
       push();
-      writer.writeStartElement(
-        prefix,
-        name,
-        namespace);
+      if (prefix != null) {
+        writer.writeStartElement(
+          prefix,
+          name,
+          namespace);
+      } else if (namespace != null) {
+        writer.writeStartElement(
+          name, 
+          namespace);
+      } else {
+        writer.writeStartElement(
+          name);
+      }
       writeNamespace(prefix,namespace,false);
       if (autoflush) writer.flush();
     } catch(XMLStreamException e) {
@@ -162,6 +178,7 @@
   
   public StreamWriter writeElementText(String value) {
     try {
+      textwritten++;
       writer.writeCharacters(value);
       if (autoflush) writer.flush();
     } catch(XMLStreamException e) {
@@ -172,6 +189,7 @@
 
   public StreamWriter writeComment(String value) {
     try {
+      if (autoindent) indent();
       writer.writeComment(value);
       if (autoflush) writer.flush();
     } catch(XMLStreamException e) {
@@ -182,6 +200,7 @@
 
   public StreamWriter writePI(String value) {
     try {
+      if (autoindent) indent();
       writer.writeProcessingInstruction(value);
       if (autoflush) writer.flush();
     } catch(XMLStreamException e) {
@@ -192,6 +211,7 @@
 
   public StreamWriter writePI(String value, String target) {
     try {
+      if (autoindent) indent();
       writer.writeProcessingInstruction(value,target);
       if (autoflush) writer.flush();
     } catch(XMLStreamException e) {
@@ -232,9 +252,12 @@
   
   private void push() {
     namespaces.push(new HashMap<String,String>());
+    depth++;
   }
   
   private void pop() {
+    depth--;
+    if (textwritten > 0) textwritten--;
     if (!namespaces.isEmpty()) namespaces.pop();
   }
    
@@ -259,6 +282,18 @@
   public StreamWriter flush() {
     try {
       writer.flush();
+    } catch (XMLStreamException e) {
+      throw new RuntimeException(e);
+    }
+    return this;
+  }
+  
+  public StreamWriter indent() {
+    try {
+      char[] indent = new char[depth*2];
+      Arrays.fill(indent, ' ');
+      writer.writeCharacters("\n");
+      writer.writeCharacters(indent,0,indent.length);
     } catch (XMLStreamException e) {
       throw new RuntimeException(e);
     }



Mime
View raw message