Skip to content

Commit

Permalink
feat(Common): introduce an XML driver
Browse files Browse the repository at this point in the history
  • Loading branch information
tecosaur committed Oct 21, 2024
1 parent 3a184f9 commit 1e444f8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Common/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Tar = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
TiffImages = "731e570b-9d59-4bfa-96dc-6df516fadf69"
WebP = "e3aaa7dc-3e4b-44e0-be63-ffb868ccd7c1"
XLSX = "fdbf4ff8-1666-58a4-91e7-1b58723a45e0"
XML = "72c71f33-b9b6-44de-8c94-c961784809e2"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"

Expand Down Expand Up @@ -74,6 +75,7 @@ TarExt = "Tar"
TiffImagesExt = "TiffImages"
WebPExt = "WebP"
XLSXExt = "XLSX"
XMLExt = "XML"
YAMLExt = "YAML"
ZipFileExt = "ZipFile"

Expand Down
1 change: 1 addition & 0 deletions Common/docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const DocSaveload = [
"Tiff",
"Webp",
"XLSX",
"XML",
"Zip",
]

Expand Down
14 changes: 14 additions & 0 deletions Common/ext/XMLExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module XMLExt

using XML
import DataToolkitCommon: _read_xml, _write_xml

function _read_xml(from::IO, as::Union{Type{XML.Node}, Type{XML.LazyNode}})
read(from, as)
end

function _write_xml(dest::IO, data::XML.Node)
write(dest, data)
end

end
4 changes: 4 additions & 0 deletions Common/src/DataToolkitCommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ include("transformers/saveload/tar.jl")
include("transformers/saveload/tiff.jl")
include("transformers/saveload/toml.jl")
include("transformers/saveload/xlsx.jl")
include("transformers/saveload/xml.jl")
include("transformers/saveload/webp.jl")
include("transformers/saveload/yaml.jl")
include("transformers/saveload/zip.jl")
Expand Down Expand Up @@ -88,6 +89,7 @@ function __init__()
@addpkg TOML "fa267f1f-6049-4f14-aa54-33bafae1ed76"
@addpkg WebP "e3aaa7dc-3e4b-44e0-be63-ffb868ccd7c1"
@addpkg XLSX "fdbf4ff8-1666-58a4-91e7-1b58723a45e0"
@addpkg XML "72c71f33-b9b6-44de-8c94-c961784809e2"
@addpkg YAML "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
@addpkg ZipFile "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
# Plugins
Expand Down Expand Up @@ -135,6 +137,7 @@ function __init__()
(:loader, :toml) => TOML_DOC,
(:loader, :webp) => WEBP_DOC,
(:loader, :xlsx) => XLSX_DOC,
(:loader, :xlsx) => XML_DOC,
(:loader, :yaml) => YAML_DOC,
(:loader, :zip) => ZIP_DOC,
(:writer, :compressed) => COMPRESSION_DOC,
Expand All @@ -156,6 +159,7 @@ function __init__()
(:writer, :serialization) => SERIALIZATION_DOC,
(:writer, :sqlite) => SQLITE_DOC,
(:writer, :tiff) => TIFF_DOC,
(:loader, :xlsx) => XML_DOC,
(:writer, :webp) => WEBP_DOC,
])
end
Expand Down
44 changes: 44 additions & 0 deletions Common/src/transformers/saveload/xml.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function _read_xml end # Implemented in `../../../ext/XMLExt.jl`
function _write_xml end # Implemented in `../../../ext/XMLExt.jl`

function load(loader::DataLoader{:xml}, from::IO, as::Type)
@require XML
QualifiedType(as) (QualifiedType(:XML, :Node), QualifiedType(:XML, :LazyNode)) || return
invokelatest(_read_xml, from, as)
end

supportedtypes(::Type{DataLoader{:xml}}) =
[QualifiedType(:XML, :LazyNode), QualifiedType(:XML, :Node)]

function save(writer::DataWriter{:xml}, dest::IO, data)
@require XML
QualifiedType(typeof(data)) == QualifiedType(:XML, :Node) || return
invokelatest(_write_xml, dest, data)
end

function createauto(::Type{DataLoader{:xml}}, source::String)
!isnothing(match(r"\.xml$"i, source))
end

const XML_DOC = md"""
Load and write XML data.
# Input/output
The `xml` driver expects data to be provided via an `IO` stream.
This driver supports parsing to two data types:
- `XML.LazyNode`
- `XML.Node`
# Required packages
- `XML`
# Usage examples
```toml
[[sample.loader]]
driver = "xml"
```
"""

0 comments on commit 1e444f8

Please sign in to comment.