abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r443068 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/model/ core/src/main/java/org/apache/abdera/util/ parser/src/main/java/org/apache/abdera/parser/stax/
Date Wed, 13 Sep 2006 18:47:58 GMT
Author: jmsnell
Date: Wed Sep 13 11:47:57 2006
New Revision: 443068

URL: http://svn.apache.org/viewvc?view=rev&rev=443068
Log:
Per RFC4287 and APP Draft -10 section 11.2, alternate and edit-media links may be selected
by 
a combination of the hreflang and type attributes. Provide getAlternateLink and getEditMediaLink

alternatives that select the link based on hreflang and type.

Fix the algorithm in MimeTypeHelper.isMatch so that wildcards (e.g. */*, text/*, etc) are
properly
supported.


Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Source.java
    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/FOMElement.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMSource.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=443068&r1=443067&r2=443068
==============================================================================
--- 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 Wed
Sep 13 11:47:57 2006
@@ -581,6 +581,12 @@
   Link getAlternateLink();
   
   /**
+   * Returns the first alternate link matching the specified type and hreflang
+   * @throws MimeTypeParseException 
+   */
+  Link getAlternateLink(String type, String hreflang) throws MimeTypeParseException;
+  
+  /**
    * Returns this entries first enclosure link
    */
   Link getEnclosureLink();
@@ -596,14 +602,22 @@
   Link getEditMediaLink();
   
   /**
+   * Returns the first edit-media link matching the specified type and hreflang
+   * @throws MimeTypeParseException 
+   */
+  Link getEditMediaLink(String type, String hreflang) throws MimeTypeParseException;
+  
+  /**
    * Returns this entries first self link
    */
   Link getSelfLink();
  
   URI getLinkResolvedHref(String rel) throws URISyntaxException;
   URI getAlternateLinkResolvedHref() throws URISyntaxException;
+  URI getAlternateLinkResolvedHref(String type, String hreflang) throws URISyntaxException,
MimeTypeParseException;
   URI getEnclosureLinkResolvedHref() throws URISyntaxException;
   URI getEditLinkResolvedHref() throws URISyntaxException;
   URI getEditMediaLinkResolvedHref() throws URISyntaxException;
+  URI getEditMediaLinkResolvedHref(String type, String hreflang) throws URISyntaxException,
MimeTypeParseException;
   URI getSelfLinkResolvedHref() throws URISyntaxException;
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Source.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Source.java?view=diff&rev=443068&r1=443067&r2=443068
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Source.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Source.java Wed
Sep 13 11:47:57 2006
@@ -468,9 +468,12 @@
    */
   Link getAlternateLink();
   
+  Link getAlternateLink(String type, String hreflang) throws MimeTypeParseException;
+  
   URI getLinkResolvedHref(String rel) throws URISyntaxException;
   URI getSelfLinkResolvedHref() throws URISyntaxException;
   URI getAlternateLinkResolvedHref() throws URISyntaxException;
+  URI getAlternateLinkResolvedHref(String type, String hreflang) throws URISyntaxException,
MimeTypeParseException;
   
   Collection getCollection();
   

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=443068&r1=443067&r2=443068
==============================================================================
--- 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 11:47:57 2006
@@ -29,14 +29,36 @@
 import org.apache.abdera.model.Service;
 
 public class MimeTypeHelper {
-
+   
   public static boolean isMatch(String a, String b) {
+    if ((a == null || a.length() == 0) && 
+        (b == null || b.length() == 0)) 
+          return true;
     boolean answer = false;
     try {
       MimeType mta = new MimeType(a);
-      answer = mta.match(b);
+      MimeType mtb = new MimeType(b);
+      return isMatch(mta,mtb);
     } catch (Exception e) {}
     return answer;
+  }
+  
+  public static boolean isMatch(MimeType a, MimeType b) {
+    try {
+      final MimeType WILDCARD = new MimeType("*/*");
+      if (a == null || b == null) return true;
+      if (a.match(b)) return true;
+      if (a.equals(WILDCARD)) return true;
+      if (a.getPrimaryType().equals("*")) {
+        MimeType c = new MimeType(b.getPrimaryType(), a.getSubType());
+        return c.match(b);
+      }
+      if (b.getPrimaryType().equals("*")) {
+        MimeType c = new MimeType(a.getPrimaryType(), b.getSubType());
+        return c.match(a);
+      }
+    } catch (Exception e) {}
+    return false;
   }
   
   public static boolean isApp(String a) {

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java?view=diff&rev=443068&r1=443067&r2=443068
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
Wed Sep 13 11:47:57 2006
@@ -29,6 +29,8 @@
 import java.util.List;
 import java.util.Locale;
 
+import javax.activation.MimeType;
+import javax.activation.MimeTypeParseException;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
@@ -39,11 +41,13 @@
 import org.apache.abdera.model.Div;
 import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Link;
 import org.apache.abdera.model.Text;
 import org.apache.abdera.parser.Parser;
 import org.apache.abdera.parser.ParserOptions;
 import org.apache.abdera.parser.stax.util.FOMList;
 import org.apache.abdera.util.Constants;
+import org.apache.abdera.util.MimeTypeHelper;
 import org.apache.abdera.util.URIHelper;
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMComment;
@@ -586,4 +590,23 @@
     return locale;
   }
 
+  protected Link selectLink(
+    List<Link> links, 
+    String type, 
+    String hreflang) 
+      throws MimeTypeParseException {
+    for (Link link : links) {
+      MimeType mt = link.getMimeType();
+      boolean typematch =  
+        MimeTypeHelper.isMatch(
+          (mt != null) ? mt.toString() : null, type);
+      boolean langmatch = 
+        "*".equals(hreflang) ||
+        ((hreflang != null) ? 
+          hreflang.equals(link.getHrefLang()) : 
+          link.getHrefLang() == null);
+      if (typematch && langmatch) return link;
+    }
+    return null;
+  }
 }

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=443068&r1=443067&r2=443068
==============================================================================
--- 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
Wed Sep 13 11:47:57 2006
@@ -758,4 +758,36 @@
     Content content = getContentElement();
     return (content != null) ? content.getMimeType() : null;
   }
