Exemplo n.º 1
0
        private void TryUpdateValue()
        {
            if (!IsValid || string.IsNullOrEmpty(Descriptor.Path))
            {
                return;
            }

            var cell = MarkupFactory.FindElementByDescriptor((IHtmlDocument)Document, Descriptor);

            if (cell == null)
            {
                Value = null;
                return;
            }

            if (Descriptor.ValueFormat == null)
            {
                Value = cell.InnerText;
                return;
            }

            object value;

            if (Descriptor.ValueFormat.TryConvert(cell.InnerText, out value))
            {
                Value = value.ToString();
            }
            else
            {
                Value = cell.InnerText;
            }
        }
Exemplo n.º 2
0
        protected override void OnDocumentChanged()
        {
            var cell = MarkupFactory.FindElementByDescriptor((IHtmlDocument)Document, Descriptor);

            MarkupBehavior.PathToSelectedElement = cell != null?cell.GetPath().ToString() : null;
        }
Exemplo n.º 3
0
        private void TryFetch()
        {
            if (myDocumentBrowser == null)
            {
                return;
            }

            myData.Clear();

            var descriptors = mySelectedSource.Figures
                              .Cast <IPathDescriptor>()
                              .Where(f => f.Figure == myFigureType.Name);

            foreach (var descriptor in descriptors)
            {
                try
                {
                    ILocatorMacroResolver resolver = new StockMacroResolver(Stock);
                    if (CustomResolverCreator != null)
                    {
                        resolver = CustomResolverCreator(resolver);
                    }
                    myDocumentBrowser.Navigate(DocumentType.Html, mySelectedSource.Location, resolver);

                    var htmlDocument = ( IHtmlDocument )myDocumentBrowser.Document;

                    // Mark the part of the document described by the FigureDescriptor to have a preview

                    var cell = ( HtmlElementAdapter )MarkupFactory.FindElementByDescriptor(htmlDocument, descriptor);
                    if (cell != null)
                    {
                        cell.Element.ScrollIntoView(false);
                    }

                    var marker = MarkupFactory.CreateMarker(descriptor);
                    marker.Mark(cell);

                    // already extract data here to check for format issues etc

                    var parser = DocumentProcessingFactory.CreateParser(htmlDocument, descriptor);
                    var table  = parser.ExtractTable();

                    var converter = DocumentProcessingFactory.CreateConverter(descriptor, mySelectedSource, CurrenciesLut.Currencies);
                    var series    = converter.Convert(table, Stock);
                    myData.AddRange(series);

                    // we found s.th. with this format
                    // -> skip alternative formats
                    break;
                }
                catch (Exception ex)
                {
                    ex.Data["Figure"]            = myFigureType.Name;
                    ex.Data["DataSource.Vendor"] = mySelectedSource.Vendor;
                    ex.Data["DataSource.Name"]   = mySelectedSource.Name;
                    ex.Data["Location"]          = mySelectedSource.Location.ToString();
                    ex.Data["FigureDescriptor"]  = descriptor.GetType().FullName;

                    if (ThrowOnError)
                    {
                        throw new Exception("Failed to extract data from datasource", ex);
                    }
                    else
                    {
                        myLogger.Error(ex, "Failed to fetch '{0}' from site {1}", myFigureType.Name, mySelectedSource.Name);
                    }
                }
            }
        }