dommap.tcl

Go to the documentation of this file.
00001 /*  dommap.tcl --*/
00002 /* */
00003 /*  Apply a mapping function to a DOM structure*/
00004 /* */
00005 /*  Copyright (c) 1998-2003 Zveno Pty Ltd*/
00006 /*  http://www.zveno.com/*/
00007 /* */
00008 /*  See the file "LICENSE" in this distribution for information on usage and*/
00009 /*  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00010 /* */
00011 /*  $Id: dommap.tcl,v 1.5 2003/12/09 04:56:43 balls Exp $*/
00012 
00013 package provide dommap 1.0
00014 
00015 /*  We need the DOM*/
00016 package require dom 2.6
00017 
00018 namespace dommap {
00019     namespace export map
00020 }
00021 
00022 /*  dommap::apply --*/
00023 /* */
00024 /*  Apply a function to a DOM document.*/
00025 /* */
00026 /*  The callback command is invoked with the node ID of the*/
00027 /*  matching DOM node as its argument.  The command may return*/
00028 /*  an error, continue or break code to alter the processing*/
00029 /*  of further nodes.*/
00030 /* */
00031 /*  Filter functions may be applied to match particular*/
00032 /*  nodes.  Valid functions include:*/
00033 /* */
00034 /*  -nodeType regexp*/
00035 /*  -nodeName regexp*/
00036 /*  -nodeValue regexp*/
00037 /*  -attribute {regexp regexp}*/
00038 /* */
00039 /*  If a filter is specified then the node must match for the*/
00040 /*  callback command to be invoked.  If a filter is not specified*/
00041 /*  then all nodes match that filter.*/
00042 /* */
00043 /*  Arguments:*/
00044 /*  node    DOM document node*/
00045 /*  cmd callback command*/
00046 /*  args    configuration options*/
00047 /* */
00048 /*  Results:*/
00049 /*  Depends on callback command*/
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 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1