summaryrefslogtreecommitdiffstats
path: root/tclxml/tclxslt/process.tcl
blob: d38e5fbc665f2fbd2199b5b2b89120ce9c594875 (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
# process.tcl --
#
#	XSLT extension providing processing functions
#
# Copyright (c) 2007 Packaged Press
# http://www.packagedpress.com/
# Copyright (c) 2002-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: process.tcl,v 1.1.1.1 2009/01/16 22:11:49 joye Exp $

package provide xslt::process 1.1

package require uri 1.1
package require xslt::cache 3.2

namespace eval xslt::process {
    namespace export transform fop
    namespace export transform-result
    namespace export dtd-valid
}

# Add support for the dom: URI scheme.
#
# This scheme allows a script to reference an in-memory DOM tree.

proc ::uri::SplitDom url {
    return [list dom $url]
}

proc ::uri::JoinDom args {
    array set components {
	dom {}
    }
    array set components $args

    return dom:$components(dom)
}

# xslt::process::transform --
#
#	Perform an XSL Transformation.
#
# TODO:
#	Return messages
#	Cache source and stylesheet documents.
#	Generate dependency documents.
#
# Arguments:
#	src	Location of source document
#	ssheet	Location of stylesheet
#	result	Location for result document
#	params	Parameters (nodelist)
#	args	not needed
#
# Results:
#	Returns empty string for success

# This version forks a process
proc xslt::process::transform_fork {src ssheet result {params {}} args} {
    if {[catch {exec tclxsltproc -config /Users/steve/scms/lib/config.tcl --xinclude -o $result $ssheet $src} out]} {
	return $out
    } else {
	return {}
    }
}

# This version performs the transformation in-process.
proc xslt::process::transform:dbg {src ssheet result {params {}} args} {
    puts stderr [list process::transform $src $ssheet $result $params $args]
    if {[catch {eval transform:dbg [list $src $ssheet $result] $params $args} msg]} {
	puts stderr "\nprocess::transform returned error $msg\nStack trace:$::errorInfo\n"
	return -code error $msg
    } else {
	puts stderr [list process::transform ran OK]
	return $msg
    }
}
proc xslt::process::transform {srcNd ssheetNd resultNd {params {}} args} {

    # The filenames may be passed in as nodesets
    set src $srcNd
    catch {set src [dom::node stringValue [lindex $srcNd 0]]}
    set ssheet $ssheetNd
    catch {set ssheet [dom::node stringValue [lindex $ssheetNd 0]]}
    set result $resultNd
    catch {set result [dom::node stringValue [lindex $resultNd 0]]}

    # params will be a nodeset consisting of name/value pairs.
    # These must be converted to strings
    set parameterList {}
    switch [llength $params] {
	1 {
	    puts stderr [list xslt::process::transform params nodeType [dom::node cget $params -nodeType]]
	    set pNdList [dom::node children $params]
	}
	default {
	    set pNdList $params
	}
    }
    foreach paramNd $pNdList {
	set name [set value {}]
	foreach child [dom::node children $paramNd] {
	    set nameNd [dom::node selectNode $child name]
	    set name [dom::node stringValue $nameNd]
	    set valueNd [dom::node selectNode $child value]
	    set value [dom::node stringValue $valueNd]
	}
	if {[string compare $name {}]} {
	    lappend parameterList $name $value
	}
    }

    puts stderr [list xslt::process::transform parameters: $parameterList]

    set cleanup {}

    if {[catch {open $src} ch]} {
	# eval $cleanup
	return "unable to open source document \"$src\" for reading due to \"$ch\""
    }
    if {[catch {::dom::parse [read $ch] -baseuri $src} sourcedoc]} {
	# eval $cleanup
	return "unable to parse source document \"$src\" due to \"$sourcedoc\""
    }
    close $ch

    append cleanup "dom::destroy $sourcedoc" \n

    dom::xinclude $sourcedoc

    if {[catch {open $ssheet} ch]} {
	eval $cleanup
	return "unable to open stylesheet document \"$ssheet\" for reading due to \"$ch\""
    }
    if {[catch {::dom::parse [read $ch] -baseuri $ssheet} styledoc]} {
	eval $cleanup
	return "unable to parse stylesheet document \"$ssheet\" due to \"$styledoc\""
    }
    close $ch

    append cleanup "dom::destroy $styledoc" \n

    if {[catch {xslt::compile $styledoc} style]} {
	eval $cleanup
	return "unable to compile stylesheet \"$ssheet\" due to \"$style\""
    }

    append cleanup "rename $style {}" \n

    if {[catch {eval [list $style] transform [list $sourcedoc] $parameterList} resultdoc]} {
	eval $cleanup
	return "unable to transform document \"$src\" with stylesheet \"$ssheet\" due to \"$resultdoc\""
    }

    append cleanup "dom::destroy $resultdoc" \n

    if {[catch {open $result w} ch]} {
	eval $cleanup
	return "unable to save result document \"$result\" due to \"$ch\""
    }

    puts $ch [dom::serialize $resultdoc -method [$style cget -method]]
    close $ch

    catch {
	uplevel \#0 $cleanup
    }

    return {}
}

# xslt::process::transform-result --
#
#	Perform an XSL Transformation.
#	This version returns the result document.
#
# Arguments:
#	src	Location of source document
#	ssheet	Location of stylesheet
#	params	Parameters (nodelist)
#	args	not needed
#
# Results:
#	Returns result document.

proc xslt::process::transform-result {srcNd ssheetNd {params {}} args} {

    # The filenames may be passed in as nodesets
    set src $srcNd
    catch {set src [dom::node stringValue [lindex $srcNd 0]]}
    set ssheet $ssheetNd
    catch {set ssheet [dom::node stringValue [lindex $ssheetNd 0]]}

    # params will be a nodeset consisting of name/value pairs.
    # These must be converted to strings
    set parameterList {}
    foreach paramNd $params {
	set name [set value {}]
	foreach child [dom::node children $paramNd] {
	    set nameNd [dom::node selectNode $child name]
	    set name [dom::node stringValue $nameNd]
	    set valueNd [dom::node selectNode $child value]
	    set value [dom::node stringValue $valueNd]
	}
	if {[string compare $name {}]} {
	    lappend parameterList $name $value
	}
    }

    if {[catch {eval xslt::cache::transform [list $src $ssheet] $parameterList} rd]} {
	return "unable to perform transformation due to \"$rd\""
    }

    return $rd
}

# xslt::process::checkwffdoc --
#
#	Test a document for well-formedness
#
# Arguments:
#	doc	DOM token for document to check
#	args	not needed
#
# Results:
#	Returns success message

proc xslt::process::checkwffdoc {doc args} {
    return "of course it's well-formed, it's a DOM tree!"
}

# xslt::process::dtd-valid --
#
#	Test a document for (DTD) validity
#
# Arguments:
#	uri	URI for document to check, supports dom: scheme
#	args	not needed
#
# Results:
#	Returns success/failure message

proc xslt::process::dtd-valid {uri args} {
    array set components [uri::split $uri]

    switch -- $components(scheme) {
	file {
	    set ch [open $components(path)]
	    set xmldata [read $ch]
	    close $ch
	    set doc [dom::parse $xmldata -baseuri $uri]
	    set cleanup [list dom::destroy $doc]
	}
	dom {
	    set doc $components(dom)
	    set cleanup {}
	}
	default {
	    # TODO: support http: scheme
	    return -code error "unable to resolve entity $uri"
	}
    }

    if {[catch {dom::validate $doc} msg]} {
	set result $msg
    } else {
	set result {document is valid}
    }

    eval $cleanup

    return $result
}

# xslt::process::fop --
#
#	Format an XSL FO document using FOP
#
# Arguments:
#	fo	Location of FO document
#	pdf	Location for PDF document
#	params	Parameters (nodelist)
#	args	not needed
#
# Results:
#	Returns success message

proc xslt::process::fop {fo pdf params args} {
    return "format fo $fo to produce $pdf"
}

# xslt::process::log --
#
#	Emit a log message.  The application is expected to override this.
#
# Arguments:
#	msg	Log message
#	args	not needed
#
# Results:
#	None

proc xslt::process::log {msg args} {
    Stderr Log:\ $msg
    return {}
}