Exemplo n.º 1
0
        private static DirectoryScanOptions ReadDirectoryScanOptions(ScanFilterOptions globalExcludes, SetRecord set)
        {
            if (set == null)
            {
                return(null);
            }
            if (!set.TryGetValue("path", out RecordBase pathRecord) || !set.TryGetValue("depth", out RecordBase depthRecord))
            {
                return(null);
            }
            if (!(pathRecord is ScalerRecord pathScaler) || !(depthRecord is ScalerRecord depthScaler))
            {
                return(null);
            }
            if (!pathScaler.TryReadAs(out string path) || !depthScaler.TryReadAs(out int depth))
            {
                return(null);
            }

            if (depth <= 0)
            {
                depth = 1;
            }

            ScanFilterOptions includeOptions;
            ScanFilterOptions excludeOptions;

            if (set.TryGetValue("include", out RecordBase includeRecordBase) && includeRecordBase is SetRecord includeRecord)
            {
                includeOptions = ReadFilterOptions(includeRecord);
            }
            else
            {
                includeOptions = ScanFilterOptions.CreateIncludeDefault();
            }

            if (set.TryGetValue("exclude", out RecordBase excludeRecordBase) && excludeRecordBase is SetRecord excludeRecord)
            {
                excludeOptions = ReadFilterOptions(excludeRecord);
            }
            else
            {
                excludeOptions = ScanFilterOptions.CreateExcludeDefault();
            }

            bool ignoreGlobalExclude = false;

            if (set.TryGetValue("ignoreGlobalExclude", out RecordBase ignoreGlobalExcludeBase) &&
                ignoreGlobalExcludeBase is ScalerRecord ignoreGlobalExcludeScaler &&
                ignoreGlobalExcludeScaler.TryReadAs <bool>(out bool ignoreGlobalExcludeValue))
            {
                ignoreGlobalExclude = ignoreGlobalExcludeValue;
            }
            if (!ignoreGlobalExclude)
            {
                excludeOptions.Combine(globalExcludes);
            }
            return(new DirectoryScanOptions(path, depth, includeOptions, excludeOptions));
        }
Exemplo n.º 2
0
        public static IConfiguartion Read(SetRecord set)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }

            RecordBase record;
            SetRecord  setIncludeItems    = null;
            SetRecord  setGlobalExcludes  = null;
            ListRecord setScanDirectories = null;

            if (set.TryGetValue("scan", out record))
            {
                setScanDirectories = record as ListRecord;
            }
            if (set.TryGetValue("include", out record))
            {
                setIncludeItems = record as SetRecord;
            }
            if (set.TryGetValue("exclude", out record))
            {
                setGlobalExcludes = record as SetRecord;
            }

            ItemIncludeOptions resultInclude = ReadItemIncludeOptions(setIncludeItems);
            ScanFilterOptions  resultExclude = null;

            if (setGlobalExcludes == null)
            {
                resultExclude = ScanFilterOptions.CreateExcludeDefault();
            }
            else
            {
                resultExclude = ReadFilterOptions(setGlobalExcludes);
            }

            List <DirectoryScanOptions> resultScanDirectories = new List <DirectoryScanOptions>();

            if (setScanDirectories != null)
            {
                foreach (RecordBase scanOptions in setScanDirectories)
                {
                    if (scanOptions is SetRecord setScanOptions)
                    {
                        resultScanDirectories.Add(ReadDirectoryScanOptions(resultExclude, setScanOptions));
                    }
                }
            }

            return(new Configuration(resultExclude, resultInclude, resultScanDirectories));
        }
