Ejemplo n.º 1
0
        private void FindToken(int currentCharIndex, out int lengthOfFoundToken, out byte tokenByte)
        {
            bool endOfSearch    = false;
            int  lengthofSearch = 1;

            lengthOfFoundToken = -1;
            tokenByte          = 0x00;

            Dictionary <string, byte> filteredTokenDic = TvcBasicTokenLibrary.BasicTokens;

            while (!endOfSearch && (currentCharIndex + lengthofSearch) <= RowText.Length)
            {
                string tokenStringForSearch = RowText.Substring(currentCharIndex, lengthofSearch++).ToUpper();
                var    tokens = filteredTokenDic
                                .Where(kvp => kvp.Key.StartsWith(tokenStringForSearch, StringComparison.InvariantCultureIgnoreCase))
                                .Select(kvp => kvp);
                if (tokens.Count() == 1)
                {
                    KeyValuePair <string, byte> pair = tokens.First();
                    if (pair.Key.Equals(tokenStringForSearch))
                    {
                        endOfSearch        = true;
                        lengthOfFoundToken = pair.Key.Length;
                        tokenByte          = pair.Value;
                    }
                }
                else if (tokens.Count() > 1)
                {
                    KeyValuePair <string, byte> pair = tokens.FirstOrDefault(kvp => kvp.Key == tokenStringForSearch);
                    if (pair.Key != null)
                    {
                        lengthOfFoundToken = pair.Key.Length;
                        tokenByte          = pair.Value;
                    }
                    filteredTokenDic = tokens.ToDictionary(kvp => kvp.Key, KeyValuePair => KeyValuePair.Value);
                }
                else
                {
                    endOfSearch = true;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Validate the File against the internal FileDefinition.
        /// </summary>
        /// <param name="AbortLimit">Limit of critical errors allowed before aborting validation.</param>
        public void Validate(int AbortLimit, RowValidationFunction rvf)
        {
            StreamReader sr = new StreamReader(this.FilePath, this.FileDefinition.Encoding);

            this.ValidationErrors.Add(new ValidationError(string.Format("File Name: {0}", this.FileName), ValidationErrorSeverity.Information));
            this.ValidationErrors.Add(new ValidationError(string.Format("File Hash: {0}", this.FileHash), ValidationErrorSeverity.Information));

            int RowPosition = 0;

            string[] RowValues = null;
            string   RowText;
            int      DataRowCount = 0;

            while ((RowText = sr.ReadLine()) != null)
            {
                RowPosition++;

                // got the next row if directed to skip row
                if (RowPosition <= this.FileDefinition.SkipRows)
                {
                    continue;
                }


                // consider if the row has now values
                if (RowText == null || RowText.Trim().Length == 0)
                {
                    if (this.FileDefinition.IgnoreEmptyRows == true)
                    {
                        this.ValidationErrors.Add(new ValidationError(RowPosition, null,
                                                                      string.Format("Row {0} contains no data.", RowPosition), ValidationErrorSeverity.Warning));
                    }
                    else
                    {
                        this.ValidationErrors.Add(new ValidationError(RowPosition, null,
                                                                      string.Format("Row {0} contains no data.", RowPosition), ValidationErrorSeverity.Critical));
                    }

                    // raise the event
                    if (rvf != null)
                    {
                        rvf(RowPosition, RowText, false, this.FileDefinition.IgnoreEmptyRows?0:1, null);
                    }


                    // go to the next row
                    continue;
                }

                // convert row text to string array
                RowValues = this.FileDefinition.ConvertRowToValues(RowText);

                // determine the applicable row definition
                RowDefinition rd = this.FileDefinition.FindRowDefinition(RowValues);

                if (rd == null)
                {
                    this.ValidationErrors.Add(
                        new ValidationError(RowPosition, "Undetermined",
                                            string.Format("The row disposition could not be determined")));

                    // raise the event
                    if (rvf != null)
                    {
                        rvf(RowPosition, RowText, true, 1, null);
                    }
                }
                else
                {
                    // this row has data
                    DataRowCount++;
                    rd.Validate(RowValues, RowPosition, this.ValidationErrors);

                    // raise the event
                    if (rvf != null)
                    {
                        rvf(RowPosition, RowText, true, ValidationError.CriticalErrorCount(this.ValidationErrors, RowPosition), rd.DispositionColumnValue);
                    }
                }

                // break out of validation if abort limit is hit
                if (AbortLimit > 0 && ValidationError.CriticalErrorCount(this.ValidationErrors) >= AbortLimit)
                {
                    break;
                }
            }

            // close the stream reader
            sr.Close();

            // report the count of data rows (with disposition)
            // ADDITIONAL VALIDATION
            if (DataRowCount == 0)
            {
                this.ValidationErrors.Add(new ValidationError(
                                              string.Format("No data rows were presented in this file.")
                                              ));
            }
            else
            {
                this.ValidationErrors.Add(new ValidationError(
                                              string.Format("{0} data row(s) have disposition in this file.", DataRowCount), ValidationErrorSeverity.Information
                                              ));
            }

            // report the number of validation errors
            // ADDITIONAL VALIDATION
            int CriticalErrorCount = ValidationError.CriticalErrorCount(this.ValidationErrors);

            this.ValidationErrors.Add(new ValidationError(
                                          string.Format("{0} critical errors were identified in this file.", CriticalErrorCount), ValidationErrorSeverity.Information
                                          ));


            // report if the abort limit was reached
            // ADDITIONAL VALIDATION
            if (CriticalErrorCount > 0 && CriticalErrorCount >= AbortLimit)
            {
                this.ValidationErrors.Add(new ValidationError(
                                              string.Format("Validation was halted after {0} critical errors.",
                                                            (CriticalErrorCount > AbortLimit)?CriticalErrorCount:AbortLimit), ValidationErrorSeverity.Information
                                              ));
            }
        }
Ejemplo n.º 3
0
        private void TokenizeRow()
        {
            if (string.IsNullOrEmpty(RowText))
            {
                throw new TvcBasicException(@" A basic sor üres sor!");
            }

            byte        highByte = (byte)(RowNumber >> 8);
            byte        lowByte  = (byte)(RowNumber & 0xFF);
            List <byte> tokBytes = new List <byte> {
                0x00, lowByte, highByte
            };

            TokenizingState charState = TokenizingState.CharMustTokenized;


            for (int currentCharIndex = SkipRowNumberAndFirstSpaces(); currentCharIndex < RowText.Length;)
            {
                char currentChar = RowText[currentCharIndex];

                // The numers and spaces must not be tokenised. They appear with their ASCII code in tokenised Tvc basic Row
                if (charState == TokenizingState.CharMustTokenized && currentChar != ' ' && !char.IsNumber(currentChar))
                {
                    KeyValuePair <string, byte>?foundToken = null;

                    //Detects if the current character can be part of a basic token
                    foreach (KeyValuePair <string, byte> pair in TvcBasicTokenLibrary.BasicTokens
                             .Where(kvp => RowText.Length - currentCharIndex >= kvp.Key.Length)
                             .Select(kvp => kvp))
                    {
                        string tokenStringForSearch = RowText.Substring(currentCharIndex, pair.Key.Length).ToUpper();
                        if (tokenStringForSearch == pair.Key)
                        {
                            foundToken = pair;
                            break;
                        }
                    }

                    if (foundToken.HasValue)
                    {
                        if (foundToken.Value.Value == 0xFC ||
                            foundToken.Value.Value == 0xFE ||
                            foundToken.Value.Value == 0xFB)
                        {
                            // If the found token is 'DATA', '!', or 'REM', the following characters must not be tokenised
                            charState |= TokenizingState.CharInDataOrCommentRow;
                        }

                        tokBytes.Add(foundToken.Value.Value);
                        currentCharIndex += foundToken.Value.Key.Length;
                    }
                    else
                    {
                        // if the current character is not part of a basic token
                        if (currentChar == '"')
                        {
                            charState ^= TokenizingState.CharInLiteral;
                        }
                        // In literal the character must not be converted into upper case
                        currentChar = charState.HasFlag(TokenizingState.CharInLiteral) ? currentChar : char.ToUpper(currentChar);
                        tokBytes.Add(currentChar.ToTvcBasicAscii());
                        currentCharIndex++;
                    }
                }
                else
                {
                    if (currentChar == '"')
                    {
                        charState ^= TokenizingState.CharInLiteral;
                        //inLiteral = !inLiteral;
                        //charMustTokenize = true;
                    }
                    if (currentChar == ':' && !charState.HasFlag(TokenizingState.CharInLiteral))
                    {
                        tokBytes.Add(0xFD);
                        charState = TokenizingState.CharMustTokenized;
                        currentCharIndex++;
                    }
                    else
                    {
                        currentChar = charState.HasFlag(TokenizingState.CharInLiteral) ? currentChar : char.ToUpper(currentChar);
                        tokBytes.Add(currentChar.ToTvcBasicAscii());
                        currentCharIndex++;
                    }
                }
            }

            tokBytes.Add(0xFF);
            tokBytes[0]    = (byte)tokBytes.Count;
            TokenizedBytes = tokBytes.ToArray();
        }