Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better show #125

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ authors = ["Kenta Sato <[email protected]>"]
version = "1.1.0"

[deps]
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
XML2_jll = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a"

[compat]
Expand Down
92 changes: 46 additions & 46 deletions docs/src/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ constructor:
julia> using EzXML

julia> doc = XMLDocument()
EzXML.Document(EzXML.Node(<DOCUMENT_NODE@0x00007fd9f1f14370>))
EzXML.Document(Node(<DOCUMENT_NODE>))

julia> typeof(doc)
EzXML.Document

julia> doc.node
EzXML.Node(<DOCUMENT_NODE@0x00007fd9f1f14370>)
Node(<DOCUMENT_NODE>)

julia> typeof(doc.node)
EzXML.Node
Expand All @@ -72,20 +72,20 @@ The text just before the `@` sign shows the node type (in this example,
Let's add a root node to the document and a text node to the root node:
```jldoctest doc
julia> elm = ElementNode("root") # create an element node
EzXML.Node(<ELEMENT_NODE[root]@0x00007fd9f2a1b5f0>)
Node(<ELEMENT_NODE[root]>)

julia> setroot!(doc, elm)
EzXML.Node(<ELEMENT_NODE[root]@0x00007fd9f2a1b5f0>)
Node(<ELEMENT_NODE[root]>)

julia> print(doc)
<?xml version="1.0" encoding="UTF-8"?>
<root/>

julia> txt = TextNode("some text") # create a text node
EzXML.Node(<TEXT_NODE@0x00007fd9f2a81ee0>)
Node(<TEXT_NODE>)

julia> link!(elm, txt)
EzXML.Node(<TEXT_NODE@0x00007fd9f2a81ee0>)
Node(<TEXT_NODE>)

julia> print(doc)
<?xml version="1.0" encoding="UTF-8"?>
Expand All @@ -111,7 +111,7 @@ The value of a property will be `nothing` when there is no corresponding value.

```jldoctest
julia> elm = ElementNode("element")
EzXML.Node(<ELEMENT_NODE[element]@0x00007fd9f44122f0>)
Node(<ELEMENT_NODE[element]>)

julia> println(elm)
<element/>
Expand Down Expand Up @@ -144,7 +144,7 @@ julia> println(elm)
<ELEMENT>some text</ELEMENT>

julia> txt = TextNode(" text ")
EzXML.Node(<TEXT_NODE@0x00007fd9f441f3f0>)
Node(<TEXT_NODE>)

julia> println(txt)
text
Expand All @@ -167,19 +167,19 @@ julia> txt.content
child element to an existing node:
```jldoctest
julia> user = ElementNode("User")
EzXML.Node(<ELEMENT_NODE[User]@0x00007fd9f427c510>)
Node(<ELEMENT_NODE[User]>)

julia> println(user)
<User/>

julia> addelement!(user, "id", "167492")
EzXML.Node(<ELEMENT_NODE[id]@0x00007fd9f41ad580>)
Node(<ELEMENT_NODE[id]>)

julia> println(user)
<User><id>167492</id></User>

julia> addelement!(user, "name", "Kumiko Oumae")
EzXML.Node(<ELEMENT_NODE[name]@0x00007fd9f42942d0>)
Node(<ELEMENT_NODE[name]>)

julia> println(user)
<User><id>167492</id><name>Kumiko Oumae</name></User>
Expand Down Expand Up @@ -218,25 +218,25 @@ Likewise, `parsexml(<string or byte array>)` parses an XML string or a byte
array in memory and builds a document object:
```jldoctest dom
julia> doc = readxml("primates.xml")
EzXML.Document(EzXML.Node(<DOCUMENT_NODE@0x00007fd9f410a5f0>))
EzXML.Document(Node(<DOCUMENT_NODE>))

julia> data = String(read("primates.xml"));

julia> doc = parsexml(data)
EzXML.Document(EzXML.Node(<DOCUMENT_NODE@0x00007fd9f4051f80>))
EzXML.Document(Node(<DOCUMENT_NODE>))

```

Before traversing a document we need to get the root of the document tree.
The `.root` property returns the root element (if any) of a document:
```jldoctest dom
julia> primates = doc.root # get the root element
EzXML.Node(<ELEMENT_NODE[primates]@0x00007fd9f4086880>)
Node(<ELEMENT_NODE[primates]>)

julia> genus = elements(primates) # `elements` returns all child elements.
2-element Array{EzXML.Node,1}:
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f4041a40>)
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f40828e0>)
Node(<ELEMENT_NODE[genus]>)
Node(<ELEMENT_NODE[genus]>)

