public PathMatcher getPathMatcher()

in src/main/java/com/pastdev/jsch/nio/file/UnixSshFileSystem.java [70:195]


    public PathMatcher getPathMatcher( String syntaxAndPattern ) {
        int firstColon = syntaxAndPattern.indexOf( ':' );
        if ( firstColon == -1 ) {
            throw new IllegalArgumentException( "must be of the form 'syntax:pattern'" );
        }

        String syntax = syntaxAndPattern.substring( 0, firstColon );
        String patternString = syntaxAndPattern.substring( firstColon + 1 );

        if ( syntax.equalsIgnoreCase( "glob" ) ) {
            StringBuilder builder = new StringBuilder().append( '^' );
            char[] chars = patternString.toCharArray();
            for ( int i = 0; i < chars.length; i++ ) {
                char c = chars[i];
                if ( c == '*' ) {
                    if ( chars[i + 1] == '*' ) {
                        builder.append( ".*" );
                        i++;
                    }
                    else {
                        builder.append( "[^/]*" );
                    }
                }
                else if ( c == '?' ) {
                    builder.append( '.' );
                }
                else if ( c == '{' ) {
                    builder.append( "(?:" );
                    while ( true ) {
                        if ( ++i < chars.length ) {
                            c = chars[i];
                        }
                        else {
                            throw new IllegalArgumentException( "invalid glob '" + patternString + "'" );
                        }

                        if ( c == ',' ) {
                            builder.append( '|' );
                        }
                        else if ( c == '}' ) {

                            break;
                        }
                        else {
                            builder.append( c );
                        }
                    }
                    builder.append( ")" );
                }
                else if ( c == '[' ) {
                    builder.append( '[' );
                    boolean first = true;
                    boolean caratEncountered = false;
                    while ( true ) {
                        if ( ++i < chars.length ) {
                            c = chars[i];
                        }
                        else {
                            throw new IllegalArgumentException( "invalid glob '" + patternString + "', unclosed range" );
                        }

                        if ( first ) {
                            if ( c == '!' ) {
                                builder.append( "^" );
                            }
                            else if ( c == '^' ) {
                                caratEncountered = true;
                            }
                            else {
                                builder.append( c );
                            }
                            first = false;
                            continue;
                        }

                        if ( c == ']' ) {
                            break;
                        }
                        else if ( c == '\\' ) {
                            builder.append( "\\\\" );
                        }
                        else {
                            builder.append( c );
                        }
                    }
                    if ( caratEncountered ) builder.append( '^' );
                    builder.append( ']' );
                }
                else if ( c == '\\' ) {
                    if ( ++i < chars.length ) {
                        builder.append( chars[i] );
                    }
                    else {
                        throw new IllegalArgumentException( "invalid glob '" + patternString + "'" );
                    }
                }
                else if ( c == '.' ) {
                    builder.append( "\\." );
                }
                else if ( c == '+' ) {
                    builder.append( "\\+" );
                }
                else if ( c == '(' ) {
                    builder.append( "\\(" );
                }
                else {
                    builder.append( c );
                }
            }
            patternString = builder.append( '$' ).toString();
        }
        else if ( syntax.equalsIgnoreCase( "regex" ) ) {
            // do nothing, pattern string is already regex
        }
        else {
            throw new UnsupportedOperationException( "i dont have any clue what '" + syntax + "' is supposed to mean" );
        }

        final Pattern pattern = Pattern.compile( patternString );
        return new PathMatcher() {
            @Override
            public boolean matches( Path path ) {
                return pattern.matcher( path.toString() ).matches();
            }
        };
    }