abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r559905 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/model/ core/src/main/java/org/apache/abdera/util/ core/src/main/java/org/apache/abdera/writer/ extensions/src/main/java/org/apache/abdera/ext/json/ parser/src/m...
Date Thu, 26 Jul 2007 17:53:21 GMT
Author: jmsnell
Date: Thu Jul 26 10:53:17 2007
New Revision: 559905

URL: http://svn.apache.org/viewvc?view=rev&rev=559905
Log:
Refactor the restricted char reader a bit.
Add a new WriterOptions mechanism that allows setting the charset encoding for serialized
output, the use of compression codecs when serializing to an OutputStream, and auto closing
of the stream or writer when the serialization is complete.

Example...

    Abdera abdera = new Abdera();
    Entry entry = abdera.newEntry();
    entry.setTitle("1");
    
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    WriterOptions writeoptions = entry.getDefaultWriterOptions();
    writeoptions.setCompressionCodecs(CompressionCodec.GZIP);
    writeoptions.setCharset("UTF-16");
    writeoptions.setAutoClose(true);
    entry.getDocument().writeTo(out,writeoptions);    
    out.close();
    
    byte[] bytes = out.toByteArray();
    
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    Parser parser = abdera.getParser();
    ParserOptions options = parser.getDefaultParserOptions();
    options.setCompressionCodecs(CompressionCodec.GZIP);
    Document<Entry> doc = abdera.getParser().parse(in,null,options);
    
    doc.getRoot().toString();

Added:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriterOptions.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharReader.java
      - copied, changed from r559700, incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharFilter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlUtil.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterOptions.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterOptions.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharReader.java
      - copied, changed from r559700, incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharFilter.java
Removed:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharFilter.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharFilter.java
Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Base.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/Writer.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PeekAheadInputStream.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java
    incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/EncodingTest.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Base.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Base.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Base.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Base.java Thu Jul 26 10:53:17 2007
@@ -22,6 +22,7 @@
 import java.io.Writer;
 
 import org.apache.abdera.factory.Factory;