julia> genus[1].type, genus[1].name
(ELEMENT_NODE, "genus")
Expand Down Expand Up @@ -294,19 +294,19 @@ indicate a node type the property (function) is interested in. For example,
but `.firstelement` always returns the first element node (if any):
```jldoctest dom
julia> primates.firstnode
EzXML.Node(<TEXT_NODE@0x00007fd9f409f200>)
Node(<TEXT_NODE>)

julia> primates.firstelement
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f4041a40>)
Node(<ELEMENT_NODE[genus]>)

julia> primates.firstelement == genus[1]
true

julia> primates.lastnode
EzXML.Node(<TEXT_NODE@0x00007fd9f404bec0>)
Node(<TEXT_NODE>)

julia> primates.lastelement
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f40828e0>)
Node(<ELEMENT_NODE[genus]>)

julia> primates.lastelement === genus[2]
true
Expand Down Expand Up @@ -350,30 +350,30 @@ and elements, respectively:
julia> for node in eachnode(primates)
@show node
end
node = EzXML.Node(<TEXT_NODE@0x00007fd9f409f200>)
node = EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f4041a40>)
node = EzXML.Node(<TEXT_NODE@0x00007fd9f4060f70>)
node = EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f40828e0>)
node = EzXML.Node(<TEXT_NODE@0x00007fd9f404bec0>)
node = Node(<TEXT_NODE>)
node = Node(<ELEMENT_NODE[genus]>)
node = Node(<TEXT_NODE>)
node = Node(<ELEMENT_NODE[genus]>)
node = Node(<TEXT_NODE>)

julia> for node in eachelement(primates)
@show node
end
node = EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f4041a40>)
node = EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f40828e0>)
node = Node(<ELEMENT_NODE[genus]>)
node = Node(<ELEMENT_NODE[genus]>)

julia> nodes(primates)
5-element Array{EzXML.Node,1}:
EzXML.Node(<TEXT_NODE@0x00007fd9f409f200>)
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f4041a40>)
EzXML.Node(<TEXT_NODE@0x00007fd9f4060f70>)
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f40828e0>)
EzXML.Node(<TEXT_NODE@0x00007fd9f404bec0>)
Node(<TEXT_NODE>)
Node(<ELEMENT_NODE[genus]>)
Node(<TEXT_NODE>)
Node(<ELEMENT_NODE[genus]>)
Node(<TEXT_NODE>)

julia> elements(primates)
2-element Array{EzXML.Node,1}:
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f4041a40>)
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fd9f40828e0>)
Node(<ELEMENT_NODE[genus]>)
Node(<ELEMENT_NODE[genus]>)

```

Expand All @@ -388,22 +388,22 @@ The `findall`, `findfirst` and `findlast` functions are overloaded for XPath
query and return a vector of selected nodes:
```jldoctest xpath
julia> primates = readxml("primates.xml")
EzXML.Document(EzXML.Node(<DOCUMENT_NODE@0x00007fbeddc2a1d0>))
EzXML.Document(Node(<DOCUMENT_NODE>))

julia> findall("/primates", primates) # Find the "primates" element just under the document
1-element Array{EzXML.Node,1}:
EzXML.Node(<ELEMENT_NODE[primates]@0x00007fbeddc1e190>)
Node(<ELEMENT_NODE[primates]>)

julia> findall("//genus", primates)
2-element Array{EzXML.Node,1}:
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fbeddc12c50>)
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fbeddc16ea0>)
Node(<ELEMENT_NODE[genus]>)
Node(<ELEMENT_NODE[genus]>)

julia> findfirst("//genus", primates)
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fbeddc12c50>)
Node(<ELEMENT_NODE[genus]>)

julia> findlast("//genus", primates)
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fbeddc16ea0>)
Node(<ELEMENT_NODE[genus]>)

julia> println(findfirst("//genus", primates))
<genus name="Homo">
Expand All @@ -416,7 +416,7 @@ If you would like to change the starting node of a query, you can pass the node
as the second argument of `find*`:
```jldoctest xpath
julia> genus = findfirst("//genus", primates)
EzXML.Node(<ELEMENT_NODE[genus]@0x00007fbeddc12c50>)
Node(<ELEMENT_NODE[genus]>)

julia> println(genus)
<genus name="Homo">
Expand Down Expand Up @@ -445,7 +445,7 @@ julia> doc = parsexml("""
<child/>
</parent>
""")
EzXML.Document(EzXML.Node(<DOCUMENT_NODE@0x00007fdc67710030>))
EzXML.Document(Node(<DOCUMENT_NODE>))

julia> findall("/parent/child", doc.root) # nothing will be found
0-element Array{EzXML.Node,1}
Expand All @@ -459,7 +459,7 @@ julia> ns = namespace(doc.root) # get the namespace

julia> findall("/x:parent/x:child", doc.root, ["x"=>ns]) # specify its prefix as "x"
1-element Array{EzXML.Node,1}:
EzXML.Node(<ELEMENT_NODE[child]@0x00007fdc6774c990>)
Node(<ELEMENT_NODE[child]>)

