home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 December / PCWorld_2000-12_cd.bin / Komunikace / Comanche / xuibuilder / TclDOM-1.6 / dommap.tcl < prev    next >
Text File  |  2000-11-02  |  3KB  |  115 lines

  1. # dommap.tcl --
  2. #
  3. #    Apply a mapping function to a DOM structure
  4. #
  5. # Copyright (c) 1998 Zveno Pty Ltd
  6. # http://www.zveno.com/
  7. #
  8. # Zveno makes this software available free of charge for any purpose.
  9. # Copies may be made of this software but all of this notice must be included
  10. # on any copy.
  11. #
  12. # The software was developed for research purposes only and Zveno does not
  13. # warrant that it is error free or fit for any purpose.  Zveno disclaims any
  14. # liability for all claims, expenses, losses, damages and costs any user may
  15. # incur as a result of using, copying or modifying this software.
  16. #
  17. # $Id: dommap.tcl,v 1.1.1.1 1996/02/22 06:06:14 daniel Exp $
  18.  
  19. package provide dommap 1.0
  20.  
  21. # We need the DOM
  22. package require dom 1.4
  23.  
  24. namespace eval dommap {
  25.     namespace export map
  26. }
  27.  
  28. # dommap::apply --
  29. #
  30. #    Apply a function to a DOM document.
  31. #
  32. #    The callback command is invoked with the node ID of the
  33. #    matching DOM node as its argument.  The command may return
  34. #    an error, continue or break code to alter the processing
  35. #    of further nodes.
  36. #
  37. #    Filter functions may be applied to match particular
  38. #    nodes.  Valid functions include:
  39. #
  40. #    -nodeType regexp
  41. #    -nodeName regexp
  42. #    -nodeValue regexp
  43. #    -attribute {regexp regexp}
  44. #
  45. #    If a filter is specified then the node must match for the
  46. #    callback command to be invoked.  If a filter is not specified
  47. #    then all nodes match that filter.
  48. #
  49. # Arguments:
  50. #    node    DOM document node
  51. #    cmd    callback command
  52. #    args    configuration options
  53. #
  54. # Results:
  55. #    Depends on callback command
  56.  
  57. proc dommap::apply {node cmd args} {
  58.     array set opts $args
  59.  
  60.     # Does this node match?
  61.     set match 1
  62.     catch {set match [expr $match && [regexp $opts(-nodeType) [::dom::node cget $node -nodeType]]]}
  63.     catch {set match [expr $match && [regexp $opts(-nodeName) [::dom::node cget $node -nodeName]]]}
  64.     catch {set match [expr $match && [regexp $opts(-nodeValue) [::dom::node cget $node -nodeValue]]]}
  65.     if {$match && ![string compare [::dom::node cget $node -nodeType] element]} {
  66.     set match 0
  67.     foreach {attrName attrValue} [array get [::dom::node cget $node -attributes]] {
  68.         set match 1
  69.         catch {set match [expr $match && [regexp [lindex $opts(-attribute) 0] $attrName]]}
  70.         catch {set match [expr $match && [regexp [lindex $opts(-attribute) 1] $attrValue]]}
  71.         if {$match} break
  72.     }
  73.     }
  74.     if {$match && [set code [catch {eval $cmd [list $node]} msg]]} {
  75.     switch $code {
  76.         0 {}
  77.         3 {
  78.         return -code break
  79.         }
  80.         4 {
  81.         return -code continue
  82.         }
  83.         default {
  84.         return -code error $msg
  85.         }
  86.     }
  87.     }
  88.  
  89.     # Process children
  90.     foreach child [::dom::node children $node] {
  91.     switch [catch {eval apply [list $child] [list $cmd] $args} msg] {
  92.         0 {
  93.         # No action required
  94.         }
  95.         3 {
  96.         # break
  97.         return -code break
  98.         }
  99.         4 {
  100.         # continue - skip processing of siblings
  101.         return
  102.         }
  103.         1 -
  104.         2 -
  105.         default {
  106.         # propagate the error message
  107.         return -code error $msg
  108.         }
  109.     }
  110.     }
  111.  
  112.     return {}
  113. }
  114.  
  115.