Exemplo n.º 1
0
        public ILogFormat Create(Stream stream)
        {
            var savedPresets = Settings.Default.LexPresets ?? new XElement("Presets");

            var presets = new List<LexPreset>(
                from element in savedPresets.Elements("Preset")
                select new LexPreset {
                    Name = (string) element.Attribute("Name"),
                    CommonCode = (string) element.Attribute("CommonCode"),
                    SegmentCode = (string) element.Attribute("SegmentCode"),
                    RecordCode = (string) element.Attribute("RecordCode")
                });

            var selectView = new LexPresetsView();
            var viewModel = selectView.ViewModel;

            foreach (var preset in presets)
                viewModel.Presets.Add(preset);

            viewModel.SelectedPreset = viewModel.Presets.FirstOrDefault();

            viewModel.EditPreset = preset => {
                InvalidateCache(preset);
                ShowEditPresetDialog(selectView, stream, preset, presets.Select(x => x.Name));
            };

            viewModel.CreateNewPreset = () => {
                var preset = new LexPreset {
                    Name = "New Preset",
                    SegmentCode = "%%",
                    RecordCode = "%%"
                };

                return ShowEditPresetDialog(selectView, stream, preset, presets.Select(x => x.Name)) ? preset : null;
            };

            if (selectView.ShowDialog() != true)
                return null;

            Settings.Default.LexPresets =
                new XElement("Presets",
                    from preset in viewModel.Presets
                    select new XElement("Preset",
                        new XAttribute("Name", preset.Name),
                        new XAttribute("CommonCode", preset.CommonCode),
                        new XAttribute("SegmentCode", preset.SegmentCode),
                        new XAttribute("RecordCode", preset.RecordCode)));

            Settings.Default.Save();

            var selectedPreset = selectView.ViewModel.SelectedPreset;

            if (selectedPreset == null)
                return null;

            if (selectedPreset.Format != null)
                return selectedPreset.Format;

            return Compile(selectedPreset) ?? CompileManually(selectView, stream, selectedPreset);
        }
Exemplo n.º 2
0
        private static bool ShowEditPresetDialog(Window owner, Stream stream, LexPreset preset,
                                                 IEnumerable <string> names)
        {
            var view = new LexPresetView {
                Owner = owner
            };
            var viewModel = view.ViewModel;

            viewModel.Names            = names.ToArray();
            viewModel.LogStream        = stream;
            viewModel.Name             = preset.Name;
            viewModel.CommonCode.Text  = preset.CommonCode ?? string.Empty;
            viewModel.SegmentCode.Text = preset.SegmentCode ?? string.Empty;
            viewModel.RecordCode.Text  = preset.RecordCode ?? string.Empty;
            viewModel.IsChanged        = false;

            if (view.ShowDialog() != true)
            {
                return(false);
            }

            preset.Name        = viewModel.Name;
            preset.CommonCode  = viewModel.CommonCode.Text;
            preset.SegmentCode = viewModel.SegmentCode.Text;
            preset.RecordCode  = viewModel.RecordCode.Text;
            preset.Format      = viewModel.IsCompiled ? viewModel.Format : null;

            return(true);
        }
Exemplo n.º 3
0
        public LexPresetsViewModel()
        {
            this.Presets = new ObservableCollection <LexPreset>();

            this.SelectCommand = new RelayCommand(
                () => SelectionCompleted(),
                () => this.SelectedPreset != null);

            this.NewCommand = new RelayCommand(() => {
                var preset = this.CreateNewPreset();

                if (preset != null)
                {
                    this.Presets.Add(preset);
                    this.SelectedPreset = preset;
                }
            });

            this.EditCommand = new RelayCommand(
                () => this.EditPreset(this.selectedPreset),
                () => this.selectedPreset != null);

            this.DuplicateCommand = new RelayCommand(
                () => {
                var preset = new LexPreset {
                    Name        = this.selectedPreset.Name + " Copy",
                    CommonCode  = this.selectedPreset.CommonCode,
                    SegmentCode = this.selectedPreset.SegmentCode,
                    RecordCode  = this.selectedPreset.RecordCode
                };

                this.Presets.Add(preset);
                this.SelectedPreset = preset;
            },
                () => this.selectedPreset != null);

            this.DeleteCommand = new RelayCommand(
                () => {
                if (this.ConfirmDelete(this.selectedPreset))
                {
                    this.Presets.Remove(this.selectedPreset);
                }
            },
                () => this.selectedPreset != null);

            this.ExportCommand = new RelayCommand(
                () => Export(this.SelectedPreset),
                () => this.SelectedPreset != null);

            this.ImportCommand = new RelayCommand(this.Import);

            if (this.IsInDesignMode)
            {
                this.Presets.Add(new LexPreset {
                    Name = "New Preset"
                });
            }
        }
