jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1340147 - in /jackrabbit/oak/trunk/oak-mk/src: main/java/org/apache/jackrabbit/mk/util/NameFilter.java test/java/org/apache/jackrabbit/mk/util/NameFilterTest.java
Date Fri, 18 May 2012 16:02:15 GMT
Author: stefan
Date: Fri May 18 16:02:15 2012
New Revision: 1340147

URL: http://svn.apache.org/viewvc?rev=1340147&view=rev
Log:
OAK-75: specify format and semantics of 'filter' parameter in MicroKernel API (WIP)

Added:
    jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/NameFilter.java
    jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/NameFilterTest.java

Added: jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/NameFilter.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/NameFilter.java?rev=1340147&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/NameFilter.java
(added)
+++ jackrabbit/oak/trunk/oak-mk/src/main/java/org/apache/jackrabbit/mk/util/NameFilter.java
Fri May 18 16:02:15 2012
@@ -0,0 +1,129 @@
+/*
+ * 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.jackrabbit.mk.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Simple name filter utility class.
+ * <ul>
+ *     <li>a filter consists of one or more <i>globs</i></li>
+ *     <li>a <i>glob</i> prefixed by {@code "\-"} is treated as an exclusion
pattern;
+ *     all others are considered inclusion patterns</li>
+ *     <li>{@code "\*"} serves as a <i>wild card</i>, i.e. it matches any
substring in the target name</li>
+ *     <li>a filter matches a target name if any of the inclusion patterns match but
+ *     none of the exclusion patterns</li>
+ * </ul>
+ * Example:
+ * <p/>
+ * {@code ["foo\*", "\-foo99"]} matches {@code "foo"} and {@code "foo bar"}
+ * but not {@code "foo99"}.
+ */
+public class NameFilter {
+
+    public static String WILDCARD = "\\*";
+    public static String EXCLUDE_PREFIX = "\\-";
+
+    // list of ORed inclusion patterns
+    private final List<String> inclPatterns = new ArrayList<String>();
+    // list of ORed exclusion patterns
+    private final List<String> exclPatterns = new ArrayList<String>();
+
+    public NameFilter(String[] patterns) {
+        for (String pattern : patterns) {
+            if (pattern.startsWith(EXCLUDE_PREFIX)) {
+                exclPatterns.add(pattern.substring(EXCLUDE_PREFIX.length()));
+            } else {
+                inclPatterns.add(pattern);
+            }
+        }
+    }
+
+    public boolean matches(String name) {
+        boolean matched = false;
+        // check inclusion patterns
+        for (String pattern : inclPatterns) {
+            if (internalMatches(name, pattern, 0, 0)) {
+                matched = true;
+                break;
+            }
+        }
+        if (matched) {
+            // check exclusion patterns
+            for (String pattern : exclPatterns) {
+                if (internalMatches(name, pattern, 0, 0)) {
+                    matched = false;
+                    break;
+                }
+            }
+        }
+        return matched;
+    }
+
+    /**
+     * Internal helper used to recursively match the pattern
+     *
+     * @param s       The string to be tested
+     * @param pattern The pattern
+     * @param sOff    offset within <code>s</code>
+     * @param pOff    offset within <code>pattern</code>.
+     * @return true if <code>s</code> matched pattern, else false.
+     */
+    private static boolean internalMatches(String s, String pattern,
+                                           int sOff, int pOff) {
+        int pLen = pattern.length();
+        int sLen = s.length();
+
+        while (true) {
+            if (pOff >= pLen) {
+                return sOff >= sLen ? true : false;
+            }
+            if (sOff >= sLen && pattern.indexOf(WILDCARD, pOff) != pOff) {
+                return false;
+            }
+
+            // check for a wildcard '\*' as the next pattern;
+            // this is handled by a recursive call for
+            // each postfix of the name.
+            if (pattern.indexOf(WILDCARD, pOff) == pOff) {
+                pOff += WILDCARD.length();
+                if (pOff >= pLen) {
+                    return true;
+                }
+
+                while (true) {
+                    if (internalMatches(s, pattern, sOff, pOff)) {
+                        return true;
+                    }
+                    if (sOff >= sLen) {
+                        return false;
+                    }
+                    sOff++;
+                }
+            }
+
+            if (pOff < pLen && sOff < sLen) {
+                if (pattern.charAt(pOff) != s.charAt(sOff)) {
+                    return false;
+                }
+            }
+            pOff++;
+            sOff++;
+        }
+    }
+}

Added: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/NameFilterTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/NameFilterTest.java?rev=1340147&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/NameFilterTest.java
(added)
+++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/util/NameFilterTest.java
Fri May 18 16:02:15 2012
@@ -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.jackrabbit.mk.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests the NameFilter utility class.
+ */
+public class NameFilterTest {
+
+    @Test
+    public void test() {
+        NameFilter filter = new NameFilter(new String[]{"foo\\*", "\\-foo99"});
+        assertTrue(filter.matches("foo1"));
+        assertTrue(filter.matches("foo*"));
+        assertTrue(filter.matches("foo bar"));
+        assertTrue(filter.matches("foo 99"));
+        assertFalse(filter.matches("foo99"));
+    }
+
+}



Mime
View raw message