public IEnumerable <Trade> GetTrades()
        {
            var fhde        = new FileHelpers.DelimitedFileEngine <Trade>();
            var tradeStream = new StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ApplicationDeveloper.Data.Repositories.Trades.csv"));
            var trades      = fhde.ReadStream(tradeStream);

            return(trades);
        }
Esempio n. 2
0
        /// <summary>
        /// Open and start monitoring glossary files
        /// </summary>
        /// <param name="projFullPathFileName">Project full-path file name</param>
        /// <param name="projEntries">Project Mapping entries list</param>
        /// <param name="monitor">MappingMonitor for this project's base path</param>
        /// <param name="engineFolderName">Corresponding translator/translation engine folder name</param>
        /// <param name="srcLang">Supported source language</param>
        /// <param name="tgtLang">Supported target language</param>
        /// <returns>true for success</returns>
        public static async Task <bool> OpenAndMonitorGlossaryFiles(string projFullPathFileName, IList <MappingEntry> projEntries,
                                                                    MappingMonitor monitor,
                                                                    string engineFolderName         = "Excite",
                                                                    SupportedSourceLanguage srcLang = SupportedSourceLanguage.Japanese,
                                                                    SupportedTargetLanguage tgtLang = SupportedTargetLanguage.ChineseTraditional)
        {
            if (string.IsNullOrWhiteSpace(projFullPathFileName) || monitor == null)
            {
                return(false);
            }

            var baseProjectPath = Path.GetDirectoryName(projFullPathFileName);

            if (string.IsNullOrWhiteSpace(baseProjectPath) ||
                Directory.Exists(baseProjectPath) == false ||
                monitor.BaseProjectPath.StartsWith(baseProjectPath) == false)
            {
                return(false);
            }

            var glossaryPath = monitor.GlossaryPath;

            if (Directory.Exists(glossaryPath) == false ||
                string.IsNullOrWhiteSpace(engineFolderName) ||
                srcLang == SupportedSourceLanguage.AutoDetect)
            {
                return(false);
            }

            monitor.Stop();
            foreach (var fn in monitor.MonitoringFileList.ToList())
            {
                if (fn == projFullPathFileName)
                {
                    continue;
                }

                monitor.RemoveMonitoring(fn);
            }
            await Task.Delay(50);

            // collect mapping files
            var listAllEngine = Directory.GetFiles(glossaryPath, "*.*", SearchOption.TopDirectoryOnly).NumericSort();
            var enginePath    = Path.Combine(glossaryPath, engineFolderName);
            var langPath      = Path.Combine(enginePath, $"{srcLang.ToString()}2{tgtLang.ToString()}");          // like "Excite/Japanese2ChineseTraditional"

            IEnumerable <string> listAllLang = null, listLang = null;

            if (Directory.Exists(enginePath))
            {
                listAllLang = Directory.GetFiles(enginePath, "*.*", SearchOption.TopDirectoryOnly).NumericSort();
            }
            if (Directory.Exists(langPath))
            {
                listLang = Directory.GetFiles(langPath, "*.*", SearchOption.TopDirectoryOnly).NumericSort();
            }

            if (listLang == null)
            {
                listLang = new string[] { }
            }
            ;

            // add file names to list by descended priority
            var listFiles = new List <string>(listLang);

            if (listAllLang != null)
            {
                listFiles.AddRange(listAllLang);
            }
            if (listAllEngine != null)
            {
                listFiles.AddRange(listAllEngine);
            }

            // the listFiles is sorted by the sequence:
            //     <glossaryPath>/<engineFolder>/<langFolder>/*.* => <glossaryPath>/<engineFolder>/*.* => <glossaryPath>/*.*,
            // the later is high priority for overwriting previous terms!!

            // FileHelpers not support SmartDetect in .Net Standard 2.0
            var engine = new FileHelpers.DelimitedFileEngine <MappingEntry>(System.Text.Encoding.UTF8);

            foreach (var field in engine.Options.Fields)
            {
                field.IsOptional = true;
            }
            engine.Options.Fields[0].IsOptional = false;             // OriginalText
            // Switch error mode off
            engine.ErrorManager.ErrorMode = FileHelpers.ErrorMode.IgnoreAndContinue;

            List <(string, List <MappingEntry>)> glossaries = new List <(string, List <MappingEntry>)>();

            foreach (var fileName in listFiles)
            {
                try {
                    var fileExt = Path.GetExtension(fileName);

                    switch (fileExt)
                    {
                    case MappingEntryCsv.FileExtension:
                        engine.Options.Delimiter = ",";
                        break;

                    case MappingEntryTsv.FileExtension:
                        engine.Options.Delimiter = "\t";
                        break;
                    }

                    var list = engine.ReadFileAsList(fileName);
                    if (list == null)
                    {
                        // try another delmiter
                        engine.Options.Delimiter = engine.Options.Delimiter == "," ? "\t" : ",";
                        list = engine.ReadFileAsList(fileName);
                    }

                    if (list == null || list.Count <= 0)
                    {
                        continue;
                    }

                    if (list != null)
                    {
                        // ignore first line for Header fields
                        var first = list[0];
                        if (first.OriginalText == nameof(MappingEntry.OriginalText) &&
                            first.MappingText == nameof(MappingEntry.MappingText))
                        {
                            list.RemoveAt(0);
                        }

                        glossaries.Add((fileName, list));
                        continue;
                    }

                    // TODO: try to parse .xlxs file...
                }
                catch {
                    continue;
                }
            }

            // prepared, then clear existed project.conf mapping
            var projMappingColl = monitor.GetMappingCollection(projFullPathFileName);

            monitor.RemoveMonitoring(projFullPathFileName);

            foreach (var glossary in glossaries)
            {
                monitor.AddMonitoring(glossary.Item1, glossary.Item2);
            }

            // finally, add back project.conf's mapping for highest priority
            if (projMappingColl == null)
            {
                monitor.AddMonitoring(projFullPathFileName, projEntries);
            }
            else
            {
                monitor.AddMonitoring(projFullPathFileName, projMappingColl);
            }

            monitor.Start();
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Try parse and extrace glossary MappingEntry
        /// </summary>
        /// <param name="glossaryFullPathFileName">Full-path glossary file name</param>
        /// <returns>Extracted MappingEntry list</returns>
        public static List <MappingEntry> TryParseAndExtractMappingEntries(string glossaryFullPathFileName)
        {
            if (File.Exists(glossaryFullPathFileName) == false)
            {
                return(null);
            }

            var engine = new FileHelpers.DelimitedFileEngine <MappingEntry>(System.Text.Encoding.UTF8);

            foreach (var field in engine.Options.Fields)
            {
                field.IsOptional = true;
            }
            engine.Options.Fields[0].IsOptional = false;             // OriginalText
            // Switch error mode off
            engine.ErrorManager.ErrorMode = FileHelpers.ErrorMode.IgnoreAndContinue;

            try {
                var fileExt = Path.GetExtension(glossaryFullPathFileName);

                switch (fileExt)
                {
                case MappingEntryCsv.FileExtension:
                    engine.Options.Delimiter = ",";
                    break;

                case MappingEntryTsv.FileExtension:
                    engine.Options.Delimiter = "\t";
                    break;
                }

                var list = engine.ReadFileAsList(glossaryFullPathFileName);
                if (list == null)
                {
                    // try another delmiter
                    engine.Options.Delimiter = engine.Options.Delimiter == "," ? "\t" : ",";
                    list = engine.ReadFileAsList(glossaryFullPathFileName);
                }


                if (list != null)
                {
                    // ignore first line for Header fields
                    var first = list[0];
                    if (first.OriginalText == nameof(MappingEntry.OriginalText) &&
                        first.MappingText == nameof(MappingEntry.MappingText))
                    {
                        list.RemoveAt(0);
                    }

                    return(list);
                }

                // TODO: try to parse .xlxs file...
            }
            catch {
                return(null);
            }

            return(null);
        }