W3C DOM access to trees

Introduction

This facility can be used to put a subset of the W3C DOM (created for XML documents) on trees consisting of Python objects. The subset is:

The DOM layer is very efficient - only an additional API is added on the existing tree. For the same reason the DOM view is "live", so that changes done in the tree is immediately reflected.

Why?

Also, if write support is added it means that any XML transformation can be used - but that will probably not happen.

Adding DOM support to a tree

In order to provide DOM access to your tree, these steps must be followed:

The children can then be accessed as normal attributes, however the assigned object's parentNode will automatically be updated on assignment and set to None when something else is assigned instead. It is ok to assign lists to the attributes, if so, the parentNode of all the items in the list (which are assumed to also be nodes) are updated as they are added to or removed from the list.

Example:

   1 class IfNode(PyDOMNode)
   2     __childattrs__ = ["condition", "body"]
   3 
   4 
   5 ifnode = IfNode()
   6 # let a, b, c and d be simple statements...
   7 
   8 ifnode.body = a
   9 assert a.parentNode == ifnode
  10 ifnode.body = b
  11 assert a.parentNode == None
  12 ifnode.body = [a, b, c]
  13 assert b.parentNode == ifnode and c.parentNode == ifnode
  14 del ifnode.body[2]
  15 assert c.parentNode == None
  16 ifnode.body = c
  17 assert b.parentNode == None

BTW, this is all easily implemented using a metaclass creating property accessors...

Example/current progress

I currently have this running:

   1 A = parse_string_to_pyrex_tree("""
   2 a = True
   3 if a:
   4     print "Hello"
   5 """)
   6 
   7 dumpxml(A)

using the standard Cython parser, yielding

<?xml version='1.0' encoding='UTF-8'?>
<pyr:ModuleNode xmlns:pyr='cython:pyrextree'>
  <body>
    <pyr:StatListNode>
      <stats>
        <pyr:SingleAssignmentNode>
          <lhs>
            <pyr:NameNode name='a'/>
          </lhs>
          <rhs>
            <pyr:BoolNode value='True'/>
          </rhs>
        </pyr:SingleAssignmentNode>
        <pyr:IfStatNode>
          <if_clauses>
            <pyr:IfClauseNode>
              <condition>
                <pyr:NameNode name='a'/>
              </condition>
              <body>
                <pyr:PrintStatNode>
                  <args>
                    <pyr:StringNode value='Hello'/>
                  </args>
                </pyr:PrintStatNode>
              </body>
            </pyr:IfClauseNode>
          </if_clauses>
        </pyr:IfStatNode>
      </stats>
    </pyr:StatListNode>
  </body>
</pyr:ModuleNode>

enhancements/w3cdom (last edited 2009-03-01 01:02:24 by localhost)