Exemplo n.º 4
0
        private static void InvalidateCache(LexPreset preset)
        {
            var directory      = GetCacheDirectory();
            var presetAssembly = GetCachedAssembly(preset, directory);

            if (presetAssembly.Exists)
            {
                presetAssembly.Delete();
            }
        }
Exemplo n.º 5
0
        public LexPresetsViewModel()
        {
            this.Presets = new ObservableCollection<LexPreset>();

            this.SelectCommand = new RelayCommand(
                () => SelectionCompleted(),
                () => this.SelectedPreset != null);

            this.NewCommand = new RelayCommand(() => {
                var preset = this.CreateNewPreset();

                if (preset != null) {
                    this.Presets.Add(preset);
                    this.SelectedPreset = preset;
                }
            });

            this.EditCommand = new RelayCommand(
                () => this.EditPreset(this.selectedPreset),
                () => this.selectedPreset != null);

            this.DuplicateCommand = new RelayCommand(
                () => {
                    var preset = new LexPreset {
                        Name = this.selectedPreset.Name + " Copy",
                        CommonCode = this.selectedPreset.CommonCode,
                        SegmentCode = this.selectedPreset.SegmentCode,
                        RecordCode = this.selectedPreset.RecordCode
                    };

                    this.Presets.Add(preset);
                    this.SelectedPreset = preset;
                },
                () => this.selectedPreset != null);

            this.DeleteCommand = new RelayCommand(
                () => {
                    if (this.ConfirmDelete(this.selectedPreset))
                        this.Presets.Remove(this.selectedPreset);
                },
                () => this.selectedPreset != null);

            this.ExportCommand = new RelayCommand(
                () => Export(this.SelectedPreset),
                () => this.SelectedPreset != null);

            this.ImportCommand = new RelayCommand(this.Import);

            if (this.IsInDesignMode)
                this.Presets.Add(new LexPreset {Name = "New Preset"});
        }
Exemplo n.º 6
0
        private static ILogFormat Compile(LexPreset preset)
        {
            var compiler = new LexCompiler();

            var directory = GetCacheDirectory();

            LexCompiler.LexFormatScanners result;

            var presetAssembly = GetCachedAssembly(preset, directory);

            if (directory.Exists)
            {
                if (presetAssembly.Exists)
                {
                    using (var fileStream = presetAssembly.OpenRead())
                        result = compiler.LoadCompiled(fileStream);

                    if (result.Success)
                    {
                        return new LexLogFormat {
                                   SegmentsScannerType = result.SegmentsScannerType,
                                   RecordsScannerType  = result.RecordsScannerType
                        }
                    }
                    ;
                }
            }
            else
            {
                directory.Create();
            }

            using (var fileStream = presetAssembly.Create())
                result = compiler.Compile(
                    string.Concat(preset.CommonCode, Environment.NewLine, preset.SegmentCode),
                    string.Concat(preset.CommonCode, Environment.NewLine, preset.RecordCode),
                    fileStream);

            if (result.Success)
            {
                return new LexLogFormat {
                           SegmentsScannerType = result.SegmentsScannerType,
                           RecordsScannerType  = result.RecordsScannerType
                }
            }
            ;

            return(null);
        }
Exemplo n.º 7
0
        private void Export(LexPreset preset)
        {
            var fileName = SelectFileForExport();

            if (fileName == null)
            {
                return;
            }

            var document = new XDocument(
                new XElement("Preset",
                             new XAttribute("Name", preset.Name),
                             new XElement("Common", preset.CommonCode),
                             new XElement("Segment", preset.SegmentCode),
                             new XElement("Record", preset.RecordCode)));

            document.Save(fileName);
        }
