Class GlobToRegex


  • final class GlobToRegex
    extends java.lang.Object
    Translates globs to regex patterns.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  GlobToRegex.State
      Converter state.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private GlobToRegex​(java.lang.String glob, java.lang.String separators)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void append​(char c)
      Appends the regex form of the given normal character or separator from the glob.
      private void appendBracketEnd()
      Appends the regex form of the end of a glob [] section.
      private void appendBracketStart()
      Appends the regex form of the start of a glob [] section.
      private void appendCurlyBraceEnd()
      Appends the regex form of the end of a glob {} section.
      private void appendCurlyBraceStart()
      Appends the regex form of the start of a glob {} section.
      private void appendExact​(char c)
      Appends the given character as-is to the regex.
      private void appendInBracket​(char c)
      Appends the regex form of the given character within a glob [] section.
      private void appendNonSeparator()
      Appends the regex form that matches anything except the separators for the path type.
      private void appendNormal​(char c)
      Appends the regex form of the given normal character from the glob.
      private void appendQuestionMark()
      Appends the regex form of the glob ? character.
      private void appendSeparator()
      Appends the regex form matching the separators for the path type.
      private void appendStar()
      Appends the regex form of the glob * character.
      private void appendStarStar()
      Appends the regex form of the glob ** pattern.
      private void appendSubpatternSeparator()
      Appends the regex form of the separator (,) within a glob {} section.
      private java.lang.String convert()
      Converts the glob to a regex one character at a time.
      private GlobToRegex.State currentState()
      Returns the current state.
      private void popState()
      Returns to the previous state.
      private void pushState​(GlobToRegex.State state)
      Enters the given state.
      private java.util.regex.PatternSyntaxException syntaxError​(java.lang.String desc)
      Throws a PatternSyntaxException.
      static java.lang.String toRegex​(java.lang.String glob, java.lang.String separators)
      Converts the given glob to a regular expression pattern.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • glob

        private final java.lang.String glob
      • separators

        private final java.lang.String separators
      • builder

        private final java.lang.StringBuilder builder
      • index

        private int index
      • ESCAPE

        private static final GlobToRegex.State ESCAPE
        State following the reading of a single \.
      • STAR

        private static final GlobToRegex.State STAR
        State following the reading of a single *.
      • BRACKET_FIRST_CHAR

        private static final GlobToRegex.State BRACKET_FIRST_CHAR
        State immediately following the reading of a [.
      • BRACKET

        private static final GlobToRegex.State BRACKET
        State inside [brackets], but not at the first character inside the brackets.
      • CURLY_BRACE

        private static final GlobToRegex.State CURLY_BRACE
        State inside {curly braces}.
    • Constructor Detail

      • GlobToRegex

        private GlobToRegex​(java.lang.String glob,
                            java.lang.String separators)
    • Method Detail

      • toRegex

        public static java.lang.String toRegex​(java.lang.String glob,
                                               java.lang.String separators)
        Converts the given glob to a regular expression pattern. The given separators determine what characters the resulting expression breaks on for glob expressions such as * which should not cross directory boundaries.

        Basic conversions (assuming / as only separator):

        
         ?        = [^/]
         *        = [^/]*
         **       = .*
         [a-z]    = [[^/]&&[a-z]]
         [!a-z]   = [[^/]&&[^a-z]]
         {a,b,c}  = (a|b|c)
         
      • convert

        private java.lang.String convert()
        Converts the glob to a regex one character at a time. A state stack (states) is maintained, with the state at the top of the stack being the current state at any given time. The current state is always used to process the next character. When a state processes a character, it may pop the current state or push a new state as the current state. The resulting regex is written to builder.
      • pushState

        private void pushState​(GlobToRegex.State state)
        Enters the given state. The current state becomes the previous state.
      • popState

        private void popState()
        Returns to the previous state.
      • syntaxError

        private java.util.regex.PatternSyntaxException syntaxError​(java.lang.String desc)
        Throws a PatternSyntaxException.
      • appendExact

        private void appendExact​(char c)
        Appends the given character as-is to the regex.
      • append

        private void append​(char c)
        Appends the regex form of the given normal character or separator from the glob.
      • appendNormal

        private void appendNormal​(char c)
        Appends the regex form of the given normal character from the glob.
      • appendSeparator

        private void appendSeparator()
        Appends the regex form matching the separators for the path type.
      • appendNonSeparator

        private void appendNonSeparator()
        Appends the regex form that matches anything except the separators for the path type.
      • appendQuestionMark

        private void appendQuestionMark()
        Appends the regex form of the glob ? character.
      • appendStar

        private void appendStar()
        Appends the regex form of the glob * character.
      • appendStarStar

        private void appendStarStar()
        Appends the regex form of the glob ** pattern.
      • appendBracketStart

        private void appendBracketStart()
        Appends the regex form of the start of a glob [] section.
      • appendBracketEnd

        private void appendBracketEnd()
        Appends the regex form of the end of a glob [] section.
      • appendInBracket

        private void appendInBracket​(char c)
        Appends the regex form of the given character within a glob [] section.
      • appendCurlyBraceStart

        private void appendCurlyBraceStart()
        Appends the regex form of the start of a glob {} section.
      • appendSubpatternSeparator

        private void appendSubpatternSeparator()
        Appends the regex form of the separator (,) within a glob {} section.
      • appendCurlyBraceEnd

        private void appendCurlyBraceEnd()
        Appends the regex form of the end of a glob {} section.