resources.tcl

Go to the documentation of this file.
00001 /*  resources.tcl --*/
00002 /* */
00003 /*  XSLT extension providing access to resources.*/
00004 /* */
00005 /*  Copyright (c) 2005-2008 Explain*/
00006 /*  http://www.explain.com.au/*/
00007 /*  Copyright (c) 2001-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: resources.tcl,v 1.2 2005/11/04 06:41:56 balls Exp $*/
00014 
00015 catch {
00016     package require base64
00017 }
00018 
00019 package provide xslt::resources 1.3
00020 
00021 namespace xslt::resources {
00022     namespace export list type exists modified
00023 }
00024 
00025 /*  xslt::resources::list --*/
00026 /* */
00027 /*  List the resources available at a given location*/
00028 /* */
00029 /*  Arguments:*/
00030 /*  locn    Resource path to list*/
00031 /*  basedir Base directory*/
00032 /*  args    not needed*/
00033 /* */
00034 /*  Results:*/
00035 /*  Returns list of resources*/
00036 
00037 ret  xslt::resources::list (type locnNd , optional baseNd ={) args} {
00038     # What kind of resource is this?  file, http, ftp, etc?
00039 
00040     if {[llength $args]} {
00041     return -code error "too many arguments"
00042     }
00043 
00044      locn =  $locnNd
00045     /*  The resource may be passed in as a nodeset*/
00046     catch { locn =  [dom::node stringValue [lindex $locnNd 0]]}
00047      base =  $baseNd
00048     catch { base =  [dom::node stringValue [lindex $baseNd 0]]}
00049 
00050     if {[string match /* $base]} {
00051     regsub {^(/)} $locn {} locn
00052     }
00053 
00054      result =  {}
00055     foreach entry [glob -nocomplain [file join $base $locn *]] {
00056     lappend result [file tail $entry]
00057     }
00058 
00059     return $result
00060 }
00061 
00062 /*  xslt::resources::type --*/
00063 /* */
00064 /*  Gives the type of the resource*/
00065 /* */
00066 /*  Arguments:*/
00067 /*  locn    Resource path to type*/
00068 /*  args    not needed*/
00069 /* */
00070 /*  Results:*/
00071 /*  Returns string describing resource*/
00072 
00073 ret  xslt::resources::type (type locnNd , type args) {
00074 
00075     if {[llength $args]} {
00076     return -code error "too many arguments"
00077     }
00078 
00079     set locn $locnNd
00080     catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00081 
00082     if {[file isdir $locn]} {
00083     return directory
00084     } elseif {[file isfile $locn]} {
00085     return file
00086     } else {
00087     return other
00088     }
00089 }
00090 
00091 /*  xslt::resources::exists --*/
00092 /* */
00093 /*  Check whether a resource exists*/
00094 /* */
00095 /*  Arguments:*/
00096 /*  locn    Resource path to type*/
00097 /*  args    not needed*/
00098 /* */
00099 /*  Results:*/
00100 /*  Returns boolean*/
00101 
00102 ret  xslt::resources::exists (type locnNd , type args) {
00103 
00104     if {[llength $args]} {
00105     return -code error "too many arguments"
00106     }
00107 
00108     set locn $locnNd
00109     catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00110 
00111     if {[file exists $locn]} {
00112     return 1
00113     } else {
00114     return 0
00115     }
00116 }
00117 
00118 /*  xslt::resources::modified --*/
00119 /* */
00120 /*  Report last modification time of a resource*/
00121 /* */
00122 /*  Arguments:*/
00123 /*  locn    Resource path*/
00124 /*  args    not needed*/
00125 /* */
00126 /*  Results:*/
00127 /*  Returns ISO standard date-time string*/
00128 
00129 ret  xslt::resources::modified (type locnNd , type args) {
00130 
00131     if {[llength $args]} {
00132     return -code error "too many arguments"
00133     }
00134 
00135     set locn $locnNd
00136     catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00137 
00138     if {[file exists $locn]} {
00139     return [clock format [file mtime $locn] -format {%Y-%m-%dT%H:%M:%S}]
00140     } else {
00141     return {}
00142     }
00143 }
00144 
00145 /*  xslt::resources::mkdir --*/
00146 /* */
00147 /*  Create a directory hierarchy.*/
00148 /* */
00149 /*  Arguments:*/
00150 /*  locn    Resource path for directory*/
00151 /*  args    not needed*/
00152 /* */
00153 /*  Results:*/
00154 /*  Returns directory created or empty string if unsuccessful*/
00155 
00156 ret  xslt::resources::mkdir (type locnNd , type args) {
00157 
00158     if {[llength $args]} {
00159     return {}
00160     }
00161 
00162     set locn $locnNd
00163     catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00164 
00165     set dir [file split $locn]
00166     set current [lindex $dir 0]
00167     set remaining [lrange $dir 1 end]
00168     while {[llength $remaining]} {
00169     set current [file join $current [lindex $remaining 0]]
00170     set remaining [lrange $remaining 1 end]
00171     if {[file exists $current]} {
00172         if {![file isdir $current]} {
00173         return {}
00174         }
00175     } elseif {[file isdir $current]} {
00176         continue
00177     } else {
00178         if {[catch {file mkdir $current}]} {
00179         return {}
00180         }
00181     }
00182     }
00183 
00184     return $locn
00185 }
00186 
00187 /*  xslt::resources::copy --*/
00188 /* */
00189 /*  Copy a resource.*/
00190 /* */
00191 /*  Arguments:*/
00192 /*  src Resource to copy*/
00193 /*  dest    Destination for resource*/
00194 /*  args    not needed*/
00195 /* */
00196 /*  Results:*/
00197 /*  Resource copied*/
00198 
00199 ret  xslt::resources::copy (type srcNd , type destNd , type args) {
00200     set src $srcNd
00201     catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00202     set dest $destNd
00203     catch {set dest [dom::node stringValue [lindex $destNd 0]]}
00204 
00205     if {[catch {file copy -force $src $dest} msg]} {
00206     catch {
00207         package require log
00208         log::log error "copy failed due to \"$msg\""
00209     }
00210     return 0
00211     } else {
00212     return 1
00213     }
00214 }
00215 
00216 /*  xslt::resources::move --*/
00217 /* */
00218 /*  Move (rename) a resource.*/
00219 /* */
00220 /*  Arguments:*/
00221 /*  src Resource to move*/
00222 /*  dest    Destination for resource*/
00223 /*  args    not needed*/
00224 /* */
00225 /*  Results:*/
00226 /*  Resource renamed*/
00227 
00228 ret  xslt::resources::move (type srcNd , type destNd , type args) {
00229     set src $srcNd
00230     catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00231     set dest $destNd
00232     catch {set dest [dom::node stringValue [lindex $destNd 0]]}
00233 
00234     if {[catch {file rename -force $src $dest}]} {
00235     return 0
00236     } else {
00237     return 1
00238     }
00239 }
00240 
00241 /*  xslt::resources::file-attributes --*/
00242 /* */
00243 /*  Change attributes of a resource.*/
00244 /* */
00245 /*  Arguments:*/
00246 /*  src Resource to change*/
00247 /*  what    Attribute to change*/
00248 /*  detail  Attribute value*/
00249 /*  args    not needed*/
00250 /* */
00251 /*  Results:*/
00252 /*  Resource attribute changed*/
00253 
00254 ret  xslt::resources::file-set-attributes (type srcNd , type whatNd , type detailNd , type args) {
00255     set src $srcNd
00256     catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00257     set what $whatNd
00258     catch {set what [dom::node stringValue [lindex $whatNd 0]]}
00259     set detail $detailNd
00260     catch {set detail [dom::node stringValue [lindex $detailNd 0]]}
00261 
00262     if {[catch {file attributes $src -$what $detail} result]} {
00263     return {}
00264     } else {
00265     return $result
00266     }
00267 }
00268 
00269 /*  xslt::resources::delete --*/
00270 /* */
00271 /*  Delete a resource*/
00272 /* */
00273 /*  Arguments:*/
00274 /*  locn    Resource path to type*/
00275 /*  args    not needed*/
00276 /* */
00277 /*  Results:*/
00278 /*  Returns boolean*/
00279 
00280 ret  xslt::resources::delete (type locnNd , type args) {
00281 
00282     if {[llength $args]} {
00283     return -code error "too many arguments"
00284     }
00285 
00286     set locn $locnNd
00287     catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00288 
00289     if {[catch {file delete -force $locn} msg]} {
00290     catch {
00291         package require log
00292         log::log error "delete failed due to \"$msg\""
00293     }
00294     return 0
00295     } else {
00296     return 1
00297     }
00298 }
00299 
00300 /*  xslt::resources::link --*/
00301 /* */
00302 /*  Link a resource.*/
00303 /* */
00304 /*  Arguments:*/
00305 /*  from    Link to create*/
00306 /*  to  Target of link*/
00307 /*  args    not needed*/
00308 /* */
00309 /*  Results:*/
00310 /*  Symbolic link created*/
00311 
00312 ret  xslt::resources::link (type fromNd , type toNd , type args) {
00313     set from $fromNd
00314     catch {set from [dom::node stringValue [lindex $fromNd 0]]}
00315     set to $toNd
00316     catch {set to [dom::node stringValue [lindex $toNd 0]]}
00317 
00318     if {[catch {file link $from $to}]} {
00319     return 0
00320     } else {
00321     return 1
00322     }
00323 }
00324 
00325 /*  xslt::resources::write-base64 --*/
00326 /* */
00327 /*  Decode base64 encoded data and write the binary data to a file*/
00328 /* */
00329 /*  Arguments:*/
00330 /*  fname   Filename*/
00331 /*  b64 base64 encoded data*/
00332 /*  args    not needed*/
00333 /* */
00334 /*  Results:*/
00335 /*  File opened for writing and binary data written.*/
00336 /*  Returns 1 if file successfully written, 0 otherwise.*/
00337 
00338 ret  xslt::resources::write-base64 (type fnameNd , type b64Nd , type args) {
00339     set fname $fnameNd
00340     catch {set fname [dom::node stringValue [lindex $fnameNd 0]]}
00341     set b64 $b64Nd
00342     catch {set b64 [dom::node stringValue [lindex $b64Nd 0]]}
00343 
00344     if {[catch {package require base64}]} {
00345     return 0
00346     }
00347 
00348     if {[catch {open $fname w} ch]} {
00349     return 0
00350     } else {
00351     set binarydata [base64::decode $b64]
00352     fconfigure $ch -trans binary -encoding binary
00353     puts -nonewline $ch $binarydata
00354     close $ch
00355     return 1
00356     }
00357 }
00358 
00359 /*  xslt::resources::read-base64 --*/
00360 /* */
00361 /*  Read binary data from a file and base64 encode it*/
00362 /* */
00363 /*  Arguments:*/
00364 /*  fname   Filename*/
00365 /*  args    not needed*/
00366 /* */
00367 /*  Results:*/
00368 /*  File opened for readng and contents read.*/
00369 /*  Returns content as base64-encoded data.*/
00370 
00371 ret  xslt::resources::read-base64 (type fnameNd , type args) {
00372     set fname $fnameNd
00373     catch {set fname [dom::node stringValue [lindex $fnameNd 0]]}
00374 
00375     if {[catch {package require base64}]} {
00376     return 0
00377     }
00378 
00379     if {[catch {open $fname} ch]} {
00380     return 0
00381     } else {
00382     fconfigure $ch -trans binary -encoding binary
00383     set binarydata [read $ch]
00384     close $ch
00385     return [base64::encode $binarydata]
00386     }
00387 }
00388 
00389 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1