diff --git a/.gitignore b/.gitignore index 69efa02..f49c5e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_STORE *.log *.json out/* diff --git a/main.go b/main.go index 07cbc4d..7446a2c 100644 --- a/main.go +++ b/main.go @@ -40,17 +40,25 @@ // detailled analyze var listDTD []string +var listXSD []string // var listXSD []string -func getAllDTD(path string, file os.FileInfo, err error) error { +func getAllElementForValidationXML(path string, file os.FileInfo, err error) error { if err != nil { panic(err) } - if !file.IsDir() && filepath.Ext(path) == ".dtd" { - absDtd, _ := filepath.Abs(path) - listDTD = append(listDTD, absDtd) + if !file.IsDir() { + if filepath.Ext(path) == ".dtd" { + absDtd, _ := filepath.Abs(path) + listDTD = append(listDTD, absDtd) + } + if filepath.Ext(path) == ".xsd" { + absXsd, _ := filepath.Abs(path) + listXSD = append(listXSD, absXsd) + } } + return nil } @@ -171,7 +179,7 @@ for _, file := range files { if strings.Contains(file.Name(), nameForConfiguration[0]) { - filepath.Walk(*configurationFolder+"/"+file.Name(), getAllDTD) + filepath.Walk(*configurationFolder+"/"+file.Name(), getAllElementForValidationXML) } } } diff --git a/struct.go b/struct.go index b980539..56961d2 100644 --- a/struct.go +++ b/struct.go @@ -33,9 +33,11 @@ } type DetailledAnalysis struct { - isValidAgainstDTD bool - validationDTDInfos string - validationsErrors struct{} + isValidAgainstDTD bool + validationDTDInfos string + validationsErrors []string + isValidAgainstSchema bool + validationSchemaErrors []string } type Message struct { diff --git a/xml.go b/xml.go index e14392d..31c944a 100644 --- a/xml.go +++ b/xml.go @@ -127,9 +127,9 @@ } func processDetailledAnalysis(pathXml string, dtdInDoctype string) DetailledAnalysis { - xmlDetailled := DetailledAnalysis{isValidAgainstDTD: false} + xmlDetailled := DetailledAnalysis{isValidAgainstDTD: false, isValidAgainstSchema: false} - listDTD = append(listDTD, "./example/dtd/note.dtd") + // if dtd exist in xml file process only this if dtdInDoctype != "" { for _, dtdPath := range listDTD { if strings.HasSuffix(dtdPath, dtdInDoctype) { @@ -141,6 +141,7 @@ } } } else { + // check with all dtd and stop if one is true for _, dtdPath := range listDTD { result, _ := exec.Command("xmllint", "--dtdvalid", dtdPath, pathXml, "--noout", "--nowarning").CombinedOutput() if string(result) == "" { @@ -148,7 +149,7 @@ xmlDetailled.validationDTDInfos = dtdPath break } else { - + xmlDetailled.validationsErrors = append(xmlDetailled.validationsErrors, string(result)) } } } @@ -158,5 +159,20 @@ return xmlDetailled } + // if dtd is valid check schema validation + for _, xsdPath := range listXSD { + result, _ := exec.Command("xmllint", "--schema", xsdPath, pathXml, "--noout", "--nowarning").CombinedOutput() + if strings.Contains(string(result), "validates") { + xmlDetailled.isValidAgainstSchema = true + break + } else { + xmlDetailled.validationSchemaErrors = append(xmlDetailled.validationsErrors, string(result)) + } + } + + if xmlDetailled.isValidAgainstSchema == false { + return xmlDetailled + } + return xmlDetailled } diff --git a/xml_test.go b/xml_test.go index a8731e5..fdd99b3 100644 --- a/xml_test.go +++ b/xml_test.go @@ -34,13 +34,23 @@ } func TestValidDTD(t *testing.T) { + listDTD = append(listDTD, "./example/dtd/note.dtd") xmlData.path = "./example/xml/test-default.xml" result := processDetailledAnalysis(xmlData.path, "note.dtd") assert.Equal(t, result.isValidAgainstDTD, true, "XML must be valid according to the DTD") } -func TestInValidDTD(t *testing.T) { +func TestInvalidDTD(t *testing.T) { + listDTD = append(listDTD, "./example/dtd/note.dtd") xmlData.path = "./example/xml/test-default-bad-doctype.xml" result := processDetailledAnalysis(xmlData.path, "bad-doctype.dtd") assert.Equal(t, result.isValidAgainstDTD, false, "XML must not be valid according to the DTD") } + +func TestValidSchema(t *testing.T) { + listDTD = append(listDTD, "./example/dtd/note.dtd") + listXSD = append(listXSD, "./example/xsd/note.xsd") + xmlData.path = "./example/xml/test-default.xml" + result := processDetailledAnalysis(xmlData.path, "") + assert.Equal(t, result.isValidAgainstSchema, true, "XML must be valid according to the Schema") +}