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