@@ -33,52 +33,61 @@ type RuleSet struct {
3333}
3434
3535// DeprecatedReferences maps deprecated pattern IDs to their new versions
36- var DeprecatedReferences = map [string ]string {
37- // Add deprecated pattern mappings here
38- // Example: "rulesets_java_design_ExcessiveClassLength": "category_java_design_ExcessiveClassLength",
36+ var DeprecatedReferences = map [string ]string {}
37+
38+ // Add PMD headers
39+ var (
40+ pmd6Header = `<?xml version="1.0"?>
41+ <ruleset name="Codacy PMD Ruleset"
42+ xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
43+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44+ xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
45+ <description>Codacy PMD Ruleset</description>`
46+
47+ pmd7Header = `<?xml version="1.0" encoding="UTF-8"?>
48+ <ruleset name="Codacy PMD 7 Ruleset"
49+ xmlns="https://pmd.github.io/ruleset/2.0.0"
50+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
51+ xsi:schemaLocation="https://pmd.github.io/ruleset/2.0.0 https://pmd.github.io/schemas/pmd-7.0.0.xsd">
52+ <description>Codacy PMD 7 Ruleset</description>`
53+ )
54+
55+ // CreatePmd6Config creates a PMD 6 configuration from the provided tool configuration
56+ func CreatePmd6Config (configuration []domain.PatternConfiguration ) string {
57+ return createPmdConfigGeneric (configuration , pmd6Header , convertPatternIDToPMD , false )
58+ }
59+
60+ // CreatePmd7Config creates a PMD 7 configuration from the provided tool configuration
61+ func CreatePmd7Config (configuration []domain.PatternConfiguration ) string {
62+ return createPmdConfigGeneric (configuration , pmd7Header , convertPatternIDToPMD7 , true )
3963}
4064
4165// prefixPatternID adds the appropriate prefix to a pattern ID
4266func prefixPatternID (patternID string ) string {
4367 parts := strings .Split (patternID , "_" )
44-
45- // Handle different pattern ID formats
4668 switch len (parts ) {
4769 case 2 :
48- // Format: "patternCategory_patternName"
4970 return fmt .Sprintf ("category_java_%s_%s" , parts [0 ], parts [1 ])
5071 case 3 :
51- // Format: "langAlias_patternCategory_patternName"
5272 return fmt .Sprintf ("category_%s_%s_%s" , parts [0 ], parts [1 ], parts [2 ])
5373 case 4 :
54- // Format: "root_langAlias_patternCategory_patternName"
5574 return fmt .Sprintf ("%s_%s_%s_%s" , parts [0 ], parts [1 ], parts [2 ], parts [3 ])
5675 default :
57- // Return as is if format is unknown
5876 return patternID
5977 }
6078}
6179
62- // convertPatternIDToPMD converts a Codacy pattern ID to PMD format
80+ // convertPatternIDToPMD converts a Codacy pattern ID to PMD 6 format
6381func convertPatternIDToPMD (patternID string ) (string , error ) {
64- // Check if this is a deprecated pattern
6582 if newID , ok := DeprecatedReferences [patternID ]; ok {
6683 patternID = newID
6784 }
6885
69- // Handle both formats:
70- // 1. "java/design/NPathComplexity"
71- // 2. "PMD_category_java_design_NPathComplexity"
72- // 3. "PMD_category_apex_security_ApexSharingViolations"
73- // 4. "PMD_category_plsql_errorprone_TO_TIMESTAMPWithoutDateFormat"
74-
7586 var parts []string
7687 if strings .Contains (patternID , "/" ) {
7788 parts = strings .Split (patternID , "/" )
7889 } else {
79- // Remove PMD_ prefix if present
8090 id := strings .TrimPrefix (patternID , "PMD_" )
81- // Split by underscore and remove "category" if present
8291 parts = strings .Split (id , "_" )
8392 if parts [0 ] == "category" {
8493 parts = parts [1 :]
@@ -89,30 +98,20 @@ func convertPatternIDToPMD(patternID string) (string, error) {
8998 return "" , fmt .Errorf ("invalid pattern ID format: %s" , patternID )
9099 }
91100
92- // Extract language, category, and rule
93- language := parts [0 ] // java, apex, etc.
94- category := parts [1 ] // design, security, etc.
95- rule := parts [2 ] // rule name
96-
97- // If there are more parts, combine them with the rule name
98- if len (parts ) > 3 {
99- rule = strings .Join (parts [2 :], "_" )
100- }
101+ language := parts [0 ]
102+ category := parts [1 ]
103+ rule := strings .Join (parts [2 :], "_" )
101104
102105 return fmt .Sprintf ("category/%s/%s.xml/%s" , language , category , rule ), nil
103106}
104107
105108// convertPatternIDToPMD7 converts a Codacy pattern ID to PMD 7 format
106109func convertPatternIDToPMD7 (patternID string ) (string , error ) {
107- // Strip deprecated mappings
108110 if newID , ok := DeprecatedReferences [patternID ]; ok {
109111 patternID = newID
110112 }
111113
112- // Remove "PMD7_" prefix if present
113114 patternID = strings .TrimPrefix (patternID , "PMD7_" )
114-
115- // Now handle patternIDs like: category_java_bestpractices_UnusedLocalVariable
116115 parts := strings .Split (patternID , "_" )
117116
118117 if len (parts ) < 4 || parts [0 ] != "category" {
@@ -126,48 +125,6 @@ func convertPatternIDToPMD7(patternID string) (string, error) {
126125 return fmt .Sprintf ("category/%s/%s.xml/%s" , lang , category , rule ), nil
127126}
128127
129- // generateRuleXML generates XML for a single rule
130- func generateRuleXML (rule Rule ) (string , error ) {
131- pmdRef , err := convertPatternIDToPMD (rule .PatternID )
132- if err != nil {
133- return "" , err
134- }
135-
136- if len (rule .Parameters ) == 0 {
137- return fmt .Sprintf (` <rule ref="%s"/>` , pmdRef ), nil
138- }
139-
140- // Generate rule with parameters
141- var params strings.Builder
142- for _ , param := range rule .Parameters {
143- // Skip enabled and version parameters, but include all others
144- if param .Name != "enabled" && param .Name != "version" {
145- params .WriteString (fmt .Sprintf (`
146- <property name="%s" value="%s"/>` , param .Name , param .Value ))
147- }
148- }
149-
150- // If no parameters left after filtering, just output the rule without properties
151- if params .Len () == 0 {
152- return fmt .Sprintf (` <rule ref="%s"/>` , pmdRef ), nil
153- }
154-
155- result := fmt .Sprintf (` <rule ref="%s">
156- <properties>%s
157- </properties>
158- </rule>` , pmdRef , params .String ())
159-
160- return result , nil
161- }
162-
163- // Add PMD 7 header
164- var pmd7Header = `<?xml version="1.0" encoding="UTF-8"?>
165- <ruleset name="Codacy PMD 7 Ruleset"
166- xmlns="https://pmd.github.io/ruleset/2.0.0"
167- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
168- xsi:schemaLocation="https://pmd.github.io/ruleset/2.0.0 https://pmd.github.io/schemas/pmd-7.0.0.xsd">
169- <description>Codacy PMD 7 Ruleset</description>`
170-
171128// createPmdConfigGeneric abstracts the config creation for both PMD and PMD7
172129func createPmdConfigGeneric (
173130 configuration []domain.PatternConfiguration ,
@@ -176,7 +133,6 @@ func createPmdConfigGeneric(
176133 isPMD7 bool ,
177134) string {
178135 if len (configuration ) == 0 {
179- // Fallback empty ruleset for PMD 7, or default for PMD 6
180136 if header == pmd7Header {
181137 return header + `</ruleset>`
182138 }
@@ -199,21 +155,15 @@ func createPmdConfigGeneric(
199155 value = param .Default
200156 }
201157 if value != "" {
202- params = append (params , Parameter {
203- Name : param .Name ,
204- Value : value ,
205- })
158+ params = append (params , Parameter {Name : param .Name , Value : value })
206159 }
207160 }
208161 }
209162
210163 patternID := pattern .PatternDefinition .Id
211- if ! isPMD7 {
212- if ! strings .HasPrefix (patternID , "PMD_" ) && ! strings .Contains (patternID , "/" ) {
213- patternID = prefixPatternID (patternID )
214- }
164+ if ! isPMD7 && ! strings .HasPrefix (patternID , "PMD_" ) && ! strings .Contains (patternID , "/" ) {
165+ patternID = prefixPatternID (patternID )
215166 }
216- // For PMD7, do not alter the patternID here; let convertPatternIDToPMD7 handle it.
217167
218168 rules = append (rules , Rule {
219169 PatternID : patternID ,
@@ -222,7 +172,6 @@ func createPmdConfigGeneric(
222172 })
223173 }
224174
225- // Build the XML
226175 var rulesetXML strings.Builder
227176 rulesetXML .WriteString (header )
228177 if ! strings .HasSuffix (header , "\n " ) {
@@ -239,7 +188,6 @@ func createPmdConfigGeneric(
239188 if err != nil {
240189 continue
241190 }
242-
243191 if seen [ref ] {
244192 continue
245193 }
@@ -252,12 +200,9 @@ func createPmdConfigGeneric(
252200
253201 rulesetXML .WriteString (fmt .Sprintf (` <rule ref="%s">
254202 <properties>` , ref ))
255-
256203 for _ , p := range rule .Parameters {
257- rulesetXML .WriteString (fmt .Sprintf (`
258- <property name="%s" value="%s"/>` , p .Name , p .Value ))
204+ rulesetXML .WriteString (fmt .Sprintf ("\n <property name=\" %s\" value=\" %s\" />" , p .Name , p .Value ))
259205 }
260-
261206 rulesetXML .WriteString (`
262207 </properties>
263208 </rule>
@@ -267,19 +212,3 @@ func createPmdConfigGeneric(
267212 rulesetXML .WriteString ("</ruleset>" )
268213 return rulesetXML .String ()
269214}
270-
271- // CreatePmdConfig creates a PMD 6 configuration from the provided tool configuration
272- func CreatePmdConfig (configuration []domain.PatternConfiguration ) string {
273- return createPmdConfigGeneric (configuration , `<?xml version="1.0"?>
274- <ruleset name="Codacy PMD Ruleset"
275- xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
276- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
277- xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
278- <description>Codacy PMD Ruleset</description>
279-
280- ` , convertPatternIDToPMD , false )
281- }
282-
283- func CreatePmd7Config (configuration []domain.PatternConfiguration ) string {
284- return createPmdConfigGeneric (configuration , pmd7Header , convertPatternIDToPMD7 , true )
285- }
0 commit comments