Exemplo n.º 8
0
        private static ILogFormat CompileManually(Window owner, Stream stream, LexPreset preset)
        {
            var view = new LexPresetView {
                Owner = owner
            };
            var viewModel = view.ViewModel;

            viewModel.LogStream        = stream;
            viewModel.Name             = preset.Name;
            viewModel.CommonCode.Text  = preset.CommonCode ?? string.Empty;
            viewModel.SegmentCode.Text = preset.SegmentCode ?? string.Empty;
            viewModel.RecordCode.Text  = preset.RecordCode ?? string.Empty;
            viewModel.IsChanged        = false;

            if (view.ShowDialog() != true)
            {
                return(null);
            }

            return(viewModel.Format);
        }
Exemplo n.º 9
0
        private static ILogFormat Compile(LexPreset preset)
        {
            var compiler = new LexCompiler();

            var directory = GetCacheDirectory();

            LexCompiler.LexFormatScanners result;

            var presetAssembly = GetCachedAssembly(preset, directory);

            if (directory.Exists) {
                if (presetAssembly.Exists) {
                    using (var fileStream = presetAssembly.OpenRead())
                        result = compiler.LoadCompiled(fileStream);

                    if (result.Success)
                        return new LexLogFormat {
                            SegmentsScannerType = result.SegmentsScannerType,
                            RecordsScannerType = result.RecordsScannerType
                        };
                }
            } else
                directory.Create();

            using (var fileStream = presetAssembly.Create())
                result = compiler.Compile(
                    string.Concat(preset.CommonCode, Environment.NewLine, preset.SegmentCode),
                    string.Concat(preset.CommonCode, Environment.NewLine, preset.RecordCode),
                    fileStream);

            if (result.Success)
                return new LexLogFormat {
                    SegmentsScannerType = result.SegmentsScannerType,
                    RecordsScannerType = result.RecordsScannerType
                };

            return null;
        }
Exemplo n.º 10
0
        private void Import()
        {
            var fileName = SelectFileForImport();

            if (fileName == null)
            {
                return;
            }

            try {
                var document = XDocument.Load(fileName).Root ?? new XElement("Preset");

                var preset = new LexPreset {
                    Name        = (string)document.Attribute("Name"),
                    CommonCode  = (string)document.Element("Common"),
                    SegmentCode = (string)document.Element("Segment"),
                    RecordCode  = (string)document.Element("Record")
                };

                this.Presets.Add(preset);
            } catch (XmlException exception) {
                throw new ApplicationException("Invalid file format", exception);
            }
        }
Exemplo n.º 11
0
        private static bool ShowEditPresetDialog(Window owner, Stream stream, LexPreset preset,
            IEnumerable<string> names)
        {
            var view = new LexPresetView {Owner = owner};
            var viewModel = view.ViewModel;

            viewModel.Names = names.ToArray();
            viewModel.LogStream = stream;
            viewModel.Name = preset.Name;
            viewModel.CommonCode.Text = preset.CommonCode ?? string.Empty;
            viewModel.SegmentCode.Text = preset.SegmentCode ?? string.Empty;
            viewModel.RecordCode.Text = preset.RecordCode ?? string.Empty;
            viewModel.IsChanged = false;

            if (view.ShowDialog() != true)
                return false;

            preset.Name = viewModel.Name;
            preset.CommonCode = viewModel.CommonCode.Text;
            preset.SegmentCode = viewModel.SegmentCode.Text;
            preset.RecordCode = viewModel.RecordCode.Text;
            preset.Format = viewModel.IsCompiled ? viewModel.Format : null;

            return true;
        }
Exemplo n.º 12
0
        private static void InvalidateCache(LexPreset preset)
        {
            var directory = GetCacheDirectory();
            var presetAssembly = GetCachedAssembly(preset, directory);

            if (presetAssembly.Exists)
                presetAssembly.Delete();
        }
Exemplo n.º 13
0
 private static FileInfo GetCachedAssembly(LexPreset preset, DirectoryInfo directory)
 {
     return new FileInfo(Path.Combine(directory.FullName, Uri.EscapeDataString(preset.Name) + ".lexpreset"));
 }
