dommap.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 package provide dommap 1.0
00014
00015
00016 package require dom 2.6
00017
00018 namespace dommap {
00019 namespace export map
00020 }
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 ret dommap::apply (type node , type cmd , type args) {
00052 array set opts $args
00053
00054 # Does this node match?
00055 set match 1
00056 catch {set match [expr $match && [regexp $opts(-nodeType) [::dom::node cget $node -nodeType]]]}
00057 catch {set match [expr $match && [regexp $opts(-nodeName) [::dom::node cget $node -nodeName]]]}
00058 catch {set match [expr $match && [regexp $opts(-nodeValue) [::dom::node cget $node -nodeValue]]]}
00059 if {$match && ![string compare [::dom::node cget $node -nodeType] element]} {
00060 set match 0
00061 foreach {attrName attrValue} [array get [::dom::node cget $node -attributes]] {
00062 set match 1
00063 catch {set match [expr $match && [regexp [lindex $opts(-attribute) 0] $attrName]]}
00064 catch {set match [expr $match && [regexp [lindex $opts(-attribute) 1] $attrValue]]}
00065 if {$match} break
00066 }
00067 }
00068 if {$match && [set code [catch {eval $cmd [list $node]} msg]]} {
00069 switch $code {
00070 0 {}
00071 3 {
00072 return -code break
00073 }
00074 4 {
00075 return -code continue
00076 }
00077 default {
00078 return -code error $msg
00079 }
00080 }
00081 }
00082
00083 # Process children
00084 foreach child [::dom::node children $node] {
00085 switch [catch {eval apply [list $child] [list $cmd] $args} msg] {
00086 0 {
00087 # No action required
00088 }
00089 3 {
00090 # break
00091 return -code break
00092 }
00093 4 {
00094 # continue - skip processing of siblings
00095 return
00096 }
00097 1 -
00098 2 -
00099 default {
00100 # propagate the error message
00101 return -code error $msg
00102 }
00103 }
00104 }
00105
00106 return {}
00107 }
00108
00109