Esempio n. 1
0
        public PatternBuilder AddGuid(string format, bool force)
        {
            var          token = new Token();
            ControlBlock cb    = null;

            if (!string.IsNullOrWhiteSpace(format))
            {
                if (!string.Equals("N", format, StringComparison.CurrentCulture) && !string.Equals("D", format, StringComparison.CurrentCulture) &&
                    !string.Equals("B", format, StringComparison.CurrentCulture) && !string.Equals("P", format, StringComparison.CurrentCulture) &&
                    !string.Equals("X", format, StringComparison.CurrentCulture))
                {
                    throw new PatternBuilderException($"An unrecognized GUID format string was found.  Format string found: '{format}'.");
                }

                if (force)
                {
                    token.Type  = TokenType.LITERAL;
                    token.Value = Guid.NewGuid().ToString(format);
                }
                else
                {
                    token.Type = TokenType.CONTROL_BLOCK;
                    cb         = new ControlBlock()
                    {
                        Type         = ControlBlockType.FCB,
                        FunctionName = "GUID",
                        Function     = () => Guid.NewGuid().ToString(format),
                        Format       = format,
                    };
                }
            }
            else
            {
                if (force)
                {
                    token.Type  = TokenType.LITERAL;
                    token.Value = Guid.NewGuid().ToString();
                }
                else
                {
                    token.Type = TokenType.CONTROL_BLOCK;
                    cb         = new ControlBlock()
                    {
                        Type         = ControlBlockType.FCB,
                        FunctionName = "GUID",
                        Function     = () => Guid.NewGuid().ToString()
                    };
                }
            }

            token.ControlBlock = cb;
            _patternList.Last().Tokens.Add(token);
            return(this);
        }
Esempio n. 2
0
        public PatternBuilder AddDateTime(string format, bool force)
        {
            var          token = new Token();
            ControlBlock cb    = null;

            if (!string.IsNullOrWhiteSpace(format))
            {
                if (force)
                {
                    token.Type  = TokenType.LITERAL;
                    token.Value = DateTime.Now.ToString(format);
                }
                else
                {
                    token.Type = TokenType.CONTROL_BLOCK;
                    cb         = new ControlBlock()
                    {
                        Type         = ControlBlockType.FCB,
                        FunctionName = "DATETIME",
                        Function     = () => DateTime.Now.ToString(format),
                        Format       = format
                    };
                }
            }
            else
            {
                if (force)
                {
                    token.Type  = TokenType.LITERAL;
                    token.Value = DateTime.Now.ToString();
                }
                else
                {
                    token.Type = TokenType.CONTROL_BLOCK;
                    cb         = new ControlBlock()
                    {
                        Type         = ControlBlockType.FCB,
                        FunctionName = "DATETIME",
                        Function     = () => DateTime.Now.ToString()
                    };
                }
            }

            token.ControlBlock = cb;
            _patternList.Last().Tokens.Add(token);
            return(this);
        }
