Exemplo n.º 1
0
 void IEnumerator.Reset()
 {
     xml?.Dispose();
     xml        = null;
     columns    = null;
     currentRow = null;
     state      = ReadingState.Unread;
 }                 // proc Reset
Exemplo n.º 2
0
                }                 // proc Dispose

                #endregion

                #region -- IEnumerator ------------------------------------------------------------

                private bool MoveNext(bool headerOnly)
                {
                    switch (state)
                    {
                        #region -- ReadingState.Unread --
                    case ReadingState.Unread:
                        // open the xml stream
                        xml = owner.request.GetXmlStreamAsync(owner.path, owner.acceptedMimeType).Result;

                        xml.Read();
                        if (xml.NodeType == XmlNodeType.XmlDeclaration)
                        {
                            xml.Read();
                        }

                        if (xml.NodeType != XmlNodeType.Element || xml.LocalName != xnView.LocalName)
                        {
                            throw new InvalidDataException($"Expected \"{xnView}\", read \"{xml.LocalName}\".");
                        }

                        xml.Read();
                        if (xml.NodeType != XmlNodeType.Element || xml.LocalName != xnFields.LocalName)
                        {
                            throw new InvalidDataException($"Expected \"{xnFields}\", read \"{xml.LocalName}\".");
                        }

                        var viewColumns = new List <ViewDataColumn>();
                        var fields      = (XElement)XNode.ReadFrom(xml);
                        foreach (var field in fields.Elements())
                        {
                            var columnName     = field.Name.LocalName;
                            var columnDataType = LuaType.GetType(field.GetAttribute("type", "string"), lateAllowed: false).Type;
                            var columnId       = field.GetAttribute("field", String.Empty);

                            var attributes = new PropertyDictionary();

                            // add colum id
                            if (!String.IsNullOrEmpty(columnId))
                            {
                                attributes.SetProperty("field", typeof(string), columnId);
                            }

                            foreach (var c in field.Elements("attribute"))
                            {
                                if (c.IsEmpty)
                                {
                                    continue;
                                }

                                var attributeName = c.GetAttribute("name", String.Empty);
                                if (String.IsNullOrEmpty(attributeName))
                                {
                                    continue;
                                }

                                attributes.SetProperty(attributeName, LuaType.GetType(c.GetAttribute("type", "string"), lateAllowed: false).Type, c.Value);
                            }                                     // foreach c

                            viewColumns.Add(new ViewDataColumn(columnName, columnDataType, attributes));
                        }                                 // foreach field

                        if (viewColumns.Count < 1)
                        {
                            throw new InvalidDataException("No header found.");
                        }
                        columns = viewColumns.ToArray();

                        state = ReadingState.FetchFirstRow;
                        if (headerOnly)
                        {
                            return(true);
                        }
                        else
                        {
                            goto case ReadingState.FetchFirstRow;
                        }

                        #endregion
                        #region -- ReadingState.FetchFirstRow --
                    case ReadingState.FetchFirstRow:
                        if (xml.NodeType != XmlNodeType.Element || xml.LocalName != xnRows.LocalName)
                        {
                            throw new InvalidDataException($"Expected \"{xnRows}\", read \"{xml.LocalName}\".");
                        }

                        if (xml.IsEmptyElement)
                        {
                            xml.Read();
                            if (xml.NodeType != XmlNodeType.EndElement || xml.LocalName != xnView.LocalName)
                            {
                                throw new InvalidDataException($"Expected \"{xnView}\", read \"{xml.LocalName}\".");
                            }

                            xml.Read();
                            if (!xml.EOF)
                            {
                                throw new InvalidDataException("Unexpected eof.");
                            }

                            state = ReadingState.Complete;
                            goto case ReadingState.Complete;
                        }                                 // if xml.IsEmptyElement
                        else
                        {
                            xml.Read();
                            state = ReadingState.FetchRows;
                            goto case ReadingState.FetchRows;
                        }

                        #endregion
                        #region -- ReadingState.FetchRows --
                    case ReadingState.FetchRows:
                        if (xml.NodeType != XmlNodeType.Element || xml.LocalName != xnRow.LocalName)
                        {
                            throw new InvalidDataException($"Expected \"r\", read \"{xml.LocalName}\".");
                        }

                        var values = new object[columns.Length];

                        if (!xml.IsEmptyElement)
                        {
                            var rowData = (XElement)XNode.ReadFrom(xml);
                            foreach (var column in rowData.Elements())
                            {
                                var columnIndex = Array.FindIndex(columns, c => String.Compare(c.Name, column.Name.LocalName, StringComparison.OrdinalIgnoreCase) == 0);
                                if (columnIndex != -1)
                                {
                                    values[columnIndex] = Procs.ChangeType(column.Value, columns[columnIndex].DataType);
                                }
                            }
                        }                                 // if xml.IsEmptyElement
                        else
                        {
                            // Without a call to XNode.ReadFrom() it's necessary to read to the next node.
                            xml.Read();
                        }

                        currentRow = new ViewDataRow(this, values);

                        if (xml.NodeType == XmlNodeType.Element && xml.LocalName == xnRow.LocalName)
                        {
                            return(true);
                        }

                        if (xml.NodeType != XmlNodeType.EndElement || xml.LocalName != xnRows.LocalName)
                        {
                            throw new InvalidDataException($"Expected \"{xnRows}\", read \"{xml.LocalName}\".");
                        }

                        xml.Read();
                        if (xml.NodeType != XmlNodeType.EndElement || xml.LocalName != xnView.LocalName)
                        {
                            throw new InvalidDataException($"Expected \"{xnView}\", read \"{xml.LocalName}\".");
                        }

                        xml.Read();
                        if (!xml.EOF)
                        {
                            throw new InvalidDataException("Unexpected eof.");
                        }

                        state = ReadingState.Complete;
                        return(true);

                        #endregion
                    case ReadingState.Complete:
                        return(false);

                    default:
                        throw new InvalidOperationException("The state of the object is invalid.");
                    }             // switch state
                }                 // func MoveNext