-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyx2xml.py
executable file
·79 lines (73 loc) · 2.4 KB
/
pyx2xml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain a
# copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Inspired by Sean McGrath's pyx2xml, and David Mertz' XML matters
# Cobbled together by Thorsten Behrens <[email protected]>
#
import sys, os, re
def grabNS (qname):
global num_ns, uris
delimit = qname.find('}')
uri = qname[1:delimit]
name = qname[delimit+1:]
if not uri in uris:
num_ns += 1
uris[uri] = 'ns'+str(num_ns)
return uris[uri] + ':' + name
num_ns = 0
get_attrs = 0
uris = {}
lines = []
unescape = lambda s: s.replace(r'\t','\t').replace(r'\n','\n').replace(r'\\','\\')
ns_handling = len(sys.argv) > 1 and sys.argv[1] == '-ns'
if ns_handling:
writeln = lambda s: lines.append(s)
else:
writeln = lambda s: sys.stdout.write(s+'\n')
writeln('<?xml version="1.0" encoding="UTF-8"?>')
curr_line=""
for line in sys.stdin:
if get_attrs and line[0] != 'A':
# attr section ends here
get_attrs = 0
curr_line += '>'
if line[0] == '?':
writeln(curr_line+'<?%s?>' % line[1:-1])
curr_line=""
elif line[0] == '(':
curr_line += '<%s' % grabNS(line[1:-1])
get_attrs = 1
elif line[0] == 'A':
name,val = line[1:].split(None, 1)
curr_line += ' %s="%s"' % (grabNS(name), unescape(val)[:-1])
elif line[:3] == r'-\n':
writeln(curr_line)
curr_line=""
elif line[0] == '-':
curr_line += unescape(line[1:-1])
elif line[0] == ')':
curr_line += '</%s>' % grabNS(line[1:-1])
if len(curr_line):
writeln(curr_line)
if ns_handling:
opening_tag=re.compile("(\\s*<\\s*)([^\\? \\t\\n\\r\\f\\v]+)")
ns_written=False
for line in lines:
if not ns_written and re.match(opening_tag,line):
line = re.split(opening_tag,line)
line.insert(3, ' ' + ' '.join(['xmlns:'+n+'="'+u+'"' for (u,n) in uris.items()]))
sys.stdout.write(''.join(line) + '\n')
ns_written = True
else:
sys.stdout.write(line+'\n')