+import org.apache.abdera.writer.WriterOptions;
 
 /**
  * The Base interface provides the basis for the Feed Object Model API and 
@@ -34,6 +35,42 @@
 public interface Base 
   extends Cloneable {
 
+  WriterOptions getDefaultWriterOptions();
+  
+  /**
+   * Serializes the model component out to the specified stream
+   */
+  void writeTo(OutputStream out, WriterOptions options) throws IOException;
+  
+  /**
+   * Serializes the model component out to the specified java.io.Writer
+   */
+  void writeTo(Writer out, WriterOptions options) throws IOException;
+  
+  /**
+   * Serializes the model component out to the specified stream using the 
+   * given abdera writer
+   */
+  void writeTo(org.apache.abdera.writer.Writer writer, OutputStream out) throws IOException;
+  
+  /**
+   * Serializes the model component out to the specified java.io.Writer using the 
+   * given abdera writer
+   */
+  void writeTo(org.apache.abdera.writer.Writer writer, Writer out) throws IOException;
+  
+  /**
+   * Serializes the model component out to the specified stream using the 
+   * given abdera writer
+   */
+  void writeTo(org.apache.abdera.writer.Writer writer, OutputStream out, WriterOptions options) throws IOException;
+  
+  /**
+   * Serializes the model component out to the specified java.io.Writer using the 
+   * given abdera writer
+   */
+  void writeTo(org.apache.abdera.writer.Writer writer, Writer out, WriterOptions options) throws IOException;
+  
   /**
    * Serializes the model component out to the specified stream
    * @param out The java.io.OutputStream to use when serializing the Base. The charset encoding specified for the document will be used

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java Thu Jul 26 10:53:17 2007
@@ -26,6 +26,7 @@
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.i18n.lang.Lang;
 import org.apache.abdera.util.EntityTag;
+import org.apache.abdera.util.XmlUtil.XMLVersion;
 
 /**
  * <p>The top level artifact of the Feed Object Model.  The Parser component
@@ -165,4 +166,8 @@
   
   void setMustPreserveWhitespace(boolean preserve);
   
+  /**
+   * Get the XMLVersion used by this document
+   */
+  XMLVersion getXmlVersion();
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java Thu Jul 26 10:53:17 2007
@@ -30,6 +30,7 @@
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.i18n.lang.Lang;
+import org.apache.abdera.writer.WriterOptions;
 
 public abstract class ElementWrapper 
   implements Element {
@@ -234,6 +235,33 @@
   public void setMustPreserveWhitespace(boolean preserve) {
     internal.setMustPreserveWhitespace(preserve);
   }
-  
+
+  public void writeTo(OutputStream out, WriterOptions options) throws IOException {
+    internal.writeTo(out,options);
+  }
+
+  public void writeTo(org.apache.abdera.writer.Writer writer, OutputStream out, WriterOptions options) throws IOException {
+    internal.writeTo(writer,out,options);
+  }
+
+  public void writeTo(org.apache.abdera.writer.Writer writer, OutputStream out) throws IOException {
+    internal.writeTo(writer,out);
+  }
+
+  public void writeTo(org.apache.abdera.writer.Writer writer, Writer out, WriterOptions options) throws IOException {
+    internal.writeTo(writer,out,options);
+  }
+
+  public void writeTo(org.apache.abdera.writer.Writer writer, Writer out) throws IOException {
+    internal.writeTo(writer,out);
+  }
+
+  public void writeTo(Writer out, WriterOptions options) throws IOException {
+    internal.writeTo(out,options);
+  }
+
+  public WriterOptions getDefaultWriterOptions() {
+    return internal.getDefaultWriterOptions();
+  }
   
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractNamedWriter.java Thu Jul 26 10:53:17 2007
@@ -23,6 +23,7 @@
  * Base abstract implementation of NamedWriter
  */
 public abstract class AbstractNamedWriter 
+  extends AbstractWriter
   implements NamedWriter {
 
   protected final String name;

Added: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriter.java?view=auto&rev=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriter.java (added)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriter.java Thu Jul 26 10:53:17 2007
@@ -0,0 +1,98 @@
+/*
+* 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.
+*/
+package org.apache.abdera.util;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.DeflaterOutputStream;
+
+import org.apache.abdera.model.Base;
+import org.apache.abdera.writer.Writer;
+import org.apache.abdera.writer.WriterOptions;
+
+public abstract class AbstractWriter 
+  implements Writer {
+
+  protected WriterOptions options;
+  
+  public WriterOptions getDefaultWriterOptions() {
+    if (options == null) options = initDefaultWriterOptions();
+
+    // Make a copy of the options, so that changes to it don't result in
+    // changes to the Parser's defaults.  Also, this allows us to remain
+    // thread safe without having to make ParseOptions implementations
+    // synchronized.
+
+    try {
+      return (WriterOptions) options.clone();
+    } catch (CloneNotSupportedException cnse) {
+      // This shouldn't actually happen
+      throw new RuntimeException(cnse);
+    }
+  }
+
+  protected abstract WriterOptions initDefaultWriterOptions();
+  
+  public synchronized void setDefaultWriterOptions(WriterOptions options) {
+    // Ok, we need to make a defensive copy of the options, since otherwise
+    // the caller still has access to the object, which means our access to
+    // it isn't certain to be thread safe.
+
+    try {
+      this.options = 
+        (options != null) ? 
+          (WriterOptions) options.clone() : 
+          initDefaultWriterOptions();
+    } catch (CloneNotSupportedException cnse) {
+      // This shouldn't actually happen
+      throw new RuntimeException(cnse);
+    }
+  }
+
+  public Object write(Base base) throws IOException {
+    return write(base,getDefaultWriterOptions());
+  }
+
+  public void writeTo(Base base, OutputStream out) throws IOException {
+    writeTo(base,out,getDefaultWriterOptions());
+  }
+
+  public void writeTo(Base base, java.io.Writer out) throws IOException {
+    writeTo(base,out,getDefaultWriterOptions());
+  }
+   
+  protected OutputStream getCompressedOutputStream(
+    OutputStream out, 
+    WriterOptions options)
+      throws IOException {
+    if (options.getCompressionCodecs() != null) {
+      out = CompressionUtil.getEncodedOutputStream(
+        out, options.getCompressionCodecs());
+    }
+    return out;
+  }
+  
+  protected void finishCompressedOutputStream(
+    OutputStream out, 
+    WriterOptions options) 
+      throws IOException {
+    if (options.getCompressionCodecs() != null) {
+      ((DeflaterOutputStream)out).finish();
+    }
+  }
+}

Added: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriterOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriterOptions.java?view=auto&rev=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriterOptions.java (added)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractWriterOptions.java Thu Jul 26 10:53:17 2007
@@ -0,0 +1,60 @@
+/*
+* 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.
+*/
+package org.apache.abdera.util;
+
+import org.apache.abdera.util.CompressionUtil.CompressionCodec;
+import org.apache.abdera.writer.WriterOptions;
+
+public abstract class AbstractWriterOptions 
+  implements WriterOptions {
+
+  protected String charset = null;
+  protected CompressionCodec[] codecs = null;
+  protected boolean autoclose = false;
+  
+  public Object clone() throws CloneNotSupportedException {
+    AbstractWriterOptions copy = (AbstractWriterOptions) super.clone();
+    return copy;
+  }
+  
+  public CompressionCodec[] getCompressionCodecs() {
+    return codecs;
+  }
+
+  public void setCompressionCodecs(CompressionCodec... codecs) {
+    this.codecs = codecs;
+  }
+
+  public String getCharset() {
+    return charset;
+  }
+  
+  public void setCharset(String charset) {
+    this.charset = charset;
+  }
+
+  public boolean getAutoClose() {
+    return autoclose;
+  }
+
+  public void setAutoClose(boolean autoclose) {
+    this.autoclose = autoclose;
+  }
+  
+  
+}

Copied: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharReader.java (from r559700, incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharFilter.java)
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharReader.java?view=diff&rev=559905&p1=incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharFilter.java&r1=559700&p2=incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharReader.java&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharFilter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlRestrictedCharReader.java Thu Jul 26 10:53:17 2007
@@ -7,60 +7,58 @@
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 
-import org.apache.abdera.i18n.ChainableBitSet;
+import org.apache.abdera.util.XmlUtil.XMLVersion;
 
 /**
  * A reader implementation that filters out characters that are not allowed
- * in XML 1.0 or XML 1.1 documents.  The default mode is to assume XML 1.0.
+ * in XML 1.0 or XML 1.1 documents.  The default xMLVersion is to assume XML 1.0.
  * 
  * By default, invalid characters are simply removed from the stream.  
  * Alternatively, a replacement character can be provided so long as it
  * is a valid XML character itself.
  */
-public class XmlRestrictedCharFilter 
+public class XmlRestrictedCharReader 
   extends FilterReader {
 
   /**
-   * The mode determines which set of restrictions to apply depending 
+   * The XMLVersion determines which set of restrictions to apply depending 
    * on the XML version being parsed
    */
-  public enum Mode { XML10, XML11 };
-  
-  private final ChainableBitSet set;
+  private final XMLVersion version;
   private final char replacement;
   
-  protected XmlRestrictedCharFilter(InputStream in) {
+  public XmlRestrictedCharReader(InputStream in) {
     this(new InputStreamReader(in));
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     InputStream in, 
     String charset) 
       throws UnsupportedEncodingException {
     this(new InputStreamReader(in,charset));
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     InputStream in, 
-    Mode mode) {
-      this(new InputStreamReader(in),mode);
+    XMLVersion version) {
+      this(new InputStreamReader(in),version);
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     InputStream in, 
     String charset, 
-    Mode mode) 
+    XMLVersion version) 
       throws UnsupportedEncodingException {
-    this(new InputStreamReader(in,charset),mode);
+    this(new InputStreamReader(in,charset),version);
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     InputStream in, 
     char replacement) {
       this(new InputStreamReader(in),replacement);
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     InputStream in, 
     String charset,
     char replacement) 
@@ -68,50 +66,49 @@
     this(new InputStreamReader(in,charset),replacement);
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     InputStream in, 
-    Mode mode, 
+    XMLVersion version, 
     char replacement) {
-      this(new InputStreamReader(in),mode, replacement);
+      this(new InputStreamReader(in),version, replacement);
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     InputStream in, 
     String charset, 
-    Mode mode,
+    XMLVersion version,
     char replacement) 
       throws UnsupportedEncodingException {
-    this(new InputStreamReader(in,charset),mode,replacement);
+    this(new InputStreamReader(in,charset),version,replacement);
   }
   
-  
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     Reader in) {
-      this(in,Mode.XML10,(char)0);
+      this(in,XMLVersion.XML10,(char)0);
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     Reader in, 
-    Mode mode) {
-      this(in,mode,(char)0);
+    XMLVersion version) {
+      this(in,version,(char)0);
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     Reader in, 
     char replacement) {
-      this(in,Mode.XML10,replacement);
+      this(in,XMLVersion.XML10,replacement);
   }
   
-  protected XmlRestrictedCharFilter(
+  public XmlRestrictedCharReader(
     Reader in, 
-    Mode mode, 
+    XMLVersion version, 
     char replacement) {
       super(in);
-      this.set = mode == Mode.XML10 ? restrictedchar10 : restrictedchar11;
+      this.version = version;
       this.replacement = replacement;
       if (replacement != 0 && 
           ((!Character.isValidCodePoint(replacement)) || 
-          set.get(replacement))) 
+          XmlUtil.restricted(version,replacement))) 
             throw new IllegalArgumentException();
   }
 
