Remove duplicate code for checking substrings. (FELIX-2035)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@929684 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java b/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java
index 5ccae21..b0b13f9 100644
--- a/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java
+++ b/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java
@@ -19,9 +19,9 @@
 package org.apache.felix.framework;
 
 import java.util.*;
+import org.apache.felix.framework.capabilityset.SimpleFilter;
 import org.apache.felix.framework.resolver.Module;
 
-import org.apache.felix.framework.util.Util;
 
 class FindEntriesEnumeration implements Enumeration
 {
@@ -75,7 +75,7 @@
         // File pattern defaults to "*" if not specified.
         filePattern = (filePattern == null) ? "*" : filePattern;
 
-        m_filePattern = Util.parseSubstring(filePattern);
+        m_filePattern = SimpleFilter.parseSubstring(filePattern);
 
         m_next = findNext();
     }
@@ -135,7 +135,7 @@
                         String lastElement = entryName.substring(startIdx, endIdx);
 
                         // See if the file pattern matches the last element of the path.
-                        if (Util.checkSubstring(m_filePattern, lastElement))
+                        if (SimpleFilter.compareSubstring(m_filePattern, lastElement))
                         {
                             // Convert entry name into an entry URL.
                             return m_modules.get(m_moduleIndex).getEntry(entryName);
diff --git a/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java b/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
index 43ab382..8127b49 100644
--- a/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
+++ b/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
@@ -383,7 +383,7 @@
                 case SimpleFilter.APPROX :
                     return compareApproximate(((Comparable) lhs), rhs);
                 case SimpleFilter.SUBSTRING :
-                    return SimpleFilter.compareSubstring((String) lhs, (List<String>) rhs);
+                    return SimpleFilter.compareSubstring((List<String>) rhs, (String) lhs);
                 default:
                     throw new RuntimeException(
                         "Unknown comparison operator: " + op);
diff --git a/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java b/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java
index 1e0ae63..0cabf9a 100644
--- a/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java
+++ b/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java
@@ -366,7 +366,6 @@
         return new SimpleFilter(attr, value, op);
     }
 
-// TODO: FELIX3 - Merge with Util class.
     public static List<String> parseSubstring(String value)
     {
         List<String> pieces = new ArrayList();
@@ -457,8 +456,7 @@
         return sb.toString();
     }
 
-// TODO: FELIX3 - Merge with Util class.
-    public static boolean compareSubstring(String s, List<String> pieces)
+    public static boolean compareSubstring(List<String> pieces, String s)
     {
         // Walk the pieces to match the string
         // There are implicit stars between each piece,
diff --git a/framework/src/main/java/org/apache/felix/framework/util/Util.java b/framework/src/main/java/org/apache/felix/framework/util/Util.java
index d3477cc..deb96c7 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/Util.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/Util.java
@@ -530,149 +530,4 @@
         Map headerMap = module.getHeaders();
         return headerMap.containsKey(Constants.FRAGMENT_HOST);
     }
