Ejemplo n.º 1
0
        private static bool HasVerticalColumns(HtmlNode table)
        {
#if NET20
            if (0 != IEnumerableExtensionMethods.Count(table.Descendants("thead")))
#else
            if (table.Descendants("thead").Any())
#endif
            {
                return(false);
            }

#if NET20
            var row = IEnumerableExtensionMethods.FirstOrDefault(table.Descendants("tr"));
#else
            var row = table.Descendants("tr").FirstOrDefault();
#endif
            if (null == row)
            {
                return(false);
            }

#if NET20
            return(0 != IEnumerableExtensionMethods.Count(row.Descendants("th")));
#else
            return(row.Descendants("th").Any());
#endif
        }
Ejemplo n.º 2
0
        public virtual void Save(Lexicon lexicon)
        {
            Trace.WriteIf(Tracing.Is.TraceVerbose, string.Empty);
            if (null == lexicon)
            {
                throw new ArgumentNullException("lexicon");
            }

            using (var writers = new StreamWriterDictionary("CANONICAL,SYNONYMS")
            {
                Access = FileAccess.Write,
                Mode = FileMode.Create,
                Share = FileShare.None
            })
            {
#if NET20
                if (0 == IEnumerableExtensionMethods.Count(lexicon))
#else
                if (!lexicon.Any())
#endif
                {
                    writers.Item(Location.FullName).WriteLine(string.Empty);
                    return;
                }

#if NET20
                var items = new SortedList <string, LexicalItem>();
                foreach (var item in lexicon)
                {
                    items.Add(item.CanonicalForm, item);
                }

                foreach (var item in items)
                {
                    var synonyms = new SortedList <string, string>();
                    foreach (var synonym in item.Value.Synonyms)
                    {
                        synonyms.Add(synonym, synonym);
                    }

                    writers
                    .Item(Location.FullName)
                    .WriteLine(StringExtensionMethods.FormatWith(
                                   "{0},{1}",
                                   CsvStringExtensionMethods.FormatCommaSeparatedValue(item.Value.CanonicalForm),
                                   CsvStringExtensionMethods.FormatCommaSeparatedValue(IEnumerableExtensionMethods.Concat(synonyms.Values, ';'))));
                }
#else
                foreach (var item in lexicon.OrderBy(x => x.CanonicalForm))
                {
                    writers
                    .Item(Location.FullName)
                    .WriteLine("{0},{1}".FormatWith(
                                   item.CanonicalForm.FormatCommaSeparatedValue(),
                                   item.Synonyms.OrderBy(x => x).Concat(';').FormatCommaSeparatedValue()));
                }
#endif
            }
        }
Ejemplo n.º 3
0
        public override IEnumerable <object[]> GetData(MethodInfo methodUnderTest,
                                                       Type[] parameterTypes)
        {
            if (null == methodUnderTest)
            {
                throw new ArgumentNullException("methodUnderTest");
            }

            if (null == parameterTypes)
            {
                throw new ArgumentNullException("parameterTypes");
            }

#if NET20
            if (IEnumerableExtensionMethods.Count(Locations) != parameterTypes.Length)
            {
                throw new InvalidOperationException(StringExtensionMethods.FormatWith(Resources.Attribute_CountsDiffer, IEnumerableExtensionMethods.Count(Locations), parameterTypes.Length));
            }
#else
            if (Locations.Count() != parameterTypes.Length)
            {
                throw new InvalidOperationException(Resources.Attribute_CountsDiffer.FormatWith(Locations.Count(), parameterTypes.Length));
            }
#endif

            var list  = new List <object>();
            var index = -1;
            foreach (var location in Locations)
            {
                index++;
                if (parameterTypes[index] == typeof(HtmlDocument) ||
                    parameterTypes[index] == typeof(IXPathNavigable))
                {
                    list.Add(Download(location));
                    continue;
                }

                if (parameterTypes[index] == typeof(XPathNavigator))
                {
                    list.Add(Download(location).CreateNavigator());
                    continue;
                }

                if (parameterTypes[index] == typeof(DataSet))
                {
#if NET20
                    list.Add(HtmlDocumentExtensionMethods.TabularData(Download(location)));
#else
                    list.Add(Download(location).TabularData());
#endif
                    continue;
                }

                throw new InvalidOperationException(Resources.HtmlAttribute_UnsupportedParameterType);
            }

            yield return(list.ToArray());
        }
