static string IconForType(ParserItem item) { switch (item.ItemType) { case ParserItemType.Module: return(s_ImgModule); case ParserItemType.Class: return(s_ImgClass); case ParserItemType.Function: return(s_ImgFunc); case ParserItemType.Attribute: case ParserItemType.Local: return(s_ImgAttr); default: return(String.Empty); } }
/// <summary> /// Parse data. /// </summary> private void ParseData() { ParserItem it = ActiveItem; if (it.value is System.Boolean) { ValueType.SelectedIndex = (int)DataType.TYPE_BOOL; ValueText.Text = ((System.Boolean)it.value) ? "true" : "false"; } else if (it.value is System.Int64 || it.value is System.Int32) { ValueType.SelectedIndex = (int)DataType.TYPE_INT; ValueText.Text = it.value + ""; } else if (it.value is System.Double) { ValueType.SelectedIndex = (int)DataType.TYPE_DOUBLE; ValueText.Text = (it.value + "").Replace(',', '.'); } else if (it.value == null) { ValueType.SelectedIndex = (int)DataType.TYPE_NULL; ValueText.Text = ""; } else { // Parse occurrences List <ParserError> errors = new List <ParserError>(); Occurences = Parser.findMarkUpOccurences((String)it.value, ActiveRequest.ParentScenario.customVariables, errors); if (Occurences.Count == 1 && errors.Count == 0 && Occurences[0].type == "text") { ValueType.SelectedIndex = (int)DataType.TYPE_STRING; ValueText.Text = (String)it.value; } else { RefreshOccurencies(); } } }
private void PrintArray(string key, ParserItem it, List <ParserItem> list, bool comma = false) { string commaStr = (comma) ? "," : ""; bool changed = false; // PRint item? if (DrawArray && DrawVisibleProperty) { DrawArrayProperty(key, it); DrawVisibleProperty = false; changed = true; } else if (key != null) { DrawText(string.Format("\"{0}\" : ", key) + "["); } else { DrawText("["); } X += 20; NextLine(); var lastItem = list[list.Count - 1]; foreach (var item in list) { DrawParserItem(null, item, item != lastItem); } X -= 20; DrawText("]" + commaStr); if (changed) { DrawVisibleProperty = true; } }
public static bool Parse(string data, out string errorMessage, out IList <Tag> tags) { errorMessage = null; tags = null; Match m = null; Group mg = null; int i = 0, l = 0, index = 0; var t = (Factory.TagOption)null; var line = string.Empty; var parserItem = (ParserItem)null; var parserItems = new List <ParserItem>(); var rawLines = DataBlock(data); for (i = 0, l = rawLines.Length; i < l; i++) { line = rawLines[i]; if (line.StartsWith(":")) { m = label.Match(line); if (m.Success) { t = Factory.Tag(m.Groups[1].Value, m.Groups[2].Value); if (null == t) { errorMessage = string.Format("Unknown field {0}{1}.", m.Groups[1].Value, m.Groups[2].Value); return(false); } else { parserItem = new ParserItem { TagOption = t, Tag = new Field(m.Groups[1].Value, m.Groups[2].Value, index) }; ++index; parserItems.Add(parserItem); line = m.Groups[3].Value; } } else { errorMessage = string.Format("Invalid field definition at line {0}.", i + 1); return(false); } } if (null == parserItem || parserItem.TagOption.MultiLine == parserItem.Lines) { errorMessage = string.Format("Expect field definition at the line {0}.", i + 1); return(false); } else { parserItem.Append(line); } } foreach (var item in parserItems) { var lineIndex = 0; NameMap map = null; var lines = item.Lines; var options = item.TagOption; var tag = item.Tag; TagValue grouping; TagValue previous; tag.Raw = item.Data; // item.TagOption.CounterPostfix remove this at some poit and support SubTag field which can combine multiple values againts one key if (options.Blob) { tag.Value = item.Data; continue; } else { grouping = null; previous = null; foreach (var r in options.Rows) { map = null; if (r.ValueNames != null && r.ValueNames[0].StartsWith("$")) { map = DecodeNames(r.ValueNames[0]); } i = 0; do { line = item[lineIndex]; for (int idx = 0, len = r.Regex.Length; idx < len; idx++) { m = r.Regex[idx].Match(line); if (m.Success) { if (map == null) { if (r.ValueNames == null || 0 == r.ValueNames.Length) { tag.Value = m.Value; } else { foreach (var id in r.ValueNames) { if ((mg = m.Groups[id]).Success) { tag.Add(id, mg.Value); } } } } else { if (map.ByIndex) { if (map.Grouping(idx)) { var groupingKey = map.GroupingKey(idx); if (map.Previous(idx)) { if (previous != null && previous.Id == groupingKey) { grouping = previous; } } else { if (grouping == null || grouping.Id != groupingKey) { grouping = tag.Add(groupingKey); } } } foreach (var id in map.GetOthers(idx)) { if ((mg = m.Groups[id]).Success) { if (null == grouping) { tag.Add(id, mg.Value); } else { grouping.Add(id, mg.Value); } } } previous = grouping; grouping = null; } else { item.Tag.Add( map.GetId(m), map.GetValue(m)); foreach (var id in map.GetOthers(m)) { if ((mg = m.Groups[id]).Success) { tag.Add(id, mg.Value); } } } } break; } } if (!m.Success && !r.Optional) { errorMessage = string.Format("Field {0} isn't optional.", item.Tag.Id); return(false); } if (m.Success) { ++lineIndex; ++i; } }while (i < r.Lines && lineIndex < lines && m.Success); if (lineIndex >= lines) // no more data if regex exists we need to skep them { break; } } } if (lineIndex < lines) { errorMessage = string.Format("Field {0} can't match data.", item.Tag.Id); return(false); } } tags = parserItems .Select(item => item.Tag) .Cast <Tag>() .ToList(); return(true); }
static CompletionData CreateCompletionData(ParserItem item, string triggerWord, string suffix) { var name = item.FullName.Substring(triggerWord.Length); return(new CompletionData(name, IconForType(item), item.Documentation, name + suffix)); }
static CompletionData CreateCompletionData(ParserItem item, string triggerWord) { return(CreateCompletionData(item, triggerWord, "")); }
public async Task Run() { try { List <IdPrice> prices = Repository.Parser.GetAllPricesPrev().ToList(); prices.Sort((p1, p2) => p1.ObjectID.CompareTo(p2.ObjectID)); if (status != ParserStatus.Ready) { return; } status = ParserStatus.Parsing; List <int> objects = Repository.EuroMade.GetAvailableObjects(); List <int> downloaded = Repository.Parser.GetDownloadedObjects(); List <int> objectsToCheck = new List <int>(); foreach (var item in objects) { if (!downloaded.Contains(item)) { objectsToCheck.Add(item); } } TotalObjectCount = objects.Count; int processedObject = downloaded.Count(); foreach (var id in objectsToCheck) { processedObject++; CurrentObject = processedObject; EuroItem euroItem = null; try { euroItem = Repository.EuroMade.GetEuroItem(id); } catch (Exception e) { } if (euroItem != null) { ParserItem parserItem = new ParserItem(); parserItem.EuroMadePrice = euroItem.EuroMadePrice; parserItem.Uri = euroItem.Uri; parserItem.UploadToMarket = euroItem.UploadToMarket; parserItem.Name = euroItem.Name; parserItem.ObjectID = euroItem.ObjectID; parserItem.AvailabilityEuroMade = euroItem.Available; parserItem.VendorCode = euroItem.VendorCode; decimal prevPrice = 0; try { prevPrice = prices.Where(e => e.ObjectID == parserItem.ObjectID).First().Price; } catch (InvalidOperationException) { } parserItem.OriginalPrice = prevPrice; parserItem.AvailabilityVerkk = false; parserItem.AvailabilityCount1 = 0; parserItem.AvailabilityCount2 = 0; parserItem.Discount = false; parserItem.DiscountValue = 0; parserItem.Height = 0; parserItem.Weight = 0; parserItem.Length = 0; parserItem.Width = 0; parserItem.EAN = "0"; parserItem.VerkkPrice = 0; parserItem.DiscountUntilDate = null; parserItem.DiscountUntilTime = null; var config = Configuration.Default.WithDefaultLoader(); IDocument document = null; try { document = await BrowsingContext.New(config).OpenAsync(euroItem.Uri); } catch (Exception) { } var cellSelector = ".product-basic-details .add-to-cart .vk-button"; var button = document.QuerySelector(cellSelector); if (button == null) { continue; } if (document.QuerySelector("#avail-0-content") == null) { try { Repository.Parser.Insert(parserItem); } catch (Exception e) { } continue; } else { parserItem.AvailabilityVerkk = true; } IElement parameters = null; try { parameters = document.QuerySelectorAll(".product-details__category")?[1]; } catch (ArgumentOutOfRangeException) { parameters = null; } string size = null; if (parameters != null) { try { size = parameters?.QuerySelectorAll(".product-details-row__value")?[0]?.TextContent; } catch (ArgumentOutOfRangeException) { size = null; } } string weight = null; if (parameters != null) { try { weight = parameters?.QuerySelectorAll(".product-details-row__value")?[1]?.TextContent; } catch (ArgumentOutOfRangeException) { weight = null; } } string ean = null; try { ean = document.QuerySelectorAll(".product-details__category")?[0] ?.QuerySelectorAll(".product-details-row__value")?[2] ?.TextContent; } catch (ArgumentOutOfRangeException) { ean = null; } if (ean == null) { parserItem.EAN = "0"; } else { parserItem.EAN = ean; } if (weight == null) { parserItem.Weight = 0; } else { if (double.TryParse(weight.Replace("kg", string.Empty).Trim(), out double weightValue)) { parserItem.Weight = weightValue; } else { parserItem.Weight = 0; } } if (size == null) { parserItem.Height = 0; parserItem.Width = 0; parserItem.Length = 0; } else { List <string> sizes = size .Replace("cm", string.Empty) .Split('x') .ToList(); if (sizes.Count == 3) { if (int.TryParse(sizes[0].Trim(), out int width)) { parserItem.Width = width; } else { parserItem.Width = 0; } if (int.TryParse(sizes[1].Trim(), out int height)) { parserItem.Height = height; } else { parserItem.Height = 0; } if (int.TryParse(sizes[2].Trim(), out int length)) { parserItem.Length = Convert.ToInt32(sizes[2].Trim()); } else { parserItem.Length = 0; } } else { parserItem.Height = 0; parserItem.Width = 0; parserItem.Length = 0; } } var discountValue = document.QuerySelector(".price-tag-discount__amount"); if (discountValue != null) { parserItem.Discount = true; string discountString = discountValue.TextContent .Replace("€", string.Empty) .Replace(",", "."); if (decimal.TryParse(discountString, NumberStyles.AllowParentheses | NumberStyles.Float, CultureInfo.InvariantCulture, out decimal discountOutValue)) { parserItem.DiscountValue = Math.Abs(discountOutValue); } else { parserItem.DiscountValue = 0; } } else { parserItem.Discount = false; parserItem.DiscountValue = 0; } var price = document.QuerySelector(".price-tag-content__price-tag-price--current .price-tag-price__euros") ?.GetAttribute("content"); if (price != null) { if (decimal.TryParse(price, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out decimal priceVerkk)) { parserItem.VerkkPrice = priceVerkk; } else { parserItem.VerkkPrice = 0; } } else { parserItem.VerkkPrice = 0; } var available1 = document.QuerySelector(".stock-indicator__link") ?.TextContent ?.Replace("yli", string.Empty) ?.Trim(); var available2 = document.QuerySelector(".product-info-row__location") ?.TextContent ?.Replace("yli", string.Empty) ?.Trim(); IHtmlCollection <IElement> available3row = null; string available3 = null; try { available3row = document.QuerySelectorAll(".product-info-row"); foreach (var item in available3row) { var name = item.QuerySelector(".product-info-row__name")?.TextContent ?? string.Empty; if (name == "Vantaan varasto") { available3 = document.QuerySelector(".product-info-row__location") ?.TextContent ?.Replace("yli", string.Empty) ?.Trim(); } } } catch (ArgumentOutOfRangeException) { available3row = null; } if (available1 == null) { parserItem.AvailabilityCount1 = 0; } else { if (int.TryParse(available1, out int available1value)) { parserItem.AvailabilityCount1 = available1value; } else { parserItem.AvailabilityCount1 = 0; } } if (available2 == null) { parserItem.AvailabilityCount2 = 0; } else { if (int.TryParse(available2, out int available2value)) { parserItem.AvailabilityCount2 = available2value; } else { parserItem.AvailabilityCount2 = 0; } } if (available3 == null) { parserItem.AvailabilityCount3 = 0; } else { if (int.TryParse(available3, out int available3value)) { parserItem.AvailabilityCount3 = available3value; } else { parserItem.AvailabilityCount3 = 0; } } var discountDate = document.QuerySelector(".discount-info-details em")?.TextContent; if (discountDate != null) { Regex dateReg = new Regex(@"(\d+\.\d+\.\d+)"); Regex timeReg = new Regex(@"( \d+\.\d+ )"); Match dateMatch = dateReg.Match(discountDate); if (dateMatch.Success) { try { parserItem.DiscountUntilDate = DateTime.ParseExact(dateMatch.Value, @"d.M.yyyy", CultureInfo.InvariantCulture); } catch (FormatException e) { } } Match timeMatch = timeReg.Match(discountDate); if (timeMatch.Success) { try { DateTime dt = new DateTime(); dt = DateTime.ParseExact(timeMatch.Value.Trim(), @"H.m", CultureInfo.InvariantCulture); parserItem.DiscountUntilTime = new TimeSpan(0, dt.Hour, dt.Minute, dt.Second, 0); } catch (FormatException e) { } } } try { Repository.Parser.Insert(parserItem); } catch (Exception e) { } } } List <IdPrice> pricesParser = Repository.Parser.GetAllPricesParser().ToList(); prices.Sort((p1, p2) => p1.ObjectID.CompareTo(p2.ObjectID)); Repository.Parser.TruncatePrices(); Repository.Parser.SavePrices(pricesParser); status = ParserStatus.Done; } catch (Exception e) { } }
/// <summary> /// Parse data. /// </summary> private void ParseData() { ParserItem it = ActiveItem; Value.Active = true; if (it.value is System.Boolean) { ValueType.SelectedItem = DataType.TYPE_BOOL; ValueText.Text = ((System.Boolean)it.value) ? "true" : "false"; } else if (it.value is System.Int64 || it.value is System.Int32) { ValueType.SelectedItem = DataType.TYPE_INT; ValueText.Text = it.value + ""; } else if (it.value is System.Double) { ValueType.SelectedItem = DataType.TYPE_DOUBLE; ValueText.Text = (it.value + "").Replace(',', '.'); } else if (it.value == null) { ValueType.SelectedItem = DataType.TYPE_NULL; ValueText.Text = ""; } else { String text = (String)it.value; String[] tokens = text.Split('#'); try { if (tokens.Length == 6) { // Set format active Format.Active = true; // value var value = tokens [3]; ConditionValue.Text = value; // Var name var variable = tokens [4]; SaveToVariable.Text = variable; // Type var type = tokens[1]; FormatTypeSelect.SelectedItem = type; if (FormatTypeSelect.SelectedIndex < 0) { throw new Exception("Invalid method"); } // Method var method = tokens [2]; if (method == "ip_") { FormatOperationsSelect.SelectedItem = "ip_"; } else { // Simple method? UseVariable.State = (method.IndexOf("uv", 0) >= 0) ? CheckBoxState.On : CheckBoxState.Off; EvalIfPresent.State = (method.IndexOf("ip", 0) >= 0) ? CheckBoxState.On : CheckBoxState.Off; // Method name FormatOperationsSelect.SelectedItem = method.Replace("uv_", "").Replace("ip_", ""); if (FormatOperationsSelect.SelectedIndex < 0) { throw new Exception("Invalid operation"); } } } else { throw new Exception("Cannot evaluate"); } } catch { Value.Active = true; } ValueType.SelectedItem = DataType.TYPE_STRING; ValueText.Text = text; } // Visible items GroupChanged_Event(null, null); }
/// <summary> /// Parse data. /// </summary> void ParseData() { List <ParserItem> items = (List <ParserItem>)ActiveItem.value; if (items.Count == 0) { return; } // First is parser item! ParserItem it = items [0]; try { String text = (String)it.value; String[] tokens = text.Split('#'); if (tokens.Length == 6) { // value var value = tokens [3]; ConditionValue.Text = value; // Var name var variable = tokens [4]; SaveToVariable.Text = variable; // Method var method = tokens [2]; // Only if present test? if (method == "ip_") { FormatOperationsSelect.SelectedItem = "ip_"; } else { // Simple method? UseVariable.State = (method.IndexOf("uv", 0) >= 0) ? CheckBoxState.On : CheckBoxState.Off; // If preset exists? EvalIfPresent.State = (method.IndexOf("ip", 0) >= 0) ? CheckBoxState.On : CheckBoxState.Off; // Method name FormatOperationsSelect.SelectedItem = method.Replace("uv_", "").Replace("ip_", ""); if (FormatOperationsSelect.SelectedIndex < 0) { throw new Exception("Invalid operation"); } } // Type var type = tokens[1]; if (type == "array") { ItemCount.Active = true; } else { AllItemsTemplate.Active = true; FormatTypeSelect.SelectedItem = type; if (FormatTypeSelect.SelectedIndex < 0) { throw new Exception("Invalid method"); } } } else { throw new Exception("Cannot evaluate"); } } catch { // Nothing what so ever! ItemCount.Active = true; } }
void DrawProperty(string key, string value, ParserItem it, bool comma = true) { DrawColorProperty(key, value, Colors.DarkBlue, it, comma); }
/// <summary> /// Create clickable item on target destination! /// </summary> private void CreateClickableItem(double corX, double corY, double corWidth, double corHeight, ParserItem item) { if (hscroll != null && vscroll != null) { corX -= hscroll.Value; corY -= vscroll.Value; } // Not clickable (out of range) if (corX < 0 || corY < 0) { return; } // Create Item CanvasItems.Add( new ClickableResponseItem() { X = corX, Y = corY, Height = corY + corHeight, Width = corX + corWidth, Item = item } ); }