You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2006/09/07 22:27:24 UTC

svn commit: r441207 - in /incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util: AbderaDataSource.java Constants.java MimeTypeHelper.java

Author: jmsnell
Date: Thu Sep  7 13:27:24 2006
New Revision: 441207

URL: http://svn.apache.org/viewvc?view=rev&rev=441207
Log:
Make the code used to determine the mime type of Abdera objects reusable

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/MimeTypeHelper.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java?view=diff&rev=441207&r1=441206&r2=441207
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java Thu Sep  7 13:27:24 2006
@@ -26,11 +26,6 @@
 import javax.activation.DataSource;
 
 import org.apache.abdera.model.Base;
-import org.apache.abdera.model.Document;
-import org.apache.abdera.model.Element;
-import org.apache.abdera.model.Entry;
-import org.apache.abdera.model.Feed;
-import org.apache.abdera.model.Service;
 
 public class AbderaDataSource 
   implements DataSource {
@@ -42,35 +37,7 @@
   }
   
   public String getContentType() {
-    String type = null;
-    if (base instanceof Document) {
-      Document doc = (Document) base;
-      if (doc.getContentType() != null) {
-        type = doc.getContentType().toString();
-      } else {
-        if (doc.getRoot() instanceof Feed ||
-            doc.getRoot() instanceof Entry) {
-          type = "application/atom+xml";
-        } else if (doc.getRoot() instanceof Service) {
-          type = "application/atomserv+xml";
-        } else {
-          type = "application/xml";
-        }
-      }
-    } else if (base instanceof Feed || base instanceof Entry) {
-      Document doc = ((Element)base).getDocument();
-      if (doc != null && doc.getContentType() != null)
-        type = doc.getContentType().toString();
-      if (type == null)
-        type = "application/atom+xml";
-    } else if (base instanceof Service) {
-      Document doc = ((Element)base).getDocument();
-      if (doc != null)
-        type = doc.getContentType().toString();
-      if (type == null)
-        type = "application/atomserv+xml";      
-    }
-    return (type != null) ? type : "application/xml";
+    return MimeTypeHelper.getMimeType(base);
   }
 
   public InputStream getInputStream() throws IOException {

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java?view=diff&rev=441207&r1=441206&r2=441207
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java Thu Sep  7 13:27:24 2006
@@ -40,6 +40,8 @@
   public static final String CONTROL_PREFIX       = "";
   
   public static final String ATOM_MEDIA_TYPE      = "application/atom+xml";
+  public static final String APP_MEDIA_TYPE       = "application/atomserv+xml";
+  public static final String XML_MEDIA_TYPE       = "application/xml";
   public static final String MULTIPART_MEDIA_TYPE = "multipart/related";
   
   public static final String ATOM_NS              = "http://www.w3.org/2005/Atom";

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/MimeTypeHelper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/MimeTypeHelper.java?view=diff&rev=441207&r1=441206&r2=441207
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/MimeTypeHelper.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/MimeTypeHelper.java Thu Sep  7 13:27:24 2006
@@ -20,6 +20,13 @@
 import javax.activation.MimeType;
 import javax.activation.MimeTypeParseException;
 
+import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.model.Service;
+
 public class MimeTypeHelper {
 
   public static boolean isMatch(String a, String b) {
@@ -61,4 +68,29 @@
     }
     return answer;
   }
+  
+  public static <T extends Base>String getMimeType(T base) {
+    String type = null;
+    if (base instanceof Document) {
+      Document doc = (Document)base;
+      MimeType mt = doc.getContentType();
+      type = (mt != null) ? mt.toString() : getMimeType(doc.getRoot());
+    } else if (base instanceof Element) {
+      Element el = (Element)base;
+      if (el.getDocument() != null) {
+        MimeType mt = el.getDocument().getContentType();
+        type = (mt != null) ? mt.toString() : null;
+      }
+      if (type == null) {
+        if (el instanceof Feed || el instanceof Entry)
+          type = Constants.ATOM_MEDIA_TYPE;
+        else if (el instanceof Service)
+          type = Constants.APP_MEDIA_TYPE;
+        else 
+          type = Constants.XML_MEDIA_TYPE;
+      }
+    }    
+    return (type != null) ? type : Constants.XML_MEDIA_TYPE;
+  }
 }
+