Esempio n. 3
0
        private void HandleFunctionBlock(ref Token token, string pattern, int originalPosition)
        {
            var EOS   = false;
            var force = false;
            var cb    = new ControlBlock()
            {
                Type = ControlBlockType.FCB
            };
            var  functionName = new StringBuilder();
            var  formatter    = new StringBuilder(string.Empty);
            char functionCode = 'x';

            while (!EOS)
            {
                pos++;
                if (pos >= pattern.Length)
                {
                    throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                }

                switch (pattern[pos])
                {
                case 'T':
                case 'G':
                    functionCode = pattern[pos];
                    pos++;
                    if (pos >= pattern.Length)
                    {
                        throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                    }
                    if (char.Equals(pattern[pos], '}'))
                    {
                        EOS = true;
                        //cb.FunctionName = functionCode.ToString();
                        //if (char.Equals(functionCode, 'T'))
                        //    cb.Function = () => DateTime.Now.ToString();
                        //else
                        //    cb.Function = () => Guid.NewGuid().ToString();

                        //token.ControlBlock = cb;
                        //EOS = true;
                    }
                    else if (char.Equals(pattern[pos], '?'))
                    {
                        if (!char.Equals(pattern[pos + 1], '}'))
                        {
                            throw new InvalidPatternException($"Expecting a control block closing brace at positions {pos + 1}, which was not found.");
                        }

                        force = true;
                        EOS   = true;
                        pos++;
                    }
                    else if (char.Equals(pattern[pos], ':'))
                    {
                        cb.FunctionName = functionCode.ToString();
                        while (!EOS)
                        {
                            pos++;
                            if (pos >= pattern.Length)
                            {
                                throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                            }
                            if (char.Equals(pattern[pos], '}'))
                            {
                                if (formatter.Length < 1)
                                {
                                    throw new InvalidPatternException($"Expected function formatting string at position {pos}.");
                                }

                                EOS = true;
                            }
                            else if (char.Equals(pattern[pos], '?'))
                            {
                                if (!char.Equals(pattern[pos + 1], '}'))
                                {
                                    throw new InvalidPatternException($"Expecting a control block closing brace at positions {pos + 1}, which was not found.");
                                }

                                force = true;
                            }
                            else
                            {
                                formatter.Append(pattern[pos]);
                            }
                        }
                    }
                    else
                    {
                        throw new InvalidPatternException($"An unexpected token '{pattern[pos]}' was found at position {pos}.");
                    }
                    break;

                default:
                    functionName.Append(pattern[pos]);
                    while (true)
                    {
                        pos++;
                        if (pos >= pattern.Length)
                        {
                            throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                        }

                        if (pattern[pos].Equals('}'))
                        {
                            break;
                        }
                        functionName.Append(pattern[pos]);
                    }
                    cb.FunctionName = functionName.ToString();

                    if (string.IsNullOrWhiteSpace(cb.FunctionName))
                    {
                        throw new InvalidPatternException($"User-defined function name cannot be null, empty, or whitespace (UDF starting at position {originalPosition}).");
                    }

                    EOS = true;
                    break;
                }
            }

            var format = formatter.ToString();

            if (char.Equals(functionCode, 'G'))
            {
                cb.FunctionName = "GUID";
                if (!string.IsNullOrWhiteSpace(format))
                {
                    if (!string.Equals("N", format, StringComparison.CurrentCulture) && !string.Equals("D", format, StringComparison.CurrentCulture) &&
                        !string.Equals("B", format, StringComparison.CurrentCulture) && !string.Equals("P", format, StringComparison.CurrentCulture) &&
                        !string.Equals("X", format, StringComparison.CurrentCulture))
                    {
                        throw new InvalidPatternException($"An unrecognized GUID format string was found.  Format string found: '{format}'.");
                    }


                    if (force)
                    {
                        token.Type  = TokenType.LITERAL;
                        token.Value = Guid.NewGuid().ToString(format);
                        cb          = null;
                    }
                    else
                    {
                        cb.Function = () => Guid.NewGuid().ToString(format);
                    }
                }
                else
                {
                    if (force)
                    {
                        token.Type  = TokenType.LITERAL;
                        token.Value = Guid.NewGuid().ToString();
                        cb          = null;
                    }
                    else
                    {
                        cb.Function = () => Guid.NewGuid().ToString();
                    }
                }
            }
            else if (char.Equals(functionCode, 'T'))
            {
                cb.FunctionName = "DATETIME";
                if (!string.IsNullOrWhiteSpace(format))
                {
                    if (force)
                    {
                        token.Type  = TokenType.LITERAL;
                        token.Value = DateTime.Now.ToString(format);
                        cb          = null;
                    }
                    else
                    {
                        cb.Function = () => DateTime.Now.ToString(format);
                    }
                }
                else
                {
                    if (force)
                    {
                        token.Type  = TokenType.LITERAL;
                        token.Value = DateTime.Now.ToString();
                        cb          = null;
                    }
                    else
                    {
                        cb.Function = () => DateTime.Now.ToString();
                    }
                }
            }

            token.ControlBlock = cb;

            if (TokenType.DUMMY == token.Type)
            {
                token.Type = TokenType.CONTROL_BLOCK;

                if (isInGroup)
                {
                    tokenizedGroup.Add(currentGroup);
                    currentGroup = new TokenGroup();
                    currentGroup.Tokens.Add(token);
                    handled = true;
                }
            }
        }
Esempio n. 4
0
        private void HandleExclusionBlock(ref Token token, string pattern, int originalPosition)
        {
            Token tempToken = null;
            var   cb        = new ControlBlock();
            var   sb        = new StringBuilder();

            cb.Type = ControlBlockType.ECB;
            if (pos == 1)
            {
                //This is global ECB
                cb.Global = true;
            }
            else
            {
                //Get the previous token in the the list.  If it is not a regular token (i.e. it is a control box) throw exception
                //tempToken = tokenizedList.Last();
                tempToken = currentGroup.Tokens.Last();
                if (tempToken.ControlBlock != null) //&& tempToken.ControlBlock.Global == true)
                {
                    throw new DuplicateGlobalControlBlockException($"A second exclusion control block was found starting at position {originalPosition}.");
                }

                cb.Global = false;
            }

            if (pos + 1 >= pattern.Length)
            {
                throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
            }

            var process = true;

            while (process)
            {
                pos++;
                if (pos >= pattern.Length)
                {
                    throw new InvalidPatternException($"No closing control block token found for the control block starting at position {originalPosition}.");
                }

                switch (pattern[pos])
                {
                case '{':
                    sb.Append(pattern[pos]);
                    break;

                case '\\':
                    pos++;
                    if (pattern[pos].Equals('}'))
                    {
                        sb.Append('}');
                    }
                    else if (pattern[pos].Equals('\\'))
                    {
                        sb.Append('\\');
                    }
                    else if (pattern[pos].Equals('-'))
                    {
                        sb.Append('-');
                    }
                    else
                    {
                        throw new InvalidPatternException($"Unknown escape sequence \\{pattern[pos]} at position {pos - 1}.");
                    }
                    break;

                case '}':
                    process = false;
                    break;

                default:
                    sb.Append(pattern[pos]);
                    break;
                }
            }

            cb.ExceptValues = sb.ToString().ToCharArray();
            if (cb.Global)
            {
                token.ControlBlock = cb;
                //tokenizedList.Add(token);
                currentGroup.Tokens.Add(token);
            }
            else
            {
                if (TokenType.DUMMY == token.Type)
                {
                    currentGroup.ControlBlock = cb;
                }
                else
                {
                    tempToken.ControlBlock = cb;
                    //tokenizedList.RemoveAt(tokenizedList.Count - 1);
                    //tokenizedList.Add(tempToken);
                    currentGroup.Tokens.RemoveAt(currentGroup.Tokens.Count - 1);
                    currentGroup.Tokens.Add(tempToken);
                }
            }

            handled = true;
        }