You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2010/03/31 21:36:26 UTC

svn commit: r929684 - in /felix/trunk/framework/src: main/java/org/apache/felix/framework/ main/java/org/apache/felix/framework/capabilityset/ main/java/org/apache/felix/framework/util/ main/java/org/apache/felix/framework/util/manifestparser/ test/jav...

Author: rickhall
Date: Wed Mar 31 19:36:26 2010
New Revision: 929684

URL: http://svn.apache.org/viewvc?rev=929684&view=rev
Log:
Remove duplicate code for checking substrings. (FELIX-2035)

Added:
    felix/trunk/framework/src/test/java/org/apache/felix/framework/capabilityset/
    felix/trunk/framework/src/test/java/org/apache/felix/framework/capabilityset/SimpleFilterTest.java
      - copied, changed from r929662, felix/trunk/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java
Removed:
    felix/trunk/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java
Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java?rev=929684&r1=929683&r2=929684&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/FindEntriesEnumeration.java Wed Mar 31 19:36:26 2010
@@ -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 @@ class FindEntriesEnumeration implements 
         // 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 @@ class FindEntriesEnumeration implements 
                         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);

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java?rev=929684&r1=929683&r2=929684&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java Wed Mar 31 19:36:26 2010
@@ -383,7 +383,7 @@ public class CapabilitySet
                 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);

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java?rev=929684&r1=929683&r2=929684&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java Wed Mar 31 19:36:26 2010
@@ -366,7 +366,6 @@ public class SimpleFilter
         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 @@ loop:   for (;;)
         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,

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java?rev=929684&r1=929683&r2=929684&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java Wed Mar 31 19:36:26 2010
@@ -530,149 +530,4 @@ public class Util
         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

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java?rev=929684&r1=929683&r2=929684&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/CapabilityImpl.java Wed Mar 31 19:36:26 2010
@@ -25,6 +25,7 @@ import java.util.StringTokenizer;
 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 @@ public class CapabilityImpl implements C
 
                 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 @@ public class CapabilityImpl implements C
             (!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 @@ public class CapabilityImpl implements C
             (!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;
     }

Copied: felix/trunk/framework/src/test/java/org/apache/felix/framework/capabilityset/SimpleFilterTest.java (from r929662, felix/trunk/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java)
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/test/java/org/apache/felix/framework/capabilityset/SimpleFilterTest.java?p2=felix/trunk/framework/src/test/java/org/apache/felix/framework/capabilityset/SimpleFilterTest.java&p1=felix/trunk/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java&r1=929662&r2=929684&rev=929684&view=diff
==============================================================================
--- felix/trunk/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java (original)
+++ felix/trunk/framework/src/test/java/org/apache/felix/framework/capabilityset/SimpleFilterTest.java Wed Mar 31 19:36:26 2010
@@ -16,49 +16,49 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.felix.framework.util;
+package org.apache.felix.framework.capabilityset;
 
 import junit.framework.TestCase;
 import java.util.List;
 
-public class UtilTest extends TestCase
+public class SimpleFilterTest 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"));
+        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"));
     }
 }