Changes an XML data stream on the fly to specify a new encoding data is a raw sequence of bytes (not Unicode) that is presumed to be in %encoding already encoding is a string recognized by encodings.aliases
(data, encoding)
| 3716 | return true_encoding, http_encoding, xml_encoding, sniffed_xml_encoding, acceptable_content_type |
| 3717 | |
| 3718 | def _toUTF8(data, encoding): |
| 3719 | '''Changes an XML data stream on the fly to specify a new encoding |
| 3720 | |
| 3721 | data is a raw sequence of bytes (not Unicode) that is presumed to be in %encoding already |
| 3722 | encoding is a string recognized by encodings.aliases |
| 3723 | ''' |
| 3724 | # strip Byte Order Mark (if present) |
| 3725 | if (len(data) >= 4) and (data[:2] == _l2bytes([0xfe, 0xff])) and (data[2:4] != _l2bytes([0x00, 0x00])): |
| 3726 | encoding = 'utf-16be' |
| 3727 | data = data[2:] |
| 3728 | elif (len(data) >= 4) and (data[:2] == _l2bytes([0xff, 0xfe])) and (data[2:4] != _l2bytes([0x00, 0x00])): |
| 3729 | encoding = 'utf-16le' |
| 3730 | data = data[2:] |
| 3731 | elif data[:3] == _l2bytes([0xef, 0xbb, 0xbf]): |
| 3732 | encoding = 'utf-8' |
| 3733 | data = data[3:] |
| 3734 | elif data[:4] == _l2bytes([0x00, 0x00, 0xfe, 0xff]): |
| 3735 | encoding = 'utf-32be' |
| 3736 | data = data[4:] |
| 3737 | elif data[:4] == _l2bytes([0xff, 0xfe, 0x00, 0x00]): |
| 3738 | encoding = 'utf-32le' |
| 3739 | data = data[4:] |
| 3740 | newdata = unicode(data, encoding) |
| 3741 | declmatch = re.compile('^<\?xml[^>]*?>') |
| 3742 | newdecl = '''<?xml version='1.0' encoding='utf-8'?>''' |
| 3743 | if declmatch.search(newdata): |
| 3744 | newdata = declmatch.sub(newdecl, newdata) |
| 3745 | else: |
| 3746 | newdata = newdecl + u'\n' + newdata |
| 3747 | return newdata.encode('utf-8') |
| 3748 | |
| 3749 | def _stripDoctype(data): |
| 3750 | '''Strips DOCTYPE from XML document, returns (rss_version, stripped_data) |