From cc59a4dcb03bb5c8e6aac351b21c5ab03c929b51 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 00:07:25 -1000 Subject: [PATCH 01/23] add validate methods --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index e051d86..0ca8fcf 100644 --- a/README.md +++ b/README.md @@ -315,3 +315,15 @@ save_file(xdoc, filename) # save xdoc to an XML file string(xdoc) # formatted XML doc to a string show(io, xdoc) # output formatted XML document ``` + +##### Functions to validate a document + +```julia +xsd = XMLSchema(url) # parse an XSD schema file + +isvalid = validate(xmlfile, schema) # validate an XML file against a previously loaded XSD schema +isvalid = validate(doc, schema) # validate a LightXML XML Document against a previously loaded XSD schema +isvalid = validate(url, schema) # validate a URI or file against an XSD Schema document +isvalid = validate(element, schema) # validate a LightXML XML Node (a subtree) against an XSD Schema document +''' + From 3e31fb36a71c9a00cbcd36d5d9b38a891f43b5c8 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 00:08:44 -1000 Subject: [PATCH 02/23] Add files via upload --- test/valid.xml | 24 ++++++++++++++++++++++++ test/valid.xsd | 33 +++++++++++++++++++++++++++++++++ test/validate.jl | 1 + 3 files changed, 58 insertions(+) create mode 100644 test/valid.xml create mode 100644 test/valid.xsd create mode 100644 test/validate.jl diff --git a/test/valid.xml b/test/valid.xml new file mode 100644 index 0000000..7a6140b --- /dev/null +++ b/test/valid.xml @@ -0,0 +1,24 @@ + + + + John Smith + + Ola Nordmann +
Langgt 23
+ 4000 Stavanger + Norway +
+ + Empire Burlesque + Special Edition + 1 + 10.90 + + + Hide your heart + 1 + 9.90 + +
diff --git a/test/valid.xsd b/test/valid.xsd new file mode 100644 index 0000000..5209496 --- /dev/null +++ b/test/valid.xsd @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/validate.jl b/test/validate.jl new file mode 100644 index 0000000..8c15537 --- /dev/null +++ b/test/validate.jl @@ -0,0 +1 @@ +@test validate("valid.xml", "valid.xsd") == true From f872179b3771b63cc2a11e461b8cb54e3e43bdcb Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 00:09:20 -1000 Subject: [PATCH 03/23] Update runtests.jl --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index d7f2b6f..9ff12a4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,7 +1,7 @@ using LightXML using Test -tests = ["parse", "create", "cdata", "pi"] +tests = ["parse", "create", "cdata", "pi", "validate"] for t in tests fpath = "$t.jl" From 5781102958632b85c8db311df993d952baa89367 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 00:09:57 -1000 Subject: [PATCH 04/23] Add files via upload --- src/schema.jl | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/schema.jl diff --git a/src/schema.jl b/src/schema.jl new file mode 100644 index 0000000..6c96c14 --- /dev/null +++ b/src/schema.jl @@ -0,0 +1,58 @@ +""" +An XML Schema Document, produced by an XML document that is XML for the schema. +""" +mutable struct XMLSchema + ptr::Xptr + isvalid::Bool +end + +function XMLSchema(context::Xptr) + schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), context) + schema != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) + return XMLSchema(schema, true) +end + +function XMLSchema(url::String) + ctxt = ccall((:xmlSchemaNewParserCtxt, libxml2), Xptr, (Cstring,), url) + ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema at " * url)) + return XMLSchema(ctxt) +end + +function XMLSchema(doc::XMLDocument) + ctxt = ccall((:xmlSchemaNewDocParserCtxt, libxml2), Xptr, (Xptr,), doc.ptr) + ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) + return XMLSchema(ctxt) +end + +function validate(xml::XMLDocument, schema::XMLSchema) + ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) + err = ccall((:xmlSchemaValidateDoc, libxml2), + Cint, (Xptr, Xptr), ctxt, xml.ptr) + return err == 0 ? true : false +end + +function validate(url::String, schema::XMLSchema) + ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) + err = ccall((:xmlSchemaValidateFile, libxml2), + Cint, (Xptr, Cstring), ctxt, url) + return err == 0 ? true : false +end + +function validate(elem::XMLNode, schema::XMLSchema) + ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) + err = ccall((:xmlSchemaValidateOneElement, libxml2), + Cint, (Xptr, Cstring), ctxt, elem.ptr) + return err == 0 ? true : false +end + +function validate(url::String, schemafile::String) + ctxt = ccall((:xmlSchemaNewParserCtxt, libxml2), Xptr, (Cstring,), schemafile) + ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema at " * schemafile)) + schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), ctxt) + schema != C_NULL || throw(XMLValidationError("Bad XML Schema at " * url)) + ccall((:xmlSchemaFreeParserCtxt, libxml2), Cvoid, (Xptr,), ctxt) + ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) + err = ccall((:xmlSchemaValidateFile, libxml2), + Cint, (Ptr{LightXML.xmlBuffer}, Cstring), ctxt, url) + return err == 0 ? true : false +end From 61f80fbc7abb24bac63d6b654c9d13c6f4947489 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 00:11:20 -1000 Subject: [PATCH 05/23] add schema, export validate --- src/LightXML.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/LightXML.jl b/src/LightXML.jl index 823895a..a84b13f 100644 --- a/src/LightXML.jl +++ b/src/LightXML.jl @@ -19,7 +19,10 @@ export # document XMLDocument, version, encoding, compression, standalone, root, - parse_file, parse_string, save_file, set_root, create_root + parse_file, parse_string, save_file, set_root, create_root, + + # schema + XMLSchema, validate const Xchar = UInt8 const Xstr = Ptr{Xchar} @@ -43,5 +46,6 @@ include("utils.jl") include("nodes.jl") include("document.jl") include("cdata.jl") +include("schema.jl") end From 2848996851428967275b2e2e7e52d946fa444ab8 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 00:12:21 -1000 Subject: [PATCH 06/23] add XMLValidationError --- src/errors.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/errors.jl b/src/errors.jl index af47540..7d5c7f3 100644 --- a/src/errors.jl +++ b/src/errors.jl @@ -15,3 +15,7 @@ end struct XMLTreeError{T<:AbstractString} <: XMLError msg::T end + +struct XMLValidationError{T<:AbstractString} <: XMLError + msg::T +end From dfdbfaa68eb9957180eb73bfd5eb516c5f4081f8 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 07:15:45 -1000 Subject: [PATCH 07/23] more tests --- test/validate.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/validate.jl b/test/validate.jl index 8c15537..c48723d 100644 --- a/test/validate.jl +++ b/test/validate.jl @@ -1 +1,13 @@ @test validate("valid.xml", "valid.xsd") == true + +@test validate("valid.xml", "valid.xsd") == true + +doc = parse_file("valid.xml") +schema = XMLSchema("valid.xsd") + +@test validate("valid.xml", schema) == true + +@test validate(doc, schema) == true + +@test validate(root(doc), schema) == true + From 2ab855ee707bbfc9f6614c8b0fe31658806546a0 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 07:16:51 -1000 Subject: [PATCH 08/23] correct struct.ptr references --- src/schema.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/schema.jl b/src/schema.jl index 6c96c14..9505c14 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -25,23 +25,23 @@ function XMLSchema(doc::XMLDocument) end function validate(xml::XMLDocument, schema::XMLSchema) - ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) + ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateDoc, libxml2), Cint, (Xptr, Xptr), ctxt, xml.ptr) return err == 0 ? true : false end function validate(url::String, schema::XMLSchema) - ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) + ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateFile, libxml2), Cint, (Xptr, Cstring), ctxt, url) return err == 0 ? true : false end -function validate(elem::XMLNode, schema::XMLSchema) - ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) +function validate(elem::XMLElement, schema::XMLSchema) + ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateOneElement, libxml2), - Cint, (Xptr, Cstring), ctxt, elem.ptr) + Cint, (Xptr, Xptr), ctxt, elem.node.ptr) return err == 0 ? true : false end From c5734fa12a3d496124c00bc6bf5189ac6f487dd0 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 08:47:04 -1000 Subject: [PATCH 09/23] add methods --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ca8fcf..325729b 100644 --- a/README.md +++ b/README.md @@ -319,11 +319,13 @@ show(io, xdoc) # output formatted XML document ##### Functions to validate a document ```julia -xsd = XMLSchema(url) # parse an XSD schema file +xsd = XMLSchema(url) # parse an XSD schema file or URL isvalid = validate(xmlfile, schema) # validate an XML file against a previously loaded XSD schema isvalid = validate(doc, schema) # validate a LightXML XML Document against a previously loaded XSD schema isvalid = validate(url, schema) # validate a URI or file against an XSD Schema document isvalid = validate(element, schema) # validate a LightXML XML Node (a subtree) against an XSD Schema document + +isvalid = validate(xmlfile, schemafile) # validate an XML file or URL against a XSD schem file or URL ''' From b12eaf5198b459d984d2c5228b0fc3bb610a3bea Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 08:57:43 -1000 Subject: [PATCH 10/23] documentation --- src/schema.jl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/schema.jl b/src/schema.jl index 9505c14..a96f2ba 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -6,24 +6,39 @@ mutable struct XMLSchema isvalid::Bool end +""" +Create an XMLSchema from a C library schema context pointer +""" function XMLSchema(context::Xptr) schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), context) schema != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) return XMLSchema(schema, true) end +""" +Create an XMLSchema from a file or url +""" function XMLSchema(url::String) ctxt = ccall((:xmlSchemaNewParserCtxt, libxml2), Xptr, (Cstring,), url) ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema at " * url)) return XMLSchema(ctxt) end +""" +Create an XMLSchema from an XMLDocument +""" function XMLSchema(doc::XMLDocument) ctxt = ccall((:xmlSchemaNewDocParserCtxt, libxml2), Xptr, (Xptr,), doc.ptr) ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) return XMLSchema(ctxt) end +""" +Validate an XMLDocument with an XMLSchema +Returns true if valid + NB: There might be a memory leak from the ctxt pointer here, but calling the libxml2 free context function + to free ctxt causes a segfault in the library, so hopefully this is freed elsewhere by the C library +""" function validate(xml::XMLDocument, schema::XMLSchema) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateDoc, libxml2), @@ -31,6 +46,10 @@ function validate(xml::XMLDocument, schema::XMLSchema) return err == 0 ? true : false end +""" +Validate an XML file or url with an XMLSchema +Returns true if valid +""" function validate(url::String, schema::XMLSchema) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateFile, libxml2), @@ -38,6 +57,10 @@ function validate(url::String, schema::XMLSchema) return err == 0 ? true : false end +""" +Validate an XMLElement of an XMLDocument with an XMLSchema +Returns true if valid +""" function validate(elem::XMLElement, schema::XMLSchema) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateOneElement, libxml2), @@ -45,6 +68,10 @@ function validate(elem::XMLElement, schema::XMLSchema) return err == 0 ? true : false end +""" +Validate an XML file or url with an XSD file or url +Returns true if valid +""" function validate(url::String, schemafile::String) ctxt = ccall((:xmlSchemaNewParserCtxt, libxml2), Xptr, (Cstring,), schemafile) ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema at " * schemafile)) From beed84157108dc6ef479e9ece0a71d7fa23aab99 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 18 Sep 2020 12:59:10 -1000 Subject: [PATCH 11/23] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 325729b..f975f0b 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,7 @@ isvalid = validate(doc, schema) # validate a LightXML XML Document against isvalid = validate(url, schema) # validate a URI or file against an XSD Schema document isvalid = validate(element, schema) # validate a LightXML XML Node (a subtree) against an XSD Schema document -isvalid = validate(xmlfile, schemafile) # validate an XML file or URL against a XSD schem file or URL -''' +isvalid = validate(xmlfile, schemafile) # validate an XML file or URL against a XSD schem file or URL +``` + From 2ebfab87d3d3deaea0ac1f2a9ec731348452eb57 Mon Sep 17 00:00:00 2001 From: Bill Date: Thu, 24 Sep 2020 19:30:17 -1000 Subject: [PATCH 12/23] use Libc.free to free ctxt --- src/schema.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/schema.jl b/src/schema.jl index a96f2ba..392c083 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -36,13 +36,12 @@ end """ Validate an XMLDocument with an XMLSchema Returns true if valid - NB: There might be a memory leak from the ctxt pointer here, but calling the libxml2 free context function - to free ctxt causes a segfault in the library, so hopefully this is freed elsewhere by the C library """ function validate(xml::XMLDocument, schema::XMLSchema) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateDoc, libxml2), Cint, (Xptr, Xptr), ctxt, xml.ptr) + Libc.free(ctxt) return err == 0 ? true : false end @@ -54,6 +53,7 @@ function validate(url::String, schema::XMLSchema) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateFile, libxml2), Cint, (Xptr, Cstring), ctxt, url) + Libc.free(ctxt) return err == 0 ? true : false end @@ -65,7 +65,8 @@ function validate(elem::XMLElement, schema::XMLSchema) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) err = ccall((:xmlSchemaValidateOneElement, libxml2), Cint, (Xptr, Xptr), ctxt, elem.node.ptr) - return err == 0 ? true : false + Libc.free(ctxt) + return err == 0 ? true : false end """ @@ -81,5 +82,6 @@ function validate(url::String, schemafile::String) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) err = ccall((:xmlSchemaValidateFile, libxml2), Cint, (Ptr{LightXML.xmlBuffer}, Cstring), ctxt, url) + Libc.free(ctxt) return err == 0 ? true : false end From c8ed390a3f0fe5d79ce209a2e5934054acc31131 Mon Sep 17 00:00:00 2001 From: Bill Date: Sun, 27 Sep 2020 22:02:46 -1000 Subject: [PATCH 13/23] change constructors, add a finalizer --- src/schema.jl | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/schema.jl b/src/schema.jl index 392c083..2fa9dfb 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -1,18 +1,14 @@ """ -An XML Schema Document, produced by an XML document that is XML for the schema. +An XML Schema Document, produced by an XML file or XMLDocument that is XML for the schema. """ mutable struct XMLSchema ptr::Xptr - isvalid::Bool -end - -""" -Create an XMLSchema from a C library schema context pointer -""" -function XMLSchema(context::Xptr) - schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), context) - schema != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) - return XMLSchema(schema, true) + function XMLSchema(ctxt::Xptr) + schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), ctxt) + schema != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) + obj = new(schema) + finalizer((x) -> Libc.free(x.ptr), obj) + end end """ From 7163c24863b40dad66203f8c746d470de53cbd0d Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 30 Sep 2020 00:44:17 -1000 Subject: [PATCH 14/23] update XMLSchema constructor --- src/schema.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/schema.jl b/src/schema.jl index 2fa9dfb..9429e78 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -6,6 +6,7 @@ mutable struct XMLSchema function XMLSchema(ctxt::Xptr) schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), ctxt) schema != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) + ccall((:xmlSchemaFreeParserCtxt, libxml2), Cvoid, (Xptr,), ctxt) obj = new(schema) finalizer((x) -> Libc.free(x.ptr), obj) end From 360dca2de7e353609f8c26fa4bb7e7d39311b5ab Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 30 Sep 2020 07:37:48 -1000 Subject: [PATCH 15/23] code review suggestions --- src/schema.jl | 54 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/schema.jl b/src/schema.jl index 9429e78..16e50ca 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -8,7 +8,7 @@ mutable struct XMLSchema schema != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) ccall((:xmlSchemaFreeParserCtxt, libxml2), Cvoid, (Xptr,), ctxt) obj = new(schema) - finalizer((x) -> Libc.free(x.ptr), obj) + finalizer(x -> Libc.free(x.ptr), obj) end end @@ -30,16 +30,30 @@ function XMLSchema(doc::XMLDocument) return XMLSchema(ctxt) end +""" +Use an existing XMLschema to validate +""" +function _schema_valid_ctxt(f::Function, schema::XMLSchema) + ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) + local err + err = try + f(ctxt) + finally + Libc.free(ctxt) + end + return err +end + """ Validate an XMLDocument with an XMLSchema Returns true if valid """ function validate(xml::XMLDocument, schema::XMLSchema) - ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) - err = ccall((:xmlSchemaValidateDoc, libxml2), - Cint, (Xptr, Xptr), ctxt, xml.ptr) - Libc.free(ctxt) - return err == 0 ? true : false + err = _schema_valid_ctxt(schema) do ctxt + ccall((:xmlSchemaValidateDoc, libxml2), + Cint, (Xptr, Xptr), ctxt, xml.ptr) + end + return err == 0 end """ @@ -47,11 +61,11 @@ Validate an XML file or url with an XMLSchema Returns true if valid """ function validate(url::String, schema::XMLSchema) - ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) - err = ccall((:xmlSchemaValidateFile, libxml2), - Cint, (Xptr, Cstring), ctxt, url) - Libc.free(ctxt) - return err == 0 ? true : false + err = _schema_valid_ctxt(schema) do ctxt + ccall((:xmlSchemaValidateFile, libxml2), + Cint, (Xptr, Cstring), ctxt, url) + end + return err == 0 end """ @@ -59,11 +73,11 @@ Validate an XMLElement of an XMLDocument with an XMLSchema Returns true if valid """ function validate(elem::XMLElement, schema::XMLSchema) - ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) - err = ccall((:xmlSchemaValidateOneElement, libxml2), - Cint, (Xptr, Xptr), ctxt, elem.node.ptr) - Libc.free(ctxt) - return err == 0 ? true : false + err = _schema_valid_ctxt(schema) do ctxt + ccall((:xmlSchemaValidateOneElement, libxml2), + Cint, (Xptr, Xptr), ctxt, elem.node.ptr) + end + return err == 0 end """ @@ -74,11 +88,11 @@ function validate(url::String, schemafile::String) ctxt = ccall((:xmlSchemaNewParserCtxt, libxml2), Xptr, (Cstring,), schemafile) ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema at " * schemafile)) schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), ctxt) - schema != C_NULL || throw(XMLValidationError("Bad XML Schema at " * url)) ccall((:xmlSchemaFreeParserCtxt, libxml2), Cvoid, (Xptr,), ctxt) + schema != C_NULL || throw(XMLValidationError("Bad XML Schema at " * url)) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) - err = ccall((:xmlSchemaValidateFile, libxml2), + Libc.free(schema) + err = ccall((:xmlSchemaValidateFile, libxml2), Cint, (Ptr{LightXML.xmlBuffer}, Cstring), ctxt, url) - Libc.free(ctxt) - return err == 0 ? true : false + return err == 0 end From 06c55cd18d35574c66356d9b6f77390af3b3fbe9 Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 30 Sep 2020 07:41:41 -1000 Subject: [PATCH 16/23] code review suggested change --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index f975f0b..e72acc1 100644 --- a/README.md +++ b/README.md @@ -325,7 +325,6 @@ isvalid = validate(xmlfile, schema) # validate an XML file against a previously isvalid = validate(doc, schema) # validate a LightXML XML Document against a previously loaded XSD schema isvalid = validate(url, schema) # validate a URI or file against an XSD Schema document isvalid = validate(element, schema) # validate a LightXML XML Node (a subtree) against an XSD Schema document - isvalid = validate(xmlfile, schemafile) # validate an XML file or URL against a XSD schem file or URL ``` From cfa8c5c732574e5284a63658e098ca45266ea8d2 Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 30 Sep 2020 08:59:18 -1000 Subject: [PATCH 17/23] put tests into a testset --- test/validate.jl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/test/validate.jl b/test/validate.jl index c48723d..6980a98 100644 --- a/test/validate.jl +++ b/test/validate.jl @@ -1,13 +1,14 @@ -@test validate("valid.xml", "valid.xsd") == true +@testset "XML Validation with XSD" begin + + @test validate("valid.xml", "valid.xsd") -@test validate("valid.xml", "valid.xsd") == true + doc = parse_file("valid.xml") + schema = XMLSchema("valid.xsd") -doc = parse_file("valid.xml") -schema = XMLSchema("valid.xsd") + @test validate("valid.xml", schema) -@test validate("valid.xml", schema) == true + @test validate(doc, schema) -@test validate(doc, schema) == true - -@test validate(root(doc), schema) == true + @test validate(root(doc), schema) +end From 9c3ca91416d06608363aa20e777b42c96c672c95 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 30 Sep 2020 22:51:54 -0500 Subject: [PATCH 18/23] Update src/schema.jl --- src/schema.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/schema.jl b/src/schema.jl index 16e50ca..82d9102 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -35,7 +35,6 @@ Use an existing XMLschema to validate """ function _schema_valid_ctxt(f::Function, schema::XMLSchema) ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) - local err err = try f(ctxt) finally From 44bb208167f2da08bcd285f3f3bf2b4d4c9fa98a Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 30 Sep 2020 20:26:20 -1000 Subject: [PATCH 19/23] recheck a failing test --- test/validate.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/validate.jl b/test/validate.jl index 6980a98..8fc597e 100644 --- a/test/validate.jl +++ b/test/validate.jl @@ -1,3 +1,7 @@ + +# test this one inside and outide the testset +@test validate("valid.xml", "valid.xsd") + @testset "XML Validation with XSD" begin @test validate("valid.xml", "valid.xsd") From b80f02a5ab2f0688cf2dfe1d4ec5089f457d4fcb Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 30 Sep 2020 20:29:08 -1000 Subject: [PATCH 20/23] Create invalid.xml --- test/invalid.xml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/invalid.xml diff --git a/test/invalid.xml b/test/invalid.xml new file mode 100644 index 0000000..57970dc --- /dev/null +++ b/test/invalid.xml @@ -0,0 +1,24 @@ + + + + John Smith + + Ola Nordmann +
Langgt 23
+ 4000 Stavanger + Norway +
+ + Empire Burlesque, Gold Ribbon + Special Edition + 1 + 10.90 + + + Hide your heart + 1 + 9.90 + +
From 7d3d68de2ca1667819583ceb647a6b6619b7a96d Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 30 Sep 2020 20:31:08 -1000 Subject: [PATCH 21/23] add tests --- test/validate.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/validate.jl b/test/validate.jl index 8fc597e..61fd835 100644 --- a/test/validate.jl +++ b/test/validate.jl @@ -1,15 +1,18 @@ # test this one inside and outide the testset @test validate("valid.xml", "valid.xsd") +@test validate("invalid.xml", "valid.xsd") == false @testset "XML Validation with XSD" begin @test validate("valid.xml", "valid.xsd") + @test validate("invalid.xml", "valid.xsd") == false doc = parse_file("valid.xml") schema = XMLSchema("valid.xsd") @test validate("valid.xml", schema) + @test validate("invalid.xml", schema) == false @test validate(doc, schema) From f8dda7b58642199d2ad3ef0dbfa68fb673d7bcf4 Mon Sep 17 00:00:00 2001 From: Bill Date: Thu, 1 Oct 2020 22:35:55 -1000 Subject: [PATCH 22/23] change two-file validation method --- src/schema.jl | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/schema.jl b/src/schema.jl index 82d9102..2bda3d6 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -84,14 +84,6 @@ Validate an XML file or url with an XSD file or url Returns true if valid """ function validate(url::String, schemafile::String) - ctxt = ccall((:xmlSchemaNewParserCtxt, libxml2), Xptr, (Cstring,), schemafile) - ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema at " * schemafile)) - schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), ctxt) - ccall((:xmlSchemaFreeParserCtxt, libxml2), Cvoid, (Xptr,), ctxt) - schema != C_NULL || throw(XMLValidationError("Bad XML Schema at " * url)) - ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema) - Libc.free(schema) - err = ccall((:xmlSchemaValidateFile, libxml2), - Cint, (Ptr{LightXML.xmlBuffer}, Cstring), ctxt, url) - return err == 0 + schema = XMLSchema(schemafile) + return validate(url, schema) end From 32dce3f0fc1b18e05ea06050dc9e284e60ceece1 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 2 Oct 2020 07:56:45 -1000 Subject: [PATCH 23/23] clean up testset --- test/validate.jl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/validate.jl b/test/validate.jl index 61fd835..55d56c6 100644 --- a/test/validate.jl +++ b/test/validate.jl @@ -1,8 +1,3 @@ - -# test this one inside and outide the testset -@test validate("valid.xml", "valid.xsd") -@test validate("invalid.xml", "valid.xsd") == false - @testset "XML Validation with XSD" begin @test validate("valid.xml", "valid.xsd")