Skip to content

Commit e43ae9b

Browse files
committed
Implementation of show methods.
1 parent d9567b6 commit e43ae9b

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

src/CosObject.jl

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Base:get,length
1+
import Base:get, length, show
22

33
export CosDict, CosString, CosNumeric, CosBoolean, CosTrue, CosFalse,
44
CosObject, CosNull, CosNullType,CosFloat, CosInt, CosArray, CosName,
@@ -30,6 +30,10 @@ end
3030
val::Int
3131
end
3232

33+
show(io::IO, o::CosObject) = print(io, o.val)
34+
35+
show(io::IO, o::CosNullType) = print(io, "null")
36+
3337
"""
3438
A parsed data structure to ensure the object information is stored as an object.
3539
This has no meaning without a associated CosDoc. When a reference object is hit
@@ -40,27 +44,51 @@ the object should be searched from the CosDoc and returned.
4044
CosIndirectObjectRef(num::Int, gen::Int)=new((num,gen))
4145
end
4246

47+
show(io::IO, o::CosIndirectObjectRef) = @printf io "%d %d R" o.val[1] o.val[2]
48+
4349
#hash(o::CosIndirectObjectRef, h::UInt=zero(UInt)) = hash(o.val, h)
4450
#isequal(r1::CosIndirectObjectRef, r2::CosIndirectObjectRef) = isequal(r1.val, r2.val)
4551

4652
@compat mutable struct CosIndirectObject{T <: CosObject} <: CosObject
47-
num::Int
48-
gen::Int
49-
obj::T
53+
num::Int
54+
gen::Int
55+
obj::T
5056
end
5157

5258
get(o::CosIndirectObject) = get(o.obj)
5359

60+
showref(io::IO, o::CosIndirectObject) = @printf io "%d %d R" o.num o.gen
61+
62+
showref(io::IO, o::CosObject)=show(io, o)
63+
64+
function show(io::IO, o::CosIndirectObject)
65+
str = @sprintf "%d %d obj\n" o.num o.gen
66+
print(io, str)
67+
show(io, o.obj)
68+
end
69+
5470
@compat struct CosName <: CosObject
5571
val::Symbol
5672
CosName(str::String)=new(Symbol("CosName_",str))
5773
end
5874

75+
function show(io::IO, o::CosName)
76+
val = String(o.val)
77+
val = split(val,'_')[2]
78+
str = @sprintf "/%s" val
79+
print(io, str)
80+
end
81+
5982
@compat struct CosXString <: CosString
6083
val::Vector{UInt8}
6184
CosXString(arr::Vector{UInt8})=new(arr)
6285
end
6386

87+
function show(io::IO, o::CosXString)
88+
str = @sprintf "%s" "<"*String(copy(o.val))*">"
89+
print(io, str)
90+
end
91+
6492
Base.convert(::Type{Vector{UInt8}}, xstr::CosXString)=
6593
(xstr.val |> String |> hex2bytes)
6694

@@ -75,6 +103,11 @@ end
75103

76104
CosLiteralString(str::AbstractString)=CosLiteralString(transcode(UInt8,str))
77105

106+
function show(io::IO, o::CosLiteralString)
107+
str = @sprintf "%s" "("*String(copy(o.val))*")"
108+
print(io, str)
109+
end
110+
78111
Base.convert(::Type{Vector{UInt8}}, str::CosLiteralString)=copy(str.val)
79112

80113
Base.convert(::Type{String}, str::CosLiteralString)=
@@ -92,6 +125,14 @@ Base.convert(::Type{String}, str::CosLiteralString)=
92125
CosArray()=new(Array{CosObject,1}())
93126
end
94127

128+
function show(io::IO, o::CosArray)
129+
print(io, '[')
130+
for obj in o.val
131+
showref(io, obj)
132+
print(io, ' ')
133+
end
134+
print(io, ']')
135+
end
95136

96137
get(o::CosArray, isNative=false)=isNative ? map((x)->get(x),o.val) : o.val
97138
length(o::CosArray)=length(o.val)
@@ -105,6 +146,18 @@ get(dict::CosDict, name::CosName)=get(dict.val,name,CosNull)
105146

106147
get(o::CosIndirectObject{CosDict}, name::CosName) = get(o.obj, name)
107148

149+
function show(io::IO, o::CosDict)
150+
print(io, "<<\n")
151+
for key in keys(o.val)
152+
print(io, "\t")
153+
show(io, key)
154+
print(io, "\t")
155+
showref(io, get(o,key))
156+
println(io, "")
157+
end
158+
print(io, ">>")
159+
end
160+
108161
"""
109162
Set the value to object. If the object is CosNull the key is deleted.
110163
"""
@@ -126,6 +179,11 @@ set!(o::CosIndirectObject{CosDict}, name::CosName, obj::CosObject) =
126179
CosStream(d::CosDict,isInternal::Bool=true)=new(d,isInternal)
127180
end
128181

182+
function show(io::IO, stm::CosStream)
183+
show(io, stm.extent);
184+
print(io, "stream\n...\nendstream\n")
185+
end
186+
129187
get(stm::CosStream, name::CosName) = get(stm.extent, name)
130188

131189
get(o::CosIndirectObject{CosStream}, name::CosName) = get(o.obj,name)
@@ -141,7 +199,7 @@ Decodes the stream and provides output as an BufferedInputStream.
141199
"""
142200
get(stm::CosStream) = decode(stm)
143201

144-
@compat mutable struct CosObjectStream<: CosObject
202+
@compat mutable struct CosObjectStream <: CosObject
145203
stm::CosStream
146204
n::Int
147205
first::Int
@@ -162,6 +220,8 @@ get(stm::CosStream) = decode(stm)
162220
end
163221
end
164222

223+
show(io::IO, os::CosObjectStream) = show(io, os.stm)
224+
165225
get(os::CosObjectStream, name::CosName) = get(os.stm, name)
166226

167227
get(os::CosIndirectObject{CosObjectStream}, name::CosName) = get(os.obj,name)

src/PDPageElement.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BufferedStreams
2+
import Base: show
23

34
@compat abstract type PDPageObject end
45

@@ -20,6 +21,14 @@ end
2021
PDPageElement(ts::AbstractString,ver::Tuple{Int,Int},nop::Int=0)=
2122
PDPageElement(Symbol(ts),ver,nop,Vector{CosObject}())
2223

24+
function show(io::IO, e::PDPageElement)
25+
for op in e.operands
26+
show(io, op)
27+
print(io, ' ')
28+
end
29+
print(io, String(e.t))
30+
end
31+
2332
@compat mutable struct PDPageObjectGroup <: PDPageObject
2433
isEOG::Bool
2534
objs::Vector{Union{PDPageObject,CosObject}}
@@ -153,6 +162,10 @@ end
153162
new(PDPageElement(ts,ver,nop))
154163
end
155164

165+
show(io::IO, e::PDPage_BeginGroup) = show(io, e.elem)
166+
167+
show(io::IO, e::PDPage_EndGroup) = show(io, e.elem)
168+
156169
function collect_object(grp::PDPageObjectGroup,
157170
beg::PDPage_BeginGroup,
158171
bis::BufferedInputStream)

0 commit comments

Comments
 (0)