-
-    //
-    // The following substring-related code was lifted and modified
-    // from the LDAP parser code.
-    //
-
-    public static List<String> parseSubstring(String target)
-    {
-        ArrayList pieces = new ArrayList();
-        StringBuffer ss = new StringBuffer();
-        // int kind = SIMPLE; // assume until proven otherwise
-        boolean wasStar = false; // indicates last piece was a star
-        boolean leftstar = false; // track if the initial piece is a star
-        boolean rightstar = false; // track if the final piece is a star
-
-        int idx = 0;
-
-        // We assume (sub)strings can contain leading and trailing blanks
-loop:   for (;;)
-        {
-            if (idx >= target.length())
-            {
-                if (wasStar)
-                {
-                    // insert last piece as "" to handle trailing star
-                    rightstar = true;
-                }
-                else
-                {
-                    pieces.add(ss.toString());
-                    // accumulate the last piece
-                    // note that in the case of
-                    // (cn=); this might be
-                    // the string "" (!=null)
-                }
-                ss.setLength(0);
-                break loop;
-            }
-
-            char c = target.charAt(idx++);
-            if (c == '*')
-            {
-                if (wasStar)
-                {
-                    // encountered two successive stars;
-                    // I assume this is illegal
-                    throw new IllegalArgumentException("Invalid filter string: " + target);
-                }
-                if (ss.length() > 0)
-                {
-                    pieces.add(ss.toString()); // accumulate the pieces
-                    // between '*' occurrences
-                }
-                ss.setLength(0);
-                // if this is a leading star, then track it
-                if (pieces.size() == 0)
-                {
-                    leftstar = true;
-                }
-                ss.setLength(0);
-                wasStar = true;
-            }
-            else
-            {
-                wasStar = false;
-                ss.append(c);
-            }
-        }
-        if (leftstar || rightstar || pieces.size() > 1)
-        {
-            // insert leading and/or trailing "" to anchor ends
-            if (rightstar)
-            {
-                pieces.add("");
-            }
-            if (leftstar)
-            {
-                pieces.add(0, "");
-            }
-        }
-        pieces.trimToSize();
-        return pieces;
-    }
-
-    public static boolean checkSubstring(List<String> pieces, String s)
-    {
-        // Walk the pieces to match the string
-        // There are implicit stars between each piece,
-        // and the first and last pieces might be "" to anchor the match.
-        // assert (pieces.length > 1)
-        // minimal case is <string>*<string>
-
-        boolean result = true;
-        int len = pieces.size();
-
-        int index = 0;
-
-loop:   for (int i = 0; i < len; i++)
-        {
-            String piece = pieces.get(i);
-
-            // If this is the first piece, then make sure the
-            // string starts with it.
-            if (i == 0)
-            {
-                if (!s.startsWith(piece))
-                {
-                    result = false;
-                    break loop;
-                }
-            }
-
-            // If this is the last piece, then make sure the
-            // string ends with it.
-            if (i == len - 1)
-            {
-                if (s.endsWith(piece))
-                {
-                    result = true;
-                }
-                else
-                {
-                    result = false;
-                }
-                break loop;
-            }
-
-            // If this is neither the first or last piece, then
-            // make sure the string contains it.
-            if ((i > 0) && (i < (len - 1)))
-            {
-                index = s.indexOf(piece, index);
-                if (index < 0)
-                {
-                    result = false;
-                    break loop;
-                }
-            }
-
-            // Move string index beyond the matching piece.
-            index += piece.length();
-        }
-
-        return result;
-    }
 }
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java
index 4dd4561..21cb46d 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java
@@ -25,6 +25,7 @@
 import org.apache.felix.framework.capabilityset.Attribute;
 import org.apache.felix.framework.capabilityset.Capability;
 import org.apache.felix.framework.capabilityset.Directive;
+import org.apache.felix.framework.capabilityset.SimpleFilter;
 import org.apache.felix.framework.resolver.Module;
 import org.apache.felix.framework.util.Util;
 import org.osgi.framework.Constants;
@@ -83,7 +84,7 @@
 
                 for (int filterIdx = 0; filterIdx < filters.size(); filterIdx++)
                 {
-                    List<String> substrings = Util.parseSubstring(filters.get(filterIdx));
+                    List<String> substrings = SimpleFilter.parseSubstring(filters.get(filterIdx));
                     filterList.add(substrings);
                 }
 
@@ -198,7 +199,7 @@
             (!included) && (m_includeFilter != null) && (i < m_includeFilter.size());
             i++)
         {
-            included = Util.checkSubstring(m_includeFilter.get(i), className);
+            included = SimpleFilter.compareSubstring(m_includeFilter.get(i), className);
         }
 
         // If there are no exclude filters then no classes are excluded
@@ -208,7 +209,7 @@
             (!excluded) && (m_excludeFilter != null) && (i < m_excludeFilter.size());
             i++)
         {
-            excluded = Util.checkSubstring(m_excludeFilter.get(i), className);
+            excluded = SimpleFilter.compareSubstring(m_excludeFilter.get(i), className);
         }
         return included && !excluded;
     }