Ejemplo n.º 4
0
        private static void AddVerticalDataRows(DataTable obj,
                                                HtmlNode body)
        {
#if NET20
            var rows  = IEnumerableExtensionMethods.ToList(body.Descendants("tr"));
            var cells = 0;
            foreach (var row in rows)
            {
                cells = Math.Max(cells, IEnumerableExtensionMethods.Count(row.Descendants("td")));
            }
#else
            var rows  = body.Descendants("tr").ToList();
            var cells = rows
                        .Select(row => row.Descendants("td").Count())
                        .Concat(new[] { 0 })
                        .Max();
#endif

            for (var i = 0; i < cells; i++)
            {
                obj.Rows.Add(obj.NewRow());
            }

            var column = 0;
            foreach (var item in rows)
            {
                var current = column;
                var row     = -1;
                foreach (var cell in item.Descendants("td"))
                {
                    row++;
                    for (var c = current; c < obj.Columns.Count; c++)
                    {
                        if (DBNull.Value == obj.Rows[row][current])
                        {
                            break;
                        }

                        row++;
                    }

                    var attribute = cell.Attributes["rowspan"];
                    var span      = null == attribute ? 1 : XmlConvert.ToInt32(attribute.Value);
                    if (1 == span)
                    {
                        obj.Rows[row][current] = cell;
                        continue;
                    }

                    for (var s = 0; s < span; s++)
                    {
                        obj.Rows[row][current + s] = cell;
                    }
                }

                column++;
            }
        }
Ejemplo n.º 5
0
        private static void FillDataTable(DataTable obj,
                                          HtmlNode table)
        {
#if NET20
            var body = 0 != IEnumerableExtensionMethods.Count(table.Descendants("thead"))
                           ? IEnumerableExtensionMethods.First(table.Descendants("tbody"))
                           : table;
            var rows = IEnumerableExtensionMethods.ToList(body.Descendants("tr"));
#else
            var body = table.Descendants("thead").Any()
                           ? table.Descendants("tbody").First()
                           : table;
            var rows = body.Descendants("tr").ToList();
#endif
            if (HasVerticalColumns(table))
            {
                AddVerticalDataColumns(obj, body);
                AddVerticalDataRows(obj, body);
                return;
            }

            var columns = Columns(table);
            for (var i = 0; i < columns.Count; i++)
            {
#if NET20 || NET35
                var name = string.IsNullOrEmpty(columns[i])
#else
                var name = string.IsNullOrWhiteSpace(columns[i])
#endif
                               ? XmlConvert.ToString(i)
                               : columns[i];
                obj.Columns.Add(name, typeof(HtmlNode));
            }

            AddNormalDataRows(obj, rows);
        }
Ejemplo n.º 6
0
        public override IEnumerable <object[]> GetData(MethodInfo methodUnderTest,
                                                       Type[] parameterTypes)
        {
            if (null == methodUnderTest)
            {
                throw new ArgumentNullException("methodUnderTest");
            }

            if (null == parameterTypes)
            {
                throw new ArgumentNullException("parameterTypes");
            }

            var list = new List <object>();

            if (1 == parameterTypes.Length && parameterTypes[0] == typeof(DataSet))
            {
                var data = new DataSet
                {
                    Locale = CultureInfo.InvariantCulture
                };
                foreach (var file in Files)
                {
                    data.Tables.Add(new CsvFile(file).ToDataTable());
                }

                list.Add(data);
            }
            else
            {
#if NET20
                if (IEnumerableExtensionMethods.Count(Files) != parameterTypes.Length)
                {
                    throw new InvalidOperationException(StringExtensionMethods.FormatWith(Resources.CsvFileAttribute_CountsDiffer, IEnumerableExtensionMethods.Count(Files), parameterTypes.Length));
                }
#else
                if (Files.Count() != parameterTypes.Length)
                {
                    throw new InvalidOperationException(Resources.CsvFileAttribute_CountsDiffer.FormatWith(Files.Count(), parameterTypes.Length));
                }
#endif

                var index = -1;
                foreach (var file in Files)
                {
                    index++;
                    if (parameterTypes[index] == typeof(CsvFile) ||
                        parameterTypes[index] == typeof(IEnumerable <KeyStringDictionary>))
                    {
                        list.Add(new CsvFile(file));
                        continue;
                    }

                    if (parameterTypes[index] == typeof(DataTable))
                    {
                        list.Add(new CsvFile(file).ToDataTable());
                        continue;
                    }

                    throw new InvalidOperationException(Resources.CsvFileAttribute_UnsupportedParameterType);
                }
            }

            yield return(list.ToArray());
        }