```

Expand Down Expand Up @@ -495,7 +495,7 @@ thing you needs to do is to create an `EzXML.StreamReader` object using the
`open` function:
```jldoctest stream
julia> reader = open(EzXML.StreamReader, "undirected.graphml")
EzXML.StreamReader(<READER_NONE@0x00007f9fe8d67340>)
StreamReader(<READER_NONE>)

```

Expand Down Expand Up @@ -548,7 +548,7 @@ available from the file. When you finished reading data, you need to call
`close(reader)` to release allocated resources:
```jldoctest
julia> reader = open(EzXML.StreamReader, "undirected.graphml")
EzXML.StreamReader(<READER_NONE@0x00007fd642e80d90>)
StreamReader(<READER_NONE>)

julia> while (item = iterate(reader)) != nothing
@show reader.type, reader.name
Expand Down Expand Up @@ -599,7 +599,7 @@ iterable via the `for` loop. Therefore, you can iterate over all components
without explicitly calling `iterate` as follows:
```jldoctest
julia> reader = open(EzXML.StreamReader, "undirected.graphml")
EzXML.StreamReader(<READER_NONE@0x00007fd642e9a6b0>)
StreamReader(<READER_NONE>)

julia> for typ in reader
@show typ, reader.name
Expand Down
1 change: 0 additions & 1 deletion src/EzXML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export
hasnodeattributes,
nodeattributes

using Printf: @printf
using XML2_jll: libxml2

include("error.jl")
Expand Down
6 changes: 6 additions & 0 deletions src/document.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ function HTMLDocument(uri=nothing, externalID=nothing)
return Document(node.ptr)
end

function Base.show(io::IO, doc::Document)
prefix = "EzXML.Document"
node = repr(doc.node)
print(io, "$(prefix)($node)")
end

function Base.print(io::IO, doc::Document)
print(io, doc.node)
end
Expand Down
4 changes: 2 additions & 2 deletions src/node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ function ismanaged(node::Node)
end

function Base.show(io::IO, node::Node)
prefix = isdefined(Main, :Node) ? "Node" : "EzXML.Node"
prefix = "Node"
ntype = nodetype(node)
if ntype ∈ (ELEMENT_NODE, ATTRIBUTE_NODE) && hasnodename(node)
desc = string(repr(ntype), '[', nodename(node), ']')
else
desc = repr(ntype)
end
@printf(io, "%s(<%s@%p>)", prefix, desc, node.ptr)
print(io, "$(prefix)(<$(desc)>)")
end

function Base.print(io::IO, node::Node)
Expand Down
5 changes: 3 additions & 2 deletions src/streamreader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ end
end

function Base.show(io::IO, reader::StreamReader)
prefix = isdefined(Main, :StreamReader) ? "StreamReader" : "EzXML.StreamReader"
@printf(io, "%s(<%s@%p>)", prefix, repr(nodetype(reader)), reader.ptr)
prefix = "StreamReader"
nodetype_reader = repr(nodetype(reader))
print(io, "$(prefix)(<$(nodetype_reader)>)")
end

# Reader type (enum xmlReaderTypes).
Expand Down
10 changes: 5 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1301,14 +1301,14 @@ end
@testset "Misc" begin
@testset "show" begin
doc = parsexml("""<root attr="val"/>""")
@test occursin(r"^EzXML.Node\(<DOCUMENT_NODE@0x[a-f0-9]+>\)$", repr(doc.node))
@test occursin(r"^EzXML.Node\(<ELEMENT_NODE\[root\]@0x[a-f0-9]+>\)$", repr(root(doc)))
@test occursin(r"^EzXML.Node\(<ATTRIBUTE_NODE\[attr\]@0x[a-f0-9]+>\)$", repr(attributes(root(doc))[1]))
@test occursin(r"^EzXML.Document\(EzXML.Node\(<DOCUMENT_NODE@0x[a-f0-9]+>\)\)$", repr(doc))
@test occursin(r"^Node\(<DOCUMENT_NODE>\)$", repr(doc.node))
@test occursin(r"^Node\(<ELEMENT_NODE\[root\]>\)$", repr(root(doc)))
@test occursin(r"^Node\(<ATTRIBUTE_NODE\[attr\]>\)$", repr(attributes(root(doc))[1]))
@test occursin(r"^EzXML.Document\(Node\(<DOCUMENT_NODE>\)\)$", repr(doc))

sample2 = joinpath(dirname(@__FILE__), "sample2.xml")
reader = open(EzXML.StreamReader, sample2)
@test occursin(r"^EzXML.StreamReader\(<[A-Z_]+@0x[a-f0-9]+>\)$", repr(reader))
@test occursin(r"^StreamReader\(<[A-Z_]+>\)$", repr(reader))
close(reader)
end

Expand Down