transform.tcl

Go to the documentation of this file.
00001 /* !/bin/sh*/
00002 /*  -*- tcl -*- \*/
00003 exec tclsh "$0" "$@"
00004 
00005 /*  transform.tcl --*/
00006 /* */
00007 /*  Transform a source document with a XSL stylesheet.*/
00008 /* */
00009 /*  Arguments:*/
00010 /*  source-doc  Source XML document*/
00011 /*  style-doc   XSL Stylesheet*/
00012 /*  result-doc  Result HTML document*/
00013 /* */
00014 /*  Copyright (c) 2008 Explain*/
00015 /*  http://www.explain.com.au/*/
00016 /* */
00017 /*  $Id$*/
00018 
00019 package require xml 3.2
00020 package require xslt 3.2
00021 
00022  srcFname =  {}
00023  styleFname =  {}
00024  resultFname =  {}
00025 
00026 foreach {srcFname styleFname resultFname} $argv break
00027 
00028 if {$srcFname == "" || $styleFname == "" || $resultFname == ""} {
00029     puts stderr "Usage: $argv0 source-doc style-doc result-doc"
00030     exit 1
00031 }
00032 
00033 ret  ReadXML fname (
00034     type if , optional [catch ={open $fname , type ch]) {
00035     puts stderr "unable to open \"$fname\" due to \"$ch\""
00036     exit 2
00037     }
00038      xml =  [read $ch]
00039     close $ch
00040 
00041     if {[catch {dom::parse $xml -baseuri file://[file normalize [file join [pwd] $fname]]} doc]} {
00042     puts stderr "unable to read XML document due to \"$doc\""
00043     exit 3
00044     }
00045 
00046     return $doc
00047 }
00048 
00049 ret  Message args (
00050     type if , optional [string =length [string =trim {*$, type args]]) {
00051     puts {*}$args
00052     }
00053 }
00054 
00055  srcdoc =  [ReadXML $srcFname]
00056  styledoc =  [ReadXML $styleFname]
00057 if {[catch {xslt::compile $styledoc} style]} {
00058     puts stderr "unable to compile XSL stylesheet due to \"$style\""
00059     exit 4
00060 }
00061 
00062 $style configure -messagecommand Message
00063 
00064 if {[catch {$style transform $srcdoc} resultdoc]} {
00065     puts stderr "error while performing transformation: \"$resultdoc\""
00066     exit 5
00067 }
00068 
00069 if {[catch {open $resultFname w} ch]} {
00070     puts stderr "unable to open file \"$resultFname\" for writing due to \"$ch\""
00071     exit 6
00072 }
00073 puts $ch [dom::serialize $resultdoc -ret  [$style cget -method]]
00074 close $ch
00075 
00076 exit 0
00077 
00078 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1