@@ -119,10 +116,10 @@
   public int read() throws IOException {
     int c = -1;
     if (replacement == 0) {
-      while(((c = super.read()) != -1 && set.get(c))) {}
+      while(((c = super.read()) != -1 && XmlUtil.restricted(version,c))) {}
     } else {
       c = super.read();
-      if (c != -1 && set.get(c)) c = replacement;
+      if (c != -1 && XmlUtil.restricted(version,c)) c = replacement;
     }
     return c;
   }
@@ -138,18 +135,4 @@
     return n - off;
   }
 
-  private final ChainableBitSet restrictedchar10 =
-    new ChainableBitSet().set2(0, 8)
-                         .set2(11, 12)
-                         .set2(14, 31)
-                         .set2(55296, 57343)
-                         .set2(65534, 65535);
-
-  private final ChainableBitSet restrictedchar11 = 
-    new ChainableBitSet().set2(0, 8)
-                         .set2(11, 12)
-                         .set2(14, 31)
-                         .set2(127, 159)
-                         .set2(55296, 57343)
-                         .set2(65534, 65535);
 }

Added: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlUtil.java?view=auto&rev=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlUtil.java (added)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/XmlUtil.java Thu Jul 26 10:53:17 2007
@@ -0,0 +1,60 @@
+/*
+* 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.
+*/
+package org.apache.abdera.util;
+
+import org.apache.abdera.i18n.ChainableBitSet;
+
+public class XmlUtil {
+
+  public enum XMLVersion { XML10, XML11 };
+  
+  private static final ChainableBitSet restrictedchar10 =
+    new ChainableBitSet().set2(0, 8)
+                         .set2(11, 12)
+                         .set2(14, 31)
+                         .set2(55296, 57343)
+                         .set2(65534, 65535);
+
+  private static final ChainableBitSet restrictedchar11 = 
+    new ChainableBitSet().set2(0, 8)
+                         .set2(11, 12)
+                         .set2(14, 31)
+                         .set2(127, 159)
+                         .set2(55296, 57343)
+                         .set2(65534, 65535);
+  
+  public static boolean restricted(XMLVersion version, char c) {
+    return restricted(version,(int)c);
+  }
+  
+  public static boolean restricted(XMLVersion version, int c) {
+    switch(version) {
+      case XML11:
+        return restrictedchar11.get(c);
+      default:
+        return restrictedchar10.get(c);
+    }
+  }
+  
+  public static XMLVersion getVersion(String version) {
+    return version == null ? XMLVersion.XML10 :
+           version.equals("1.0") ? XMLVersion.XML10 :
+           version.equals("1.1") ? XMLVersion.XML11 : 
+           XMLVersion.XML10;
+  }
+}

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/Writer.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/Writer.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/Writer.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/Writer.java Thu Jul 26 10:53:17 2007
@@ -46,4 +46,33 @@
    * Return the serialized form of the Abdera Base
    */
   Object write(Base base) throws IOException;
