abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r607810 - in /incubator/abdera/java/trunk/core/src: main/java/org/apache/abdera/util/EntityTag.java test/java/org/apache/abdera/test/core/EntityTagTest.java
Date Tue, 01 Jan 2008 06:53:35 GMT
Author: jmsnell
Date: Mon Dec 31 22:53:34 2007
New Revision: 607810

URL: http://svn.apache.org/viewvc?rev=607810&view=rev
Log:
Parsing improvements for EntityTag and a test case

Added:
    incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/EntityTagTest.java
Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java?rev=607810&r1=607809&r2=607810&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java Mon
Dec 31 22:53:34 2007
@@ -19,6 +19,8 @@
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.abdera.i18n.text.Localizer;
 import org.apache.commons.codec.binary.Hex;
@@ -26,57 +28,66 @@
 /**
  * Implements an EntityTag.
  */
-public class EntityTag implements Cloneable, Serializable {
-
+public class EntityTag 
+  implements Cloneable, 
+             Serializable,
+             Comparable<EntityTag> {
+  
   private static final long serialVersionUID = 1559972888659121461L;
   
-  private static final String INVALID_ENTITY_TAG = Localizer
-      .get("INVALID.ENTITY.TAG");
-
+  private static final Pattern PATTERN = 
+    Pattern.compile("(\\*)|([wW]/)?\"([^\"]*)\"");
+  
+  private static final String INVALID_ENTITY_TAG = 
+    Localizer.get("INVALID.ENTITY.TAG");
+  
   public static final EntityTag WILD = new EntityTag("*");
   
   public static EntityTag parse(String entity_tag) {
     if (entity_tag == null || entity_tag.length() == 0)
       throw new IllegalArgumentException(INVALID_ENTITY_TAG);
-    boolean weak = entity_tag.startsWith("W/");
-    boolean wild = entity_tag.equals("*");
-    if (!wild && !weak && !entity_tag.startsWith("\""))
+    Matcher m = PATTERN.matcher(entity_tag);
+    if (m.find()) {
+      boolean wild = m.group(1) != null;
+      boolean weak = m.group(2) != null;
+      String tag = wild ? "*" : m.group(3);
+      return new EntityTag(tag, weak, wild);
+    } else {
       throw new IllegalArgumentException(INVALID_ENTITY_TAG);
-    String tag = wild ? "*" : entity_tag.substring((weak) ? 3 : 1, entity_tag.length() -
1);
-    return new EntityTag(tag, weak);
+    }
   }
-
+  
   public static EntityTag[] parseTags(String entity_tags) {
     if (entity_tags == null || entity_tags.length() == 0)
       return new EntityTag[0];
-    String[] tags = entity_tags.split("((?<=\")\\s*,\\s*(?=(W/)?\"))");
+    String[] tags = entity_tags.split("((?<=\")\\s*,\\s*(?=([wW]/)?\"|\\*))");
     List<EntityTag> etags = new ArrayList<EntityTag>();
     for (String tag : tags) {
       etags.add(EntityTag.parse(tag.trim()));
     }
     return etags.toArray(new EntityTag[etags.size()]);
   }
-
+  
   public static boolean matchesAny(EntityTag tag1, String tags) {
     return matchesAny(tag1, parseTags(tags), false);
   }
-
+  
   public static boolean matchesAny(EntityTag tag1, String tags, boolean weak) {
     return matchesAny(tag1, parseTags(tags), weak);
   }
-
+  
   public static boolean matchesAny(String tag1, String tags) {
     return matchesAny(parse(tag1), parseTags(tags), false);
   }
-
+  
   public static boolean matchesAny(String tag1, String tags, boolean weak) {
     return matchesAny(parse(tag1), parseTags(tags), weak);
   }
-
+  
   public static boolean matchesAny(EntityTag tag1, EntityTag[] tags) {
     return matchesAny(tag1, tags, false);
   }
-
+  
   public static boolean matchesAny(EntityTag tag1, EntityTag[] tags,
       boolean weak) {
     if (tags == null)
@@ -88,43 +99,59 @@
     }
     return false;
   }
-
+  
   public static boolean matches(EntityTag tag1, EntityTag tag2) {
     return tag1.equals(tag2);
   }
-
+  
   public static boolean matches(String tag1, String tag2) {
     EntityTag etag1 = parse(tag1);
     EntityTag etag2 = parse(tag2);
     return etag1.equals(etag2);
   }
-
+  
   public static boolean matches(EntityTag tag1, String tag2) {
     return tag1.equals(parse(tag2));
   }
-
+  
   private final String tag;
-
+  
   private final boolean weak;
   private final boolean wild;
-
+  
   public EntityTag(String tag) {
     this(tag, false);
   }
-
+  
   public EntityTag(String tag, boolean weak) {
-    checkTag(tag);
+    EntityTag etag = attemptParse(tag);
+    if (etag == null) {
+      if (tag.indexOf('"') > -1)
+        throw new IllegalArgumentException(INVALID_ENTITY_TAG);
+      this.tag = tag;
+      this.weak = weak;
+      this.wild = tag.equals("*");
+    } else {
+      this.tag = etag.tag;
+      this.weak = etag.weak;
+      this.wild = etag.wild;
+    }
+  }
+  
+  private EntityTag(String tag, boolean weak, boolean wild) {
     this.tag = tag;
     this.weak = weak;
-    this.wild = tag.equals("*");
+    this.wild = wild;
   }
-
-  private void checkTag(String tag) {
-    if (tag.startsWith("\"") || 
-        (tag.endsWith("\"") && !tag.endsWith("\\\"")) )
-      throw new IllegalArgumentException("Invalid Entity Tag");
+  
+  private EntityTag attemptParse(String tag) {
+    try {
+      return parse(tag);
+    } catch (Exception e) {
+      return null;
+    }
   }
-
+  
   public boolean isWild() {
     return wild;
   }
@@ -132,11 +159,11 @@
   public String getTag() {
     return tag;
   }
-
+  
   public boolean isWeak() {
     return weak;
   }
-
+  
   public String toString() {
     StringBuilder buf = new StringBuilder();
     if (wild) {
@@ -150,7 +177,7 @@
     }
     return buf.toString();
   }
-
+  
   @Override public int hashCode() {
     final int prime = 31;
     int result = 1;
@@ -159,7 +186,7 @@
     result = prime * result + (wild ? 1231 : 1237);
     return result;
   }
-
+  
   @Override public boolean equals(Object obj) {
     if (this == obj) return true;
     if (obj == null) return false;
@@ -173,12 +200,16 @@
     if (wild != other.wild) return false;
     return true;
   }
-
+  
   @Override
-  protected Object clone() throws CloneNotSupportedException {
-    return super.clone();
+  protected EntityTag clone() {
+    try {
+      return (EntityTag) super.clone();
+    } catch (CloneNotSupportedException e) {
+      return new EntityTag(tag,weak,wild); // not going to happen
+    }
   }
-
+  
   /**
    * Utility method for generating ETags. Works by concatenating the UTF-8 bytes
    * of the provided strings then generating an MD5 hash of the result.
@@ -202,7 +233,7 @@
     }
     return new EntityTag(etag);
   }
-
+  
   /**
    * Checks that the passed in ETag matches the ETag generated by the generate
    * method
@@ -211,7 +242,7 @@
     EntityTag etag2 = generate(material);
     return EntityTag.matches(etag, etag2);
   }
-
+  
   public static String toString(EntityTag... tags) {
     StringBuilder buf = new StringBuilder();
     for (EntityTag tag : tags) {
@@ -230,4 +261,12 @@
     }
     return buf.toString();
   }
-}
+  
+  public int compareTo(EntityTag o) {
+    if (o.isWild() && !isWild()) return 1;
+    if (isWild() && !o.isWild()) return -1;
+    if (o.isWeak() && !isWeak()) return -1;
+    if (isWeak() && !o.isWeak()) return 1;
+    return tag.compareTo(o.tag);
+  }
+}
\ No newline at end of file

Added: incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/EntityTagTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/EntityTagTest.java?rev=607810&view=auto
==============================================================================
--- incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/EntityTagTest.java
(added)
+++ incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/EntityTagTest.java
Mon Dec 31 22:53:34 2007
@@ -0,0 +1,76 @@
+/*
+* 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.test.core;
+
+import junit.framework.TestCase;
+
+import org.apache.abdera.util.EntityTag;
+
+public class EntityTagTest extends TestCase {
+
+  public static void testEntityTag() throws Exception {
+    String[] tags = {
+      "hello",
+      "\"hello\"",
+      "W/\"hello\"",
+      "*"
+    };
+    EntityTag[] etags = new EntityTag[tags.length];
+    for (int n = 0; n < tags.length; n++) {
+      etags[n] = new EntityTag(tags[n]);
+    }
+    assertFalse(etags[0].isWeak());
+    assertFalse(etags[0].isWild());
+    assertFalse(etags[1].isWeak());
+    assertFalse(etags[1].isWild());
+    assertTrue(etags[2].isWeak());
+    assertFalse(etags[2].isWild());
+    assertFalse(etags[3].isWeak());
+    assertTrue(etags[3].isWild());
+    assertEquals(etags[0].getTag(),"hello");
+    assertEquals(etags[1].getTag(),"hello");
+    assertEquals(etags[2].getTag(),"hello");
+    assertEquals(etags[3].getTag(),"*");
+    assertEquals(etags[0].toString(),tags[1]);
+    assertEquals(etags[1].toString(),tags[1]);
+    assertEquals(etags[2].toString(),tags[2]);
+    assertEquals(etags[3].toString(),tags[3]);
+    
+    assertTrue(EntityTag.matches(etags[3], etags[0]));
+    assertTrue(EntityTag.matches(etags[3], etags[1]));
+    assertTrue(EntityTag.matches(etags[3], etags[2]));
+    assertTrue(EntityTag.matches(etags[3], etags[3]));
+
+    assertTrue(EntityTag.matches(etags[0], etags[1]));
+    assertFalse(EntityTag.matches(etags[0], etags[2]));
+    
+    assertTrue(EntityTag.matchesAny(etags[3], new EntityTag[] {etags[0], etags[1], etags[2]}));
+    assertTrue(EntityTag.matchesAny(etags[0], new EntityTag[] {etags[3], etags[1], etags[2]}));
+    assertTrue(EntityTag.matchesAny(etags[1], new EntityTag[] {etags[0], etags[3], etags[2]}));
+    assertTrue(EntityTag.matchesAny(etags[2], new EntityTag[] {etags[0], etags[1], etags[3]}));
+    
+    java.util.Arrays.sort(etags);
+    assertEquals(etags[0].toString(),tags[3]);
+    assertEquals(etags[1].toString(),tags[1]);
+    assertEquals(etags[2].toString(),tags[1]);
+    assertEquals(etags[3].toString(),tags[2]);
+    EntityTag etag = EntityTag.generate("a","b","c","d");
+    assertEquals(etag.toString(),"\"e2fc714c4727ee9395f324cd2e7f331f\"");
+  }
+  
+}



Mime
View raw message