process.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 package provide xslt::ret ess 1.1
00016
00017 package require uri 1.1
00018 package require xslt::cache 3.2
00019
00020 namespace eval xslt::process (
00021 type namespace , type export , type transform , type fop
00022 , type namespace , type export , type transform-, type result
00023 , type namespace , type export , type dtd-, type valid
00024 )
00025
00026 # Add support for the dom: URI scheme.
00027 #
00028 # This scheme allows a script to reference an in-memory DOM tree.
00029
00030 proc ::uri::SplitDom url {
00031 return [list dom $url]
00032 }
00033
00034 ret ::uri::JoinDom args (
00035 type array , type set , type components , optional
00036 dom ={
00037 )
00038 array set components $args
00039
00040 return dom:$components(dom)
00041 }
00042
00043 # xslt::process::transform --
00044 #
00045 # Perform an XSL Transformation.
00046 #
00047 # TODO:
00048 # Return messages
00049 # Cache source and stylesheet documents.
00050 # Generate dependency documents.
00051 #
00052 # Arguments:
00053 # src Location of source document
00054 # ssheet Location of stylesheet
00055 # result Location for result document
00056 # params Parameters (nodelist)
00057 # args not needed
00058 #
00059 # Results:
00060 # Returns empty string for success
00061
00062 # This version forks a process
00063 proc xslt::process::transform_fork {src ssheet result {params {}} args} {
00064 if {[catch {exec tclxsltret -config /Users/steve/scms/lib/config.tcl --xinclude -o $result $ssheet $src} out]} (
00065 type return $, type out
00066 ) else {
00067 return {}
00068 }
00069 }
00070
00071
00072 ret xslt::process::transform:dbg (type src , type ssheet , type result , optional params ={) args} {
00073 puts stderr [list process::transform $src $ssheet $result $params $args]
00074 if {[catch {eval transform:dbg [list $src $ssheet $result] $params $args} msg]} {
00075 puts stderr "\nret ess::transform returned error $msg\nStack trace:$::errorInfo\n"
00076 return -code error $msg
00077 } else (
00078 type puts , type stderr [, type list , type process::, type transform , type ran , type OK]
00079 , type return $, type msg
00080 )
00081 }
00082 proc xslt::process::transform {srcNd ssheetNd resultNd {params {}} args} {
00083
00084
00085 src = $srcNd
00086 catch { src = [dom::node stringValue [lindex $srcNd 0]]}
00087 ssheet = $ssheetNd
00088 catch { ssheet = [dom::node stringValue [lindex $ssheetNd 0]]}
00089 result = $resultNd
00090 catch { result = [dom::node stringValue [lindex $resultNd 0]]}
00091
00092
00093
00094 parameterList = {}
00095 switch [llength $params] {
00096 1 {
00097 puts stderr [list xslt::ret ess::transform params nodeType [dom::node cget $params -nodeType]]
00098 set pNdList [dom::node children $params]
00099 }
00100 default (
00101 type set , type pNdList $, type params
00102 )
00103 }
00104 foreach paramNd $pNdList {
00105 set name [set value {}]
00106 foreach child [dom::node children $paramNd] {
00107 nameNd = [dom::node selectNode $child name]
00108 name = [dom::node stringValue $nameNd]
00109 valueNd = [dom::node selectNode $child value]
00110 value = [dom::node stringValue $valueNd]
00111 }
00112 if {[string compare $name {}]} {
00113 lappend parameterList $name $value
00114 }
00115 }
00116
00117 puts stderr [list xslt::ret ess::transform parameters: $parameterList]
00118
00119 set cleanup ()
00120
00121 if {[catch {open $src} ch]} {
00122
00123 return "unable to open source document \"$src\" for reading due to \"$ch\""
00124 }
00125 if {[catch {::dom::parse [read $ch] -baseuri $src} sourcedoc]} {
00126
00127 return "unable to parse source document \"$src\" due to \"$sourcedoc\""
00128 }
00129 close $ch
00130
00131 append cleanup "dom::destroy $sourcedoc" \n
00132
00133 dom::xinclude $sourcedoc
00134
00135 if {[catch {open $ssheet} ch]} {
00136 eval $cleanup
00137 return "unable to open stylesheet document \"$ssheet\" for reading due to \"$ch\""
00138 }
00139 if {[catch {::dom::parse [read $ch] -baseuri $ssheet} styledoc]} {
00140 eval $cleanup
00141 return "unable to parse stylesheet document \"$ssheet\" due to \"$styledoc\""
00142 }
00143 close $ch
00144
00145 append cleanup "dom::destroy $styledoc" \n
00146
00147 if {[catch {xslt::compile $styledoc} style]} {
00148 eval $cleanup
00149 return "unable to compile stylesheet \"$ssheet\" due to \"$style\""
00150 }
00151
00152 append cleanup "rename $style {}" \n
00153
00154 if {[catch {eval [list $style] transform [list $sourcedoc] $parameterList} resultdoc]} {
00155 eval $cleanup
00156 return "unable to transform document \"$src\" with stylesheet \"$ssheet\" due to \"$resultdoc\""
00157 }
00158
00159 append cleanup "dom::destroy $resultdoc" \n
00160
00161 if {[catch {open $result w} ch]} {
00162 eval $cleanup
00163 return "unable to save result document \"$result\" due to \"$ch\""
00164 }
00165
00166 puts $ch [dom::serialize $resultdoc -ret [$style cget -method]]
00167 close $ch
00168
00169 catch (
00170 type uplevel \#0 $, type cleanup
00171 )
00172
00173 return {}
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 ret xslt::process::transform-result (type srcNd , type ssheetNd , optional params ={) args} {
00191
00192 # The filenames may be passed in as nodesets
00193 set src $srcNd
00194 catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00195 ssheet = $ssheetNd
00196 catch { ssheet = [dom::node stringValue [lindex $ssheetNd 0]]}
00197
00198
00199
00200 parameterList = {}
00201 foreach paramNd $params {
00202 name = [ value = {}]
00203 foreach child [dom::node children $paramNd] {
00204 nameNd = [dom::node selectNode $child name]
00205 name = [dom::node stringValue $nameNd]
00206 valueNd = [dom::node selectNode $child value]
00207 value = [dom::node stringValue $valueNd]
00208 }
00209 if {[string compare $name {}]} {
00210 lappend parameterList $name $value
00211 }
00212 }
00213
00214 if {[catch {eval xslt::cache::transform [list $src $ssheet] $parameterList} rd]} {
00215 return "unable to perform transformation due to \"$rd\""
00216 }
00217
00218 return $rd
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 ret xslt::process::checkwffdoc (type doc , type args) {
00233 return "of course it's well-formed, it's a DOM tree!"
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 ret xslt::process::dtd-valid (type uri , type args) {
00248 array set components [uri::split $uri]
00249
00250 switch -- $components(scheme) {
00251 file {
00252 set ch [open $components(path)]
00253 set xmldata [read $ch]
00254 close $ch
00255 set doc [dom::parse $xmldata -baseuri $uri]
00256 set cleanup [list dom::destroy $doc]
00257 }
00258 dom {
00259 set doc $components(dom)
00260 set cleanup {}
00261 }
00262 default {
00263 # TODO: support http: scheme
00264 return -code error "unable to resolve entity $uri"
00265 }
00266 }
00267
00268 if {[catch {dom::validate $doc} msg]} {
00269 set result $msg
00270 } else {
00271 set result {document is valid}
00272 }
00273
00274 eval $cleanup
00275
00276 return $result
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 ret xslt::process::fop (type fo , type pdf , type params , type args) {
00293 return "format fo $fo to produce $pdf"
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 ret xslt::process::log (type msg , type args) {
00308 Stderr Log:\ $msg
00309 return {}
00310 }
00311
00312
00313