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/08 00:48:09 UTC

svn commit: r441282 - in /incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter: AbstractListParseFilter.java AbstractParseFilter.java CompoundParseFilter.java SafeContentWhiteListParseFilter.java WhiteListParseFilter.java

Author: jmsnell
Date: Thu Sep  7 15:48:09 2006
New Revision: 441282

URL: http://svn.apache.org/viewvc?view=rev&rev=441282
Log:
Threadsafety, API and implementation improvements the various Filter helpers

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/CompoundParseFilter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/SafeContentWhiteListParseFilter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/WhiteListParseFilter.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java?view=diff&rev=441282&r1=441281&r2=441282
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java Thu Sep  7 15:48:09 2006
@@ -18,6 +18,7 @@
 package org.apache.abdera.util.filter;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -35,25 +36,22 @@
   extends AbstractParseFilter
   implements Cloneable, ListParseFilter {
   
-  private List<QName> qnames = null;
-  private Map<QName,List<QName>> attributes = null;
+  private final List<QName> qnames = Collections.synchronizedList(new ArrayList<QName>());
+  private final Map<QName,List<QName>> attributes = Collections.synchronizedMap(new HashMap<QName,List<QName>>());
   
   public Object clone() throws CloneNotSupportedException {
     return super.clone();
   }
   
   public void add(QName qname) {
-    if (qnames == null) qnames = new ArrayList<QName>();
     if (!contains(qname)) qnames.add(qname);
   }
 
   public boolean contains(QName qname) {
-    if (qnames == null) qnames = new ArrayList<QName>();
     return qnames.contains(qname);
   }
 
   public void add(QName parent, QName attribute) {
-    if (attributes == null) attributes = new HashMap<QName,List<QName>>();
     if (attributes.containsKey(parent)) {
       List<QName> attrs = attributes.get(parent);
       if (!attrs.contains(attribute)) attrs.add(attribute);
@@ -65,7 +63,6 @@
   }
 
   public boolean contains(QName qname, QName attribute) {
-    if (attributes == null) attributes = new HashMap<QName,List<QName>>();
     if (attributes.containsKey(qname)) {
       List<QName> attrs = attributes.get(qname);
       return attrs.contains(attribute);

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java?view=diff&rev=441282&r1=441281&r2=441282
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java Thu Sep  7 15:48:09 2006
@@ -22,42 +22,43 @@
 public abstract class AbstractParseFilter 
   implements ParseFilter {
 
+  private static final byte COMMENTS = 1;
+  private static final byte WHITESPACE = 2;
+  private static final byte PI = 4;
+  
   protected byte flags = 0;
   
+  private void toggle(boolean s, byte flag) {
+    if (s) flags |= flag;
+    else flags &= ~flag;
+  }
+  
+  private boolean check(byte flag) {
+    return (flags & flag) == flag;
+  }
+  
   public void setIgnoreComments(boolean ignore) {
-    if (getIgnoreComments()) {
-      if (!ignore) flags ^= 1;
-    } else {
-      if (ignore) flags |= 1;
-    }
+    toggle(ignore,COMMENTS);
   }
 
   public void setIgnoreWhitespace(boolean ignore) {
-    if (getIgnoreWhitespace()) {
-      if (!ignore) flags ^= 2;
-    } else {
-      if (ignore) flags |= 2;
-    }
+    toggle(ignore,(byte)WHITESPACE);
   }
   
   public void setIgnoreProcessingInstructions(boolean ignore) {
-    if (getIgnoreProcessingInstructions()) {
-      if (!ignore) flags ^= 4;
-    } else {
-      if (ignore) flags |= 4;
-    }
+    toggle(ignore,(byte)PI);
   }
   
   public boolean getIgnoreComments() {
-    return (flags & 1) == 1;
+    return check(COMMENTS);
   }
 
   public boolean getIgnoreProcessingInstructions() {
-    return (flags & 4) == 4;
+    return check(PI);
   }
 
   public boolean getIgnoreWhitespace() {
-    return (flags & 2) == 2;
+    return check(WHITESPACE);
   }
 
   public Object clone() throws CloneNotSupportedException {

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/CompoundParseFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/CompoundParseFilter.java?view=diff&rev=441282&r1=441281&r2=441282
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/CompoundParseFilter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/CompoundParseFilter.java Thu Sep  7 15:48:09 2006
@@ -41,15 +41,29 @@
   implements ParseFilter {
 
   public enum Condition {
-    ACCEPTABLE_TO_ALL, 
-    ACCEPTABLE_TO_ANY, 
+    ACCEPTABLE_TO_ALL,
+    ACCEPTABLE_TO_ANY,
     UNACCEPTABLE_TO_ALL,
-    UNACCEPTABLE_TO_ANY,
+    UNACCEPTABLE_TO_ANY;
+    
+    byte evaluate(boolean b) {
+      if (b) {
+        switch(this) {
+          case ACCEPTABLE_TO_ANY:   return  1;
+          case UNACCEPTABLE_TO_ALL: return -1;
+        }
+      } else {
+        switch(this) {
+          case ACCEPTABLE_TO_ALL:   return -1;
+          case UNACCEPTABLE_TO_ANY: return  1;
+        }
+      }
+      return 0;
+    }
   };
   
-  private Condition condition = Condition.ACCEPTABLE_TO_ANY;
-  private static final ParseFilter[] empty = new ParseFilter[0];
-  protected ParseFilter[] filters = null;
+  protected final Condition condition;
+  protected final ParseFilter[] filters;
   
   public CompoundParseFilter(Condition condition, ParseFilter... filters) {
     this.filters = filters;
@@ -57,29 +71,18 @@
   }
   
   public CompoundParseFilter(ParseFilter... filters) {
-    this.filters = filters;
+    this(Condition.ACCEPTABLE_TO_ANY, filters);
   }
   
   private ParseFilter[] getFilters() {
-    return (filters != null) ? filters : empty;
+    return filters;
   }
-  
+
   public boolean acceptable(QName qname) {
     for (ParseFilter filter : getFilters()) {
-      if (filter.acceptable(qname)) {
-        switch(condition) {
-          case ACCEPTABLE_TO_ANY:
-            return true;
-          case UNACCEPTABLE_TO_ALL:
-            return false;
-        }
-      } else {
-        switch(condition) {
-          case ACCEPTABLE_TO_ALL:
-            return false;
-          case UNACCEPTABLE_TO_ANY:
-            return true;
-        }
+      switch(condition.evaluate(filter.acceptable(qname))) {
+        case  1: return true;
+        case -1: return false;
       }
     }
     return true;
@@ -87,25 +90,14 @@
 
   public boolean acceptable(QName qname, QName attribute) {
     for (ParseFilter filter : getFilters()) {
-      if (filter.acceptable(qname,attribute)) {
-        switch(condition) {
-          case ACCEPTABLE_TO_ANY:
-            return true;
-          case UNACCEPTABLE_TO_ALL:
-            return false;
-        }
-      } else {
-        switch(condition) {
-          case ACCEPTABLE_TO_ALL:
-            return false;
-          case UNACCEPTABLE_TO_ANY:
-            return true;
-        }
+      switch(condition.evaluate(filter.acceptable(qname,attribute))) {
+        case  1: return true;
+        case -1: return false;
       }
     }
     return true;
   }
-
+  
   public Object clone() throws CloneNotSupportedException {
     return super.clone();
   }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/SafeContentWhiteListParseFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/SafeContentWhiteListParseFilter.java?view=diff&rev=441282&r1=441281&r2=441282
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/SafeContentWhiteListParseFilter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/SafeContentWhiteListParseFilter.java Thu Sep  7 15:48:09 2006
@@ -45,7 +45,14 @@
     method, multiple, name, nohref, noshade, nowrap, prompt, readonly, rel, 
     rev, rows, rowspan, rules, scope, selected, shape, size, span, src, 
     start, summary, tabindex, target, title, type, usemap, valign, value, 
-    vspace, width,
+    vspace, width;
+    
+    static String fix(String v) {
+      if (v.equals("char"))  return "CHAR";
+      if (v.equals("for"))   return "FOR";
+      if (v.equals("class")) return "CLASS";
+      return v;
+    }
   };
   
   public boolean acceptable(QName qname) {
@@ -63,12 +70,9 @@
   public boolean acceptable(QName qname, QName attribute) {
     if (qname.getNamespaceURI().equals(Constants.XHTML_NS)) {
       try {
-        String lp = attribute.getLocalPart();
-        lp = lp.replace('-', '_');
-        lp = (lp.equals("char")) ? "CHAR" : lp;
-        lp = (lp.equals("for")) ? "FOR" : lp;
-        lp = (lp.equals("class")) ? "CLASS" : lp;
-        xhtml_attributes.valueOf(lp);
+        xhtml_attributes.valueOf(
+          xhtml_attributes.fix(
+            attribute.getLocalPart()));
         return true;
       } catch (Exception e) {}
       return false;

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/WhiteListParseFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/WhiteListParseFilter.java?view=diff&rev=441282&r1=441281&r2=441282
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/WhiteListParseFilter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/WhiteListParseFilter.java Thu Sep  7 15:48:09 2006
@@ -27,9 +27,11 @@
 public class WhiteListParseFilter 
   extends AbstractListParseFilter {
 
-  boolean listAttributesExplicitly = false;
+  private final boolean listAttributesExplicitly;
   
-  public WhiteListParseFilter() {}
+  public WhiteListParseFilter() {
+    this(false);
+  }
   
   /**
    * If listAttributesExplicity == true, attributes MUST be whitelisted