Пример #1
0
        }         // proc OnStop

        private static PropertyDictionary ConvertProperties(IEnumerable <string> properties)
        {
            var configurationProperties = new PropertyDictionary();

            foreach (var c in properties)
            {
                var equalAt = c.IndexOf('=');
                if (equalAt == -1)
                {
                    configurationProperties.SetProperty(c.Trim(), typeof(bool), true);
                }
                else
                {
                    var name  = c.Substring(0, equalAt).Trim();
                    var value = c.Substring(equalAt + 1).Trim();
                    int t1;
                    if (Int32.TryParse(value, out t1))
                    {
                        configurationProperties.SetProperty(name, typeof(int), value);
                    }
                    else
                    {
                        configurationProperties.SetProperty(name, typeof(string), value);
                    }
                }
            }

            return(configurationProperties);
        }         // proc ConvertProperties
Пример #2
0
        }         // proc UpdateData

        private bool TryParseColumn(XElement cur, out SimpleDataColumn col)
        {
            var name = cur.GetAttribute("name", null);

            if (String.IsNullOrEmpty(name))
            {
                goto Error;
            }

            var type = LuaType.GetType(cur.GetAttribute("type", null), lateAllowed: false);

            var attributes = new PropertyDictionary();

            foreach (var xAttr in cur.Elements("attribute"))
            {
                if (TryParseAttribute(xAttr, out var attr))
                {
                    attributes.SetProperty(attr);
                }
            }

            col = new SimpleDataColumn(name, type, attributes);
            return(true);

Error:
            col = null;
            return(false);
        }         // func TryParseColumn
Пример #3
0
        }         // proc WriteAttributeString

        /// <summary>Convert the xml node to an property dictionary.</summary>
        /// <param name="attributes"></param>
        /// <param name="wellKnownProperties"></param>
        /// <returns></returns>
        public static IPropertyReadOnlyDictionary ToPropertyDictionary(this IEnumerable <XElement> attributes, params KeyValuePair <string, Type>[] wellKnownProperties)
        {
            var props = new PropertyDictionary();

            foreach (var x in attributes)
            {
                var propertyName = x.GetAttribute <string>("name", null);
                if (String.IsNullOrEmpty(propertyName))
                {
                    throw new ArgumentException("@name is missing.");
                }

                Type dataType;
                var  wellKnownPropertyIndex = Array.FindIndex(wellKnownProperties, c => String.Compare(c.Key, propertyName, StringComparison.OrdinalIgnoreCase) == 0);
                if (wellKnownPropertyIndex == -1)
                {
                    dataType = LuaType.GetType(x.GetAttribute("dataType", "string"));
                }
                else
                {
                    dataType = wellKnownProperties[wellKnownPropertyIndex].Value;
                }

                props.SetProperty(propertyName, dataType, x.Value);
            }
            return(props);
        }         // func IPropertyReadOnlyDictionary
Пример #4
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