Exemplo n.º 14
0
 private static FileInfo GetCachedAssembly(LexPreset preset, DirectoryInfo directory)
 {
     return(new FileInfo(Path.Combine(directory.FullName, Uri.EscapeDataString(preset.Name) + ".lexpreset")));
 }
Exemplo n.º 15
0
        private void Import()
        {
            var fileName = SelectFileForImport();

            if (fileName == null)
                return;

            try {
                var document = XDocument.Load(fileName).Root ?? new XElement("Preset");

                var preset = new LexPreset {
                    Name = (string) document.Attribute("Name"),
                    CommonCode = (string) document.Element("Common"),
                    SegmentCode = (string) document.Element("Segment"),
                    RecordCode = (string) document.Element("Record")
                };

                this.Presets.Add(preset);
            } catch (XmlException exception) {
                throw new ApplicationException("Invalid file format", exception);
            }
        }
Exemplo n.º 16
0
        private void Export(LexPreset preset)
        {
            var fileName = SelectFileForExport();

            if (fileName == null)
                return;

            var document = new XDocument(
                new XElement("Preset",
                    new XAttribute("Name", preset.Name),
                    new XElement("Common", preset.CommonCode),
                    new XElement("Segment", preset.SegmentCode),
                    new XElement("Record", preset.RecordCode)));

            document.Save(fileName);
        }
Exemplo n.º 17
0
        private static ILogFormat CompileManually(Window owner, Stream stream, LexPreset preset)
        {
            var view = new LexPresetView {Owner = owner};
            var viewModel = view.ViewModel;

            viewModel.LogStream = stream;
            viewModel.Name = preset.Name;
            viewModel.CommonCode.Text = preset.CommonCode ?? string.Empty;
            viewModel.SegmentCode.Text = preset.SegmentCode ?? string.Empty;
            viewModel.RecordCode.Text = preset.RecordCode ?? string.Empty;
            viewModel.IsChanged = false;

            if (view.ShowDialog() != true)
                return null;

            return viewModel.Format;
        }
Exemplo n.º 18
0
        public ILogFormat Create(Stream stream)
        {
            var savedPresets = Settings.Default.LexPresets ?? new XElement("Presets");

            var presets = new List <LexPreset>(
                from element in savedPresets.Elements("Preset")
                select new LexPreset {
                Name        = (string)element.Attribute("Name"),
                CommonCode  = (string)element.Attribute("CommonCode"),
                SegmentCode = (string)element.Attribute("SegmentCode"),
                RecordCode  = (string)element.Attribute("RecordCode")
            });


            var selectView = new LexPresetsView();
            var viewModel  = selectView.ViewModel;

            foreach (var preset in presets)
            {
                viewModel.Presets.Add(preset);
            }

            viewModel.SelectedPreset = viewModel.Presets.FirstOrDefault();

            viewModel.EditPreset = preset => {
                InvalidateCache(preset);
                ShowEditPresetDialog(selectView, stream, preset, presets.Select(x => x.Name));
            };

            viewModel.CreateNewPreset = () => {
                var preset = new LexPreset {
                    Name        = "New Preset",
                    SegmentCode = "%%",
                    RecordCode  = "%%"
                };

                return(ShowEditPresetDialog(selectView, stream, preset, presets.Select(x => x.Name)) ? preset : null);
            };

            if (selectView.ShowDialog() != true)
            {
                return(null);
            }

            Settings.Default.LexPresets =
                new XElement("Presets",
                             from preset in viewModel.Presets
                             select new XElement("Preset",
                                                 new XAttribute("Name", preset.Name),
                                                 new XAttribute("CommonCode", preset.CommonCode),
                                                 new XAttribute("SegmentCode", preset.SegmentCode),
                                                 new XAttribute("RecordCode", preset.RecordCode)));

            Settings.Default.Save();

            var selectedPreset = selectView.ViewModel.SelectedPreset;

            if (selectedPreset == null)
            {
                return(null);
            }

            if (selectedPreset.Format != null)
            {
                return(selectedPreset.Format);
            }

            return(Compile(selectedPreset) ?? CompileManually(selectView, stream, selectedPreset));
        }