Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 1 | """xmlWriter.py -- Simple XML authoring class""" |
| 2 | |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 3 | import string |
| 4 | import struct |
Just | e414c92 | 2000-01-04 13:51:59 +0000 | [diff] [blame] | 5 | import os |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 6 | |
| 7 | INDENT = " " |
| 8 | |
jvr | 81b0c2b | 2002-09-09 18:17:12 +0000 | [diff] [blame^] | 9 | |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 10 | class XMLWriter: |
| 11 | |
jvr | 81b0c2b | 2002-09-09 18:17:12 +0000 | [diff] [blame^] | 12 | def __init__(self, file, indentwhite=INDENT, idlefunc=None, encoding="ISO-8859-1"): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 13 | if type(file) == type(""): |
| 14 | self.file = open(file, "w") |
Just | e414c92 | 2000-01-04 13:51:59 +0000 | [diff] [blame] | 15 | if os.name == "mac": |
| 16 | import macfs |
| 17 | macfs.FSSpec(file).SetCreatorType('R*ch', 'TEXT') |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 18 | else: |
| 19 | # assume writable file object |
| 20 | self.file = file |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 21 | self.indentwhite = indentwhite |
| 22 | self.indentlevel = 0 |
| 23 | self.stack = [] |
| 24 | self.needindent = 1 |
jvr | 33f3327 | 2002-07-23 16:41:08 +0000 | [diff] [blame] | 25 | self.idlefunc = idlefunc |
| 26 | self.idlecounter = 0 |
jvr | 81b0c2b | 2002-09-09 18:17:12 +0000 | [diff] [blame^] | 27 | if encoding: |
| 28 | self.writeraw('<?xml version="1.0" encoding="%s"?>' % encoding) |
| 29 | else: |
| 30 | self.writeraw('<?xml version="1.0"?>') |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 31 | self.newline() |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 32 | |
| 33 | def close(self): |
| 34 | self.file.close() |
| 35 | |
| 36 | def write(self, data): |
| 37 | self.writeraw(escape(data)) |
| 38 | |
| 39 | def write_noindent(self, data): |
| 40 | self.file.write(escape(data)) |
| 41 | |
| 42 | def write8bit(self, data): |
| 43 | self.writeraw(escape8bit(data)) |
| 44 | |
| 45 | def write16bit(self, data): |
| 46 | self.writeraw(escape16bit(data)) |
| 47 | |
| 48 | def writeraw(self, data): |
| 49 | if self.needindent: |
| 50 | self.file.write(self.indentlevel * self.indentwhite) |
| 51 | self.needindent = 0 |
| 52 | self.file.write(data) |
| 53 | |
| 54 | def newline(self): |
| 55 | self.file.write("\n") |
| 56 | self.needindent = 1 |
jvr | 33f3327 | 2002-07-23 16:41:08 +0000 | [diff] [blame] | 57 | idlecounter = self.idlecounter |
| 58 | if not idlecounter % 100 and self.idlefunc is not None: |
| 59 | self.idlefunc() |
| 60 | self.idlecounter = idlecounter + 1 |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 61 | |
| 62 | def comment(self, data): |
| 63 | data = escape(data) |
| 64 | lines = string.split(data, "\n") |
| 65 | self.writeraw("<!-- " + lines[0]) |
| 66 | for line in lines[1:]: |
| 67 | self.newline() |
| 68 | self.writeraw(" " + line) |
| 69 | self.writeraw(" -->") |
| 70 | |
| 71 | def simpletag(self, _TAG_, *args, **kwargs): |
| 72 | attrdata = apply(self.stringifyattrs, args, kwargs) |
| 73 | data = "<%s%s/>" % (_TAG_, attrdata) |
| 74 | self.writeraw(data) |
| 75 | |
| 76 | def begintag(self, _TAG_, *args, **kwargs): |
| 77 | attrdata = apply(self.stringifyattrs, args, kwargs) |
| 78 | data = "<%s%s>" % (_TAG_, attrdata) |
| 79 | self.writeraw(data) |
| 80 | self.stack.append(_TAG_) |
| 81 | self.indent() |
| 82 | |
| 83 | def endtag(self, _TAG_): |
| 84 | assert self.stack and self.stack[-1] == _TAG_, "nonmatching endtag" |
| 85 | del self.stack[-1] |
| 86 | self.dedent() |
| 87 | data = "</%s>" % _TAG_ |
| 88 | self.writeraw(data) |
| 89 | |
| 90 | def dumphex(self, data): |
| 91 | linelength = 16 |
| 92 | hexlinelength = linelength * 2 |
| 93 | chunksize = 8 |
| 94 | for i in range(0, len(data), linelength): |
| 95 | hexline = hexStr(data[i:i+linelength]) |
| 96 | line = "" |
| 97 | white = "" |
| 98 | for j in range(0, hexlinelength, chunksize): |
| 99 | line = line + white + hexline[j:j+chunksize] |
| 100 | white = " " |
| 101 | self.writeraw(line) |
| 102 | self.newline() |
| 103 | |
| 104 | def indent(self): |
| 105 | self.indentlevel = self.indentlevel + 1 |
| 106 | |
| 107 | def dedent(self): |
| 108 | assert self.indentlevel > 0 |
| 109 | self.indentlevel = self.indentlevel - 1 |
| 110 | |
| 111 | def stringifyattrs(self, *args, **kwargs): |
| 112 | if kwargs: |
| 113 | assert not args |
| 114 | attributes = kwargs.items() |
| 115 | attributes.sort() |
| 116 | elif args: |
| 117 | assert len(args) == 1 |
| 118 | attributes = args[0] |
| 119 | else: |
| 120 | return "" |
| 121 | data = "" |
| 122 | for attr, value in attributes: |
| 123 | data = data + ' %s="%s"' % (attr, escapeattr(str(value))) |
| 124 | return data |
| 125 | |
| 126 | |
| 127 | def escape(data): |
| 128 | data = string.replace(data, "&", "&") |
| 129 | data = string.replace(data, "<", "<") |
| 130 | return data |
| 131 | |
| 132 | def escapeattr(data): |
| 133 | data = string.replace(data, "&", "&") |
| 134 | data = string.replace(data, "<", "<") |
| 135 | data = string.replace(data, '"', """) |
| 136 | return data |
| 137 | |
| 138 | def escape8bit(data): |
| 139 | def escapechar(c): |
| 140 | n = ord(c) |
| 141 | if c in "<&": |
| 142 | if c == "&": |
| 143 | return "&" |
| 144 | else: |
| 145 | return "<" |
| 146 | elif 32 <= n <= 127: |
| 147 | return c |
| 148 | else: |
| 149 | return "&#" + `n` + ";" |
| 150 | return string.join(map(escapechar, data), "") |
| 151 | |
| 152 | needswap = struct.pack("h", 1) == "\001\000" |
| 153 | |
| 154 | def escape16bit(data): |
| 155 | import array |
| 156 | a = array.array("H") |
| 157 | a.fromstring(data) |
| 158 | if needswap: |
| 159 | a.byteswap() |
| 160 | def escapenum(n, amp=ord("&"), lt=ord("<")): |
| 161 | if n == amp: |
| 162 | return "&" |
| 163 | elif n == lt: |
| 164 | return "<" |
| 165 | elif 32 <= n <= 127: |
| 166 | return chr(n) |
| 167 | else: |
| 168 | return "&#" + `n` + ";" |
| 169 | return string.join(map(escapenum, a), "") |
| 170 | |
| 171 | |
| 172 | def hexStr(s): |
| 173 | h = string.hexdigits |
| 174 | r = '' |
| 175 | for c in s: |
| 176 | i = ord(c) |
| 177 | r = r + h[(i >> 4) & 0xF] + h[i & 0xF] |
| 178 | return r |
| 179 | |