|
| 1 | +using System.Collections.Generic; |
| 2 | + |
| 3 | +namespace SEE.DataModel.DG.IO |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Contains parser configuration and node-indexing helpers for importing external analysis reports. |
| 7 | + /// Parsing configuration for Checkstyle XML reports. |
| 8 | + /// </summary> |
| 9 | + /// <remarks> |
| 10 | + /// Supported nodes: |
| 11 | + /// <list type="bullet"> |
| 12 | + /// <item><description><c><file></c>: File-level aggregation (counts by severity).</description></item> |
| 13 | + /// <item><description><c><error></c>: Individual violations (line/column + rendered message).</description></item> |
| 14 | + /// </list> |
| 15 | + /// |
| 16 | + /// Key idea: |
| 17 | + /// Metrics are configured per context (see <see cref="XPathMapping.MetricsByContext"/>), |
| 18 | + /// so the parser does not need XPath hacks that force irrelevant metrics to evaluate to NaN. |
| 19 | + /// </remarks> |
| 20 | + internal sealed class CheckstyleParsingConfig : ParsingConfig |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Context name for individual Checkstyle violations (<c><error></c> nodes). |
| 24 | + /// </summary> |
| 25 | + private const string errorContext = "error"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Context name for file-level aggregation (<c><file></c> nodes). |
| 29 | + /// </summary> |
| 30 | + private const string fileContext = "file"; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initializes a new instance of the <see cref="CheckstyleParsingConfig"/> class. |
| 34 | + /// </summary> |
| 35 | + public CheckstyleParsingConfig() |
| 36 | + { |
| 37 | + ToolId = "Checkstyle"; |
| 38 | + |
| 39 | + // Used by CheckstyleIndexNodeStrategy to trim absolute paths down to package/class. |
| 40 | + SourceRootMarker = "src/main/java/"; |
| 41 | + |
| 42 | + XPathMapping = new XPathMapping |
| 43 | + { |
| 44 | + // Parse both file-level and error-level nodes. |
| 45 | + SearchedNodes = $"//{fileContext}|//{errorContext}", |
| 46 | + |
| 47 | + // Build a stable "full path" identifier for each node. |
| 48 | + // For Checkstyle, the report contains absolute file paths in @name. |
| 49 | + PathBuilders = new Dictionary<string, string> |
| 50 | + { |
| 51 | + [fileContext] = "string(@name)", |
| 52 | + [errorContext] = "string(ancestor::file/@name)", |
| 53 | + }, |
| 54 | + |
| 55 | + // File name extraction (optional for CheckstyleIndexNodeStrategy, but useful for other strategies). |
| 56 | + // We keep this context-first to avoid tag-name ambiguity. |
| 57 | + FileName = new Dictionary<string, string> |
| 58 | + { |
| 59 | + // The file element has the file path in @name. |
| 60 | + [fileContext] = "string(@name)", |
| 61 | + // The error element inherits the file path from its ancestor <file>. |
| 62 | + [errorContext] = "string(ancestor::file/@name)", |
| 63 | + }, |
| 64 | + |
| 65 | + // Location is only meaningful for <error>, but parsing it globally is safe: |
| 66 | + // for <file> these attributes are missing and are simply ignored. |
| 67 | + LocationMapping = new Dictionary<string, string> |
| 68 | + { |
| 69 | + ["StartLine"] = "string(@line)", |
| 70 | + ["StartColumn"] = "string(@column)", |
| 71 | + }, |
| 72 | + |
| 73 | + // Context-specific metrics. |
| 74 | + MetricsByContext = new Dictionary<string, Dictionary<string, string>> |
| 75 | + { |
| 76 | + [fileContext] = new Dictionary<string, string> |
| 77 | + { |
| 78 | + // Count errors by severity within this file. |
| 79 | + // count(error[@severity='warning']) counts all child <error> elements. |
| 80 | + ["Aggregated.WarningCount"] = "string(count(error[@severity='warning']))", |
| 81 | + ["Aggregated.ErrorCount"] = "string(count(error[@severity='error']))", |
| 82 | + ["Aggregated.InfoCount"] = "string(count(error[@severity='info']))", |
| 83 | + |
| 84 | + // Total violations for this file. |
| 85 | + ["Aggregated.ViolationCount"] = "string(count(error))", |
| 86 | + }, |
| 87 | + |
| 88 | + [errorContext] = new Dictionary<string, string> |
| 89 | + { |
| 90 | + // A short, human-friendly issue string. |
| 91 | + // Using substring(...) with a large length keeps this robust even if message is empty. |
| 92 | + ["ContextLevel.Issue"] = |
| 93 | + "substring(concat('Line ', @line, ': [', @severity, '] ', @message), 1, 10000)", |
| 94 | + }, |
| 95 | + }, |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Creates the parser implementation that can read Checkstyle reports using this configuration. |
| 101 | + /// </summary> |
| 102 | + /// <returns>A report parser configured for Checkstyle XML input.</returns> |
| 103 | + internal override IReportParser CreateParser() |
| 104 | + { |
| 105 | + return new XmlReportParser(this); |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Creates the index-node strategy that maps findings to the corresponding graph node identifiers. |
| 110 | + /// </summary> |
| 111 | + /// <returns>An index node strategy suitable for Checkstyle file paths.</returns> |
| 112 | + public override IIndexNodeStrategy CreateIndexNodeStrategy() |
| 113 | + { |
| 114 | + return new CheckstyleIndexNodeStrategy(this); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments