abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r559718 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/parser/ core/src/main/java/org/apache/abdera/util/ parser/src/main/java/org/apache/abdera/parser/stax/ parser/src/test/java/org/apache/abdera/test/parser/ parse...
Date Thu, 26 Jul 2007 06:48:52 GMT
Author: jmsnell
Date: Wed Jul 25 23:48:51 2007
New Revision: 559718

URL: http://svn.apache.org/viewvc?view=rev&rev=559718
Log:
This makes it possible to parse GZIP and Deflate compressed streams directly with 
Parser.  Previously, this was only supported via the Client module when parsing 
responses to requests.

Added:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/CompressionUtil.java
Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java
    incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/EncodingTest.java
    incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/TestSuite.java
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java?view=diff&rev=559718&r1=559717&r2=559718
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
Wed Jul 25 23:48:51 2007
@@ -19,6 +19,7 @@
 
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.filter.ParseFilter;
+import org.apache.abdera.util.CompressionUtil.CompressionCodec;
 
 /**
  * Parser options are used to modify the behavior of the parser. 
@@ -112,4 +113,17 @@
    */
   void setFilterRestrictedCharacterReplacement(char replacement);
   
+  /**
+   * When parsing an InputStream that contains compressed data, use these
+   * codecs to decompress the stream. Only used when parsing an InputStream.
+   * Ignored when parsing a Reader
+   */
+  CompressionCodec[] getCompressionCodecs();
+
+  /**
+   * When parsing an InputStream that contains compressed data, use these
+   * codecs to decompress the stream. Only used when parsing an InputStream.
+   * Ignored when parsing a Reader
+   */
+  void setCompressionCodecs(CompressionCodec... codecs);
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java?view=diff&rev=559718&r1=559717&r2=559718
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
Wed Jul 25 23:48:51 2007
@@ -20,6 +20,7 @@
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.filter.ParseFilter;
 import org.apache.abdera.parser.ParserOptions;
+import org.apache.abdera.util.CompressionUtil.CompressionCodec;
 
 /**
  * Non thread-safe abstract implementation of ParserOptions
@@ -34,6 +35,7 @@
   protected boolean preserve = true;
   protected boolean filterreserved = false;
   protected char replacement = 0;
+  protected CompressionCodec[] codecs = null;
 
   protected abstract void initFactory();
   protected abstract void checkFactory(Factory factory);
@@ -107,4 +109,13 @@
   public void setFilterRestrictedCharacterReplacement(char replacement) {
     this.replacement = replacement;
   }
+  
+  public CompressionCodec[] getCompressionCodecs() {
+    return codecs;
+  }
+  
+  public void setCompressionCodecs(CompressionCodec... codecs) {
+    this.codecs = codecs;
+  }
+  
 }

Added: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/CompressionUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/CompressionUtil.java?view=auto&rev=559718
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/CompressionUtil.java
(added)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/CompressionUtil.java
Wed Jul 25 23:48:51 2007
@@ -0,0 +1,78 @@
+/*
+* 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.InputStream;
+import java.io.OutputStream;
+import java.util.zip.DeflaterOutputStream;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+import java.util.zip.InflaterInputStream;
+
+public class CompressionUtil {
+
+  public enum CompressionCodec { GZIP, XGZIP, DEFLATE }
+  
+  public static OutputStream getEncodedOutputStream(
+    OutputStream out, 
+    CompressionCodec encoding) 
+      throws IOException {
+    return getEncodedOutputStream(
+      out, new CompressionCodec[] {encoding});
+  }
+  
+  public static OutputStream getEncodedOutputStream(
+    OutputStream out, 
+    CompressionCodec... encodings) 
+      throws IOException {
+    for (CompressionCodec encoding:encodings) {
+      switch(encoding) {
+        case GZIP:
+          out = new GZIPOutputStream(out); break;
+        case DEFLATE:
+          out = new DeflaterOutputStream(out); break;
+      }
+    }
+    return out;
+  }
+  
+  public static InputStream getDecodingInputStream(
+    InputStream in, 
+    CompressionCodec encoding) 
+      throws IOException {
+    switch(encoding) {
+      case GZIP:
+      case XGZIP:
+        in = new GZIPInputStream(in); break;
+      case DEFLATE:
+        in = new InflaterInputStream(in); break;
+    }
+    return in;
+  }
+  
+  public static InputStream getDecodingInputStream(
+    InputStream in, 
+    CompressionCodec... encoding) 
+      throws IOException {
+    for (CompressionCodec codec : encoding) {
+      in = getDecodingInputStream(in, codec);
+    }
+    return in;
+  }
+}

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=559718&r1=559717&r2=559718
==============================================================================
--- 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
Wed Jul 25 23:48:51 2007
@@ -32,6 +32,7 @@
 import org.apache.abdera.parser.stax.util.FOMSniffingInputStream;
 import org.apache.abdera.parser.stax.util.FOMXmlRestrictedCharFilter;
 import org.apache.abdera.util.AbstractParser;
+import org.apache.abdera.util.CompressionUtil;
 import org.apache.abdera.util.Messages;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.axiom.om.OMDocument;
@@ -90,6 +91,10 @@
       throw new IllegalArgumentException(Messages.get("INPUTSTREAM.NOT.NULL"));
     try {
       if (options == null) options = getDefaultParserOptions();
+      if (options.getCompressionCodecs() != null) {
+        in = CompressionUtil.getDecodingInputStream(
+          in, options.getCompressionCodecs());
+      }
       String charset = options.getCharset();
       if (charset == null && options.getAutodetectCharset()) {
         FOMSniffingInputStream sin = 

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=559718&r1=559717&r2=559718
==============================================================================
--- 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
Wed Jul 25 23:48:51 2007
@@ -17,11 +17,20 @@
 */
 package org.apache.abdera.test.parser;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.io.StringReader;
 import java.util.Date;
 
 import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Content;