+
+  public Link getAlternateLink(
+    String type, 
+    String hreflang) 
+      throws MimeTypeParseException {
+    return selectLink(getLinks(Link.REL_ALTERNATE), type, hreflang);
+  }
+
+  public java.net.URI getAlternateLinkResolvedHref(
+    String type, 
+    String hreflang) 
+      throws URISyntaxException, 
+             MimeTypeParseException {
+    Link link = getAlternateLink(type, hreflang);
+    return (link != null) ? link.getResolvedHref() : null;
+  }
+
+  public Link getEditMediaLink(
+    String type, 
+    String hreflang) 
+      throws MimeTypeParseException {
+    return selectLink(getLinks(Link.REL_EDIT_MEDIA), type, hreflang);
+  }
+
+  public java.net.URI getEditMediaLinkResolvedHref(
+    String type, 
+    String hreflang) 
+      throws URISyntaxException, 
+             MimeTypeParseException {
+    Link link = getEditMediaLink(type, hreflang);
+    return (link != null) ? link.getResolvedHref() : null;
+  }
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMSource.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMSource.java?view=diff&rev=443068&r1=443067&r2=443068
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMSource.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMSource.java
Wed Sep 13 11:47:57 2006
@@ -573,4 +573,21 @@
     else 
       _removeChildren(COLLECTION, false);
   }
+  
+  public Link getAlternateLink(
+    String type, 
+    String hreflang) 
+      throws MimeTypeParseException {
+    return selectLink(getLinks(Link.REL_ALTERNATE), type, hreflang);
+  }
+
+  public java.net.URI getAlternateLinkResolvedHref(
+    String type, 
+    String hreflang) 
+      throws URISyntaxException, 
+             MimeTypeParseException {
+    Link link = getAlternateLink(type, hreflang);
+    return (link != null) ? link.getResolvedHref() : null;
+  }
+
 }



Mime
View raw message