Пример #1
0
        public ContoursElevationRuleMap Parse(Stream inputStream)
        {
            WikiParser.SetWikiContentSource(inputStream);

            rules           = new ContoursElevationRuleMap();
            templateCounter = 0;
            string currentMainSection = null;

            while (true)
            {
                WikiContentType wikiContentType;

                wikiContentType = WikiParser.Next();

                if (wikiContentType == WikiContentType.Eof)
                {
                    break;
                }

                if (wikiContentType == WikiContentType.Section || wikiContentType == WikiContentType.Text)
                {
                    WikiSection section = WikiParser.Context.LowestSection;
                    if (section == null)
                    {
                        continue;
                    }

                    if (wikiContentType == WikiContentType.Section)
                    {
                        if (section.SectionLevel == 1)
                        {
                            currentMainSection = section.SectionName;
                        }
                    }
                    else if (wikiContentType == WikiContentType.Text)
                    {
                        continue;
                    }

                    if (currentMainSection == null)
                    {
                        continue;
                    }

                    switch (currentMainSection.ToLowerInvariant())
                    {
                    case "contours rendering rules":
                    {
                        SkipToTableAndParseIt(ParseRule);
                        break;
                    }
                    }
                }
            }

            return(rules);
        }
Пример #2
0
        /// <summary>
        /// Parses the rendering rules and adds the parsed rules to the <see cref="Rules"/> property.
        /// </summary>
        public void Parse()
        {
            string currentMainSection      = null;
            string currentSubsection       = null;
            bool   ignoreRestOfMainSection = false;
            bool   ignoreRestOfSubsection  = false;
            bool   moveNext = true;

            rules = new RenderingRuleSet();

            while (true)
            {
                WikiContentType wikiContentType;

                if (moveNext)
                {
                    wikiContentType = WikiParser.Next();
                }
                else
                {
                    wikiContentType = WikiParser.Context.CurrentType;
                    moveNext        = true;
                }

                if (wikiContentType == WikiContentType.Eof)
                {
                    break;
                }

                if (wikiContentType == WikiContentType.Section || wikiContentType == WikiContentType.Text)
                {
                    WikiSection section = WikiParser.Context.LowestSection;
                    if (section == null)
                    {
                        continue;
                    }

                    if (wikiContentType == WikiContentType.Section)
                    {
                        if (section.SectionLevel == 1)
                        {
                            if (log.IsDebugEnabled)
                            {
                                log.DebugFormat("Found new main section: '{0}'", section.SectionName);
                            }

                            currentMainSection      = section.SectionName;
                            currentSubsection       = null;
                            ignoreRestOfMainSection = false;
                            ignoreRestOfSubsection  = false;
                        }
                        else if (section.SectionLevel == 2)
                        {
                            currentSubsection      = section.SectionName;
                            ignoreRestOfSubsection = false;
                        }
                    }
                    else if (wikiContentType == WikiContentType.Text &&
                             (ignoreRestOfMainSection || ignoreRestOfSubsection))
                    {
                        continue;
                    }

                    switch (currentMainSection.ToLowerInvariant())
                    {
                    case "rendering rules":
                    {
                        if (currentSubsection == null)
                        {
                            continue;
                        }

                        switch (section.SectionName.ToLowerInvariant())
                        {
                        case "options":
                            SkipToTableAndParseIt(ParseOption);
                            AssertRulesVersionCompatibility();
                            ignoreRestOfSubsection = true;
                            break;

                        case "points":
                            SkipToTableAndParseIt(ParsePointRule);
                            ignoreRestOfSubsection = true;
                            break;

                        case "lines":
                            SkipToTableAndParseIt(ParseLineRule);
                            ignoreRestOfSubsection = true;
                            break;

                        case "areas":
                            SkipToTableAndParseIt(ParseAreaRule);
                            ignoreRestOfSubsection = true;
                            break;

                        default:
                            ThrowParseError("Invalid section: '{0}'", section.SectionName);
                            return;
                        }

                        break;
                    }

                    case "standard garmin types":
                    {
                        if (currentSubsection == null)
                        {
                            continue;
                        }

                        switch (section.SectionName.ToLowerInvariant())
                        {
                        case "points":
                            SkipToTableAndParseIt((tableRowCells)
                                                  =>
                                                  ParseStandardGarminTypesDictionary(
                                                      tableRowCells,
                                                      typesRegistry.GarminPointTypesDictionary));
                            ignoreRestOfSubsection = true;
                            break;

                        case "lines":
                            SkipToTableAndParseIt((tableRowCells)
                                                  =>
                                                  ParseStandardGarminTypesDictionary(
                                                      tableRowCells,
                                                      typesRegistry.GarminLineTypesDictionary));
                            ignoreRestOfSubsection = true;
                            break;

                        case "areas":

                            SkipToTableAndParseIt((tableRowCells)
                                                  =>
                                                  ParseStandardGarminTypesDictionary(
                                                      tableRowCells,
                                                      typesRegistry.GarminAreaTypesDictionary));
                            ignoreRestOfSubsection = true;
                            break;

                        default:
                            ThrowParseError("Invalid Garmin standard types section: '{0}'",
                                            section.SectionName);
                            return;
                        }

                        break;
                    }

                    case "characters conversion table":
                    {
                        SkipToTableAndParseIt(ParseCharactersConversionTable);
                        ignoreRestOfMainSection = true;
                        break;
                    }

                    case "patterns":
                    {
                        if (currentSubsection == null)
                        {
                            continue;
                        }

                        ParsePointPattern(section.SectionName);
                        moveNext = false;
                        break;
                    }
                    }
                }
            }
        }
Пример #3
0
        private void ParsePointPattern(string patternName)
        {
            currentParsedPattern = new PatternDefinition(patternName);

            SkipToTableAndParseIt(ParseColorTable);

            // collect all pattern lines
            StringBuilder patternText = new StringBuilder();

            while (true)
            {
                WikiContentType wikiContentType = WikiParser.Next();

                if (wikiContentType != WikiContentType.Text)
                {
                    break;
                }

                patternText.Append(WikiParser.Context.CurrentLine);
            }

            MatchCollection matches = regexPointPattern.Matches(patternText.ToString());

            if (matches.Count < 1)
            {
                ThrowParseError("Invalid point pattern: '{0}'", patternText.ToString());
            }

            Match match = matches[0];
            Group group = match.Groups["line"];

            foreach (Capture capture in group.Captures)
            {
                string patternLine = capture.Value;

                if (patternLine != null)
                {
                    // all pattern lines have to be of the same length
                    if (currentParsedPattern.Width != -1)
                    {
                        if (patternLine.Length != currentParsedPattern.Width)
                        {
                            ThrowParseError("All pattern lines have to be of the same width.");
                        }
                    }

                    currentParsedPattern.PatternLines.Add(patternLine);

                    if (currentParsedPattern.Width > 24)
                    {
                        ThrowParseError("Point pattern can have a maximum width of 24 characters");
                    }
                }
            }

            if (currentParsedPattern.Height > 24)
            {
                ThrowParseError("Point pattern can have a maximum height of 24 characters");
            }

            this.typesRegistry.Patterns.Add(currentParsedPattern.PatternName, currentParsedPattern);
        }