protected void startImport_Click(object sender, EventArgs e) { PanelUpload.Visible = false; if (ProductFile.HasFile) { string tempFile = Server.MapPath(string.Format("~/App_Data/products{0}.csv", Guid.NewGuid())); try { int count = 0; ProductFile.PostedFile.SaveAs(tempFile); var lines = File.ReadLines(tempFile).Select(a => a.Split(';')); foreach (var product in lines) { // first row is just headers, we'll by pass it if (count > 0) { ImportProduct( int.Parse(product[0]), product[1], int.Parse(product[2]), int.Parse(product[3])); } count++; } ProductCount.Text = count.ToString(); PanelWoot.Visible = true; PanelFail.Visible = false; } catch (Exception ee) { failDetail.Text = ee.ToString(); PanelWoot.Visible = false; PanelFail.Visible = true; } finally { if (File.Exists(tempFile)) { File.Delete(tempFile); } } } }
public static File ParseCore(string filePath, CharacterPositionFinder finder, Encoding encoding) { var text = SystemFile.ReadAllText(filePath, encoding); var lastCharacter = text.Length - 1; var file = new File { Name = filePath, FooterSpan = CharacterSpan.None, // there is no footer LocationSpan = lastCharacter >= 0 ? new LocationSpan(finder.GetLineInfo(0), finder.GetLineInfo(lastCharacter)) : new LocationSpan(LineInfo.None, LineInfo.None), }; try { var parser = new GherkinParser(); var document = parser.Parse(filePath); var root = new Container { Type = nameof(GherkinDocument), Name = string.Empty, LocationSpan = file.LocationSpan, HeaderSpan = new CharacterSpan(0, 0), // there is no header FooterSpan = new CharacterSpan(Math.Max(0, lastCharacter), lastCharacter), // there is no footer }; file.Children.Add(root); var feature = document.Feature; if (feature != null) { // get locations ordered so that we know the position of the feature var locations = document.Comments.Select(_ => _.Location).ToList(); locations.Add(feature.Location); var sortedLocations = locations.OrderBy(_ => _.Line).ThenBy(_ => _.Column).ToList(); var positionAfterFeature = sortedLocations.IndexOf(feature.Location) + 1; var location = positionAfterFeature < sortedLocations.Count - 1 ? GetLineInfo(locations[positionAfterFeature + 1]) : file.LocationSpan.End; var parsedChild = ParseFeature(feature, finder, location); root.Children.Add(parsedChild); } } catch (Exception ex) { // try to adjust location span to include full file content // but ignore empty files as parsing errors var lines = SystemFile.ReadLines(filePath).Count(); if (lines == 0) { file.LocationSpan = new LocationSpan(LineInfo.None, LineInfo.None); } else { file.ParsingErrors.Add(new ParsingError { ErrorMessage = ex.Message, Location = LineInfo.None, }); file.LocationSpan = new LocationSpan(new LineInfo(1, 0), new LineInfo(lines + 1, 0)); } } return(file); }
public static File ParseCore(string filePath, CharacterPositionFinder finder, Encoding encoding) { var text = SystemFile.ReadAllText(filePath, encoding); var file = new File { Name = filePath, FooterSpan = new CharacterSpan(0, -1), // there is no footer }; try { var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); var document = MarkdownParser.Parse(text, pipeline); var textProvider = new TextProvider(text); var locationSpan = GetLocationSpan(document, finder); var rootBlock = new Container { Type = "markdown", Name = string.Empty, LocationSpan = locationSpan, HeaderSpan = new CharacterSpan(0, 0), // there is no header FooterSpan = new CharacterSpan(text.Length - 1, text.Length - 1), // there is no footer }; file.Children.Add(rootBlock); // Parse the file and display the text content of each of the elements. var blocks = document.Where(_ => !_.Span.IsEmpty).ToList(); foreach (var block in blocks) { var parsedBlock = ParseBlock(block, finder, textProvider); rootBlock.Children.Add(parsedBlock); } file.LocationSpan = locationSpan; } catch (Exception ex) { // try to adjust location span to include full file content // but ignore empty files as parsing errors var lines = SystemFile.ReadLines(filePath).Count(); if (lines == 0) { file.LocationSpan = new LocationSpan(new LineInfo(0, -1), new LineInfo(0, -1)); } else { file.ParsingErrors.Add(new ParsingError { ErrorMessage = ex.Message, Location = new LineInfo(0, -1), }); file.LocationSpan = new LocationSpan(new LineInfo(1, 0), new LineInfo(lines + 1, 0)); } } return(file); }
public static File ParseCore(string filePath, CharacterPositionFinder finder, IXmlFlavor flavor, Encoding encoding) { using (var reader = new XmlTextReader(new StreamReader(SystemFile.OpenRead(filePath), encoding))) { var file = new File { Name = filePath, FooterSpan = CharacterSpan.None, // there is no footer }; var fileBegin = new LineInfo(reader.LineNumber + 1, reader.LinePosition); try { var dummyRoot = new Container(); // Parse the XML and display the text content of each of the elements. // as there are XMLs that have the declaration on same line as the root element, we just loop and parse until the end while (!reader.EOF) { Parse(reader, dummyRoot, finder, flavor); } var root = dummyRoot.Children.OfType <Container>().Last(); var rootEnd = FixRoot(root, dummyRoot); var fileEnd = new LineInfo(reader.LineNumber, reader.LinePosition - 1); var positionAfterLastElement = new LineInfo(rootEnd.LineNumber, rootEnd.LinePosition + 1); // we calculate the next one (either a new line character or a regular one) file.LocationSpan = new LocationSpan(fileBegin, fileEnd); if (positionAfterLastElement < fileEnd) { file.FooterSpan = GetCharacterSpan(new LocationSpan(positionAfterLastElement, fileEnd), finder); } file.Children.Add(root); } catch (XmlException ex) { // try to adjust location span to include full file content // but ignore empty files as parsing errors var lines = SystemFile.ReadLines(filePath).Count(); if (lines == 0) { file.LocationSpan = new LocationSpan(LineInfo.None, LineInfo.None); } else { file.ParsingErrors.Add(new ParsingError { ErrorMessage = ex.Message, Location = new LineInfo(ex.LineNumber, ex.LinePosition), }); file.LocationSpan = new LocationSpan(new LineInfo(1, 0), new LineInfo(lines + 1, 0)); } } return(file); } }