Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2..../xml/sax
File: saxutils.py
"""\
[0] Fix | Delete
A library of useful helper classes to the SAX classes, for the
[1] Fix | Delete
convenience of application and driver writers.
[2] Fix | Delete
"""
[3] Fix | Delete
[4] Fix | Delete
import os, urlparse, urllib, types
[5] Fix | Delete
import io
[6] Fix | Delete
import sys
[7] Fix | Delete
import handler
[8] Fix | Delete
import xmlreader
[9] Fix | Delete
[10] Fix | Delete
try:
[11] Fix | Delete
_StringTypes = [types.StringType, types.UnicodeType]
[12] Fix | Delete
except AttributeError:
[13] Fix | Delete
_StringTypes = [types.StringType]
[14] Fix | Delete
[15] Fix | Delete
def __dict_replace(s, d):
[16] Fix | Delete
"""Replace substrings of a string using a dictionary."""
[17] Fix | Delete
for key, value in d.items():
[18] Fix | Delete
s = s.replace(key, value)
[19] Fix | Delete
return s
[20] Fix | Delete
[21] Fix | Delete
def escape(data, entities={}):
[22] Fix | Delete
"""Escape &, <, and > in a string of data.
[23] Fix | Delete
[24] Fix | Delete
You can escape other strings of data by passing a dictionary as
[25] Fix | Delete
the optional entities parameter. The keys and values must all be
[26] Fix | Delete
strings; each key will be replaced with its corresponding value.
[27] Fix | Delete
"""
[28] Fix | Delete
[29] Fix | Delete
# must do ampersand first
[30] Fix | Delete
data = data.replace("&", "&amp;")
[31] Fix | Delete
data = data.replace(">", "&gt;")
[32] Fix | Delete
data = data.replace("<", "&lt;")
[33] Fix | Delete
if entities:
[34] Fix | Delete
data = __dict_replace(data, entities)
[35] Fix | Delete
return data
[36] Fix | Delete
[37] Fix | Delete
def unescape(data, entities={}):
[38] Fix | Delete
"""Unescape &amp;, &lt;, and &gt; in a string of data.
[39] Fix | Delete
[40] Fix | Delete
You can unescape other strings of data by passing a dictionary as
[41] Fix | Delete
the optional entities parameter. The keys and values must all be
[42] Fix | Delete
strings; each key will be replaced with its corresponding value.
[43] Fix | Delete
"""
[44] Fix | Delete
data = data.replace("&lt;", "<")
[45] Fix | Delete
data = data.replace("&gt;", ">")
[46] Fix | Delete
if entities:
[47] Fix | Delete
data = __dict_replace(data, entities)
[48] Fix | Delete
# must do ampersand last
[49] Fix | Delete
return data.replace("&amp;", "&")
[50] Fix | Delete
[51] Fix | Delete
def quoteattr(data, entities={}):
[52] Fix | Delete
"""Escape and quote an attribute value.
[53] Fix | Delete
[54] Fix | Delete
Escape &, <, and > in a string of data, then quote it for use as
[55] Fix | Delete
an attribute value. The \" character will be escaped as well, if
[56] Fix | Delete
necessary.
[57] Fix | Delete
[58] Fix | Delete
You can escape other strings of data by passing a dictionary as
[59] Fix | Delete
the optional entities parameter. The keys and values must all be
[60] Fix | Delete
strings; each key will be replaced with its corresponding value.
[61] Fix | Delete
"""
[62] Fix | Delete
entities = entities.copy()
[63] Fix | Delete
entities.update({'\n': '&#10;', '\r': '&#13;', '\t':'&#9;'})
[64] Fix | Delete
data = escape(data, entities)
[65] Fix | Delete
if '"' in data:
[66] Fix | Delete
if "'" in data:
[67] Fix | Delete
data = '"%s"' % data.replace('"', "&quot;")
[68] Fix | Delete
else:
[69] Fix | Delete
data = "'%s'" % data
[70] Fix | Delete
else:
[71] Fix | Delete
data = '"%s"' % data
[72] Fix | Delete
return data
[73] Fix | Delete
[74] Fix | Delete
[75] Fix | Delete
def _gettextwriter(out, encoding):
[76] Fix | Delete
if out is None:
[77] Fix | Delete
import sys
[78] Fix | Delete
out = sys.stdout
[79] Fix | Delete
[80] Fix | Delete
if isinstance(out, io.RawIOBase):
[81] Fix | Delete
buffer = io.BufferedIOBase(out)
[82] Fix | Delete
# Keep the original file open when the TextIOWrapper is
[83] Fix | Delete
# destroyed
[84] Fix | Delete
buffer.close = lambda: None
[85] Fix | Delete
else:
[86] Fix | Delete
# This is to handle passed objects that aren't in the
[87] Fix | Delete
# IOBase hierarchy, but just have a write method
[88] Fix | Delete
buffer = io.BufferedIOBase()
[89] Fix | Delete
buffer.writable = lambda: True
[90] Fix | Delete
buffer.write = out.write
[91] Fix | Delete
try:
[92] Fix | Delete
# TextIOWrapper uses this methods to determine
[93] Fix | Delete
# if BOM (for UTF-16, etc) should be added
[94] Fix | Delete
buffer.seekable = out.seekable
[95] Fix | Delete
buffer.tell = out.tell
[96] Fix | Delete
except AttributeError:
[97] Fix | Delete
pass
[98] Fix | Delete
# wrap a binary writer with TextIOWrapper
[99] Fix | Delete
return _UnbufferedTextIOWrapper(buffer, encoding=encoding,
[100] Fix | Delete
errors='xmlcharrefreplace',
[101] Fix | Delete
newline='\n')
[102] Fix | Delete
[103] Fix | Delete
[104] Fix | Delete
class _UnbufferedTextIOWrapper(io.TextIOWrapper):
[105] Fix | Delete
def write(self, s):
[106] Fix | Delete
super(_UnbufferedTextIOWrapper, self).write(s)
[107] Fix | Delete
self.flush()
[108] Fix | Delete
[109] Fix | Delete
[110] Fix | Delete
class XMLGenerator(handler.ContentHandler):
[111] Fix | Delete
[112] Fix | Delete
def __init__(self, out=None, encoding="iso-8859-1"):
[113] Fix | Delete
handler.ContentHandler.__init__(self)
[114] Fix | Delete
out = _gettextwriter(out, encoding)
[115] Fix | Delete
self._write = out.write
[116] Fix | Delete
self._flush = out.flush
[117] Fix | Delete
self._ns_contexts = [{}] # contains uri -> prefix dicts
[118] Fix | Delete
self._current_context = self._ns_contexts[-1]
[119] Fix | Delete
self._undeclared_ns_maps = []
[120] Fix | Delete
self._encoding = encoding
[121] Fix | Delete
[122] Fix | Delete
def _qname(self, name):
[123] Fix | Delete
"""Builds a qualified name from a (ns_url, localname) pair"""
[124] Fix | Delete
if name[0]:
[125] Fix | Delete
# Per http://www.w3.org/XML/1998/namespace, The 'xml' prefix is
[126] Fix | Delete
# bound by definition to http://www.w3.org/XML/1998/namespace. It
[127] Fix | Delete
# does not need to be declared and will not usually be found in
[128] Fix | Delete
# self._current_context.
[129] Fix | Delete
if 'http://www.w3.org/XML/1998/namespace' == name[0]:
[130] Fix | Delete
return 'xml:' + name[1]
[131] Fix | Delete
# The name is in a non-empty namespace
[132] Fix | Delete
prefix = self._current_context[name[0]]
[133] Fix | Delete
if prefix:
[134] Fix | Delete
# If it is not the default namespace, prepend the prefix
[135] Fix | Delete
return prefix + ":" + name[1]
[136] Fix | Delete
# Return the unqualified name
[137] Fix | Delete
return name[1]
[138] Fix | Delete
[139] Fix | Delete
# ContentHandler methods
[140] Fix | Delete
[141] Fix | Delete
def startDocument(self):
[142] Fix | Delete
self._write(u'<?xml version="1.0" encoding="%s"?>\n' %
[143] Fix | Delete
self._encoding)
[144] Fix | Delete
[145] Fix | Delete
def endDocument(self):
[146] Fix | Delete
self._flush()
[147] Fix | Delete
[148] Fix | Delete
def startPrefixMapping(self, prefix, uri):
[149] Fix | Delete
self._ns_contexts.append(self._current_context.copy())
[150] Fix | Delete
self._current_context[uri] = prefix
[151] Fix | Delete
self._undeclared_ns_maps.append((prefix, uri))
[152] Fix | Delete
[153] Fix | Delete
def endPrefixMapping(self, prefix):
[154] Fix | Delete
self._current_context = self._ns_contexts[-1]
[155] Fix | Delete
del self._ns_contexts[-1]
[156] Fix | Delete
[157] Fix | Delete
def startElement(self, name, attrs):
[158] Fix | Delete
self._write(u'<' + name)
[159] Fix | Delete
for (name, value) in attrs.items():
[160] Fix | Delete
self._write(u' %s=%s' % (name, quoteattr(value)))
[161] Fix | Delete
self._write(u'>')
[162] Fix | Delete
[163] Fix | Delete
def endElement(self, name):
[164] Fix | Delete
self._write(u'</%s>' % name)
[165] Fix | Delete
[166] Fix | Delete
def startElementNS(self, name, qname, attrs):
[167] Fix | Delete
self._write(u'<' + self._qname(name))
[168] Fix | Delete
[169] Fix | Delete
for prefix, uri in self._undeclared_ns_maps:
[170] Fix | Delete
if prefix:
[171] Fix | Delete
self._write(u' xmlns:%s="%s"' % (prefix, uri))
[172] Fix | Delete
else:
[173] Fix | Delete
self._write(u' xmlns="%s"' % uri)
[174] Fix | Delete
self._undeclared_ns_maps = []
[175] Fix | Delete
[176] Fix | Delete
for (name, value) in attrs.items():
[177] Fix | Delete
self._write(u' %s=%s' % (self._qname(name), quoteattr(value)))
[178] Fix | Delete
self._write(u'>')
[179] Fix | Delete
[180] Fix | Delete
def endElementNS(self, name, qname):
[181] Fix | Delete
self._write(u'</%s>' % self._qname(name))
[182] Fix | Delete
[183] Fix | Delete
def characters(self, content):
[184] Fix | Delete
if not isinstance(content, unicode):
[185] Fix | Delete
content = unicode(content, self._encoding)
[186] Fix | Delete
self._write(escape(content))
[187] Fix | Delete
[188] Fix | Delete
def ignorableWhitespace(self, content):
[189] Fix | Delete
if not isinstance(content, unicode):
[190] Fix | Delete
content = unicode(content, self._encoding)
[191] Fix | Delete
self._write(content)
[192] Fix | Delete
[193] Fix | Delete
def processingInstruction(self, target, data):
[194] Fix | Delete
self._write(u'<?%s %s?>' % (target, data))
[195] Fix | Delete
[196] Fix | Delete
[197] Fix | Delete
class XMLFilterBase(xmlreader.XMLReader):
[198] Fix | Delete
"""This class is designed to sit between an XMLReader and the
[199] Fix | Delete
client application's event handlers. By default, it does nothing
[200] Fix | Delete
but pass requests up to the reader and events on to the handlers
[201] Fix | Delete
unmodified, but subclasses can override specific methods to modify
[202] Fix | Delete
the event stream or the configuration requests as they pass
[203] Fix | Delete
through."""
[204] Fix | Delete
[205] Fix | Delete
def __init__(self, parent = None):
[206] Fix | Delete
xmlreader.XMLReader.__init__(self)
[207] Fix | Delete
self._parent = parent
[208] Fix | Delete
[209] Fix | Delete
# ErrorHandler methods
[210] Fix | Delete
[211] Fix | Delete
def error(self, exception):
[212] Fix | Delete
self._err_handler.error(exception)
[213] Fix | Delete
[214] Fix | Delete
def fatalError(self, exception):
[215] Fix | Delete
self._err_handler.fatalError(exception)
[216] Fix | Delete
[217] Fix | Delete
def warning(self, exception):
[218] Fix | Delete
self._err_handler.warning(exception)
[219] Fix | Delete
[220] Fix | Delete
# ContentHandler methods
[221] Fix | Delete
[222] Fix | Delete
def setDocumentLocator(self, locator):
[223] Fix | Delete
self._cont_handler.setDocumentLocator(locator)
[224] Fix | Delete
[225] Fix | Delete
def startDocument(self):
[226] Fix | Delete
self._cont_handler.startDocument()
[227] Fix | Delete
[228] Fix | Delete
def endDocument(self):
[229] Fix | Delete
self._cont_handler.endDocument()
[230] Fix | Delete
[231] Fix | Delete
def startPrefixMapping(self, prefix, uri):
[232] Fix | Delete
self._cont_handler.startPrefixMapping(prefix, uri)
[233] Fix | Delete
[234] Fix | Delete
def endPrefixMapping(self, prefix):
[235] Fix | Delete
self._cont_handler.endPrefixMapping(prefix)
[236] Fix | Delete
[237] Fix | Delete
def startElement(self, name, attrs):
[238] Fix | Delete
self._cont_handler.startElement(name, attrs)
[239] Fix | Delete
[240] Fix | Delete
def endElement(self, name):
[241] Fix | Delete
self._cont_handler.endElement(name)
[242] Fix | Delete
[243] Fix | Delete
def startElementNS(self, name, qname, attrs):
[244] Fix | Delete
self._cont_handler.startElementNS(name, qname, attrs)
[245] Fix | Delete
[246] Fix | Delete
def endElementNS(self, name, qname):
[247] Fix | Delete
self._cont_handler.endElementNS(name, qname)
[248] Fix | Delete
[249] Fix | Delete
def characters(self, content):
[250] Fix | Delete
self._cont_handler.characters(content)
[251] Fix | Delete
[252] Fix | Delete
def ignorableWhitespace(self, chars):
[253] Fix | Delete
self._cont_handler.ignorableWhitespace(chars)
[254] Fix | Delete
[255] Fix | Delete
def processingInstruction(self, target, data):
[256] Fix | Delete
self._cont_handler.processingInstruction(target, data)
[257] Fix | Delete
[258] Fix | Delete
def skippedEntity(self, name):
[259] Fix | Delete
self._cont_handler.skippedEntity(name)
[260] Fix | Delete
[261] Fix | Delete
# DTDHandler methods
[262] Fix | Delete
[263] Fix | Delete
def notationDecl(self, name, publicId, systemId):
[264] Fix | Delete
self._dtd_handler.notationDecl(name, publicId, systemId)
[265] Fix | Delete
[266] Fix | Delete
def unparsedEntityDecl(self, name, publicId, systemId, ndata):
[267] Fix | Delete
self._dtd_handler.unparsedEntityDecl(name, publicId, systemId, ndata)
[268] Fix | Delete
[269] Fix | Delete
# EntityResolver methods
[270] Fix | Delete
[271] Fix | Delete
def resolveEntity(self, publicId, systemId):
[272] Fix | Delete
return self._ent_handler.resolveEntity(publicId, systemId)
[273] Fix | Delete
[274] Fix | Delete
# XMLReader methods
[275] Fix | Delete
[276] Fix | Delete
def parse(self, source):
[277] Fix | Delete
self._parent.setContentHandler(self)
[278] Fix | Delete
self._parent.setErrorHandler(self)
[279] Fix | Delete
self._parent.setEntityResolver(self)
[280] Fix | Delete
self._parent.setDTDHandler(self)
[281] Fix | Delete
self._parent.parse(source)
[282] Fix | Delete
[283] Fix | Delete
def setLocale(self, locale):
[284] Fix | Delete
self._parent.setLocale(locale)
[285] Fix | Delete
[286] Fix | Delete
def getFeature(self, name):
[287] Fix | Delete
return self._parent.getFeature(name)
[288] Fix | Delete
[289] Fix | Delete
def setFeature(self, name, state):
[290] Fix | Delete
self._parent.setFeature(name, state)
[291] Fix | Delete
[292] Fix | Delete
def getProperty(self, name):
[293] Fix | Delete
return self._parent.getProperty(name)
[294] Fix | Delete
[295] Fix | Delete
def setProperty(self, name, value):
[296] Fix | Delete
self._parent.setProperty(name, value)
[297] Fix | Delete
[298] Fix | Delete
# XMLFilter methods
[299] Fix | Delete
[300] Fix | Delete
def getParent(self):
[301] Fix | Delete
return self._parent
[302] Fix | Delete
[303] Fix | Delete
def setParent(self, parent):
[304] Fix | Delete
self._parent = parent
[305] Fix | Delete
[306] Fix | Delete
# --- Utility functions
[307] Fix | Delete
[308] Fix | Delete
def prepare_input_source(source, base = ""):
[309] Fix | Delete
"""This function takes an InputSource and an optional base URL and
[310] Fix | Delete
returns a fully resolved InputSource object ready for reading."""
[311] Fix | Delete
[312] Fix | Delete
if type(source) in _StringTypes:
[313] Fix | Delete
source = xmlreader.InputSource(source)
[314] Fix | Delete
elif hasattr(source, "read"):
[315] Fix | Delete
f = source
[316] Fix | Delete
source = xmlreader.InputSource()
[317] Fix | Delete
source.setByteStream(f)
[318] Fix | Delete
if hasattr(f, "name"):
[319] Fix | Delete
source.setSystemId(f.name)
[320] Fix | Delete
[321] Fix | Delete
if source.getByteStream() is None:
[322] Fix | Delete
try:
[323] Fix | Delete
sysid = source.getSystemId()
[324] Fix | Delete
basehead = os.path.dirname(os.path.normpath(base))
[325] Fix | Delete
encoding = sys.getfilesystemencoding()
[326] Fix | Delete
if isinstance(sysid, unicode):
[327] Fix | Delete
if not isinstance(basehead, unicode):
[328] Fix | Delete
try:
[329] Fix | Delete
basehead = basehead.decode(encoding)
[330] Fix | Delete
except UnicodeDecodeError:
[331] Fix | Delete
sysid = sysid.encode(encoding)
[332] Fix | Delete
else:
[333] Fix | Delete
if isinstance(basehead, unicode):
[334] Fix | Delete
try:
[335] Fix | Delete
sysid = sysid.decode(encoding)
[336] Fix | Delete
except UnicodeDecodeError:
[337] Fix | Delete
basehead = basehead.encode(encoding)
[338] Fix | Delete
sysidfilename = os.path.join(basehead, sysid)
[339] Fix | Delete
isfile = os.path.isfile(sysidfilename)
[340] Fix | Delete
except UnicodeError:
[341] Fix | Delete
isfile = False
[342] Fix | Delete
if isfile:
[343] Fix | Delete
source.setSystemId(sysidfilename)
[344] Fix | Delete
f = open(sysidfilename, "rb")
[345] Fix | Delete
else:
[346] Fix | Delete
source.setSystemId(urlparse.urljoin(base, source.getSystemId()))
[347] Fix | Delete
f = urllib.urlopen(source.getSystemId())
[348] Fix | Delete
[349] Fix | Delete
source.setByteStream(f)
[350] Fix | Delete
[351] Fix | Delete
return source
[352] Fix | Delete
[353] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function