+import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Entry;
+import org.apache.abdera.parser.Parser;
+import org.apache.abdera.parser.ParserOptions;
+import org.apache.abdera.util.CompressionUtil;
+import org.apache.abdera.util.CompressionUtil.CompressionCodec;
 
 import junit.framework.TestCase;
 
@@ -43,4 +52,49 @@
         assertEquals(s, entry.getContent());
     }
 
+    /**
+     * Passes if the test does not throw a parse exception
+     */
+    public void testCompressionCodec() throws Exception {
+      Abdera abdera = new Abdera();
+      Entry entry = abdera.newEntry();
+      ByteArrayOutputStream out = new ByteArrayOutputStream();
+      OutputStream cout = CompressionUtil.getEncodedOutputStream(out, CompressionCodec.GZIP);
+      entry.writeTo(cout);
+      cout.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);
+      entry = doc.getRoot();
+    }
+    
+    /**
+     * Passes if the test does not throw a parse exception
+     */
+    public void testXMLRestrictedChar() throws Exception {
+      String s = "<?xml version='1.1'?><t t='\u007f' />";
+      Abdera abdera = new Abdera();
+      Parser parser = abdera.getParser();
+      ParserOptions options = parser.getDefaultParserOptions();
+      options.setFilterRestrictedCharacters(true);
+      Document doc = parser.parse(new StringReader(s), null, options);
+      doc.getRoot().toString();
+    }
+    
+    /**
+     * Passes if the test does not throw a parse exception
+     */
+    public void testXMLRestrictedChar2() throws Exception {
+      String s = "<?xml version='1.0'?><t t='\u0002' />";
+      Abdera abdera = new Abdera();
+      Parser parser = abdera.getParser();
+      ParserOptions options = parser.getDefaultParserOptions();
+      options.setFilterRestrictedCharacters(true);
+      options.setCharset("UTF-8");
+      Document doc = parser.parse(new ByteArrayInputStream(s.getBytes("UTF-8")), null, options);
+      doc.getRoot().toString();
+    }
 }

