blob: e2573f81e9f8aea9636b11848375c38b7b36294e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
# tclparser-8.0.tcl --
#
# This file provides a Tcl implementation of a XML parser.
# This file supports Tcl 8.0.
#
# See xml-8.[01].tcl for definitions of character sets and
# regular expressions.
#
# Copyright (c) 2005-2008 by Explain.
# http://www.explain.com.au/
# Copyright (c) 1998-2004 Zveno Pty Ltd
# http://www.zveno.com/
#
# See the file "LICENSE" in this distribution for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# $Id: tclparser-8.0.tcl,v 1.1.1.1 2009/01/16 22:11:49 joye Exp $
package require -exact Tcl 8.0
package require xmldefs 3.2
package require sgmlparser 1.0
package provide xml::tclparser 3.2
namespace eval xml {
# Procedures for parsing XML documents
namespace export parser
# Procedures for parsing XML DTDs
namespace export DTDparser
# Counter for creating unique parser objects
variable ParserCounter 0
}
# xml::parser --
#
# Creates XML parser object.
#
# Arguments:
# args Unique name for parser object
# plus option/value pairs
#
# Recognised Options:
# -final Indicates end of document data
# -elementstartcommand Called when an element starts
# -elementendcommand Called when an element ends
# -characterdatacommand Called when character data occurs
# -processinginstructioncommand Called when a PI occurs
# -externalentityrefcommand Called for an external entity reference
#
# (Not compatible with expat)
# -xmldeclcommand Called when the XML declaration occurs
# -doctypecommand Called when the document type declaration occurs
#
# -errorcommand Script to evaluate for a fatal error
# -warningcommand Script to evaluate for a reportable warning
# -statevariable global state variable
# -reportempty whether to provide empty element indication
#
# Results:
# The state variable is initialised.
proc xml::parser {args} {
variable ParserCounter
if {[llength $args] > 0} {
set name [lindex $args 0]
set args [lreplace $args 0 0]
} else {
set name parser[incr ParserCounter]
}
if {[info command [namespace current]::$name] != {}} {
return -code error "unable to create parser object \"[namespace current]::$name\" command"
}
# Initialise state variable and object command
upvar \#0 [namespace current]::$name parser
set sgml_ns [namespace parent]::sgml
array set parser [list name $name \
-final 1 \
-elementstartcommand ${sgml_ns}::noop \
-elementendcommand ${sgml_ns}::noop \
-characterdatacommand ${sgml_ns}::noop \
-processinginstructioncommand ${sgml_ns}::noop \
-externalentityrefcommand ${sgml_ns}::noop \
-xmldeclcommand ${sgml_ns}::noop \
-doctypecommand ${sgml_ns}::noop \
-warningcommand ${sgml_ns}::noop \
-statevariable [namespace current]::$name \
-reportempty 0 \
internaldtd {} \
]
proc [namespace current]::$name {method args} \
"eval ParseCommand $name \$method \$args"
eval ParseCommand [list $name] configure $args
return [namespace current]::$name
}
# xml::ParseCommand --
#
# Handles parse object command invocations
#
# Valid Methods:
# cget
# configure
# parse
# reset
#
# Arguments:
# parser parser object
# method minor command
# args other arguments
#
# Results:
# Depends on method
proc xml::ParseCommand {parser method args} {
upvar \#0 [namespace current]::$parser state
switch -- $method {
cget {
return $state([lindex $args 0])
}
configure {
foreach {opt value} $args {
set state($opt) $value
}
}
parse {
ParseCommand_parse $parser [lindex $args 0]
}
reset {
if {[llength $args]} {
return -code error "too many arguments"
}
ParseCommand_reset $parser
}
default {
return -code error "unknown method \"$method\""
}
}
return {}
}
# xml::ParseCommand_parse --
#
# Parses document instance data
#
# Arguments:
# object parser object
# xml data
#
# Results:
# Callbacks are invoked, if any are defined
proc xml::ParseCommand_parse {object xml} {
upvar \#0 [namespace current]::$object parser
variable Wsp
variable tokExpr
variable substExpr
set parent [namespace parent]
if {![string compare :: $parent]} {
set parent {}
}
set tokenised [lrange \
[${parent}::sgml::tokenise $xml \
$tokExpr \
$substExpr \
-internaldtdvariable [namespace current]::${object}(internaldtd)] \
4 end]
eval ${parent}::sgml::parseEvent \
[list $tokenised \
-emptyelement [namespace code ParseEmpty] \
-parseattributelistcommand [namespace code ParseAttrs]] \
[array get parser -*command] \
[array get parser -entityvariable] \
[array get parser -reportempty] \
[array get parser -final] \
-normalize 0 \
-internaldtd [list $parser(internaldtd)]
return {}
}
# xml::ParseEmpty -- Tcl 8.0 version
#
# Used by parser to determine whether an element is empty.
# This should be dead easy in XML. The only complication is
# that the RE above can't catch the trailing slash, so we have
# to dig it out of the tag name or attribute list.
#
# Tcl 8.1 REs should fix this.
#
# Arguments:
# tag element name
# attr attribute list (raw)
# e End tag delimiter.
#
# Results:
# "/" if the trailing slash is found. Optionally, return a list
# containing new values for the tag name and/or attribute list.
proc xml::ParseEmpty {tag attr e} {
if {[string match */ [string trimright $tag]] && \
![string length $attr]} {
regsub {/$} $tag {} tag
return [list / $tag $attr]
} elseif {[string match */ [string trimright $attr]]} {
regsub {/$} [string trimright $attr] {} attr
return [list / $tag $attr]
} else {
return {}
}
}
# xml::ParseAttrs --
#
# Parse element attributes.
#
# There are two forms for name-value pairs:
#
# name="value"
# name='value'
#
# Watch out for the trailing slash on empty elements.
#
# Arguments:
# attrs attribute string given in a tag
#
# Results:
# Returns a Tcl list representing the name-value pairs in the
# attribute string
proc xml::ParseAttrs attrs {
variable Wsp
variable Name
# First check whether there's any work to do
if {![string compare {} [string trim $attrs]]} {
return {}
}
# Strip the trailing slash on empty elements
regsub [format {/[%s]*$} " \t\n\r"] $attrs {} atList
set mode name
set result {}
foreach component [split $atList =] {
switch $mode {
name {
set component [string trim $component]
if {[regexp $Name $component]} {
lappend result $component
} else {
return -code error "invalid attribute name \"$component\""
}
set mode value:start
}
value:start {
set component [string trimleft $component]
set delimiter [string index $component 0]
set value {}
switch -- $delimiter {
\" -
' {
if {[regexp [format {%s([^%s]*)%s(.*)} $delimiter $delimiter $delimiter] $component discard value remainder]} {
lappend result $value
set remainder [string trim $remainder]
if {[string length $remainder]} {
if {[regexp $Name $remainder]} {
lappend result $remainder
set mode value:start
} else {
return -code error "invalid attribute name \"$remainder\""
}
} else {
set mode end
}
} else {
set value [string range $component 1 end]
set mode value:continue
}
}
default {
return -code error "invalid value for attribute \"[lindex $result end]\""
}
}
}
value:continue {
if {[regexp [format {([^%s]*)%s(.*)} $delimiter $delimiter] $component discard valuepart remainder]} {
append value = $valuepart
lappend result $value
set remainder [string trim $remainder]
if {[string length $remainder]} {
if {[regexp $Name $remainder]} {
lappend result $remainder
set mode value:start
} else {
return -code error "invalid attribute name \"$remainder\""
}
} else {
set mode end
}
} else {
append value = $component
}
}
end {
return -code error "unexpected data found after end of attribute list"
}
}
}
switch $mode {
name -
end {
# This is normal
}
default {
return -code error "unexpected end of attribute list"
}
}
return $result
}
# xml::ParseCommand_reset --
#
# Initialize parser data
#
# Arguments:
# object parser object
#
# Results:
# Parser data structure initialised
proc xml::ParseCommand_reset object {
upvar \#0 [namespace current]::$object parser
array set parser [list \
-final 1 \
internaldtd {} \
]
}
|