Exemplo n.º 3
0
        internal static WordResult FromRecord(SetRecord record)
        {
            if (record == null)
            {
                throw new ArgumentNullException(nameof(record));
            }

            if (record.TryGetValue("word", out RecordBase wordRecord) && wordRecord is ScalerRecord wordScaler)
            {
                if (wordScaler.ScalerType == ScalerType.Null)
                {
                    return(null);
                }

                string word = wordScaler.ReadAs <string>().Trim();
                WordPronunciationsCollection pronunciationCollection = null;
                if (record.TryGetValue("pronunciation", out RecordBase pronunciation))
                {
                    pronunciationCollection = WordPronunciationsCollection.FromRecord(pronunciation as SetRecord);
                }
                else
                {
                    pronunciationCollection = WordPronunciationsCollection.FromRecord(null);
                }
                WordDefinitionsCollection definitionCollection = null;
                if (record.TryGetValue("defs", out RecordBase defs))
                {
                    definitionCollection = WordDefinitionsCollection.FromRecord(defs as ListRecord);
                }
                else
                {
                    definitionCollection = WordDefinitionsCollection.FromRecord(null);
                }
                SentencesCollection sentencesCollection = null;
                if (record.TryGetValue("sams", out RecordBase sentences))
                {
                    sentencesCollection = SentencesCollection.FromRecord(sentences as ListRecord);
                }
                else
                {
                    sentencesCollection = SentencesCollection.FromRecord(null);
                }
                return(new WordResult(word, pronunciationCollection, definitionCollection, sentencesCollection));
            }
            else
            {
                throw new Exception($"can not read {nameof(WordResult)} from record");
            }
        }
