feat: [dep] add basic maven xml parser

上级 543e09f5
......@@ -4,7 +4,9 @@ import (
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/infrastructure/ast/groovy"
"github.com/phodal/coca/core/infrastructure/xmlparse"
. "github.com/phodal/coca/languages/groovy"
"os"
)
type DepApp struct {
......@@ -25,7 +27,53 @@ func (d *DepApp) BuildImportMap(deps []domain.JClassNode) map[string]domain.JImp
return impMap
}
func Analysis(str string) {
func AnalysisMaven(xmlPath string) []domain.JDependency {
xmlFile, _ := os.Open(xmlPath)
parseXml := xmlparse.ParseXml(xmlFile)
for _, element := range parseXml.Elements {
val := element.Val.(xmlparse.XmlNode)
if val.Name == "dependencies" {
return BuildDeps(val)
}
}
return nil
}
func BuildDeps(val xmlparse.XmlNode) []domain.JDependency {
var deps []domain.JDependency = nil
for _, depElement := range val.Elements {
depNode := depElement.Val.(xmlparse.XmlNode)
dependency := domain.NewJDependency("", "")
for _, depValue := range depNode.Elements {
node := depValue.Val.(xmlparse.XmlNode)
if node.Name == "groupId" {
for _, textNode := range node.Elements {
dependency.GroupId = textNode.Val.(string)
}
}
if node.Name == "artifactId" {
for _, textNode := range node.Elements {
dependency.ArtifactId = textNode.Val.(string)
}
}
if node.Name == "scope" {
for _, textNode := range node.Elements {
dependency.Scope = textNode.Val.(string)
}
}
}
deps = append(deps, *dependency)
}
return deps
}
func AnalysisGradle(str string) {
parser := ProcessGroovyString(str)
context := parser.CompilationUnit()
......
......@@ -6,7 +6,7 @@ import (
"testing"
)
func TestAnalysis(t *testing.T) {
func Test_ShouldReturnGradleDep(t *testing.T) {
g := NewGomegaWithT(t)
pluginsStr := `dependencies {
......@@ -14,11 +14,30 @@ func TestAnalysis(t *testing.T) {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
}`
Analysis(pluginsStr)
AnalysisGradle(pluginsStr)
g.Expect(true).To(Equal(true))
}
func Test_ShouldReturnCorrectMavenDeps(t *testing.T) {
g := NewGomegaWithT(t)
codePath := "../../../_fixtures/deps/maven/pom.xml"
mavenDeps := AnalysisMaven(codePath)
g.Expect(len(mavenDeps)).To(Equal(12))
g.Expect(true).To(Equal(true))
}
func Test_ShouldReturnNilWhenErrorPath(t *testing.T) {
g := NewGomegaWithT(t)
codePath := "../../../_fixtures/god_know_it"
mavenDeps := AnalysisMaven(codePath)
g.Expect(len(mavenDeps)).To(Equal(0))
}
func Test_ShouldCountDeps_WhenHadClassNodes(t *testing.T) {
g := NewGomegaWithT(t)
......
......@@ -10,10 +10,10 @@ type ElemType string
const (
eleTpText ElemType = "text" // 静态文本节点
eleTpNode ElemType = "node" // 节点子节点
eleTpNode ElemType = "XmlNode" // 节点子节点
)
type node struct {
type XmlNode struct {
Id string
Name string
Attrs map[string]xml.Attr
......@@ -25,9 +25,9 @@ type element struct {
Val interface{}
}
func ParseXml(r io.Reader) *node {
func ParseXml(r io.Reader) *XmlNode {
parser := xml.NewDecoder(r)
var root node
var root XmlNode
st := NewStack()
for {
......@@ -44,7 +44,7 @@ func ParseXml(r io.Reader) *node {
for _, val := range attr {
attrMap[val.Name.Local] = val
}
node := node{
node := XmlNode{
Name: name,
Attrs: attrMap,
Elements: make([]element, 0),
......@@ -58,15 +58,15 @@ func ParseXml(r io.Reader) *node {
case xml.EndElement: //tag end
if st.Len() > 0 {
//cur node
n := st.Pop().(node)
if st.Len() > 0 { //if the root node then append to element
//cur XmlNode
n := st.Pop().(XmlNode)
if st.Len() > 0 { //if the root XmlNode then append to element
e := element{
ElementType: eleTpNode,
Val: n,
}
pn := st.Pop().(node)
pn := st.Pop().(XmlNode)
els := pn.Elements
els = append(els, e)
pn.Elements = els
......@@ -77,7 +77,7 @@ func ParseXml(r io.Reader) *node {
}
case xml.CharData: //tag content
if st.Len() > 0 {
n := st.Pop().(node)
n := st.Pop().(XmlNode)
bytes := xml.CharData(t)
content := strings.TrimSpace(string(bytes))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册