示例#1
0
        private void OnWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            ConsoleWorker worker = (ConsoleWorker)sender;

            ConverterOptions options = this.Options;

            _wpfSettings.IncludeRuntime = options.IncludeRuntime;
            _wpfSettings.TextAsGeometry = options.TextAsGeometry;

            _fileReader.UseFrameXamlWriter = !options.UseCustomXamlWriter;

            if (options.GeneralWpf)
            {
                _fileReader.SaveXaml = options.SaveXaml;
                _fileReader.SaveZaml = options.SaveZaml;
            }
            else
            {
                _fileReader.SaveXaml = false;
                _fileReader.SaveZaml = false;
            }

            _drawing = _fileReader.Read(_sourceFile, _outputInfoDir);

            if (_drawing == null)
            {
                e.Result = "Failed";
                return;
            }

            if (options.GenerateImage)
            {
                _fileReader.SaveImage(_sourceFile, _outputInfoDir,
                                      options.EncoderType);

                _imageFile = _fileReader.ImageFile;
            }
            _xamlFile = _fileReader.XamlFile;
            _zamlFile = _fileReader.ZamlFile;

            if (_drawing.CanFreeze)
            {
                _drawing.Freeze();
            }

            e.Result = "Successful";
        }
示例#2
0
        private void ConvertFiles(DoWorkEventArgs e, DirectoryInfo target)
        {
            _fileReader.FallbackOnWriterError = _fallbackOnWriterError;

            if (e.Cancel)
            {
                return;
            }

            if (_worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            DirectoryInfo outputDir = target;

            foreach (string svgFileName in _sourceFiles)
            {
                if (_worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                string fileExt = Path.GetExtension(svgFileName);
                if (String.Equals(fileExt, ".svg", StringComparison.OrdinalIgnoreCase) ||
                    String.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        if (_worker.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }

                        if (target == null)
                        {
                            outputDir = new DirectoryInfo(
                                Path.GetDirectoryName(svgFileName));
                        }

                        DrawingGroup drawing = _fileReader.Read(svgFileName,
                                                                outputDir);

                        if (drawing == null)
                        {
                            if (_continueOnError)
                            {
                                throw new InvalidOperationException(
                                          "The conversion failed due to unknown error.");
                            }
                        }

                        if (drawing != null && _options.GenerateImage)
                        {
                            _fileReader.SaveImage(svgFileName, target,
                                                  _options.EncoderType);
                        }

                        if (drawing != null)
                        {
                            _convertedCount++;
                        }

                        if (_fileReader.WriterErrorOccurred)
                        {
                            _writerErrorOccurred = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        _errorFiles.Add(svgFileName);

                        if (_continueOnError)
                        {
                            StringBuilder builder = new StringBuilder();
                            builder.AppendLine("Error converting: " + svgFileName);
                            builder.AppendFormat("Error: Exception ({0})", ex.GetType());
                            builder.AppendLine();
                            builder.AppendLine(ex.Message);
                            builder.AppendLine(ex.ToString());

                            _worker.ReportProgress(0, builder.ToString());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
        }
示例#3
0
        private void ConvertFiles(DoWorkEventArgs e, DirectoryInfo source,
                                  DirectoryInfo target)
        {
            _fileReader.FallbackOnWriterError = _fallbackOnWriterError;

            if (e.Cancel)
            {
                return;
            }

            if (_worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            ConverterOptions options = this.Options;

            IEnumerable <string> fileIterator = DirectoryUtils.FindFiles(
                source, "*.*", SearchOption.TopDirectoryOnly);

            foreach (string svgFileName in fileIterator)
            {
                if (_worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                string fileExt = Path.GetExtension(svgFileName);
                if (string.Equals(fileExt, ".svg", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        FileAttributes fileAttr = File.GetAttributes(svgFileName);
                        if (!_includeHidden)
                        {
                            if ((fileAttr & FileAttributes.Hidden) == FileAttributes.Hidden)
                            {
                                continue;
                            }
                        }

                        FileSecurity security = null;

                        if (_includeSecurity)
                        {
                            security = File.GetAccessControl(svgFileName);
                        }

                        if (_worker.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }

                        DrawingGroup drawing = _fileReader.Read(svgFileName,
                                                                target);

                        if (drawing == null)
                        {
                            if (_continueOnError)
                            {
                                throw new InvalidOperationException(
                                          "The conversion failed due to unknown error.");
                            }
                        }

                        if (options.SaveXaml)
                        {
                            string xamlFile = _fileReader.XamlFile;
                            if (!string.IsNullOrWhiteSpace(xamlFile) &&
                                File.Exists(xamlFile))
                            {
                                File.SetAttributes(xamlFile, fileAttr);

                                // if required to set the security or access control
                                if (_includeSecurity)
                                {
                                    File.SetAccessControl(xamlFile, security);
                                }
                            }
                        }
                        if (options.SaveZaml)
                        {
                            string zamlFile = _fileReader.ZamlFile;
                            if (!string.IsNullOrWhiteSpace(zamlFile) &&
                                File.Exists(zamlFile))
                            {
                                File.SetAttributes(zamlFile, fileAttr);

                                // if required to set the security or access control
                                if (_includeSecurity)
                                {
                                    File.SetAccessControl(zamlFile, security);
                                }
                            }
                        }

                        if (drawing != null && options.GenerateImage)
                        {
                            _fileReader.SaveImage(svgFileName, target,
                                                  options.EncoderType);
                            string imageFile = _fileReader.ImageFile;
                            if (!string.IsNullOrWhiteSpace(imageFile) &&
                                File.Exists(imageFile))
                            {
                                File.SetAttributes(imageFile, fileAttr);

                                // if required to set the security or access control
                                if (_includeSecurity)
                                {
                                    File.SetAccessControl(imageFile, security);
                                }
                            }
                        }

                        if (drawing != null)
                        {
                            _convertedCount++;
                        }

                        if (_fileReader.WriterErrorOccurred)
                        {
                            _writerErrorOccurred = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        _errorFiles.Add(svgFileName);

                        if (_continueOnError)
                        {
                            StringBuilder builder = new StringBuilder();
                            builder.AppendLine("Error converting: " + svgFileName);
                            builder.AppendFormat("Error: Exception ({0})", ex.GetType());
                            builder.AppendLine();
                            builder.AppendLine(ex.Message);
                            builder.AppendLine(ex.ToString());

                            _worker.ReportProgress(0, builder.ToString());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
        }