process.tcl

Go to the documentation of this file.
00001 /*  process.tcl --*/
00002 /* */
00003 /*  XSLT extension providing processing functions*/
00004 /* */
00005 /*  Copyright (c) 2007 Packaged Press*/
00006 /*  http://www.packagedpress.com/*/
00007 /*  Copyright (c) 2002-2004 Zveno Pty Ltd*/
00008 /*  http://www.zveno.com/*/
00009 /* */
00010 /*  See the file "LICENSE" in this distribution for information on usage and*/
00011 /*  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00012 /* */
00013 /*  $Id: process.tcl,v 1.1 2004/07/11 12:18:03 balls Exp $*/
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 /*  This version performs the transformation in-process.*/
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     /*  The filenames may be passed in as nodesets*/
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     /*  params will be a nodeset consisting of name/value pairs.*/
00093     /*  These must be converted to strings*/
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     /*  eval $cleanup*/
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     /*  eval $cleanup*/
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 /*  xslt::process::transform-result --*/
00177 /* */
00178 /*  Perform an XSL Transformation.*/
00179 /*  This version returns the result document.*/
00180 /* */
00181 /*  Arguments:*/
00182 /*  src Location of source document*/
00183 /*  ssheet  Location of stylesheet*/
00184 /*  params  Parameters (nodelist)*/
00185 /*  args    not needed*/
00186 /* */
00187 /*  Results:*/
00188 /*  Returns result document.*/
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     /*  params will be a nodeset consisting of name/value pairs.*/
00199     /*  These must be converted to strings*/
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 /*  xslt::process::checkwffdoc --*/
00222 /* */
00223 /*  Test a document for well-formedness*/
00224 /* */
00225 /*  Arguments:*/
00226 /*  doc DOM token for document to check*/
00227 /*  args    not needed*/
00228 /* */
00229 /*  Results:*/
00230 /*  Returns success message*/
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 /*  xslt::process::dtd-valid --*/
00237 /* */
00238 /*  Test a document for (DTD) validity*/
00239 /* */
00240 /*  Arguments:*/
00241 /*  uri URI for document to check, supports dom: scheme*/
00242 /*  args    not needed*/
00243 /* */
00244 /*  Results:*/
00245 /*  Returns success/failure message*/
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 /*  xslt::process::fop --*/
00280 /* */
00281 /*  Format an XSL FO document using FOP*/
00282 /* */
00283 /*  Arguments:*/
00284 /*  fo  Location of FO document*/
00285 /*  pdf Location for PDF document*/
00286 /*  params  Parameters (nodelist)*/
00287 /*  args    not needed*/
00288 /* */
00289 /*  Results:*/
00290 /*  Returns success message*/
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 /*  xslt::process::log --*/
00297 /* */
00298 /*  Emit a log message.  The application is expected to override this.*/
00299 /* */
00300 /*  Arguments:*/
00301 /*  msg Log message*/
00302 /*  args    not needed*/
00303 /* */
00304 /*  Results:*/
00305 /*  None*/
00306 
00307 ret  xslt::process::log (type msg , type args) {
00308     Stderr Log:\ $msg
00309     return {}
00310 }
00311 
00312 
00313 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1