abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r437742 - in /incubator/abdera/java/trunk: build/ parser/src/main/java/org/apache/abdera/parser/stax/util/ parser/src/main/resources/ parser/src/main/resources/META-INF/ parser/src/main/resources/META-INF/services/
Date Mon, 28 Aug 2006 16:27:17 GMT
Author: jmsnell
Date: Mon Aug 28 09:27:16 2006
New Revision: 437742

URL: http://svn.apache.org/viewvc?rev=437742&view=rev
Log:
Pretty Printing!  NamedWriter that will output the XML with line breaks and indents

Example use:

  Abdera abdera = new Abdera();
  Factory factory = abdera.getFactory();

  Entry entry = factory.newEntry();
  entry.setId(FOMHelper.generateUuid());
  entry.setUpdated(new java.util.Date());
  entry.addAuthor("James");
  entry.setContent("<a><b><c><d>foo</d></c></b></a>",
Content.Type.XML);
  
  Writer pw = (Writer)abdera.getWriterFactory().getWriter("PrettyXML");
  pw.writeTo(entry,System.out);

The output produced looks like;

  <?xml version="1.0" encoding="UTF-8"?>
  <entry xmlns="http://www.w3.org/2005/Atom">
    <id>urn:uuid:...</id>
    <updated>...</updated>
    <author>
      <name>James</name>
    </author>
    <content type="application/xml">
      <a>
        <b>
          <c>
            <d>foo</d>
          </c>
        </b>
      </a>
    </content>
  </entry>

The width of the indent is currently hard coded at 2 spaces.  We could make that 
configurable.

Added:
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java
    incubator/abdera/java/trunk/parser/src/main/resources/
    incubator/abdera/java/trunk/parser/src/main/resources/META-INF/
    incubator/abdera/java/trunk/parser/src/main/resources/META-INF/services/
    incubator/abdera/java/trunk/parser/src/main/resources/META-INF/services/org.apache.abdera.writer.NamedWriter
Modified:
    incubator/abdera/java/trunk/build/build.xml

Modified: incubator/abdera/java/trunk/build/build.xml
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/build/build.xml?rev=437742&r1=437741&r2=437742&view=diff
==============================================================================
--- incubator/abdera/java/trunk/build/build.xml (original)
+++ incubator/abdera/java/trunk/build/build.xml Mon Aug 28 09:27:16 2006
@@ -43,6 +43,7 @@
   <property name="core.jar" value="${dist}/${ant.project.name}.core.${version}.jar" />
   <property name="parser" value="${basedir}/parser" />
   <property name="parser.src" value="${parser}/src/main/java" />
+  <property name="parser.resources" value="${parser}/src/main/resources" />
   <property name="parser.test" value="${parser}/src/test" />
   <property name="parser.test.java" value="${parser}/src/test/java" />
   <property name="parser.test.resources" value="${parser}/src/test/resources" />
@@ -204,6 +205,9 @@
         <include name="LICENSE" />
         <include name="NOTICE" />
       </fileset>
