public bool LoadDocument(string documentFilePath, SvgTestInfo testInfo, object extraInfo = null) { this.UnloadDocument(); if (textEditor == null || string.IsNullOrWhiteSpace(documentFilePath)) { return(false); } string fileExt = Path.GetExtension(documentFilePath); if (string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase)) { using (FileStream fileStream = File.OpenRead(documentFilePath)) { using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Decompress)) { // Text Editor does not work with this stream, so we read the data to memory stream... MemoryStream memoryStream = new MemoryStream(); // Use this method is used to read all bytes from a stream. int totalCount = 0; int bufferSize = 512; byte[] buffer = new byte[bufferSize]; while (true) { int bytesRead = zipStream.Read(buffer, 0, bufferSize); if (bytesRead == 0) { break; } memoryStream.Write(buffer, 0, bytesRead); totalCount += bytesRead; } if (totalCount > 0) { memoryStream.Position = 0; } textEditor.Load(memoryStream); memoryStream.Close(); } } } else { textEditor.Load(documentFilePath); } if (_foldingManager == null || _foldingStrategy == null) { _foldingManager = FoldingManager.Install(textEditor.TextArea); _foldingStrategy = new XmlFoldingStrategy(); } _foldingStrategy.UpdateFoldings(_foldingManager, textEditor.Document); return(true); }
public bool LoadDocument(string documentFilePath, SvgTestInfo testInfo, object extraInfo = null) { this.UnloadDocument(); if (string.IsNullOrWhiteSpace(documentFilePath) || testInfo == null) { return(false); } if (extraInfo != null) { _optionSettings = extraInfo as OptionSettings; } _svgFilePath = documentFilePath; bool isLoaded = false; string fileExt = Path.GetExtension(documentFilePath); if (string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase)) { using (FileStream fileStream = File.OpenRead(documentFilePath)) { using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Decompress)) { // Text Editor does not work with this stream, so we read the data to memory stream... MemoryStream memoryStream = new MemoryStream(); // Use this method is used to read all bytes from a stream. int totalCount = 0; int bufferSize = 512; byte[] buffer = new byte[bufferSize]; while (true) { int bytesRead = zipStream.Read(buffer, 0, bufferSize); if (bytesRead == 0) { break; } memoryStream.Write(buffer, 0, bytesRead); totalCount += bytesRead; } if (totalCount > 0) { memoryStream.Position = 0; } isLoaded = this.LoadFile(memoryStream, testInfo); memoryStream.Close(); } } } else { using (FileStream stream = File.OpenRead(documentFilePath)) { isLoaded = this.LoadFile(stream, testInfo); } } btnFilePath.IsEnabled = isLoaded; return(isLoaded); }
private bool LoadFile(Stream stream, SvgTestInfo testInfo) { testDetailsDoc.Blocks.Clear(); Regex rgx = new Regex("\\s+"); testTitle.Text = testInfo.Title; testDescrition.Text = rgx.Replace(testInfo.Description, " ").Trim(); testFilePath.Text = _svgFilePath; btnFilePath.IsEnabled = true; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = false; settings.IgnoreComments = true; settings.IgnoreProcessingInstructions = true; settings.DtdProcessing = DtdProcessing.Parse; SvgTestSuite selectedTestSuite = null; if (_optionSettings != null) { selectedTestSuite = _optionSettings.SelectedTestSuite; } if (selectedTestSuite == null) { selectedTestSuite = SvgTestSuite.GetDefault(SvgTestSuite.Create()); } int majorVersion = selectedTestSuite.MajorVersion; int minorVersion = selectedTestSuite.MinorVersion; using (XmlReader reader = XmlReader.Create(stream, settings)) { if (majorVersion == 1 && minorVersion == 1) { if (reader.ReadToFollowing("d:SVGTestCase")) { _testCase = new SvgTestCase(); while (reader.Read()) { string nodeName = reader.Name; XmlNodeType nodeType = reader.NodeType; if (nodeType == XmlNodeType.Element) { if (string.Equals(nodeName, "d:operatorScript", StringComparison.OrdinalIgnoreCase)) { string inputText = reader.ReadInnerXml(); string xamlText = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(inputText, false); Paragraph titlePara = new Paragraph(); titlePara.FontWeight = FontWeights.Bold; titlePara.FontSize = 16; titlePara.Inlines.Add(new Run("Operator Script")); if (_useConverter) { StringReader stringReader = new StringReader(xamlText); XmlReader xmlReader = XmlReader.Create(stringReader); Section nextSection = (Section)XamlReader.Load(xmlReader); if (nextSection != null) { testDetailsDoc.Blocks.Add(titlePara); testDetailsDoc.Blocks.Add(nextSection); } } else { Section pageSession = new Section(); Paragraph nextPara = new Paragraph(); //nextPara.Padding = new Thickness(3, 5, 3, 5); string paraText = rgx.Replace(inputText, " ").Trim(); nextPara.Inlines.Add(new Run(paraText)); pageSession.Blocks.Add(titlePara); pageSession.Blocks.Add(nextPara); testDetailsDoc.Blocks.Add(pageSession); } } else if (string.Equals(nodeName, "d:passCriteria", StringComparison.OrdinalIgnoreCase)) { string inputText = reader.ReadInnerXml(); string xamlText = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(inputText, false); Paragraph titlePara = new Paragraph(); titlePara.FontWeight = FontWeights.Bold; titlePara.FontSize = 16; titlePara.Inlines.Add(new Run("Pass Criteria")); if (_useConverter) { StringReader stringReader = new StringReader(xamlText); XmlReader xmlReader = XmlReader.Create(stringReader); Section nextSection = (Section)XamlReader.Load(xmlReader); if (nextSection != null) { testDetailsDoc.Blocks.Add(titlePara); testDetailsDoc.Blocks.Add(nextSection); } } else { Section pageSession = new Section(); Paragraph nextPara = new Paragraph(); //nextPara.Padding = new Thickness(3, 5, 3, 5); string paraText = rgx.Replace(inputText, " ").Trim(); nextPara.Inlines.Add(new Run(paraText)); pageSession.Blocks.Add(titlePara); pageSession.Blocks.Add(nextPara); testDetailsDoc.Blocks.Add(pageSession); } } else if (string.Equals(nodeName, "d:testDescription", StringComparison.OrdinalIgnoreCase)) { string inputText = reader.ReadInnerXml(); string xamlText = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(inputText, false); Paragraph titlePara = new Paragraph(); titlePara.FontWeight = FontWeights.Bold; titlePara.FontSize = 16; titlePara.Inlines.Add(new Run("Test Description")); if (_useConverter) { StringReader stringReader = new StringReader(xamlText); XmlReader xmlReader = XmlReader.Create(stringReader); Section nextSection = (Section)XamlReader.Load(xmlReader); if (nextSection != null) { testDetailsDoc.Blocks.Add(titlePara); testDetailsDoc.Blocks.Add(nextSection); } } else { Section pageSession = new Section(); Paragraph nextPara = new Paragraph(); //nextPara.Padding = new Thickness(3, 5, 3, 5); string paraText = rgx.Replace(inputText, " ").Trim(); nextPara.Inlines.Add(new Run(paraText)); pageSession.Blocks.Add(titlePara); pageSession.Blocks.Add(nextPara); testDetailsDoc.Blocks.Add(pageSession); } _testCase.Paragraphs.Add(inputText); } } else if (nodeType == XmlNodeType.EndElement && string.Equals(nodeName, "SVGTestCase", StringComparison.OrdinalIgnoreCase)) { break; } } } } else if (majorVersion == 1 && minorVersion == 2) { if (reader.ReadToFollowing("SVGTestCase")) { _testCase = new SvgTestCase(); while (reader.Read()) { string nodeName = reader.Name; XmlNodeType nodeType = reader.NodeType; if (nodeType == XmlNodeType.Element) { if (string.Equals(nodeName, "d:OperatorScript", StringComparison.OrdinalIgnoreCase)) { string inputText = reader.ReadInnerXml(); string xamlText = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(inputText, false); if (_useConverter) { StringReader stringReader = new StringReader(xamlText); XmlReader xmlReader = XmlReader.Create(stringReader); Section nextSection = (Section)XamlReader.Load(xmlReader); if (nextSection != null) { testDetailsDoc.Blocks.Add(nextSection); } } else { Section pageSession = new Section(); Paragraph nextPara = new Paragraph(); //nextPara.Padding = new Thickness(3, 5, 3, 5); string paraText = rgx.Replace(inputText, " ").Trim(); nextPara.Inlines.Add(new Run(paraText)); pageSession.Blocks.Add(nextPara); testDetailsDoc.Blocks.Add(pageSession); } } else if (string.Equals(nodeName, "d:PassCriteria", StringComparison.OrdinalIgnoreCase)) { string inputText = reader.ReadInnerXml(); string xamlText = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(inputText, false); if (_useConverter) { StringReader stringReader = new StringReader(xamlText); XmlReader xmlReader = XmlReader.Create(stringReader); Section nextSection = (Section)XamlReader.Load(xmlReader); if (nextSection != null) { testDetailsDoc.Blocks.Add(nextSection); } } else { Section pageSession = new Section(); Paragraph nextPara = new Paragraph(); //nextPara.Padding = new Thickness(3, 5, 3, 5); string paraText = rgx.Replace(inputText, " ").Trim(); nextPara.Inlines.Add(new Run(paraText)); pageSession.Blocks.Add(nextPara); testDetailsDoc.Blocks.Add(pageSession); } } else if (string.Equals(nodeName, "d:TestDescription", StringComparison.OrdinalIgnoreCase)) { string inputText = reader.ReadInnerXml(); string xamlText = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(inputText, false); if (_useConverter) { StringReader stringReader = new StringReader(xamlText); XmlReader xmlReader = XmlReader.Create(stringReader); Section nextSection = (Section)XamlReader.Load(xmlReader); if (nextSection != null) { testDetailsDoc.Blocks.Add(nextSection); } } else { Section pageSession = new Section(); Paragraph nextPara = new Paragraph(); //nextPara.Padding = new Thickness(3, 5, 3, 5); string paraText = rgx.Replace(inputText, " ").Trim(); nextPara.Inlines.Add(new Run(paraText)); pageSession.Blocks.Add(nextPara); testDetailsDoc.Blocks.Add(pageSession); } _testCase.Paragraphs.Add(inputText); } } else if (nodeType == XmlNodeType.EndElement && string.Equals(nodeName, "SVGTestCase", StringComparison.OrdinalIgnoreCase)) { break; } } } } else { if (reader.ReadToFollowing("SVGTestCase")) { _testCase = new SvgTestCase(); while (reader.Read()) { string nodeName = reader.Name; XmlNodeType nodeType = reader.NodeType; if (nodeType == XmlNodeType.Element) { if (string.Equals(nodeName, "OperatorScript", StringComparison.OrdinalIgnoreCase)) { string revisionText = reader.GetAttribute("version"); if (!string.IsNullOrWhiteSpace(revisionText)) { revisionText = revisionText.Replace("$", ""); _testCase.Revision = revisionText.Trim(); } string nameText = reader.GetAttribute("testname"); if (!string.IsNullOrWhiteSpace(nameText)) { _testCase.Name = nameText.Trim(); } } else if (string.Equals(nodeName, "Paragraph", StringComparison.OrdinalIgnoreCase)) { string inputText = reader.ReadInnerXml(); string xamlText = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(inputText, false); if (_useConverter) { StringReader stringReader = new StringReader(xamlText); XmlReader xmlReader = XmlReader.Create(stringReader); Section nextSection = (Section)XamlReader.Load(xmlReader); testDetailsDoc.Blocks.Add(nextSection); } else { Section pageSession = new Section(); Paragraph nextPara = new Paragraph(); //nextPara.Padding = new Thickness(3, 5, 3, 5); string paraText = rgx.Replace(inputText, " ").Trim(); nextPara.Inlines.Add(new Run(paraText)); pageSession.Blocks.Add(nextPara); testDetailsDoc.Blocks.Add(pageSession); } _testCase.Paragraphs.Add(inputText); } } else if (nodeType == XmlNodeType.EndElement && string.Equals(nodeName, "SVGTestCase", StringComparison.OrdinalIgnoreCase)) { break; } } } } } SubscribeToAllHyperlinks(); return(true); }
public bool LoadDocument(string pngFilePath, SvgTestInfo testInfo, object extraInfo) { this.UnloadDocument(); if (extraInfo == null) { return(false); } if (string.IsNullOrWhiteSpace(pngFilePath)) { return(false); } DrawingGroup drawing = extraInfo as DrawingGroup; if (drawing == null) { return(false); } BitmapSource bitmap = null; try { bitmap = new BitmapImage(new Uri(pngFilePath)); if (bitmap.DpiY < 96) // Some of the images were not created in right DPI { double dpi = 96; int width = bitmap.PixelWidth; int height = bitmap.PixelHeight; int stride = width * 4; // 4 bytes per pixel byte[] pixelData = new byte[stride * height]; bitmap.CopyPixels(pixelData, stride, 0); bitmap = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Bgra32, null, pixelData, stride); } pngResult.Source = bitmap; pngCanvas.Width = bitmap.Width + 10; pngCanvas.Height = bitmap.Height + 10; } catch (Exception ex) { Trace.TraceError(ex.ToString()); } try { Rect drawingBounds = drawing.Bounds; if (_viewBoxWidth == 0 || _viewBoxHeight == 0) { _viewBoxWidth = (int)svgDrawing.ActualHeight; _viewBoxHeight = (int)svgDrawing.ActualHeight; } svgDrawing.Width = double.NaN; svgDrawing.Height = double.NaN; svgDrawing.RenderSize = new Size(_viewBoxWidth, _viewBoxHeight); //svgDrawing.Source = new DrawingImage(drawing); svgDrawing.SetImage(drawing); if (bitmap != null && ((int)drawingBounds.Width < ImageWidth || (int)drawingBounds.Height < ImageHeight)) { svgDrawing.Width = bitmap.Width; svgDrawing.Height = bitmap.Height; } svgCanvas.Width = bitmap.Width + 10; svgCanvas.Height = bitmap.Height + 10; } catch (Exception ex) { svgDrawing.Source = null; Trace.TraceError(ex.ToString()); } return(true); }