geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r1084850 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/ test/java/org/apache/xbean/finder/
Date Thu, 24 Mar 2011 06:14:24 GMT
Author: dblevins
Date: Thu Mar 24 06:14:23 2011
New Revision: 1084850

URL: http://svn.apache.org/viewvc?rev=1084850&view=rev
Log:
XBEAN-171: RegexFilteredArchive supporting regular expressions to include/exclude classes
from scanning
XBEAN-172: PrefixFilteredArchive supports "startsWith" list of accepted prefixes
XBEAN-173: PackageFilteredArchive supports strict list of accepted package names

Added:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFilter.java
  (with props)
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ExcludeIncludeFilter.java
  (with props)
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilterList.java
  (with props)
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilteredArchive.java
  (contents, props changed)
      - copied, changed from r1084087, geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PrefixFilteredArchive.java
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/Filters.java 
 (with props)
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/IncludeExcludeFilter.java
  (with props)
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilter.java
  (contents, props changed)
      - copied, changed from r1084087, geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilteredArchive.java
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilter.java
  (contents, props changed)
      - copied, changed from r1084087, geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilteredArchive.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java
  (with props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FiltersTest.java
  (with props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java
  (with props)
Removed:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilteredArchive.java
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PrefixFilteredArchive.java
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilteredArchive.java

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFilter.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFilter.java?rev=1084850&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFilter.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFilter.java
Thu Mar 24 06:14:23 2011
@@ -0,0 +1,50 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ */
+package org.apache.xbean.finder;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ClassFilter implements FilteredArchive.Filter {
+
+    private final String name;
+
+    public ClassFilter(String name) {
+        assert name != null;
+        this.name = name;
+    }
+
+    @Override
+    public boolean accept(String className) {
+        return name.equals(className);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        ClassFilter that = (ClassFilter) o;
+
+        return name.equals(that.name);
+    }
+
+    @Override
+    public int hashCode() {
+        return name.hashCode();
+    }
+}
\ No newline at end of file

Propchange: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ExcludeIncludeFilter.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ExcludeIncludeFilter.java?rev=1084850&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ExcludeIncludeFilter.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ExcludeIncludeFilter.java
Thu Mar 24 06:14:23 2011
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ */
+package org.apache.xbean.finder;
+
+/**
+ * First, all Exclude directives are evaluated; if any match, the className is denied unless
it also matches an Include directive.
+ * Any classNames which do not match any Include or Exclude directives are permitted.
+ */
+public class ExcludeIncludeFilter implements FilteredArchive.Filter {
+
+    private final FilteredArchive.Filter include;
+    private final FilteredArchive.Filter exclude;
+
+    public ExcludeIncludeFilter(FilteredArchive.Filter include, FilteredArchive.Filter exclude)
{
+        this.include = include;
+        this.exclude = exclude;
+    }
+
+    @Override
+    public boolean accept(String className) {
+        if (exclude.accept(className)) return include.accept(className);
+        return true;
+    }
+}
\ No newline at end of file

Propchange: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ExcludeIncludeFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilterList.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilterList.java?rev=1084850&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilterList.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilterList.java
Thu Mar 24 06:14:23 2011
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ */
+package org.apache.xbean.finder;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class FilterList implements FilteredArchive.Filter {
+
+    private final List<FilteredArchive.Filter> filters = new ArrayList<FilteredArchive.Filter>();
+
+    public FilterList(FilteredArchive.Filter... filters) {
+        this(Arrays.asList(filters));
+    }
+
+    public FilterList(Iterable<FilteredArchive.Filter> filters) {
+        for (FilteredArchive.Filter filter : filters) {
+            this.filters.add(filter);
+        }
+    }
+
+    @Override
+    public boolean accept(String className) {
+        for (FilteredArchive.Filter filter : filters) {
+            if (filter.accept(className)) return true;
+        }
+
+        return false;
+    }
+
+    public List<FilteredArchive.Filter> getFilters() {
+        return filters;
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilterList.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilteredArchive.java
(from r1084087, geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PrefixFilteredArchive.java)
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilteredArchive.java?p2=geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilteredArchive.java&p1=geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PrefixFilteredArchive.java&r1=1084087&r2=1084850&rev=1084850&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PrefixFilteredArchive.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilteredArchive.java
Thu Mar 24 06:14:23 2011
@@ -18,31 +18,25 @@ package org.apache.xbean.finder;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Arrays;
 import java.util.Iterator;
-import java.util.LinkedHashSet;
 import java.util.NoSuchElementException;
-import java.util.Set;
-import java.util.regex.Pattern;
 
 /**
  * @version $Rev$ $Date$
  */
-public class PrefixFilteredArchive implements Archive {
-
-    private final Set<String> prefixes = new LinkedHashSet<String>();
+public class FilteredArchive implements Archive {
 
     private final Archive archive;
 
-    public PrefixFilteredArchive(Archive archive, String... prefixes) {
-        this(archive, Arrays.asList(prefixes));
-    }
+    private final Filter filter;
 
-    public PrefixFilteredArchive(Archive archive, Iterable<String> prefixes) {
+    public FilteredArchive(Archive archive, Filter filter) {
         this.archive = archive;
-        for (String prefix : prefixes) {
-            this.prefixes.add(prefix);
-        }
+        this.filter = filter;
+    }
+
+    public static interface Filter {
+        boolean accept(String className);
     }
 
     @Override
@@ -57,15 +51,15 @@ public class PrefixFilteredArchive imple
 
     @Override
     public Iterator<String> iterator() {
-        return new Filter(archive.iterator());
+        return new FilteredIterator(archive.iterator());
     }
 
-    private final class Filter implements Iterator<String> {
+    private final class FilteredIterator implements Iterator<String> {
         private final Iterator<String> it;
 
         private String next;
 
-        private Filter(Iterator<String> it) {
+        private FilteredIterator(Iterator<String> it) {
             this.it = it;
         }
 
@@ -94,11 +88,10 @@ public class PrefixFilteredArchive imple
 
         private void seek() {
             while (next == null && it.hasNext()) {
+
                 next = it.next();
 
-                for (String prefix : prefixes) {
-                    if (next.startsWith(prefix)) break;
-                }
+                if (filter.accept(next)) return;
 
                 next = null;
             }

Propchange: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/FilteredArchive.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/Filters.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/Filters.java?rev=1084850&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/Filters.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/Filters.java Thu
Mar 24 06:14:23 2011
@@ -0,0 +1,101 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ */
+package org.apache.xbean.finder;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class Filters {
+    private static final FilteredArchive.Filter NONE = new FilteredArchive.Filter() {
+        @Override
+        public boolean accept(String className) {
+            return false;
+        }
+    };
+
+    public static FilteredArchive.Filter packages(String... packages) {
+        List<FilteredArchive.Filter> filters = new ArrayList<FilteredArchive.Filter>();
+        for (String s : packages) {
+            filters.add(new PackageFilter(s));
+        }
+
+        return optimize(filters);
+    }
+
+    public static FilteredArchive.Filter classes(String... classes) {
+        List<FilteredArchive.Filter> filters = new ArrayList<FilteredArchive.Filter>();
+        for (String s : classes) {
+            filters.add(new ClassFilter(s));
+        }
+
+        return optimize(filters);
+    }
+
+    public static FilteredArchive.Filter patterns(String... patterns) {
+        List<FilteredArchive.Filter> filters = new ArrayList<FilteredArchive.Filter>();
+        for (String s : patterns) {
+            filters.add(new RegexFilter(s));
+        }
+
+        return optimize(filters);
+    }
+
+
+    public static FilteredArchive.Filter optimize(FilteredArchive.Filter... filters) {
+        return optimize(Arrays.asList(filters));
+    }
+
+    public static FilteredArchive.Filter optimize(List<FilteredArchive.Filter>... filterss)
{
+        Set<FilteredArchive.Filter> unwrapped = new LinkedHashSet<FilteredArchive.Filter>();
+
+        for (List<FilteredArchive.Filter> filters : filterss) {
+            unwrap(filters, unwrapped);
+        }
+
+        if (unwrapped.size() > 1) {
+            Iterator<FilteredArchive.Filter> iterator = unwrapped.iterator();
+            while (iterator.hasNext()) {
+                FilteredArchive.Filter filter = iterator.next();
+                if (filter == NONE) iterator.remove();
+            }
+        }
+
+        if (unwrapped.size() == 0) return NONE;
+        if (unwrapped.size() == 1) return unwrapped.iterator().next();
+        return new FilterList(unwrapped);
+    }
+
+    private static void unwrap(List<FilteredArchive.Filter> filters, Set<FilteredArchive.Filter>
unwrapped) {
+        for (FilteredArchive.Filter filter : filters) {
+            if (filter instanceof FilterList) {
+                FilterList filterList = (FilterList) filter;
+                unwrap(filterList.getFilters(), unwrapped);
+            } else {
+                unwrapped.add(filter);
+            }
+        }
+    }
+
+
+}

Propchange: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/Filters.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/IncludeExcludeFilter.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/IncludeExcludeFilter.java?rev=1084850&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/IncludeExcludeFilter.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/IncludeExcludeFilter.java
Thu Mar 24 06:14:23 2011
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ */
+package org.apache.xbean.finder;
+
+/**
+ * First, all Include directives are evaluated; at least one must match, or the className
is rejected.
+ * Next, all Exclude directives are evaluated. If any matches, the className is rejected.
+ * Last, any classNames which do not match an Include or a Exclude directive are denied by
default.
+ */
+public class IncludeExcludeFilter implements FilteredArchive.Filter {
+
+    private FilteredArchive.Filter include;
+    private FilteredArchive.Filter exclude;
+
+    public IncludeExcludeFilter(FilteredArchive.Filter include, FilteredArchive.Filter exclude)
{
+        this.include = include;
+        this.exclude = exclude;
+    }
+
+    @Override
+    public boolean accept(String className) {
+        if (include.accept(className)) return !exclude.accept(className);
+        return false;
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/IncludeExcludeFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilter.java
(from r1084087, geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilteredArchive.java)
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilter.java?p2=geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilter.java&p1=geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilteredArchive.java&r1=1084087&r2=1084850&rev=1084850&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilteredArchive.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilter.java
Thu Mar 24 06:14:23 2011
@@ -16,30 +16,36 @@
  */
 package org.apache.xbean.finder;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
 /**
  * @version $Rev$ $Date$
  */
-public class PackageFilteredArchive extends PrefixFilteredArchive {
+public class PackageFilter implements FilteredArchive.Filter {
+
+    private final String packageName;
+
+    public PackageFilter(String packageName) {
+        assert packageName != null;
+        if (!packageName.endsWith(".")) packageName += ".";
+        this.packageName = packageName;
+    }
 
-    public PackageFilteredArchive(Archive archive, String... prefixes) {
-        this(archive, Arrays.asList(prefixes));
+    @Override
+    public boolean accept(String className) {
+        return className.startsWith(packageName);
     }
 
-    public PackageFilteredArchive(Archive archive, Iterable<String> prefixes) {
-        super(archive, normalize(prefixes));
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        PackageFilter that = (PackageFilter) o;
+
+        return packageName.equals(that.packageName);
     }
 
-    private static Iterable<String> normalize(Iterable<String> prefixes) {
-        List<String> list = new ArrayList<String>();
-        // Ensure the package name ends in a dot
-        for (String prefix : prefixes) {
-            if (!prefix.endsWith(".")) prefix += ".";
-            list.add(prefix);
-        }
-        return list;
+    @Override
+    public int hashCode() {
+        return packageName.hashCode();
     }
 }
\ No newline at end of file

Propchange: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/PackageFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilter.java
(from r1084087, geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilteredArchive.java)
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilter.java?p2=geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilter.java&p1=geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilteredArchive.java&r1=1084087&r2=1084850&rev=1084850&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilteredArchive.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilter.java
Thu Mar 24 06:14:23 2011
@@ -16,90 +16,41 @@
  */
 package org.apache.xbean.finder;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
 import java.util.regex.Pattern;
 
 /**
  * @version $Rev$ $Date$
  */
-public class RegexFilteredArchive implements Archive {
+public class RegexFilter implements FilteredArchive.Filter {
 
-    private final Pattern include;
-    private final Pattern exclude;
+    private final Pattern pattern;
 
-    private final Archive archive;
-
-    public RegexFilteredArchive(Archive archive, String include, String exclude) {
-        this.archive = archive;
-        this.include = (".*".equals(include)) ? null : Pattern.compile(include);
-        this.exclude = ("".equals(exclude)) ? null : Pattern.compile(exclude);
+    public RegexFilter(String expression) {
+        this(Pattern.compile(expression));
     }
 
-    public RegexFilteredArchive(Archive archive, Pattern include, Pattern exclude) {
-        this.archive = archive;
-        this.include = include;
-        this.exclude = exclude;
+    public RegexFilter(Pattern pattern) {
+        assert pattern != null;
+        this.pattern = pattern;
     }
 
     @Override
-    public InputStream getBytecode(String className) throws IOException, ClassNotFoundException
{
-        return archive.getBytecode(className);
+    public boolean accept(String className) {
+        return pattern.matcher(className).matches();
     }
 
     @Override
-    public Class<?> loadClass(String className) throws ClassNotFoundException {
-        return archive.loadClass(className);
-    }
-
-    @Override
-    public Iterator<String> iterator() {
-        return new Filter(archive.iterator());
-    }
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
 
-    private final class Filter implements Iterator<String> {
-        private final Iterator<String> it;
+        RegexFilter that = (RegexFilter) o;
 
-        private String next;
-
-        private Filter(Iterator<String> it) {
-            this.it = it;
-        }
-
-        @Override
-        public boolean hasNext() {
-            if (next != null) return true;
-            if (!it.hasNext()) return false;
-            seek();
-            return hasNext();
-        }
-
-        @Override
-        public String next() {
-            if (!hasNext()) throw new NoSuchElementException();
-
-            String s = next;
-            next = null;
-
-            return s;
-        }
-
-        @Override
-        public void remove() {
-            it.remove();
-        }
-
-        private void seek() {
-            while (next == null && it.hasNext()) {
-                next = it.next();
-
-                if (include != null && include.matcher(next).matches()) break;
-                if (exclude != null && exclude.matcher(next).matches()) next = null;
-
-            }
-        }
+        return pattern.pattern().equals(that.pattern.pattern());
+    }
 
+    @Override
+    public int hashCode() {
+        return pattern.hashCode();
     }
 }

Propchange: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/RegexFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java?rev=1084850&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java
Thu Mar 24 06:14:23 2011
@@ -0,0 +1,157 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ */
+package org.apache.xbean.finder;
+
+import junit.framework.TestCase;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class FilteredArchiveTest extends TestCase {
+
+    private MockArchive archive;
+
+    public void testNothingFiltered() throws Exception {
+
+        assertEquals(archive, new FilteredArchive(archive, new Nothing()));
+    }
+
+    public void testFilterHalf() throws Exception {
+
+        List<String> list = list(new FilteredArchive(archive, new Half()));
+
+        assertEquals(25, list.size());
+    }
+
+    public void testFilterAll() throws Exception {
+
+        List<String> list = list(new FilteredArchive(archive, new All()));
+
+        assertEquals(0, list.size());
+    }
+
+    public static void assertEquals(Iterable<?> expectedList, Iterable<?> actualList)
{
+        final Iterator<?> expected = expectedList.iterator();
+        final Iterator<?> actual = actualList.iterator();
+
+        int i = 0;
+        while (expected.hasNext() && actual.hasNext()) {
+            assertEquals(expected.next(), actual.next());
+            i++;
+        }
+
+        assertEquals(i + " items were the same", expected.hasNext(), actual.hasNext());
+    }
+
+    public static class Nothing implements FilteredArchive.Filter {
+
+        @Override
+        public boolean accept(String className) {
+            return true;
+        }
+    }
+
+    public static class All implements FilteredArchive.Filter {
+
+        @Override
+        public boolean accept(String className) {
+            return false;
+        }
+    }
+
+    public static class Half implements FilteredArchive.Filter {
+
+        private boolean accept;
+
+        @Override
+        public boolean accept(String className) {
+            return accept = !accept;
+        }
+    }
+
+    public static <T> List<T> list(Iterable<T> iterable) {
+        List<T> list = new ArrayList<T>();
+
+        for (T t : iterable) {
+            list.add(t);
+        }
+
+        return list;
+    }
+
+
+    @Override
+    protected void setUp() throws Exception {
+        // 50 entries
+        archive = new MockArchive(
+                "org.apache.xbean.asm.AnnotationVisitor",
+                "org.apache.xbean.asm.AnnotationWriter",
+                "org.apache.xbean.asm.Attribute",
+                "org.apache.xbean.asm.ClassAdapter",
+                "org.apache.xbean.asm.commons.AdviceAdapter",
+                "org.apache.xbean.asm.commons.AnalyzerAdapter",
+                "org.apache.xbean.asm.commons.GeneratorAdapter",
+                "org.apache.xbean.asm.commons.JSRInlinerAdapter",
+                "org.apache.xbean.asm.commons.RemappingAnnotationAdapter",
+                "org.apache.xbean.asm.commons.RemappingClassAdapter",
+                "org.apache.xbean.asm.commons.RemappingFieldAdapter",
+                "org.apache.xbean.asm.commons.RemappingMethodAdapter",
+                "org.apache.xbean.asm.commons.RemappingSignatureAdapter",
+                "org.apache.xbean.asm.commons.SerialVersionUIDAdder",
+                "org.apache.xbean.asm.MethodAdapter",
+                "org.apache.xbean.asm.tree.AnnotationNode",
+                "org.apache.xbean.asm.tree.MultiANewArrayInsnNode",
+                "org.apache.xbean.finder.AnnotatedMember",
+                "org.apache.xbean.finder.AnnotatedMethod",
+                "org.apache.xbean.finder.AnnotatedTarget",
+                "org.apache.xbean.finder.AnnotationFinder",
+                "org.apache.xbean.finder.Archive",
+                "org.apache.xbean.finder.BundleAnnotationFinder",
+                "org.apache.xbean.finder.BundleArchive",
+                "org.apache.xbean.finder.BundleAssignableClassFinder",
+                "org.apache.xbean.finder.ClassesArchive",
+                "org.apache.xbean.finder.ClasspathArchive",
+                "org.apache.xbean.finder.MetaAnnotation",
+                "org.apache.xbean.finder.PackageFilteredArchive",
+                "org.apache.xbean.finder.PrefixFilteredArchive",
+                "org.apache.xbean.finder.RegexFilteredArchive",
+                "org.acme.bar.AnnType",
+                "org.acme.bar.FullyAnnotated",
+                "org.acme.bar.ParamA",
+                "org.apache.xbean.finder.MetaAnnotatedClassTest",
+                "org.apache.xbean.finder.MetaAnnotatedMethodTest",
+                "org.apache.xbean.finder.BundleAnnotationFinder",
+                "org.apache.xbean.finder.BundleAssignableClassFinder",
+                "org.apache.xbean.naming.context.ContextAccess",
+                "org.apache.xbean.naming.context.ContextAccessControlList",
+                "org.apache.xbean.propertyeditor.ArrayConverter",
+                "org.apache.xbean.propertyeditor.ArrayListEditor",
+                "org.apache.xbean.propertyeditor.Inet4AddressEditor",
+                "org.apache.xbean.propertyeditor.Inet6AddressEditor",
+                "org.apache.xbean.propertyeditor.InetAddressEditor",
+                "org.apache.xbean.recipe.AllPropertiesRecipe",
+                "org.apache.xbean.recipe.ArrayRecipe",
+                "org.apache.xbean.recipe.AsmParameterNameLoader",
+                "org.apache.xbean.recipe.MissingAccessorException",
+                "org.apache.xbean.recipe.XbeanAsmParameterNameLoader"
+        );
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FiltersTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FiltersTest.java?rev=1084850&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FiltersTest.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FiltersTest.java
Thu Mar 24 06:14:23 2011
@@ -0,0 +1,111 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ */
+package org.apache.xbean.finder;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class FiltersTest extends TestCase {
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    public void testPackages() throws Exception {
+        FilteredArchive.Filter filter = Filters.packages("org.foo", "org.bar");
+
+        assertTrue(filter.accept("org.foo.Red"));
+        assertTrue(filter.accept("org.bar.Orange"));
+
+        assertFalse(filter.accept("org.fooo.Orange"));
+        assertFalse(filter.accept("org.barr.Orange"));
+        assertFalse(filter.accept("org"));
+        assertFalse(filter.accept(""));
+    }
+
+    public void testClasses() throws Exception {
+        FilteredArchive.Filter filter = Filters.classes("org.foo.Red", "org.foo.Blue");
+
+        assertTrue(filter.accept("org.foo.Red"));
+        assertTrue(filter.accept("org.foo.Blue"));
+
+        assertFalse(filter.accept("org.foo.Orange"));
+        assertFalse(filter.accept("org.foo.Redd"));
+        assertFalse(filter.accept(""));
+    }
+
+    public void testPatterns() throws Exception {
+        FilteredArchive.Filter filter = Filters.patterns("org\\.foo\\..*", ".*\\.Blue");
+
+        assertTrue(filter.accept("org.foo.Red"));
+        assertTrue(filter.accept("org.foo.Blue"));
+        assertTrue(filter.accept("org.bar.Blue"));
+
+        assertFalse(filter.accept("com.foo.Orange"));
+        assertFalse(filter.accept("net.foo.Redd"));
+        assertFalse(filter.accept(""));
+    }
+
+    public void testOptimize() throws Exception {
+
+        ClassFilter foo = new ClassFilter("foo");
+        ClassFilter foo2 = new ClassFilter("foo");
+        ClassFilter foo3 = new ClassFilter("foo");
+
+        FilterList filter = new FilterList(
+                new FilterList(
+                        new FilterList(
+                                new FilterList(
+                                        foo,
+                                        new FilterList(
+                                                foo,
+                                                new FilterList(
+                                                        new FilterList(foo, foo2, foo3)
+                                                )
+
+                                        )
+                                )
+
+                        )
+                )
+        );
+
+        assertSame(foo, Filters.optimize(filter));
+    }
+
+    public void testIncludeExclude() {
+        FilteredArchive.Filter filter = new IncludeExcludeFilter(Filters.packages("org.foo",
"org.bar"), Filters.packages("org.foo.util"));
+
+        assertTrue(filter.accept("org.foo.Red"));
+        assertTrue(filter.accept("org.bar.Red"));
+
+        assertFalse(filter.accept("com.bar.Red"));
+        assertFalse(filter.accept("org.foo.util.Blue"));
+    }
+
+    public void testExcludeInclude() {
+        FilteredArchive.Filter filter = new ExcludeIncludeFilter(Filters.packages("org.foo.util"),
Filters.packages("org.foo", "org.bar"));
+
+        assertFalse(filter.accept("org.foo.Red"));
+        assertFalse(filter.accept("org.bar.Red"));
+
+        assertTrue(filter.accept("com.bar.Red"));
+        assertTrue(filter.accept("org.foo.util.Blue"));
+    }
+
+}

Propchange: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FiltersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java?rev=1084850&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java
Thu Mar 24 06:14:23 2011
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ */
+package org.apache.xbean.finder;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class MockArchive implements Archive {
+    private final List<String> list = new ArrayList<String>();
+
+    public MockArchive(String... classNames) {
+        this(Arrays.asList(classNames));
+    }
+    
+    public MockArchive(Iterable<String> classNames) {
+        for (String className : classNames) {
+            list.add(className);
+        }
+    }
+
+    @Override
+    public InputStream getBytecode(String className) throws IOException, ClassNotFoundException
{
+        return null;
+    }
+
+    @Override
+    public Class<?> loadClass(String className) throws ClassNotFoundException {
+        return null;
+    }
+
+    @Override
+    public Iterator<String> iterator() {
+        return list.iterator();
+    }
+
+}

Propchange: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message