[Japi-cvs] SF.net SVN: japi:[1378] libs/util/trunk/src
Status: Beta
Brought to you by:
christianhujer
From: <aki...@us...> - 2009-09-21 19:47:02
|
Revision: 1378 http://japi.svn.sourceforge.net/japi/?rev=1378&view=rev Author: akirschbaum Date: 2009-09-21 19:46:51 +0000 (Mon, 21 Sep 2009) Log Message: ----------- Fix GlobFileFilter implementation; add unit test. Modified Paths: -------------- libs/util/trunk/src/prj/net/sf/japi/util/filter/file/GlobFileFilter.java Added Paths: ----------- libs/util/trunk/src/tst/test/net/sf/japi/util/filter/file/GlobFileFilterTest.java Modified: libs/util/trunk/src/prj/net/sf/japi/util/filter/file/GlobFileFilter.java =================================================================== --- libs/util/trunk/src/prj/net/sf/japi/util/filter/file/GlobFileFilter.java 2009-09-13 18:11:41 UTC (rev 1377) +++ libs/util/trunk/src/prj/net/sf/japi/util/filter/file/GlobFileFilter.java 2009-09-21 19:46:51 UTC (rev 1378) @@ -26,19 +26,11 @@ * </ul> * @note Currently only matching file names, not directories is possible. Globs with '/' in it are likely to always fail on most operating systems. * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @author Andreas Kirschbaum * @since 0.1 */ public class GlobFileFilter extends RegexFileFilter { - /** The regular expressions to perform for turning a glob into a pattern. */ - private static final String[][] REGEXES = { - {"(\\(|\\.|\\+|\\))", "\\\\$1"}, // escape the characters '(', '.', '+', ')' - {"\\*", ".*"}, // replace "*" with ".*" - {"\\?", "."}, // replace "?" with "." - {"^", "^"}, // prefix with ^ - {"$", "\\$"}, // postfix with $ - }; - /** Create a GlobFileFilter. * @param globs Globs to match against. */ @@ -65,11 +57,38 @@ * @return regular expression matching the same filename as the shell glob */ public static String createPatternForGlob(final String glob) { - String pattern = glob; - for (final String[] replaceStep : REGEXES) { - pattern = pattern.replaceAll(replaceStep[0], replaceStep[1]); + final StringBuilder sb = new StringBuilder(); + boolean quoting = false; + final char[] chars = glob.toCharArray(); + for (int i = 0; i < chars.length; i++) { + final char ch = chars[i]; + if (ch == '*') { + if (quoting) { + quoting = false; + sb.append("\\E"); + } + sb.append(".*"); + } else if (ch == '?') { + if (quoting) { + quoting = false; + sb.append("\\E"); + } + sb.append("."); + } else if (ch == '\\' && i + 1 < chars.length && chars[i + 1] == 'E') { + if (quoting) { + quoting = false; + sb.append("\\E"); + } + sb.append("\\\\"); + } else { + if (!quoting) { + quoting = true; + sb.append("\\Q"); + } + sb.append(ch); + } } - return pattern; + return sb.toString(); } } // class GlobFileFilter Added: libs/util/trunk/src/tst/test/net/sf/japi/util/filter/file/GlobFileFilterTest.java =================================================================== --- libs/util/trunk/src/tst/test/net/sf/japi/util/filter/file/GlobFileFilterTest.java (rev 0) +++ libs/util/trunk/src/tst/test/net/sf/japi/util/filter/file/GlobFileFilterTest.java 2009-09-21 19:46:51 UTC (rev 1378) @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2009 Andreas Kirschbaum. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package test.net.sf.japi.util.filter.file; + +import java.util.regex.Pattern; +import net.sf.japi.util.filter.file.GlobFileFilter; +import org.jetbrains.annotations.NotNull; +import org.junit.Assert; +import org.junit.Test; + +/** Test for {@link GlobFileFilter}. + * @author Andreas Kirschbaum + */ +public class GlobFileFilterTest { + + /** Test case for {@link GlobFileFilter#createPatternForGlob(String)}. + * @noinspection JUnitTestMethodWithNoAssertions + */ + @Test + public void testConcatByte() { + checkCreatePatternForGlob("abcd", "abcd", true); + checkCreatePatternForGlob("abcd", "abc", false); + checkCreatePatternForGlob("abcd", "bcd", false); + checkCreatePatternForGlob("a", "abc", false); + checkCreatePatternForGlob("b", "abc", false); + checkCreatePatternForGlob("c", "abc", false); + checkCreatePatternForGlob("^a", "aba", false); + checkCreatePatternForGlob("$a", "aba", false); + checkCreatePatternForGlob("a^", "aba", false); + checkCreatePatternForGlob("a$", "aba", false); + checkCreatePatternForGlob("^aba", "^aba", true); + checkCreatePatternForGlob("$aba", "$aba", true); + checkCreatePatternForGlob("aba^", "aba^", true); + checkCreatePatternForGlob("aba$", "aba$", true); + checkCreatePatternForGlob("abc*def", "abcef", false); + checkCreatePatternForGlob("abc*def", "abcdef", true); + checkCreatePatternForGlob("abc*def", "abcxdef", true); + checkCreatePatternForGlob("abc*def", "abcagagadef", true); + checkCreatePatternForGlob("abc?def", "abcdef", false); + checkCreatePatternForGlob("abc?def", "abcxdef", true); + checkCreatePatternForGlob("abc?def", "abcxxdef", false); + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 256; i++) { + final char ch = (char)i; + if (ch != '*' && ch != '?') { + final String s = Character.toString(ch); + checkCreatePatternForGlob(s, s, true); + sb.append(ch); + } + } + final String s = sb.toString(); + checkCreatePatternForGlob(s, s, true); + checkCreatePatternForGlob("\\Q*\\E*\\E\\Q", "\\Q\\Eabc\\E\\Q\\E\\Q", true); + } + + /** + * Calls {@link GlobFileFilter#createPatternForGlob(String)}, matches the given name against it and check for the expected result. + * @param glob the pattern to check + * @param name the name to match against the pattern + * @param expectedResult the expected result + */ + private static void checkCreatePatternForGlob(@NotNull final String glob, @NotNull final CharSequence name, final boolean expectedResult) { + final String pattern = GlobFileFilter.createPatternForGlob(glob); + Assert.assertEquals("glob="+glob+", pattern="+pattern, expectedResult, Pattern.compile(pattern).matcher(name).matches()); + } + +} Property changes on: libs/util/trunk/src/tst/test/net/sf/japi/util/filter/file/GlobFileFilterTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |