abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r524843 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/model/ dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/ parser/src/main/java/org/apache/abdera/parser/stax/ parser/src/test/java/org/apache/abdera/tes...
Date Mon, 02 Apr 2007 17:18:02 GMT
Author: jmsnell
Date: Mon Apr  2 10:17:59 2007
New Revision: 524843

URL: http://svn.apache.org/viewvc?view=rev&rev=524843
Log:
New method setContent(InputStream) to make it easier to work with Base64 encoded binary content
New method getContentStream() makes it easier to get Base64 decoded binary content
Includes a test case

Added:
    incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/InputStreamDataSource.java
Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
    incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java?view=diff&rev=524843&r1=524842&r2=524843
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java Mon
Apr  2 10:17:59 2007
@@ -17,6 +17,8 @@
 */
 package org.apache.abdera.model;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Date;
 import java.util.List;
 
@@ -242,13 +244,30 @@
   /**
    * Sets the content for this entry
    * @param dataHandler The Data Handler containing the binary content needing Base64 encoding.
-   * @return mediaType The mediatype of the binary content
+   * @param mediaType The mediatype of the binary content
+   * @return The created content element
    * @throws MimeTypeParseException if the media type specified is malformed
    */
   Content setContent(DataHandler dataHandler, String mediatype) throws MimeTypeParseException;
   
   /**
    * Sets the content for this entry
+   * @param inputStream An inputstream providing binary content 
+   * @return The created content element
+   */
+  Content setContent(InputStream inputStream);
+  
+  /**
+   * Sets the content for this entry
+   * @param inputStream An inputstream providing binary content
+   * @param mediaType The mediatype of the binary content
+   * @return The created content element
+   * @throws MimeTypeParseException if the media type specified is malformed
+   */
+  Content setContent(InputStream inputStream, String mediatype) throws MimeTypeParseException;
+  
+  /**
+   * Sets the content for this entry
    * @param value the string value of the content
    * @param mediatype the media type for the content
    * @return The newly created atom:content
@@ -272,6 +291,13 @@
    * @return text content
    */
   String getContent();
+  
+  /**
+   * Returns an input stream from the content element value. This is particularly
+   * useful when dealing with Base64 binary content.
+   * @throws IOException 
+   */
+  InputStream getContentStream() throws IOException;
   
   /**
    * Returns the content/@src attribute, if any

Added: incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/InputStreamDataSource.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/InputStreamDataSource.java?view=auto&rev=524843
==============================================================================
--- incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/InputStreamDataSource.java
(added)
+++ incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/io/InputStreamDataSource.java
Mon Apr  2 10:17:59 2007
@@ -0,0 +1,64 @@
+/*
+* 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.i18n.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.activation.DataSource;
+
+public final class InputStreamDataSource 
+  implements DataSource {
+
+  public static final String DEFAULT_TYPE = "application/octet-stream";
+  
+  private final InputStream in;
+  private final String ctype;
+  
+  public InputStreamDataSource(
+    InputStream in) {
+      this(in,null);
+  }
+  
+  public InputStreamDataSource(
+    InputStream in, 
+    String ctype) {
+      this.in = in;
+      this.ctype = (ctype != null) ? ctype : DEFAULT_TYPE;
+  }
+  
+  public String getContentType() {
+    return ctype;
+  }
+
+  public String getName() {
+    return null;
+  }
+
+  public InputStream getInputStream() 
+    throws IOException {
+      return in;
+  }
+  
+  public OutputStream getOutputStream() 
+    throws IOException {
+      return null;
+  }
+  
+}
\ No newline at end of file

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java?view=diff&rev=524843&r1=524842&r2=524843
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
Mon Apr  2 10:17:59 2007
@@ -17,6 +17,8 @@
 */
 package org.apache.abdera.parser.stax;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Date;
 import java.util.List;
 
@@ -44,6 +46,7 @@
 import org.apache.abdera.parser.stax.util.FOMHelper;
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.URIHelper;
+import org.apache.abdera.i18n.io.InputStreamDataSource;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.i18n.iri.IRISyntaxException;
 import org.apache.axiom.om.OMContainer;
@@ -259,6 +262,30 @@
   
   /**
    * Sets the content for this entry
+   */
+  public Content setContent(InputStream in) {
+    InputStreamDataSource ds = new InputStreamDataSource(in);
+    DataHandler dh = new DataHandler(ds);
+    Content content = null;
+    try {
+      content = setContent(dh);
+    } catch (MimeTypeParseException e) {
+      // should not happen
+    }
+    return content;
+  }
+  
+  /**
+   * Sets the content for this entry
+   */
+  public Content setContent(InputStream in, String mediatype) throws MimeTypeParseException
{
+    InputStreamDataSource ds = new InputStreamDataSource(in, mediatype);
+    DataHandler dh = new DataHandler(ds);
+    return setContent(dh, mediatype);
+  }
+  
+  /**
+   * Sets the content for this entry
    * @throws MimeTypeParseException 
    */
   public Content setContent(String value, String mediatype) throws MimeTypeParseException
{
@@ -733,6 +760,12 @@
   public String getContent() {
     Content content = getContentElement();
     return (content != null) ? content.getValue() : null;
+  }
+  
+  public InputStream getContentStream() throws IOException {
+    Content content = getContentElement();
+    DataHandler dh = content.getDataHandler();
+    return dh.getInputStream();
   }
   
   public IRI getContentSrc() throws IRISyntaxException {

Modified: incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java?view=diff&rev=524843&r1=524842&r2=524843
==============================================================================
--- incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java
(original)
+++ incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java
Mon Apr  2 10:17:59 2007
@@ -17,9 +17,12 @@
 */
 package org.apache.abdera.test.parser.stax;
 
+import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.net.URL;
 import java.util.Calendar;
 import java.util.Date;
@@ -765,7 +768,32 @@
     entry.setContent(foo, "application/foo+xml");
     assertEquals(entry.getContentElement().getValueElement(),foo);
     
+  }
+  
+  public void testSetContent2() throws Exception {
+    
+    Abdera abdera = new Abdera();
+    Entry entry = abdera.newEntry();
+    InputStream in = 
+      new ByteArrayInputStream(
+        "tóst".getBytes("utf-16"));
+    
+    Document<Entry> edoc = entry.getDocument();
+    entry.setContent(in,"text/plain;charset=\"utf-16\"");
+    
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    OutputStreamWriter w = new OutputStreamWriter(out);
+    edoc.writeTo(w);
+    
+    in = new ByteArrayInputStream(out.toByteArray());
+
+    entry = (Entry) abdera.getParser().parse(in).getRoot();
+    
+    in = entry.getContentStream();
     
+    InputStreamReader r = new InputStreamReader(in,entry.getContentMimeType().getParameter("charset"));
+    BufferedReader b = new BufferedReader(r);
+    assertEquals(b.readLine(),"tóst");
     
   }
 }



Mime
View raw message