litesoft
@ 959
litesoft / trunk / Java / ScarPlus / src / com / esotericsoftware / wildcard / Pattern.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
package com.esotericsoftware.wildcard; import com.esotericsoftware.utils.*; import com.esotericsoftware.wildcard.support.*; public class Pattern { private final String mPattern; private final DirMatcher mDirMatcher; private final FileNameMatcher mFileNameMatcher; private final boolean mAllFiles; public Pattern( String pattern ) { mPattern = clean( pattern.replace( '\\', '/' ).trim() ); int lastSep = mPattern.lastIndexOf( '/' ); if ( lastSep == -1 ) { mDirMatcher = CurrentDirMatcher.INSTANCE; mFileNameMatcher = createFileNameMatcher( mPattern ); } else { mDirMatcher = createDirMatcher( mPattern.substring( 0, lastSep ).trim() ); mFileNameMatcher = createFileNameMatcher( mPattern.substring( lastSep + 1 ).trim() ); } mAllFiles = AllFileNameMatcher.INSTANCE == mFileNameMatcher; } public static String clean( String pattern ) { pattern = pattern.startsWith( "/" ) ? "." + pattern : "./" + pattern; String newPattern = pattern; newPattern = Util.replace( newPattern, "?*", "*" ); newPattern = Util.replace( newPattern, "*?", "*" ); newPattern = Util.replace( newPattern, " /", "/" ); newPattern = Util.replace( newPattern, "/ ", "/" ); newPattern = Util.replace( newPattern, "/./", "//" ); newPattern = Util.replace( newPattern, "//", "/" ); if ( newPattern.endsWith( "**" ) ) { newPattern += "/"; } if ( newPattern.endsWith( "/" ) ) { newPattern += "*"; } newPattern = processNonSlashedStarStar( newPattern ); newPattern = Util.replace( newPattern, "/**/**/", "/**/" ); return newPattern.substring( 2 ); } private static String processNonSlashedStarStar( String pPattern ) { int from = 0; for ( int at; -1 != (at = pPattern.indexOf( "**", from )); from = at + 1 ) { if ( pPattern.charAt( at + 2 ) != '/' ) { pPattern = pPattern.substring( 0, at + 2 ) + "/*" + pPattern.substring( at + 2 ); } if ( pPattern.charAt( at - 1 ) != '/' ) { pPattern = pPattern.substring( 0, at ) + "*/" + pPattern.substring( at ); at += 2; } } return pPattern; } private DirMatcher createDirMatcher( String pDirPattern ) { if ( "".equals( pDirPattern ) || ".".equals( pDirPattern ) ) { return CurrentDirMatcher.INSTANCE; } if ( "**".equals( pDirPattern ) ) { return AllDirMatcher.INSTANCE; } if ( !pDirPattern.contains( "*" ) && !pDirPattern.contains( "?" ) ) { return new ExactDirMatcher( pDirPattern ); } String[] zDirParts = FilePathPartMatcher.SLASH.split( pDirPattern, 0 ); return (zDirParts.length == 1) ? new WildSingleDirMatcher( pDirPattern ) : new WildMultiDirMatcher( zDirParts ); } private static FileNameMatcher createFileNameMatcher( String pFileNamePattern ) { pFileNamePattern = pFileNamePattern.trim(); if ( pFileNamePattern.equals( "*.*" ) || pFileNamePattern.equals( "*" ) ) { return AllFileNameMatcher.INSTANCE; } if ( !pFileNamePattern.contains( "*" ) && !pFileNamePattern.contains( "?" ) ) { return new ExactFileNameMatcher( pFileNamePattern ); } return new WildFileNameMatcher( pFileNamePattern ); } /** * return True if the directory specified with <code>dirPath</code> <i>could possibly</i> host directories that <i>could</i> host files acceptable to this Pattern * * @param dirPath !null and path separators converted to '/' */ public boolean acceptableParentDirPath( String dirPath ) { return mDirMatcher.acceptableParentDir( dirPath ); } /** * return True if the directory specified with <code>dirPath</code> <i>could</i> host files <b>directly</b> that are acceptable to this Pattern * * @param dirPath !null and path separators converted to '/' */ public boolean acceptableDirPath( String dirPath ) { return mDirMatcher.acceptable( dirPath ); } /** * return True if the directory specified with <code>dirPath</code> <i>could</i> host files <b>directly</b> that are acceptable to this Pattern AND this pattern will include all child files (optionally at any depth) * * @param dirPath !null and path separators converted to '/' */ public boolean matchesDirPathAndChildren( String dirPath ) { return mAllFiles && acceptableDirPath( dirPath ); } /** * return True if the file specified with <code>filePath</code> is acceptable to this Pattern (any parent path is checked against the <code>matchesDirPath</code>) * * @param filePath !null and path separators converted to '/' */ public boolean matchesFilePath( String filePath ) { String dirPath = ""; String fileName = filePath; int lastSep = filePath.lastIndexOf( '/' ); if ( lastSep != -1 ) { dirPath = filePath.substring( 0, lastSep ); fileName = filePath.substring( lastSep + 1 ); } return mDirMatcher.acceptable( dirPath ) && mFileNameMatcher.acceptable( fileName ); } @Override public String toString() { return mPattern; } } |