+  
+  
+  /**
+   * Serialized the given Abdera Base to the given outputstream 
+   */
+  void writeTo(Base base, 
+    OutputStream out, 
+    WriterOptions options) 
+      throws IOException;
+  
+  /**
+   * Serialized the given Abdera Base to the given writer  
+   */
+  void writeTo(
+    Base base, 
+    java.io.Writer out,
+    WriterOptions options) 
+      throws IOException;
+
+  /**
+   * Return the serialized form of the Abdera Base
+   */
+  Object write(
+    Base base, 
+    WriterOptions options) 
+      throws IOException;
+  
+  WriterOptions getDefaultWriterOptions();
+  
 }

Added: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterOptions.java?view=auto&rev=559905
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterOptions.java (added)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/WriterOptions.java Thu Jul 26 10:53:17 2007
@@ -0,0 +1,43 @@
+/*
+* 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.
+*/
+package org.apache.abdera.writer;
+
+import org.apache.abdera.util.CompressionUtil.CompressionCodec;
+
+public interface WriterOptions extends Cloneable {
+
+  /**
+   * When writing, use the specified compression codecs
+   */
+  CompressionCodec[] getCompressionCodecs();
+
+  /**
+   * When writing, use the specified compression codecs
+   */
+  void setCompressionCodecs(CompressionCodec... codecs);
+
+  Object clone() throws CloneNotSupportedException;
+  
+  String getCharset();
+  
+  void setCharset(String charset);
+  
+  boolean getAutoClose();
+  
+  void setAutoClose(boolean autoclose);
+}

Modified: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java (original)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java Thu Jul 26 10:53:17 2007
@@ -38,7 +38,9 @@
 import org.apache.abdera.model.Workspace;
 import org.apache.abdera.model.Content.Type;
 import org.apache.abdera.util.AbstractNamedWriter;
+import org.apache.abdera.util.AbstractWriterOptions;
 import org.apache.abdera.writer.NamedWriter;
+import org.apache.abdera.writer.WriterOptions;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -65,7 +67,7 @@
     return NAME;
   }
 
-  public Object write(Base base) throws IOException {
+  public Object write(Base base, WriterOptions options) throws IOException {
     try {
       return toJSON(base).toString();
     } catch (Exception e) {
@@ -73,19 +75,21 @@
     }
   }
 
-  public void writeTo(Base base, OutputStream out) throws IOException {
+  public void writeTo(Base base, OutputStream out, WriterOptions options) throws IOException {
     try {
       Object result = toJSON(base);
       out.write(result.toString().getBytes());
+      if (options.getAutoClose()) out.close();
     } catch (Exception e) {
       throw new IOException(e.getMessage());
     }
   }
 
-  public void writeTo(Base base, java.io.Writer out) throws IOException {
+  public void writeTo(Base base, java.io.Writer out, WriterOptions options) throws IOException {
     try {
       Object result = toJSON(base);
       out.write(result.toString());
+      if (options.getAutoClose()) out.close();
     } catch (Exception e) {
       throw new IOException(e.getMessage());
     }
@@ -286,6 +290,11 @@
       jslinks.put(jslink);
     }
     return jslinks;
+  }
+
+  @Override
+  protected WriterOptions initDefaultWriterOptions() {
+    return new AbstractWriterOptions() {};
   }
 
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java Thu Jul 26 10:53:17 2007
@@ -34,6 +34,10 @@
 import org.apache.abdera.model.Element;
 import org.apache.abdera.model.ElementWrapper;
 import org.apache.abdera.util.EntityTag;
+import org.apache.abdera.util.XmlUtil;
+import org.apache.abdera.util.XmlUtil.XMLVersion;
+import org.apache.abdera.writer.Writer;
+import org.apache.abdera.writer.WriterOptions;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.i18n.lang.Lang;
 import org.apache.axiom.om.OMComment;
@@ -117,6 +121,52 @@
     this.base = new IRI(base);
   }
 
+  public void writeTo(
+    OutputStream out, 
+    WriterOptions options) 
+      throws IOException {
+    FOMWriter writer = new FOMWriter();
+    writer.writeTo(this,out,options);
+  }
+  
+  public void writeTo(
+    java.io.Writer out, 
+    WriterOptions options)
+      throws IOException {
+    FOMWriter writer = new FOMWriter();
+    writer.writeTo(this,out,options);
+  }
+  
+  public void writeTo(
+    Writer writer, 
+    OutputStream out) 
+      throws IOException {
+    writer.writeTo(this,out);
+  }
+  
+  public void writeTo(
+    Writer writer,
+    java.io.Writer out) 
+      throws IOException {
+    writer.writeTo(this,out);
+  }
+  
+  public void writeTo(
+    Writer writer, 
+    OutputStream out,
+    WriterOptions options) 
+      throws IOException {
+    writer.writeTo(this,out,options);
+  }
+  
+  public void writeTo(
+    Writer writer,
+    java.io.Writer out,
+    WriterOptions options) 
+      throws IOException {
+    writer.writeTo(this,out,options);
+  }
+  
   public void writeTo(OutputStream out) throws IOException {
     writeTo(new OutputStreamWriter(out));
   }
@@ -278,5 +328,13 @@
   
   public void setMustPreserveWhitespace(boolean preserve) {
     this.preserve = preserve;
+  }
+
+  public XMLVersion getXmlVersion() {
+    return XmlUtil.getVersion(super.getXMLVersion());
+  }
+  
+  public WriterOptions getDefaultWriterOptions() {
+    return new FOMWriter().getDefaultWriterOptions();
   }
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java Thu Jul 26 10:53:17 2007
@@ -54,6 +54,8 @@
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.MimeTypeHelper;
 import org.apache.abdera.util.URIHelper;
+import org.apache.abdera.writer.Writer;
+import org.apache.abdera.writer.WriterOptions;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.i18n.lang.Lang;
 import org.apache.axiom.om.OMAttribute;
@@ -340,6 +342,52 @@
     return resolved;
   }
 
+  public void writeTo(
+    OutputStream out, 
+    WriterOptions options) 
+      throws IOException {
+    FOMWriter writer = new FOMWriter();
+    writer.writeTo(this,out,options);
+  }
+  
+  public void writeTo(
+    java.io.Writer out, 
+    WriterOptions options)
+      throws IOException {
+    FOMWriter writer = new FOMWriter();
+    writer.writeTo(this,out,options);
+  }
+  
+  public void writeTo(
+    Writer writer, 
+    OutputStream out) 
+      throws IOException {
+    writer.writeTo(this,out);
+  }
+  
+  public void writeTo(
+    Writer writer,
+    java.io.Writer out) 
+      throws IOException {
+    writer.writeTo(this,out);
+  }
+  
+  public void writeTo(
+    Writer writer, 
+    OutputStream out,
+    WriterOptions options) 
+      throws IOException {
+    writer.writeTo(this,out,options);
+  }
+  
+  public void writeTo(
+    Writer writer,
+    java.io.Writer out,
+    WriterOptions options) 
+      throws IOException {
+    writer.writeTo(this,out,options);
+  }
+  
   public void writeTo(OutputStream out) throws IOException {
     writeTo(new OutputStreamWriter(out));
   }
