3.12.12
[nijm-empathy.git] / tools / xincludator.py
blobf9ed49ce475ca9e4b4ce222db5d192f80042a20b
1 #!/usr/bin/python
3 import sys
4 from sys import argv, stdout, stderr
5 import codecs, locale
6 import os
7 import xml.dom.minidom
9 if sys.version_info[0] < 3:
10 stdout = codecs.getwriter('utf-8')(stdout)
12 NS_XI = 'http://www.w3.org/2001/XInclude'
14 def xincludate(dom, base, dropns = []):
15 remove_attrs = []
16 for i in range(dom.documentElement.attributes.length):
17 attr = dom.documentElement.attributes.item(i)
18 if attr.prefix == 'xmlns':
19 if attr.localName in dropns:
20 remove_attrs.append(attr)
21 else:
22 dropns.append(attr.localName)
23 for attr in remove_attrs:
24 dom.documentElement.removeAttributeNode(attr)
25 for include in dom.getElementsByTagNameNS(NS_XI, 'include'):
26 href = include.getAttribute('href')
27 # FIXME: assumes Unixy paths
28 filename = os.path.join(os.path.dirname(base), href)
29 subdom = xml.dom.minidom.parse(filename)
30 xincludate(subdom, filename, dropns)
31 if './' in href:
32 subdom.documentElement.setAttribute('xml:base', href)
33 include.parentNode.replaceChild(subdom.documentElement, include)
35 if __name__ == '__main__':
36 argv = argv[1:]
37 dom = xml.dom.minidom.parse(argv[0])
38 xincludate(dom, argv[0])
40 if sys.version_info[0] >= 3:
41 xml = dom.toxml(encoding=None)
42 else:
43 xml = dom.toxml()
45 stdout.write(xml)
46 stdout.write('\n')