1.1 --- a/macrotest.py Sun Jan 16 01:15:01 2005 +0000
1.2 +++ b/macrotest.py Sun Jan 16 01:15:37 2005 +0000
1.3 @@ -5,15 +5,79 @@
1.4 from libxml2dom.macrolib import *
1.5 import libxml2
1.6 import xml.dom
1.7 -import sys
1.8 +
1.9 +class Container:
1.10 + pass
1.11 +
1.12 +doc = """<?xml version="1.0"?>
1.13 +<doc>
1.14 + <element attr="value">
1.15 + <subelement/>
1.16 + </element>
1.17 +</doc>
1.18 +"""
1.19 +
1.20 +def find_root(x2_d):
1.21 + x2_root = None
1.22 +
1.23 + # Property access should be transformed.
1.24 +
1.25 + for x2_n in x2_d.childNodes:
1.26 + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
1.27 + x2_root = x2_n
1.28 + break
1.29 +
1.30 + return x2_root
1.31 +
1.32 +def test():
1.33 + global doc
1.34 +
1.35 + # Assignment should not be transformed.
1.36 +
1.37 + x2_d = parseString(doc)
1.38 +
1.39 + # Not even within containers, and not special names alone.
1.40 +
1.41 + c = Container()
1.42 + c.x2_d = x2_d
1.43
1.44 -x2_d = libxml2.parseFile(sys.argv[1])
1.45 -x2_root = None
1.46 -for x2_n in x2_d.childNodes:
1.47 - if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
1.48 - x2_root = x2_n
1.49 - break
1.50 + # Find the root element.
1.51 +
1.52 + x2_root = find_root(x2_d)
1.53 + c.x2_root = x2_root
1.54 +
1.55 + # Create new attributes.
1.56 + # Method access should be transformed.
1.57 +
1.58 + x2_root.setAttributeNS("ns", "xxx:yyy", "zzz")
1.59 + c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ")
1.60 +
1.61 + # Create new elements.
1.62 + # Method access should be transformed.
1.63 +
1.64 + x2_new = x2_d.createElementNS("ns2", "ppp:qqq")
1.65 + x2_root.appendChild(x2_new)
1.66 + x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ")
1.67 + c.x2_root.appendChild(x2_new2)
1.68
1.69 -print x2_root.localName, x2_root.namespaceURI, x2_root.getAttributeNS(None, "xmlns")
1.70 + # Create new elements using ownerDocument.
1.71 + # Chaining properties is not
1.72 +
1.73 + x2_new3 = x2_new.ownerDocument.createElement("fff")
1.74 + x2_new.appendChild(x2_new3)
1.75 + x2_new4 = x2_new2.ownerDocument.createElement("FFF")
1.76 + x2_new2.appendChild(x2_new4)
1.77 +
1.78 + # Create a new document.
1.79 +
1.80 + x2_d2 = createDocument("nsD", "newdoc", None)
1.81 + x2_root2 = find_root(x2_d2)
1.82 +
1.83 + # Attempt to import nodes from the original document.
1.84 +
1.85 + x2_imported = x2_d2.importNode(x2_root, 1)
1.86 + x2_d2.replaceChild(x2_imported, x2_root2)
1.87 +
1.88 + return x2_d, x2_d2
1.89
1.90 # vim: tabstop=4 expandtab shiftwidth=4