When updating the tree, a node could become insensitive.
[dom-editor.git] / Dome / support.py
bloba8986a242016e6d6204437313e6481770ae822fe
1 import rox
3 import sys
4 import traceback
5 from Ft.Xml.Domlette import Node, implementation
6 from Ft.Xml import XMLNS_NAMESPACE
7 from Ft.Xml.Lib.Nss import GetAllNs
9 from string import find, lower, join
10 from socket import gethostbyaddr, gethostname
12 def node_to_xml(node):
13 "Takes an XML node and returns an XML documentElement suitable for saving."
14 root = implementation.createDocument(None, 'root', None)
15 new = node.cloneNode(deep = 1)
16 new = root.importNode(new, deep = 1)
17 root.replaceChild(new, root.documentElement)
18 return root
20 def node_to_html(node):
21 "Takes an XML node and returns an HTML documentElement suitable for saving."
22 root = implementation.createHTMLDocument('HTML document')
23 def html(doc, node, html):
24 new = doc.importNode(node.cloneNode(deep = 0), deep = 0)
25 if node.nodeType == Node.ELEMENT_NODE:
26 for a in node.attributes:
27 new.setAttribute(a.localName, a.value)
28 for k in node.childNodes:
29 new.appendChild(html(doc, k, html))
30 return new
31 new = html(root, node, html)
32 root.replaceChild(new, root.documentElement)
33 return root
35 def send_to_file(data, path):
36 try:
37 file = open(path, 'wb')
38 try:
39 file.write(data)
40 finally:
41 file.close()
42 except:
43 rox.report_exception()
44 return 0
46 return 1
48 def import_with_ns(doc, node):
49 nss = GetAllNs(node)
51 node = doc.importNode(node, 1)
52 for ns in nss.keys():
53 if ns == 'xml':
54 continue
55 uri = nss[ns]
56 if ns or uri:
57 if ns is None:
58 ns = 'xmlns'
59 else:
60 ns = 'xmlns:' + ns
61 node.setAttributeNS(XMLNS_NAMESPACE, ns, uri)
62 return node