1
1
module XML
2
2
3
- using OrderedCollections: OrderedDict
4
3
using Mmap
5
4
using Tables
6
- import AbstractTrees: AbstractTrees, children, parent
7
5
8
6
export Node, RowNode, Children,
9
7
children, parent, nodetype, tag, attributes, value, depth, next, prev
@@ -82,7 +80,7 @@ Useful functions:
82
80
- next(o::RawData) --> RawData of the next chunk (or `nothing`).
83
81
- prev(o::RawData) --> RawData of the previous chunk (or `nothing`).
84
82
- tag(o::RawData) --> String of the tag name (or `nothing`).
85
- - attributes(o::RawData) --> OrderedDict {String, String} of the attributes (or `nothing`).
83
+ - attributes(o::RawData) --> Dict {String, String} of the attributes (or `nothing`).
86
84
- value(o::RawData) --> String of the value (or `nothing`).
87
85
- children(o::RawData) --> Vector{RawData} of the children (or `nothing`).
88
86
- parent(o::RawData) --> RawData of the parent (or `nothing`)
@@ -100,6 +98,9 @@ RawData(filename::String) = RawData(Mmap.mmap(filename))
100
98
101
99
parse (x:: AbstractString , :: Type{RawData} ) = RawData (Vector {UInt8} (x))
102
100
101
+ # Mostly for debugging
102
+ Base. peek (o:: RawData , n:: Int ) = String (@view (o. data[o. pos + o. len + 1 : min (end , o. pos + o. len + n + 1 )]))
103
+
103
104
Tables. rows (o:: RawData ) = o
104
105
Tables. schema (o:: RawData ) = Tables. Schema (fieldnames (RawData)[1 : end - 1 ], fieldtypes (RawData)[1 : end - 1 ])
105
106
143
144
function get_attributes (data, i, j)
144
145
i = name_start (data, i)
145
146
i > j && return nothing
146
- out = OrderedDict {String, String} ()
147
+ out = Dict {String, String} ()
147
148
while ! isnothing (i) && i < j
148
149
key, i = get_name (data, i)
149
150
# get quotechar the value is wrapped in (either ' or ")
@@ -177,7 +178,7 @@ function tag(o::RawData)
177
178
end
178
179
179
180
"""
180
- attributes(node) --> OrderedDict {String, String} or Nothing
181
+ attributes(node) --> Dict {String, String} or Nothing
181
182
182
183
Return the attributes of `ELEMENT`, `DECLARATION`, or `PROCESSING_INSTRUCTION` nodes.
183
184
"""
@@ -387,7 +388,7 @@ a tabular dataset, e.g.
387
388
struct RowNode
388
389
nodetype:: NodeType
389
390
tag:: Union{String, Nothing}
390
- attributes:: Union{OrderedDict {String, String}, Nothing}
391
+ attributes:: Union{Dict {String, String}, Nothing}
391
392
value:: Union{String, Nothing}
392
393
data:: RawData
393
394
end
@@ -416,7 +417,7 @@ Base.propertynames(o::RowNode) = (:depth, :nodetype, :tag, :attributes, :value)
416
417
Tables. rows (o:: RowNode ) = o
417
418
Tables. schema (o:: RowNode ) = Tables. Schema (
418
419
(:depth , :nodetype , :tag , :attributes , :value ),
419
- (Int, NodeType, Union{Nothing, String}, Union{Nothing, OrderedDict {String, String}}, Union{Nothing, String}),
420
+ (Int, NodeType, Union{Nothing, String}, Union{Nothing, Dict {String, String}}, Union{Nothing, String}),
420
421
)
421
422
422
423
children (o:: RowNode ) = RowNode .(children (getfield (o, :data )))
456
457
struct FastNode
457
458
nodetype:: NodeType
458
459
tag:: Union{Nothing, String}
459
- attributes:: Union{Nothing, OrderedDict {String, String}}
460
+ attributes:: Union{Nothing, Dict {String, String}}
460
461
value:: Union{Nothing, String}
461
462
children:: Union{Nothing, Vector{FastNode}}
462
463
depth:: Int
@@ -483,7 +484,7 @@ Base.lastindex(o::FastNode) = length(o.children)
483
484
Base. @kwdef struct Node
484
485
nodetype:: NodeType
485
486
tag:: Union{Nothing, String} = nothing
486
- attributes:: Union{Nothing, OrderedDict {String, String}} = nothing
487
+ attributes:: Union{Nothing, Dict {String, String}} = nothing
487
488
value:: Union{Nothing, String} = nothing
488
489
children:: Union{Nothing, Vector{Node}} = nothing
489
490
depth:: Int = - 1
@@ -544,12 +545,12 @@ _node(x::Node; depth=x.depth) = Node(x; depth)
544
545
545
546
module NodeConstructors
546
547
import .. _node
547
- import .. Node, .. OrderedDict
548
+ import .. Node, .. Dict
548
549
import .. TEXT, .. DOCUMENT, .. DTD, .. DECLARATION, .. PROCESSING_INSTRUCTION, .. COMMENT, .. CDATA, .. ELEMENT
549
550
550
551
export document, dtd, declaration, processing_instruction, comment, cdata, text, element
551
552
552
- attrs (kw) = OrderedDict {String,String} (string (k) => string (v) for (k,v) in kw)
553
+ attrs (kw) = Dict {String,String} (string (k) => string (v) for (k,v) in kw)
553
554
554
555
"""
555
556
document(children::Vector{Node})
@@ -566,13 +567,13 @@ dtd(value::AbstractString) = Node(nodetype=DTD, value=String(value))
566
567
"""
567
568
declaration(; attributes...)
568
569
"""
569
- declaration (attributes:: OrderedDict {String,String} ) = Node (;nodetype= DECLARATION, attributes)
570
+ declaration (attributes:: Dict {String,String} ) = Node (;nodetype= DECLARATION, attributes)
570
571
declaration (; kw... ) = declaration (attrs (kw))
571
572
572
573
"""
573
574
processing_instruction(tag::AbstractString; attributes...)
574
575
"""
575
- processing_instruction (tag, attributes:: OrderedDict {String,String} ) = Node (;nodetype= PROCESSING_INSTRUCTION, tag= string (tag), attributes)
576
+ processing_instruction (tag, attributes:: Dict {String,String} ) = Node (;nodetype= PROCESSING_INSTRUCTION, tag= string (tag), attributes)
576
577
processing_instruction (tag; kw... ) = processing_instruction (tag, attrs (kw))
577
578
578
579
"""
@@ -786,7 +787,7 @@ function write(io::IO, x; indent = " ")
786
787
elseif nodetype === CDATA
787
788
print (io, " <![CDATA[" , value, " ]]>" )
788
789
elseif nodetype === DOCUMENT
789
- foreach (AbstractTrees . children (x) ) do child
790
+ foreach (children) do child
790
791
write (io, child; indent)
791
792
println (io)
792
793
end
0 commit comments