abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r656937 - /incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
Date Fri, 16 May 2008 05:28:39 GMT
Author: jmsnell
Date: Thu May 15 22:28:39 2008
New Revision: 656937

URL: http://svn.apache.org/viewvc?rev=656937&view=rev
Log:
https://issues.apache.org/jira/browse/ABDERA-159 - 
Entry content is encoded as base64 even if mime type of text/* is provided

Modified:
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java

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?rev=656937&r1=656936&r2=656937&view=diff
==============================================================================
--- 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
Thu May 15 22:28:39 2008
@@ -19,6 +19,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.util.Date;
 import java.util.List;
 
@@ -33,6 +34,7 @@
 import org.apache.abdera.model.Control;
 import org.apache.abdera.model.DateTime;
 import org.apache.abdera.model.Div;
+import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Element;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
@@ -44,6 +46,7 @@
 import org.apache.abdera.model.Content.Type;
 import org.apache.abdera.parser.stax.util.FOMHelper;
 import org.apache.abdera.util.Constants;
+import org.apache.abdera.util.MimeTypeHelper;
 import org.apache.abdera.i18n.text.io.InputStreamDataSource;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.axiom.om.OMContainer;
@@ -240,6 +243,8 @@
    */
   public Content setContent(Element element, String mediaType){
     try {
+      if (MimeTypeHelper.isText(mediaType))
+        throw new IllegalArgumentException();
       FOMFactory factory = (FOMFactory) this.factory;
       Content content = factory.newContent(new MimeType(mediaType));
       content.setValueElement(element);
@@ -263,12 +268,20 @@
    * @throws MimeTypeParseException 
    */
   public Content setContent(DataHandler dataHandler, String mediatype) {
-    FOMFactory factory = (FOMFactory) this.factory;
-    Content content = factory.newContent(Content.Type.MEDIA);
-    content.setDataHandler(dataHandler);
-    if (mediatype != null) content.setMimeType(mediatype);
-    setContentElement(content);
-    return content;
+    if (MimeTypeHelper.isText(mediatype)) {
+      try {
+        return setContent(dataHandler.getInputStream(),mediatype);
+      } catch (IOException e) {
+        throw new RuntimeException(e);
+      }
+    } else {
+      FOMFactory factory = (FOMFactory) this.factory;
+      Content content = factory.newContent(Content.Type.MEDIA);
+      content.setDataHandler(dataHandler);
+      if (mediatype != null) content.setMimeType(mediatype);
+      setContentElement(content);
+      return content;
+    }
   }
   
   /**
@@ -285,9 +298,27 @@
    * Sets the content for this entry
    */
   public Content setContent(InputStream in, String mediatype){
-    InputStreamDataSource ds = new InputStreamDataSource(in, mediatype);
-    DataHandler dh = new DataHandler(ds);
-    return setContent(dh, mediatype);
+    if (MimeTypeHelper.isText(mediatype)) {
+      try {
+        StringBuilder buf = new StringBuilder();
+        Document doc = this.getDocument();
+        String charset = doc != null ? doc.getCharset() : null;
+        charset = charset != null ? charset : "UTF-8";
+        InputStreamReader isr = new InputStreamReader(in);
+        char[] data = new char[500];
+        int r = -1;
+        while ((r = isr.read(data)) != -1) {
+          buf.append(data,0,r);
+        }
+        return setContent(buf.toString(), mediatype);
+      } catch (IOException e) {
+        throw new RuntimeException(e);
+      }
+    } else {
+      InputStreamDataSource ds = new InputStreamDataSource(in, mediatype);
+      DataHandler dh = new DataHandler(ds);
+      return setContent(dh, mediatype);
+    }
   }
   
   /**



Mime
View raw message