BamlLocalization.ResourceTextReader.ReadRow C# (CSharp) Метод

ReadRow() приватный Метод

private ReadRow ( ) : bool
Результат bool
        internal bool ReadRow()
        {
            // currentChar is the first char after newlines
            int currentChar = SkipAllNewLine();

            if (currentChar < 0)
            {
                // nothing else to read
                return false;
            }

            ReadState currentState = ReadState.TokenStart;
            _columns = new ArrayList();

            StringBuilder buffer = new StringBuilder();

            while (currentState != ReadState.LineEnd)
            {
                switch(currentState)
                {
                    // start of a token
                    case ReadState.TokenStart:
                    {
                        if (currentChar == _delimiter)
                        {
                            // it is the end of the token when we see a delimeter
                            // Store token, and reset state. and ignore this char
                            StoreTokenAndResetState(ref buffer, ref currentState);
                        }
                        else if (currentChar == '\"')
                        {
                            // jump to Quoted content if it token starts with a quote.
                            // and also ignore this quote
                            currentState = ReadState.QuotedContent;
                        }
                        else if (currentChar == '\n' ||
                                (currentChar == '\r' && _reader.Peek() == '\n'))
                        {
                            // we see a '\n' or '\r\n' sequence. Go to LineEnd
                            // ignore these chars
                            currentState = ReadState.LineEnd;
                        }
                        else
                        {
                            // safe to say that this is part of a unquoted content
                            buffer.Append((Char) currentChar);
                            currentState = ReadState.UnQuotedContent;
                        }
                        break;
                    }

                    // inside of an unquoted content
                    case ReadState.UnQuotedContent :
                    {
                        if (currentChar == _delimiter)
                        {
                            // It is then end of a toekn.
                            // Store the token value and reset state
                            // igore this char as well
                            StoreTokenAndResetState(ref buffer, ref currentState);
                        }
                        else if (currentChar == '\n' ||
                                (currentChar == '\r' && _reader.Peek() == '\n'))
                        {
                            // see a new line
                            // igorne these chars and jump to LineEnd
                            currentState = ReadState.LineEnd;
                        }
                        else
                        {
                            // we are good. store this char
                            // notice, even we see a '\"', we will just treat it like
                            // a normal char
                            buffer.Append((Char) currentChar);
                        }
                        break;
                    }

                    // inside of a quoted content
                    case ReadState.QuotedContent :
                    {
                        if (currentChar ==  '\"')
                        {

                            // now it depends on whether the next char is quote also
                            if (_reader.Peek() == '\"')
                            {
                                // we will ignore the next quote.
                                currentChar =  _reader.Read();
                                buffer.Append( (Char) currentChar);
                            }
                            else
                            {   // we have a single quote. We fall back to unquoted content state
                                // and igorne the curernt quote
                                currentState = ReadState.UnQuotedContent;
                            }
                        }
                        else
                        {
                            // we are still inside of a quote, anything is accepted
                            buffer.Append((Char) currentChar);
                        }
                        break;
                    }
                }

                // read in the next char
                currentChar = _reader.Read();

                if (currentChar < 0)
                {
                    // break out of the state machine if we reach the end of the file
                    break;
                }
            }

            // we got to here either we are at LineEnd, or we are end of file
            if (buffer.Length > 0)
            {
                _columns.Add(buffer.ToString());
            }
            return true;
        }

Usage Example

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="reader">resoure text reader that reads CSV or a tab-separated txt file</param>
        internal TranslationDictionariesReader(ResourceTextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            // hash key is case insensitive strings
            _table = new Hashtable();

            // we read each Row
            int rowNumber = 0;

            while (reader.ReadRow())
            {
                rowNumber++;

                // field #1 is the baml name.
                string bamlName = reader.GetColumn(0);

                // it can't be null
                if (bamlName == null)
                {
                    throw new ApplicationException(StringLoader.Get("EmptyRowEncountered"));
                }

                if (string.IsNullOrEmpty(bamlName))
                {
                    // allow for comment lines in csv file.
                    // each comment line starts with ",". It will make the first entry as String.Empty.
                    // and we will skip the whole line.
                    continue;   // if the first column is empty, take it as a comment line
                }

                // field #2: key to the localizable resource
                string key = reader.GetColumn(1);
                if (key == null)
                {
                    throw new ApplicationException(StringLoader.Get("NullBamlKeyNameInRow"));
                }

                BamlLocalizableResourceKey resourceKey = LocBamlConst.StringToResourceKey(key);

                // get the dictionary
                BamlLocalizationDictionary dictionary = this[bamlName];
                if (dictionary == null)
                {
                    // we create one if it is not there yet.
                    dictionary     = new BamlLocalizationDictionary();
                    this[bamlName] = dictionary;
                }

                BamlLocalizableResource resource;

                // the rest of the fields are either all null,
                // or all non-null. If all null, it means the resource entry is deleted.

                // get the string category
                string categoryString = reader.GetColumn(2);
                if (categoryString == null)
                {
                    // it means all the following fields are null starting from column #3.
                    resource = null;
                }
                else
                {
                    // the rest must all be non-null.
                    // the last cell can be null if there is no content
                    for (int i = 3; i < 6; i++)
                    {
                        if (reader.GetColumn(i) == null)
                        {
                            throw new Exception(StringLoader.Get("InvalidRow"));
                        }
                    }

                    // now we know all are non-null. let's try to create a resource
                    resource = new BamlLocalizableResource();

                    // field #3: Category
                    resource.Category = (LocalizationCategory)StringCatConverter.ConvertFrom(categoryString);

                    // field #4: Readable
                    resource.Readable = (bool)BoolTypeConverter.ConvertFrom(reader.GetColumn(3));

                    // field #5: Modifiable
                    resource.Modifiable = (bool)BoolTypeConverter.ConvertFrom(reader.GetColumn(4));

                    // field #6: Comments
                    resource.Comments = reader.GetColumn(5);

                    // field #7: Content
                    resource.Content = reader.GetColumn(6);

                    // in case content being the last column, consider null as empty.
                    if (resource.Content == null)
                    {
                        resource.Content = string.Empty;
                    }

                    // field > #7: Ignored.
                }

                // at this point, we are good.
                // add to the dictionary.
                dictionary.Add(resourceKey, resource);
            }
        }
All Usage Examples Of BamlLocalization.ResourceTextReader::ReadRow