@@ -355,7 +403,6 @@
     }
   }
   
-  
   public <T extends Element>Document<T> getDocument() {
     Document<T> document = null;
     if (parent != null) {
@@ -746,5 +793,9 @@
   public void setText(DataHandler handler) {
     _removeAllChildren();
     addChild(factory.createOMText(handler, true));
+  }
+
+  public WriterOptions getDefaultWriterOptions() {
+    return new FOMWriter().getDefaultWriterOptions();
   }
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java Thu Jul 26 10:53:17 2007
@@ -30,7 +30,7 @@
 import org.apache.abdera.parser.Parser;
 import org.apache.abdera.parser.ParserOptions;
 import org.apache.abdera.parser.stax.util.FOMSniffingInputStream;
-import org.apache.abdera.parser.stax.util.FOMXmlRestrictedCharFilter;
+import org.apache.abdera.parser.stax.util.FOMXmlRestrictedCharReader;
 import org.apache.abdera.util.AbstractParser;
 import org.apache.abdera.util.CompressionUtil;
 import org.apache.abdera.util.Messages;
@@ -107,9 +107,9 @@
       }
       if (options.getFilterRestrictedCharacters()) {
         Reader rdr = (charset == null) ? 
-          new FOMXmlRestrictedCharFilter(
+          new FOMXmlRestrictedCharReader(
             in,options.getFilterRestrictedCharacterReplacement()) :
-          new FOMXmlRestrictedCharFilter(
+          new FOMXmlRestrictedCharReader(
             in,charset,options.getFilterRestrictedCharacterReplacement());
         return parse(StAXUtils.createXMLStreamReader(rdr), base, options);
       } else {
@@ -135,8 +135,8 @@
     try {
       if (options == null) options = getDefaultParserOptions();
       if (options.getFilterRestrictedCharacters() && 
-          !(in instanceof FOMXmlRestrictedCharFilter)) {
-        in = new FOMXmlRestrictedCharFilter(
+          !(in instanceof FOMXmlRestrictedCharReader)) {
+        in = new FOMXmlRestrictedCharReader(
           in,options.getFilterRestrictedCharacterReplacement());
       }
       return parse(StAXUtils.createXMLStreamReader(in), base, options);

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java Thu Jul 26 10:53:17 2007
@@ -20,14 +20,19 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.Writer;
 
 import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.util.AbstractWriter;
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.MimeTypeHelper;
+import org.apache.abdera.writer.WriterOptions;
 
 public class FOMWriter 
+  extends AbstractWriter
   implements org.apache.abdera.writer.NamedWriter {
 
   public FOMWriter() {}
@@ -36,23 +41,37 @@
   
   public void writeTo(
     Base base, 
-    OutputStream out) 
+    OutputStream out, 
+    WriterOptions options) 
       throws IOException {
-        base.writeTo(out);
+    out = getCompressedOutputStream(out, options);
+    String charset = options.getCharset();
+    if (charset != null) {
+      if (base instanceof Document)
+        ((Document)base).setCharset(charset);
+      base.writeTo(new OutputStreamWriter(out,charset));
+    } else {
+      base.writeTo(out);
+    }
+    finishCompressedOutputStream(out, options);
+    if (options.getAutoClose()) out.close();
   }
 
   public void writeTo(
     Base base, 
-    Writer out) 
+    Writer out,
+    WriterOptions options) 
       throws IOException {
-        base.writeTo(out);
+    base.writeTo(out);
+    if (options.getAutoClose()) out.close();
   }
 
   public Object write(
-    Base base) 
+    Base base,
+    WriterOptions options) 
       throws IOException {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
-    writeTo(base, out);
+    writeTo(base, out, options);
     return out.toString();
   }
 
@@ -74,6 +93,11 @@
            MimeTypeHelper.isMatch(mediatype, Constants.APP_MEDIA_TYPE) ||
            MimeTypeHelper.isMatch(mediatype, Constants.CAT_MEDIA_TYPE) ||
            MimeTypeHelper.isMatch(mediatype, Constants.XML_MEDIA_TYPE);
+  }
+
+  @Override
+  protected WriterOptions initDefaultWriterOptions() {
+    return new FOMWriterOptions();
   }
 
 }

Added: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterOptions.java?view=auto&rev=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterOptions.java (added)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriterOptions.java Thu Jul 26 10:53:17 2007
@@ -0,0 +1,25 @@
+/*
+* 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.
+*/
+package org.apache.abdera.parser.stax;
+
+import org.apache.abdera.util.AbstractWriterOptions;
+
+public class FOMWriterOptions 
+  extends AbstractWriterOptions {
+
+}

Copied: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharReader.java (from r559700, incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharFilter.java)
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharReader.java?view=diff&rev=559905&p1=incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharFilter.java&r1=559700&p2=incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharReader.java&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharFilter.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/FOMXmlRestrictedCharReader.java Thu Jul 26 10:53:17 2007
@@ -21,70 +21,71 @@
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 
-import org.apache.abdera.util.XmlRestrictedCharFilter;
+import org.apache.abdera.util.XmlRestrictedCharReader;
+import org.apache.abdera.util.XmlUtil;
 
-public final class FOMXmlRestrictedCharFilter 
-  extends XmlRestrictedCharFilter {
+public final class FOMXmlRestrictedCharReader 
+  extends XmlRestrictedCharReader {
 
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     Reader in) {
       this(new FOMXmlVersionReader(in));
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     FOMXmlVersionReader in) {
-      super(in,getMode(in.getVersion()));
+      super(in,XmlUtil.getVersion(in.getVersion()));
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     Reader in, 
     char replacement) {
       this(new FOMXmlVersionReader(in), replacement);
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     FOMXmlVersionReader in, 
     char replacement) {
-      super(in,getMode(in.getVersion()), replacement);
+      super(in,XmlUtil.getVersion(in.getVersion()), replacement);
   }
 
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     InputStream in) {
       this(new FOMXmlVersionInputStream(in));
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     FOMXmlVersionInputStream in) {
-      super(in,getMode(in.getVersion()));
+      super(in,XmlUtil.getVersion(in.getVersion()));
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     InputStream in, 
     char replacement) {
       this(new FOMXmlVersionInputStream(in), replacement);
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     FOMXmlVersionInputStream in, 
     char replacement) {
-      super(in,getMode(in.getVersion()), replacement);
+      super(in,XmlUtil.getVersion(in.getVersion()), replacement);
   }
 
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     InputStream in, 
     String charset) 
       throws UnsupportedEncodingException {
     this(new FOMXmlVersionInputStream(in),charset);
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     FOMXmlVersionInputStream in, 
     String charset) 
       throws UnsupportedEncodingException {
-    super(in,charset,getMode(in.getVersion()));
+    super(in,charset,XmlUtil.getVersion(in.getVersion()));
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     InputStream in, 
     String charset, 
     char replacement) 
@@ -92,19 +93,12 @@
     this(new FOMXmlVersionInputStream(in), charset, replacement);
   }
   