Modified: incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/TestSuite.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/TestSuite.java?view=diff&rev=559718&r1=559717&r2=559718
==============================================================================
--- incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/TestSuite.java
(original)
+++ incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/TestSuite.java
Wed Jul 25 23:48:51 2007
@@ -17,6 +17,7 @@
 */
 package org.apache.abdera.test.parser.stax;
 
+import org.apache.abdera.test.parser.EncodingTest;
 import org.apache.abdera.test.parser.stax.AtomConformanceTest;
 import org.apache.abdera.test.parser.stax.FeedParserTest;
 import org.apache.abdera.test.parser.stax.FeedValidatorTest;
@@ -36,5 +37,6 @@
     addTestSuite(ParserTest.class);
     addTestSuite(ConcurrencyTest.class);
     addTestSuite(XhtmlTest.class);
+    addTestSuite(EncodingTest.class);
   }
 }

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java?view=diff&rev=559718&r1=559717&r2=559718
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java
Wed Jul 25 23:48:51 2007
@@ -20,14 +20,12 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.zip.DeflaterOutputStream;
-import java.util.zip.GZIPInputStream;
-import java.util.zip.GZIPOutputStream;
-import java.util.zip.InflaterInputStream;
 
 import org.apache.abdera.i18n.iri.Constants;
 import org.apache.abdera.i18n.iri.Escaping;
 import org.apache.abdera.i18n.unicode.Normalizer;
+import org.apache.abdera.util.CompressionUtil;
+import org.apache.abdera.util.CompressionUtil.CompressionCodec;
 import org.apache.commons.codec.DecoderException;
 import org.apache.commons.codec.net.BCodec;
 import org.apache.commons.codec.net.QCodec;
@@ -148,35 +146,34 @@
     }
   }
   
-  
-  public enum ContentEncoding { GZIP, XGZIP, DEFLATE }
-  
-  public static OutputStream getEncodedOutputStream(OutputStream out, ContentEncoding encoding)
throws IOException {
-    return getEncodedOutputStream(out, new ContentEncoding[] {encoding});
+  /**
+   * @deprecated use CompressionUtil.getEncodedOutputStream instead
+   * @see org.apache.abdera.util.CompressionUtil
+   */
+  public static OutputStream getEncodedOutputStream(
+    OutputStream out, 
+    CompressionCodec encoding) 
+      throws IOException {
+    return CompressionUtil.getEncodedOutputStream(
+      out, new CompressionCodec[] {encoding});
   }
   
-  public static OutputStream getEncodedOutputStream(OutputStream out, ContentEncoding...
encodings) throws IOException {
-    for (ContentEncoding encoding:encodings) {
-      switch(encoding) {
-        case GZIP:
-          out = new GZIPOutputStream(out); break;
-        case DEFLATE:
-          out = new DeflaterOutputStream(out); break;
-      }
-    }
-    return out;
+  /**
+   * @deprecated use CompressionUtil.getEncodedOutputStream instead
+   * @see org.apache.abdera.util.CompressionUtil
+   */
+  public static OutputStream getEncodedOutputStream(
+    OutputStream out, 
+    CompressionCodec... encodings) 
+      throws IOException {
+    return CompressionUtil.getEncodedOutputStream(out, encodings);
   }
   
   public static InputStream getDecodingInputStream(InputStream in, String ce) throws IOException
{
     String[] encodings = CacheControlUtil.CacheControlParser.splitAndTrim(ce, ",", false);
     for (int n = encodings.length -1; n >= 0; n--) {
-      switch(ContentEncoding.valueOf(encodings[n].toUpperCase().replaceAll("-", ""))) {
-        case GZIP:
-        case XGZIP:
-          in = new GZIPInputStream(in); break;
-        case DEFLATE:
-          in = new InflaterInputStream(in); break;
-      }
+      CompressionCodec encoding = CompressionCodec.valueOf(encodings[n].toUpperCase().replaceAll("-",
""));
+      in = CompressionUtil.getDecodingInputStream(in, encoding);
     }
     return in;
   }



Mime
View raw message