Exemplo n.º 4
0
        public static SetRecord Combine(this SetRecord dest, SetRecord source)
        {
            if (dest == null)
            {
                throw new ArgumentNullException(nameof(dest));
            }
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            RecordBase temp;

            foreach (var pair in source)
            {
                if (dest.TryGetValue(pair.Key, out temp))
                {
                    if (pair.Value is SetRecord srcset && temp is SetRecord dstset)
                    {
                        Combine(dstset, srcset);
                    }
                    else
                    {
                        dest[pair.Key] = pair.Value;
                    }
                }
Exemplo n.º 5
0
        private static ItemIncludeOptions ReadItemIncludeOptions(SetRecord set)
        {
            List <string> resultDirectories = new List <string>();
            List <string> resultFiles       = new List <string>();

            // return empty list if null
            if (set == null)
            {
                return(new ItemIncludeOptions(resultDirectories, resultFiles));
            }

            RecordBase record;
            ListRecord directories = null;
            ListRecord files       = null;

            if (set.TryGetValue("directories", out record))
            {
                directories = record as ListRecord;
            }
            if (set.TryGetValue("files", out record))
            {
                files = record as ListRecord;
            }

            if (directories != null)
            {
                foreach (RecordBase rec in directories)
                {
                    if (rec is ScalerRecord scaler && scaler.ScalerType == ScalerType.String)
                    {
                        resultDirectories.Add(scaler.ReadAs <string>());
                    }
                }
            }
            if (files != null)
            {
                foreach (RecordBase rec in files)
                {
                    if (rec is ScalerRecord scaler && scaler.ScalerType == ScalerType.String)
                    {
                        resultFiles.Add(scaler.ReadAs <string>());
                    }
                }
            }
            return(new ItemIncludeOptions(resultDirectories, resultFiles));
        }
Exemplo n.º 6
0
        internal static WordPronunciationsCollection FromRecord(SetRecord set)
        {
            if (set == null)
            {
                return(new WordPronunciationsCollection(null, null));
            }

            WordPronunciation us = null;

            if (set.TryGetValue("AmE", out RecordBase ameRecord) && ameRecord is ScalerRecord ameScaler &&
                set.TryGetValue("AmEmp3", out RecordBase ameMp3Record) && ameMp3Record is ScalerRecord ameMp3Scaler)
            {
                string ame = ameScaler.ReadAs <string>();
                if (ameMp3Scaler.ScalerType == ScalerType.Null)
                {
                    us = new WordPronunciation(ame, null);
                }
                else
                {
                    string uri = ameMp3Scaler.ReadAs <string>();
                    us = new WordPronunciation(ame, new Uri(uri, UriKind.Absolute));
                }
            }
            WordPronunciation uk = null;

            if (set.TryGetValue("BrE", out RecordBase breRecord) && breRecord is ScalerRecord breScaler &&
                set.TryGetValue("BrEmp3", out RecordBase breMp3Record) && breMp3Record is ScalerRecord breMp3Scaler)
            {
                string bre = breScaler.ReadAs <string>();
                if (breMp3Scaler.ScalerType == ScalerType.Null)
                {
                    uk = new WordPronunciation(bre, null);
                }
                else
                {
                    string uri = breMp3Scaler.ReadAs <string>();
                    uk = new WordPronunciation(bre, new Uri(uri, UriKind.Absolute));
                }
            }
            return(new WordPronunciationsCollection(us, uk));
        }
Exemplo n.º 7
0
        private static void CombineSetRecord(SetRecord destination, SetRecord source)
        {
            string     key;
            RecordBase value;
            RecordBase destValue;

            foreach (var pair in source)
            {
                key   = pair.Key;
                value = pair.Value;
                if (destination.TryGetValue(key, out destValue))
                {
                    if (value is SetRecord sourceSet && destValue is SetRecord destSet)
                    {
                        CombineSetRecord(destSet, sourceSet);
                    }
                    else if (value is ListRecord sourceList && destValue is ListRecord destList)
                    {
                        foreach (RecordBase sourceListElement in sourceList)
                        {
                            destList.Add(sourceListElement);
                        }
                    }
Exemplo n.º 8
0
        private static ScanFilterOptions ReadFilterOptions(SetRecord set)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }

            string     pattern;
            Regex      tempRegex;
            RecordBase recordbase;
            ListRecord directories = null;
            ListRecord files       = null;
            ListRecord common      = null;
            ListRecord paths       = null;

            if (set.TryGetValue("directory", out recordbase))
            {
                directories = recordbase as ListRecord;
            }
            if (set.TryGetValue("file", out recordbase))
            {
                files = recordbase as ListRecord;
            }
            if (set.TryGetValue("common", out recordbase))
            {
                common = recordbase as ListRecord;
            }
            if (set.TryGetValue("path", out recordbase))
            {
                paths = recordbase as ListRecord;
            }
            List <Regex> resultDirectories = new List <Regex>();
            List <Regex> resultFiles       = new List <Regex>();
            List <Regex> resultCommon      = new List <Regex>();
            List <Regex> resultPaths       = new List <Regex>();

            if (directories != null)
            {
                foreach (RecordBase rec in directories)
                {
                    if (rec is ScalerRecord scaler && scaler.ScalerType == ScalerType.String)
                    {
                        pattern = scaler.ReadAs <string>();
                        try
                        {
                            tempRegex = new Regex(pattern);
                            resultDirectories.Add(tempRegex);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            if (files != null)
            {
                foreach (RecordBase rec in files)
                {
                    if (rec is ScalerRecord scaler && scaler.ScalerType == ScalerType.String)
                    {
                        pattern = scaler.ReadAs <string>();
                        try
                        {
                            tempRegex = new Regex(pattern);
                            resultFiles.Add(tempRegex);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            if (common != null)
            {
                foreach (RecordBase rec in common)
                {
                    if (rec is ScalerRecord scaler && scaler.ScalerType == ScalerType.String)
                    {
                        pattern = scaler.ReadAs <string>();
                        try
                        {
                            tempRegex = new Regex(pattern);
                            resultCommon.Add(tempRegex);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            if (paths != null)
            {
                foreach (RecordBase rec in paths)
                {
                    if (rec is ScalerRecord scaler && scaler.ScalerType == ScalerType.String)
                    {
                        pattern = scaler.ReadAs <string>();
                        try
                        {
                            tempRegex = new Regex(pattern);
                            resultPaths.Add(tempRegex);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return(new ScanFilterOptions(resultFiles, resultDirectories, resultCommon, resultPaths));
        }