diff --git a/framework/src/test/java/org/apache/felix/framework/capabilityset/SimpleFilterTest.java b/framework/src/test/java/org/apache/felix/framework/capabilityset/SimpleFilterTest.java
new file mode 100644
index 0000000..dea8c91
--- /dev/null
+++ b/framework/src/test/java/org/apache/felix/framework/capabilityset/SimpleFilterTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.felix.framework.capabilityset;
+
+import junit.framework.TestCase;
+import java.util.List;
+
+public class SimpleFilterTest extends TestCase
+{
+    public void testSubstringMatching()
+    {
+        List<String> pieces;
+
+        pieces = SimpleFilter.parseSubstring("foo");
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foo"));
+        assertFalse("Should not match!", SimpleFilter.compareSubstring(pieces, "barfoo"));
+        assertFalse("Should not match!", SimpleFilter.compareSubstring(pieces, "foobar"));
+
+        pieces = SimpleFilter.parseSubstring("foo*");
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foo"));
+        assertFalse("Should not match!", SimpleFilter.compareSubstring(pieces, "barfoo"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foobar"));
+
+        pieces = SimpleFilter.parseSubstring("*foo");
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foo"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "barfoo"));
+        assertFalse("Should match!", SimpleFilter.compareSubstring(pieces, "foobar"));
+
+        pieces = SimpleFilter.parseSubstring("foo*bar");
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foobar"));
+        assertFalse("Should not match!", SimpleFilter.compareSubstring(pieces, "barfoo"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foosldfjbar"));
+
+        pieces = SimpleFilter.parseSubstring("*foo*bar");
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foobar"));
+        assertFalse("Should not match!", SimpleFilter.compareSubstring(pieces, "foobarfoo"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "barfoobar"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "sdffoobsdfbar"));
+
+        pieces = SimpleFilter.parseSubstring("*foo*bar*");
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foobar"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "foobarfoo"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "barfoobar"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "sdffoobsdfbar"));
+        assertTrue("Should match!", SimpleFilter.compareSubstring(pieces, "sdffoobsdfbarlj"));
+        assertFalse("Should not match!", SimpleFilter.compareSubstring(pieces, "sdffobsdfbarlj"));
+    }
+}
diff --git a/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java b/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java
deleted file mode 100644
index 0761cb7..0000000
--- a/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * 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.felix.framework.util;
-
-import junit.framework.TestCase;
-import java.util.List;
-
-public class UtilTest extends TestCase
-{
-    public void testSubstringMatching()
-    {
-        List<String> pieces;
-
-        pieces = Util.parseSubstring("foo");
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foo"));
-        assertFalse("Should not match!", Util.checkSubstring(pieces, "barfoo"));
-        assertFalse("Should not match!", Util.checkSubstring(pieces, "foobar"));
-
-        pieces = Util.parseSubstring("foo*");
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foo"));
-        assertFalse("Should not match!", Util.checkSubstring(pieces, "barfoo"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foobar"));
-
-        pieces = Util.parseSubstring("*foo");
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foo"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "barfoo"));
-        assertFalse("Should match!", Util.checkSubstring(pieces, "foobar"));
-
-        pieces = Util.parseSubstring("foo*bar");
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foobar"));
-        assertFalse("Should not match!", Util.checkSubstring(pieces, "barfoo"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foosldfjbar"));
-
-        pieces = Util.parseSubstring("*foo*bar");
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foobar"));
-        assertFalse("Should not match!", Util.checkSubstring(pieces, "foobarfoo"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "barfoobar"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "sdffoobsdfbar"));
-
-        pieces = Util.parseSubstring("*foo*bar*");
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foobar"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "foobarfoo"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "barfoobar"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "sdffoobsdfbar"));
-        assertTrue("Should match!", Util.checkSubstring(pieces, "sdffoobsdfbarlj"));
-        assertFalse("Should not match!", Util.checkSubstring(pieces, "sdffobsdfbarlj"));
-    }
-}