+    </copy>
+    <copy todir="${parser.work}">
+      <fileset dir="${parser.resources}" includes="**/*" />
     </copy>
     <copy todir="${test}">
       <fileset dir="${parser.test.resources}">

Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java?rev=437742&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java
(added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java
Mon Aug 28 09:27:16 2006
@@ -0,0 +1,262 @@
+package org.apache.abdera.parser.stax.util;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.writer.NamedWriter;
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.util.StAXUtils;
+
+public class PrettyWriter implements NamedWriter {
+
+  public String getName() {
+    return "PrettyXML";
+  }
+
+  public Object write(Base base) throws IOException {
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    writeTo(base,out);
+    return out.toString();
+  }
+
+  public void writeTo(Base base, OutputStream out) throws IOException {
+    writeTo(base,new OutputStreamWriter(out));
+  }
+
+  public void writeTo(Base base, Writer out) throws IOException {
+    try {
+      XMLStreamWriter w = StAXUtils.createXMLStreamWriter(out);
+      XMLStreamWriter pw = new PrettyStreamWriter(w);
+      OMElement om = (base instanceof Document) ? 
+        (OMElement)((Document)base).getRoot() : 
+        (OMElement)base;
+      if (om.getParent() != null && om.getParent() instanceof OMDocument) {
+        OMDocument doc = (OMDocument) om.getParent();
+        pw.writeStartDocument(doc.getCharsetEncoding(), doc.getXMLVersion());
+      }
+      om.serialize(pw);
+      pw.writeEndDocument();
+    } catch (XMLStreamException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private static class PrettyStreamWriter implements XMLStreamWriter {
+    
+    private static final int INDENT = 2;
+    
+    private XMLStreamWriter internal = null;
+    private int depth = 0;
+    private boolean prev_was_end_element = false;
+    
+    public PrettyStreamWriter(XMLStreamWriter writer) {
+      this.internal = writer;
+    }
+
+    public void close() throws XMLStreamException {
+      internal.close();
+    }
+
+    public void flush() throws XMLStreamException {
+      internal.flush();
+    }
+
+    public NamespaceContext getNamespaceContext() {
+      return internal.getNamespaceContext();
+    }
+
+    public String getPrefix(String arg0) throws XMLStreamException {
+      return internal.getPrefix(arg0);
+    }
+
+    public Object getProperty(String arg0) throws IllegalArgumentException {
+      return internal.getProperty(arg0);
+    }
+
+    public void setDefaultNamespace(String arg0) throws XMLStreamException {
+      internal.setDefaultNamespace(arg0);
+    }
+
+    public void setNamespaceContext(NamespaceContext arg0) throws XMLStreamException {
+      internal.setNamespaceContext(arg0);
+    }
+
+    public void setPrefix(String arg0, String arg1) throws XMLStreamException {
+      internal.setPrefix(arg0, arg1);
+    }
+
+    public void writeAttribute(String arg0, String arg1) throws XMLStreamException {
+      internal.writeAttribute(arg0, arg1);
+      prev_was_end_element = false;
+    }
+
+    public void writeAttribute(String arg0, String arg1, String arg2) throws XMLStreamException
{
+      internal.writeAttribute(arg0, arg1, arg2);
+      prev_was_end_element = false;
+    }
+
+    public void writeAttribute(String arg0, String arg1, String arg2, String arg3) throws
XMLStreamException {
+      internal.writeAttribute(arg0, arg1, arg2, arg3);
+      prev_was_end_element = false;
+    }
+
+    public void writeCData(String arg0) throws XMLStreamException {
+      internal.writeCData(arg0);
+      prev_was_end_element = false;
+    }
+
+    public void writeCharacters(String arg0) throws XMLStreamException {
+      internal.writeCharacters(arg0);
+      prev_was_end_element = false;
+    }
+
+    public void writeCharacters(char[] arg0, int arg1, int arg2) throws XMLStreamException
{
+      internal.writeCharacters(arg0,arg1,arg2);
+      prev_was_end_element = false;
+    }
+
+    public void writeComment(String arg0) throws XMLStreamException {
+      writeIndent();
+      internal.writeComment(arg0);
+      prev_was_end_element = true;
+    }
+
+    public void writeDTD(String arg0) throws XMLStreamException {
+      internal.writeDTD(arg0);
+      prev_was_end_element = true;
+    }
+
+    public void writeDefaultNamespace(String arg0) throws XMLStreamException {
+      internal.writeDefaultNamespace(arg0);
+      prev_was_end_element = false;
+    }
+
+    public void writeEmptyElement(String arg0) throws XMLStreamException {
+      writeIndent();
+      internal.writeEmptyElement(arg0);
+      prev_was_end_element = true;
+    }
+
+    public void writeEmptyElement(String arg0, String arg1) throws XMLStreamException {
+      writeIndent();
+      internal.writeEmptyElement(arg0, arg1);
+      prev_was_end_element = true;
+    }
+
+    public void writeEmptyElement(String arg0, String arg1, String arg2) throws XMLStreamException
{
+      writeIndent();
+      internal.writeEmptyElement(arg0, arg1, arg2);
+      prev_was_end_element = true;
+    }
+
+    public void writeEndDocument() throws XMLStreamException {
+      internal.writeEndDocument();
+      prev_was_end_element = false;
+    }
+
+    public void writeEndElement() throws XMLStreamException {
+      depth--;
+      if (prev_was_end_element) writeIndent();
+      internal.writeEndElement();
+      prev_was_end_element = true;
+    }
+
+    public void writeEntityRef(String arg0) throws XMLStreamException {
+      internal.writeEntityRef(arg0);
+      prev_was_end_element = false;
+    }
+
+    public void writeNamespace(String arg0, String arg1) throws XMLStreamException {
+      internal.writeNamespace(arg0, arg1);
+      prev_was_end_element = false;
+    }
+
+    public void writeProcessingInstruction(String arg0) throws XMLStreamException {
+      writeIndent();
+      internal.writeProcessingInstruction(arg0);
+      prev_was_end_element = true;
+    }
+
+    public void writeProcessingInstruction(String arg0, String arg1) throws XMLStreamException
{
+      writeIndent();
+      internal.writeProcessingInstruction(arg0,arg1);
+      prev_was_end_element = true;
+    }
+
+    public void writeStartDocument() throws XMLStreamException {
+      internal.writeStartDocument();
+      prev_was_end_element = false;
+    }
+
+    public void writeStartDocument(String arg0) throws XMLStreamException {
+      internal.writeStartDocument(arg0);
+      prev_was_end_element = false;
+    }
+
+    public void writeStartDocument(String arg0, String arg1) throws XMLStreamException {
+      internal.writeStartDocument(arg0, arg1);
+      prev_was_end_element = false;
+    }
+
+    public void writeStartElement(String arg0) throws XMLStreamException {
+      writeIndent();
+      depth++;
+      internal.writeStartElement(arg0);
+      prev_was_end_element = false;
+    }
+
+    public void writeStartElement(String arg0, String arg1) throws XMLStreamException {
+      writeIndent();
+      depth++;
+      internal.writeStartElement(arg0,arg1);
+      prev_was_end_element = false;
+    }
+
+    public void writeStartElement(String arg0, String arg1, String arg2) throws XMLStreamException
{
+      writeIndent();
+      depth++;
+      internal.writeStartElement(arg0,arg1,arg2);
+      prev_was_end_element = false;
+    }
+   
+    private void writeIndent() throws XMLStreamException {
+      internal.writeCharacters("\n");
+      char[] spaces = getSpaces();
+      internal.writeCharacters(spaces, 0, spaces.length);
+    }
+    
+    private char[] getSpaces() {
+      char[] spaces = new char[INDENT * depth];
+      java.util.Arrays.fill(spaces, ' ');
+      return spaces;
+    }
+  }
+}

Added: incubator/abdera/java/trunk/parser/src/main/resources/META-INF/services/org.apache.abdera.writer.NamedWriter
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/resources/META-INF/services/org.apache.abdera.writer.NamedWriter?rev=437742&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/resources/META-INF/services/org.apache.abdera.writer.NamedWriter
(added)
+++ incubator/abdera/java/trunk/parser/src/main/resources/META-INF/services/org.apache.abdera.writer.NamedWriter
Mon Aug 28 09:27:16 2006
@@ -0,0 +1 @@
+org.apache.abdera.parser.stax.util.PrettyWriter
\ No newline at end of file



Mime
View raw message