-  public FOMXmlRestrictedCharFilter(
+  public FOMXmlRestrictedCharReader(
     FOMXmlVersionInputStream in, 
     String charset, 
     char replacement) 
       throws UnsupportedEncodingException {
-    super(in,charset, getMode(in.getVersion()), replacement);
-  }
-  
-  private static Mode getMode(String version) {
-    return version == null ? Mode.XML10 :
-           version.equals("1.0") ? Mode.XML10 :
-           version.equals("1.1") ? Mode.XML11 : 
-           Mode.XML10;
+    super(in,charset, XmlUtil.getVersion(in.getVersion()), replacement);
   }
   
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PeekAheadInputStream.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PeekAheadInputStream.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PeekAheadInputStream.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PeekAheadInputStream.java Thu Jul 26 10:53:17 2007
@@ -80,15 +80,7 @@
   }
 
   @Override
-  public synchronized void unread(byte[] b, int off, int len) throws IOException {
-    // only unread non-null bytes, otherwise, 
-    // we end up stuffing the buffer with a bunch of garbage
-    int c = off;
-    for (;c < Math.min(len, b.length - off);c++) {
-      if (b[c] == 0) break;
-    }
-    len = Math.min(len, c);
-    
+  public synchronized void unread(byte[] b, int off, int len) throws IOException { 
     if (len > pos && pos + len > buf.length) {
       resize(len-pos);
       pos += len-pos;
@@ -117,7 +109,7 @@
   
   public synchronized int peek(byte[] buf, int off, int len) throws IOException {
     int r = read(buf, off, len);
-    unread(buf,off,len);
+    unread(buf,off,r);
     return r;
   }
 

Modified: 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?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/PrettyWriter.java Thu Jul 26 10:53:17 2007
@@ -31,7 +31,9 @@
 import org.apache.abdera.model.Base;
 import org.apache.abdera.model.Document;
 import org.apache.abdera.util.AbstractNamedWriter;
+import org.apache.abdera.util.AbstractWriterOptions;
 import org.apache.abdera.writer.NamedWriter;
+import org.apache.abdera.writer.WriterOptions;
 import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.util.StAXUtils;
@@ -50,29 +52,52 @@
     super("PrettyXML", FORMATS);
   }
   
-  public Object write(Base base) throws IOException {
+  public Object write(
+    Base base, 
+    WriterOptions options) 
+      throws IOException {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
-    writeTo(base,out);
+    writeTo(base,out,options);
     return out.toString();
   }
 
-  public void writeTo(Base base, OutputStream out) throws IOException {
-    writeTo(base,new OutputStreamWriter(out));
+  public void writeTo(
+    Base base, 
+    OutputStream out, 
+    WriterOptions options) 
+      throws IOException {
+    out = getCompressedOutputStream(out, options);
+    String charset = options.getCharset();
+    if (charset != null) {
+      writeTo(base,new OutputStreamWriter(out,charset),options);
+    } else {
+      writeTo(base,new OutputStreamWriter(out),options);
+    }
+    finishCompressedOutputStream(out, options);
+    if (options.getAutoClose()) out.close();
   }
 
-  public void writeTo(Base base, Writer out) throws IOException {
+  public void writeTo(
+    Base base, 
+    Writer out, 
+    WriterOptions options) 
+      throws IOException {
     try {
       XMLStreamWriter w = StAXUtils.createXMLStreamWriter(out);
       XMLStreamWriter pw = new PrettyStreamWriter(w);
       OMElement om = (base instanceof Document) ? 
         (OMElement)((Document)base).getRoot() : 
         (OMElement)base;
+      String charset = options.getCharset();
       if (om.getParent() != null && om.getParent() instanceof OMDocument) {
         OMDocument doc = (OMDocument) om.getParent();
-        pw.writeStartDocument(doc.getCharsetEncoding(), doc.getXMLVersion());
+        pw.writeStartDocument(
+          charset != null ? charset : 
+            doc.getCharsetEncoding(), doc.getXMLVersion());
       }
       om.serialize(pw);
       pw.writeEndDocument();
+      if (options.getAutoClose()) out.close();
     } catch (XMLStreamException e) {
       throw new RuntimeException(e);
     }
@@ -267,6 +292,11 @@
       java.util.Arrays.fill(spaces, ' ');
       return spaces;
     }
+  }
+
+  @Override
+  protected WriterOptions initDefaultWriterOptions() {
+    return new AbstractWriterOptions() {};
   }
 
 }

Modified: incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/EncodingTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/EncodingTest.java?view=diff&rev=559905&r1=559904&r2=559905
==============================================================================
--- incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/EncodingTest.java (original)
+++ incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/EncodingTest.java Thu Jul 26 10:53:17 2007
@@ -31,6 +31,7 @@
 import org.apache.abdera.parser.ParserOptions;
 import org.apache.abdera.util.CompressionUtil;
 import org.apache.abdera.util.CompressionUtil.CompressionCodec;
+import org.apache.abdera.writer.WriterOptions;
 
 import junit.framework.TestCase;
 
@@ -95,6 +96,33 @@
       options.setFilterRestrictedCharacters(true);
       options.setCharset("UTF-8");
       Document doc = parser.parse(new ByteArrayInputStream(s.getBytes("UTF-8")), null, options);
+      doc.getRoot().toString();
+    }
+    
+    /**
+     * Passes if the test does not throw any exceptions
+     */
+    public void testWriterOptions() throws Exception {
+      Abdera abdera = new Abdera();
+      Entry entry = abdera.newEntry();
+      entry.setTitle("1");
+      
+      ByteArrayOutputStream out = new ByteArrayOutputStream();
+      WriterOptions writeoptions = entry.getDefaultWriterOptions();
+      writeoptions.setCompressionCodecs(CompressionCodec.DEFLATE);
+      writeoptions.setCharset("UTF-16");
+      writeoptions.setAutoClose(true);
+      entry.getDocument().writeTo(out,writeoptions);    
+      out.close();
+      
+      byte[] bytes = out.toByteArray();
+      
+      ByteArrayInputStream in = new ByteArrayInputStream(bytes);
+      Parser parser = abdera.getParser();
+      ParserOptions options = parser.getDefaultParserOptions();
+      options.setCompressionCodecs(CompressionCodec.DEFLATE);
+      Document<Entry> doc = abdera.getParser().parse(in,null,options);
+      
       doc.getRoot().toString();
     }
 }



Mime
View raw message