Skip to content

Instantly share code, notes, and snippets.

@hhatto
Created October 5, 2016 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhatto/6ca28c6bc3e961a6c95a9b22f70455d8 to your computer and use it in GitHub Desktop.
Save hhatto/6ca28c6bc3e961a6c95a9b22f70455d8 to your computer and use it in GitHub Desktop.
reconstruct gendarme xml
package main
import (
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"strings"
)
type Rule struct {
XMLName xml.Name `xml:"rule"`
Name string `xml:"Name,attr"`
Type string `xml:"Type,attr"`
Uri string `xml:"Uri,attr"`
Body string `xml:",innerxml"`
}
type Rules struct {
XMLName xml.Name `xml:"rules"`
Rule []Rule `xml:"rule"`
}
type File struct {
XMLName xml.Name `xml:"file"`
Name string `xml:"Name,attr"`
Body string `xml:",innerxml"`
}
type Files struct {
XMLName xml.Name `xml:"files"`
File []File `xml:"file"`
}
type Defect struct {
XMLName xml.Name `xml:"defect"`
Severity string `xml:"Severity,attr"`
Confidence string `xml:"Confidence,attr"`
Location string `xml:"Location,attr"`
Source string `xml:"Source,attr"`
}
type Target struct {
XMLName xml.Name `xml:"target"`
Name string `xml:"Name,attr"`
Assembly string `xml:"Assembly,attr"`
Defect Defect `xml:"defect"`
}
type Problem struct {
XMLName xml.Name `xml:"problem"`
Body string `xml:",innerxml"`
}
type Solution struct {
XMLName xml.Name `xml:"solution"`
Body string `xml:",innerxml"`
}
type ResultRule struct {
XMLName xml.Name `xml:"rule"`
Name string `xml:"Name,attr"`
Uri string `xml:"Uri,attr"`
Problem string `xml:"problem"`
Solution string `xml:"solution"`
Target []Target `xml:"target"`
}
type Results struct {
XMLName xml.Name `xml:"results"`
Rule []ResultRule `xml:"rule"`
}
type Gendarme struct {
XMLName xml.Name `xml:"gendarme-output"`
Date string `xml:"date,attr"`
Files Files `xml:"files"`
Rules Rules `xml:"rules"`
Results Results `xml:"results"`
}
func NewGendarme() *Gendarme {
return &Gendarme{
Rules: Rules{},
Files: Files{},
Results: Results{},
}
}
func main() {
include := flag.String("include-prefix", "", "include target name")
flag.Parse()
xmldoc, err := ioutil.ReadFile(flag.Arg(0))
if err != nil {
fmt.Println(err)
return
}
g := NewGendarme()
err = xml.Unmarshal(xmldoc, &g)
if err != nil {
fmt.Println(err)
return
}
for i, rule := range g.Rules.Rule {
if rule.Type != "Method" {
g.Rules.Rule[i].Type = "Method"
}
}
newResults := Results{}
newResults.XMLName = g.Results.XMLName
newResults.Rule = []ResultRule{}
for _, result := range g.Results.Rule {
newRule := result
targets := []Target{}
for _, t := range result.Target {
if strings.HasPrefix(t.Name, *include) {
if t.Defect.Source == "" {
continue
}
targets = append(targets, t)
}
}
if len(targets) != 0 {
newRule.Target = targets
newResults.Rule = append(newResults.Rule, newRule)
}
}
g.Results = newResults
filter_doc, err := xml.MarshalIndent(g, "", " ")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(`<?xml version="1.0" encoding="utf-8"?>`)
fmt.Println(string(filter_doc))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment