abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r443116 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/util/MimeTypeHelper.java parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
Date Wed, 13 Sep 2006 20:44:32 GMT
Author: jmsnell
Date: Wed Sep 13 13:44:31 2006
New Revision: 443116

URL: http://svn.apache.org/viewvc?view=rev&rev=443116
Log:
Per APP Draft -10, 

   The "app:accept" element value specifies a comma-separated list of
   media-ranges (see [RFC2616]) identifying the types of representations
   that can be POSTed to the URI of a Collection.  Whitespace separation
   of the media-range values is considered insignificant and MUST be
   ignored.

   The app:accept element is similar to the HTTP Accept request-header
   [RFC2616] with the exception that app:accept has no notion of
   preference.  As a result, the value syntax of app:accept does not use
   "accept-params" or "q" arguments as specified in [RFC2616], section
   14.1.

   The order of media-ranges is not significant.  The following lists
   are all equivalent:

     <app:accept>image/png, image/*</app:accept>
     <app:accept>image/*, image/png</app:accept>
     <app:accept>image/*</app:accept>

   A value of "entry" may appear in any list of media-ranges in an
   accept element and indicates that Atom Entry Documents can be posted
   to the Collection.  If the accept element is omitted or empty,
   clients SHOULD assume that only Atom Entry documents will be accepted
   by the Collection.

The MimeTypeHelper.condense(...) method will condense an array of media types into the most
generic 
form. e.g., [image/png, image/*] condenses to [image/*], [image/png, image/*, application/atom+xml,
entry] 
condenses to [image/*, application/atom+xml, entry], etc.

Have Collection.getAccept() always return the condensed form.

Fix Collection.accepts(...) so that it properly understands the "entry" token.

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/MimeTypeHelper.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java

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=443116&r1=443115&r2=443116
==============================================================================
--- 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
Wed Sep 13 13:44:31 2006
@@ -17,6 +17,12 @@
 */
 package org.apache.abdera.util;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
 import javax.activation.MimeType;
 import javax.activation.MimeTypeParseException;
 
@@ -134,5 +140,69 @@
     }    
     return (type != null) ? type : Constants.XML_MEDIA_TYPE;
   }
+  
+  /**
+   * This will take an array of media types and will condense them based
+   * on wildcards, etc.  For instance, 
+   * 
+   *   condense("image/png", "image/jpg", "image/*")
+   * 
+   * condenses to [image/*]
+   * 
+   *   condense("application/atom", "application/*", "image/png", "image/*")
+   *   
+   * condenses to [application/*, image/*]
+   * 
+   */
+  public static String[] condense(String... types) {
+    if (types.length <= 1) return types;
+    List<String> list = new ArrayList<String>(Arrays.asList(types));
+    List<String> res = new ArrayList<String>();
+    Collections.sort(list, getComparator());
+    for (String t:list) {
+      if (!contains(t,res) && !res.contains(t)) res.add(t); 
+    }
+    for (int n = 0; n < res.size(); n++) {
+      String t = res.get(n).intern();
+      if (contains(t, res)) res.remove(t);
+    }
+    return res.toArray(new String[res.size()]);
+  }
+  
+  private static boolean contains(String t1, List<String> t) {
+    for (String t2 : t) {
+      int c = compare(t1,t2);
+      if (c == 1) return true;
+    }
+    return false;
+  }
+  
+  public static Comparator<String> getComparator() {
+    return new Comparator<String>() {
+      public int compare(String o1, String o2) {
+        return MimeTypeHelper.compare(o1,o2);
+      }
+    };
+  }
+  
+  public static int compare(MimeType mt1, MimeType mt2) {
+    String st1 = mt1.getSubType();
+    String st2 = mt2.getSubType();
+    if (MimeTypeHelper.isMatch(mt1, mt2)) {
+      if (st1.equals("*")) return -1;
+      if (st2.equals("*")) return 1;
+    }
+    return 0;
+  }
+  
+  public static int compare(String t1, String t2) {
+    try {
+      MimeType mt1 = new MimeType(t1);
+      MimeType mt2 = new MimeType(t2);
+      return compare(mt1,mt2);
+    } catch (Exception e) {}
+    return 0;
+  }
+
 }
 

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java?view=diff&rev=443116&r1=443115&r2=443116
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
Wed Sep 13 13:44:31 2006
@@ -150,7 +150,7 @@
     for (int n = 0; n < list.length; n++) {
       list[n] = list[n].trim();
     }
-    return list;
+    return MimeTypeHelper.condense(list);
   }
 
   public void setAccept(String... mediaRanges) {
@@ -170,6 +170,8 @@
   public boolean accepts(String mediaType) {
     String[] accept = getAccept();
     for (String a : accept) {
+      if (mediaType.equalsIgnoreCase("entry") && 
+          a.equalsIgnoreCase("entry")) return true;
       if (MimeTypeHelper.isMatch(a, mediaType)) return true;
     }
     return false;



Mime
View raw message