]> git.sesse.net Git - xml-template/blobdiff - python/xmltemplate.py
More style fixes for php5.
[xml-template] / python / xmltemplate.py
index 8f2b9222eab4340ee0866138d3df8ce83f42133c..16225926876eef7dc95791a7c40401af6681b707 100644 (file)
@@ -9,12 +9,21 @@ def process_file(filename, obj, clean = True):
 
 def process(node, obj, clean = True):
        if isinstance(obj, str):                        # overwrite
-               for child in node.childNodes:
-                       node.removeChild(child)
+               while not node.firstChild is None:
+                       node.removeChild(node.firstChild)
                doc = _get_document_element(node)
                node.appendChild(doc.createTextNode(obj))
        elif isinstance(obj, xml.dom.Node):             # overwrite
-               pass
+               while not node.firstChild is None:
+                       node.removeChild(node.firstChild)
+
+               if isinstance(obj, xml.dom.minidom.Document):
+                       obj = obj.documentElement
+
+               newobj = obj.cloneNode(True)
+               node.appendChild(newobj)
+
+               process(newobj, {}, clean)
        elif isinstance(obj, dict):                     # substitute
                for child in node.childNodes:
                        processed = False
@@ -23,13 +32,21 @@ def process(node, obj, clean = True):
                                id = None
 
                                attrs = child.attributes
+                               attrs_to_remove = []
                                if not attrs is None:
                                        for i in range(attrs.length):
                                                attr = attrs.item(0)
-                                               if attr.namespaceURI == "http://template.sesse.net/" or attr.name == "id":
+                                               if attr.namespaceURI == "http://template.sesse.net/" and attr.localName == "id":
                                                        id = attr.value
                                                        if clean:
-                                                               child.removeAttribute(attr.name)
+                                                               attrs_to_remove.append(attr.name)
+                                               if attr.name.startswith("xmlns:") and attr.value == "http://template.sesse.net/" and clean:
+                                                       attrs_to_remove.append(attr.name)
+
+                                       for a in attrs_to_remove:
+                                               if child.hasAttribute(a):
+                                                       child.removeAttribute(a)
+
 
                                # check all substitutions to see if we found anything
                                # appropriate
@@ -56,11 +73,12 @@ def process(node, obj, clean = True):
                        frag.appendChild(child)
 
                for instance in obj:
-                       newnode = frag.cloneNode(True)
-                       node.appendChild(newnode)
-                       process(newnode, instance, clean)
-                       if clean:
-                               _clean(newnode)
+                       if instance is not None:
+                               newnode = frag.cloneNode(True)
+                               node.appendChild(newnode)
+                               process(newnode, instance, clean)
+                               if clean:
+                                       _clean(newnode)
 
                # remove all the <fragment> tags
 
@@ -80,10 +98,27 @@ def process(node, obj, clean = True):
                _clean(node)
 
 def alternate(tag, array, *elems):
+       i = 0
+       for ref in array:
+               if ref is not None:
+                       ref[tag] = elems[i % len(elems)]
+                       i = i + 1
+               
        return array
 
 def _clean(node):
-       pass
+       if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.namespaceURI == "http://template.sesse.net/":
+               # as this is a dummy node, we want to remove it and move everything further up
+               # after we've done any required replacements
+               doc = _get_document_element(node)
+               parent = node.parentNode
+
+               while not node.firstChild is None:
+                       child = node.firstChild
+                       node.removeChild(child)
+                       parent.insertBefore(child, node)
+
+               parent.removeChild(node)
 
 # ugh
 def _get_document_element(node):