Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2..../xml/sax
File: xmlreader.py
"""An XML Reader is the SAX 2 name for an XML parser. XML Parsers
[0] Fix | Delete
should be based on this code. """
[1] Fix | Delete
[2] Fix | Delete
import handler
[3] Fix | Delete
[4] Fix | Delete
from _exceptions import SAXNotSupportedException, SAXNotRecognizedException
[5] Fix | Delete
[6] Fix | Delete
[7] Fix | Delete
# ===== XMLREADER =====
[8] Fix | Delete
[9] Fix | Delete
class XMLReader:
[10] Fix | Delete
"""Interface for reading an XML document using callbacks.
[11] Fix | Delete
[12] Fix | Delete
XMLReader is the interface that an XML parser's SAX2 driver must
[13] Fix | Delete
implement. This interface allows an application to set and query
[14] Fix | Delete
features and properties in the parser, to register event handlers
[15] Fix | Delete
for document processing, and to initiate a document parse.
[16] Fix | Delete
[17] Fix | Delete
All SAX interfaces are assumed to be synchronous: the parse
[18] Fix | Delete
methods must not return until parsing is complete, and readers
[19] Fix | Delete
must wait for an event-handler callback to return before reporting
[20] Fix | Delete
the next event."""
[21] Fix | Delete
[22] Fix | Delete
def __init__(self):
[23] Fix | Delete
self._cont_handler = handler.ContentHandler()
[24] Fix | Delete
self._dtd_handler = handler.DTDHandler()
[25] Fix | Delete
self._ent_handler = handler.EntityResolver()
[26] Fix | Delete
self._err_handler = handler.ErrorHandler()
[27] Fix | Delete
[28] Fix | Delete
def parse(self, source):
[29] Fix | Delete
"Parse an XML document from a system identifier or an InputSource."
[30] Fix | Delete
raise NotImplementedError("This method must be implemented!")
[31] Fix | Delete
[32] Fix | Delete
def getContentHandler(self):
[33] Fix | Delete
"Returns the current ContentHandler."
[34] Fix | Delete
return self._cont_handler
[35] Fix | Delete
[36] Fix | Delete
def setContentHandler(self, handler):
[37] Fix | Delete
"Registers a new object to receive document content events."
[38] Fix | Delete
self._cont_handler = handler
[39] Fix | Delete
[40] Fix | Delete
def getDTDHandler(self):
[41] Fix | Delete
"Returns the current DTD handler."
[42] Fix | Delete
return self._dtd_handler
[43] Fix | Delete
[44] Fix | Delete
def setDTDHandler(self, handler):
[45] Fix | Delete
"Register an object to receive basic DTD-related events."
[46] Fix | Delete
self._dtd_handler = handler
[47] Fix | Delete
[48] Fix | Delete
def getEntityResolver(self):
[49] Fix | Delete
"Returns the current EntityResolver."
[50] Fix | Delete
return self._ent_handler
[51] Fix | Delete
[52] Fix | Delete
def setEntityResolver(self, resolver):
[53] Fix | Delete
"Register an object to resolve external entities."
[54] Fix | Delete
self._ent_handler = resolver
[55] Fix | Delete
[56] Fix | Delete
def getErrorHandler(self):
[57] Fix | Delete
"Returns the current ErrorHandler."
[58] Fix | Delete
return self._err_handler
[59] Fix | Delete
[60] Fix | Delete
def setErrorHandler(self, handler):
[61] Fix | Delete
"Register an object to receive error-message events."
[62] Fix | Delete
self._err_handler = handler
[63] Fix | Delete
[64] Fix | Delete
def setLocale(self, locale):
[65] Fix | Delete
"""Allow an application to set the locale for errors and warnings.
[66] Fix | Delete
[67] Fix | Delete
SAX parsers are not required to provide localization for errors
[68] Fix | Delete
and warnings; if they cannot support the requested locale,
[69] Fix | Delete
however, they must raise a SAX exception. Applications may
[70] Fix | Delete
request a locale change in the middle of a parse."""
[71] Fix | Delete
raise SAXNotSupportedException("Locale support not implemented")
[72] Fix | Delete
[73] Fix | Delete
def getFeature(self, name):
[74] Fix | Delete
"Looks up and returns the state of a SAX2 feature."
[75] Fix | Delete
raise SAXNotRecognizedException("Feature '%s' not recognized" % name)
[76] Fix | Delete
[77] Fix | Delete
def setFeature(self, name, state):
[78] Fix | Delete
"Sets the state of a SAX2 feature."
[79] Fix | Delete
raise SAXNotRecognizedException("Feature '%s' not recognized" % name)
[80] Fix | Delete
[81] Fix | Delete
def getProperty(self, name):
[82] Fix | Delete
"Looks up and returns the value of a SAX2 property."
[83] Fix | Delete
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
[84] Fix | Delete
[85] Fix | Delete
def setProperty(self, name, value):
[86] Fix | Delete
"Sets the value of a SAX2 property."
[87] Fix | Delete
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
[88] Fix | Delete
[89] Fix | Delete
class IncrementalParser(XMLReader):
[90] Fix | Delete
"""This interface adds three extra methods to the XMLReader
[91] Fix | Delete
interface that allow XML parsers to support incremental
[92] Fix | Delete
parsing. Support for this interface is optional, since not all
[93] Fix | Delete
underlying XML parsers support this functionality.
[94] Fix | Delete
[95] Fix | Delete
When the parser is instantiated it is ready to begin accepting
[96] Fix | Delete
data from the feed method immediately. After parsing has been
[97] Fix | Delete
finished with a call to close the reset method must be called to
[98] Fix | Delete
make the parser ready to accept new data, either from feed or
[99] Fix | Delete
using the parse method.
[100] Fix | Delete
[101] Fix | Delete
Note that these methods must _not_ be called during parsing, that
[102] Fix | Delete
is, after parse has been called and before it returns.
[103] Fix | Delete
[104] Fix | Delete
By default, the class also implements the parse method of the XMLReader
[105] Fix | Delete
interface using the feed, close and reset methods of the
[106] Fix | Delete
IncrementalParser interface as a convenience to SAX 2.0 driver
[107] Fix | Delete
writers."""
[108] Fix | Delete
[109] Fix | Delete
def __init__(self, bufsize=2**16):
[110] Fix | Delete
self._bufsize = bufsize
[111] Fix | Delete
XMLReader.__init__(self)
[112] Fix | Delete
[113] Fix | Delete
def parse(self, source):
[114] Fix | Delete
import saxutils
[115] Fix | Delete
source = saxutils.prepare_input_source(source)
[116] Fix | Delete
[117] Fix | Delete
self.prepareParser(source)
[118] Fix | Delete
file = source.getByteStream()
[119] Fix | Delete
buffer = file.read(self._bufsize)
[120] Fix | Delete
while buffer != "":
[121] Fix | Delete
self.feed(buffer)
[122] Fix | Delete
buffer = file.read(self._bufsize)
[123] Fix | Delete
self.close()
[124] Fix | Delete
[125] Fix | Delete
def feed(self, data):
[126] Fix | Delete
"""This method gives the raw XML data in the data parameter to
[127] Fix | Delete
the parser and makes it parse the data, emitting the
[128] Fix | Delete
corresponding events. It is allowed for XML constructs to be
[129] Fix | Delete
split across several calls to feed.
[130] Fix | Delete
[131] Fix | Delete
feed may raise SAXException."""
[132] Fix | Delete
raise NotImplementedError("This method must be implemented!")
[133] Fix | Delete
[134] Fix | Delete
def prepareParser(self, source):
[135] Fix | Delete
"""This method is called by the parse implementation to allow
[136] Fix | Delete
the SAX 2.0 driver to prepare itself for parsing."""
[137] Fix | Delete
raise NotImplementedError("prepareParser must be overridden!")
[138] Fix | Delete
[139] Fix | Delete
def close(self):
[140] Fix | Delete
"""This method is called when the entire XML document has been
[141] Fix | Delete
passed to the parser through the feed method, to notify the
[142] Fix | Delete
parser that there are no more data. This allows the parser to
[143] Fix | Delete
do the final checks on the document and empty the internal
[144] Fix | Delete
data buffer.
[145] Fix | Delete
[146] Fix | Delete
The parser will not be ready to parse another document until
[147] Fix | Delete
the reset method has been called.
[148] Fix | Delete
[149] Fix | Delete
close may raise SAXException."""
[150] Fix | Delete
raise NotImplementedError("This method must be implemented!")
[151] Fix | Delete
[152] Fix | Delete
def reset(self):
[153] Fix | Delete
"""This method is called after close has been called to reset
[154] Fix | Delete
the parser so that it is ready to parse new documents. The
[155] Fix | Delete
results of calling parse or feed after close without calling
[156] Fix | Delete
reset are undefined."""
[157] Fix | Delete
raise NotImplementedError("This method must be implemented!")
[158] Fix | Delete
[159] Fix | Delete
# ===== LOCATOR =====
[160] Fix | Delete
[161] Fix | Delete
class Locator:
[162] Fix | Delete
"""Interface for associating a SAX event with a document
[163] Fix | Delete
location. A locator object will return valid results only during
[164] Fix | Delete
calls to DocumentHandler methods; at any other time, the
[165] Fix | Delete
results are unpredictable."""
[166] Fix | Delete
[167] Fix | Delete
def getColumnNumber(self):
[168] Fix | Delete
"Return the column number where the current event ends."
[169] Fix | Delete
return -1
[170] Fix | Delete
[171] Fix | Delete
def getLineNumber(self):
[172] Fix | Delete
"Return the line number where the current event ends."
[173] Fix | Delete
return -1
[174] Fix | Delete
[175] Fix | Delete
def getPublicId(self):
[176] Fix | Delete
"Return the public identifier for the current event."
[177] Fix | Delete
return None
[178] Fix | Delete
[179] Fix | Delete
def getSystemId(self):
[180] Fix | Delete
"Return the system identifier for the current event."
[181] Fix | Delete
return None
[182] Fix | Delete
[183] Fix | Delete
# ===== INPUTSOURCE =====
[184] Fix | Delete
[185] Fix | Delete
class InputSource:
[186] Fix | Delete
"""Encapsulation of the information needed by the XMLReader to
[187] Fix | Delete
read entities.
[188] Fix | Delete
[189] Fix | Delete
This class may include information about the public identifier,
[190] Fix | Delete
system identifier, byte stream (possibly with character encoding
[191] Fix | Delete
information) and/or the character stream of an entity.
[192] Fix | Delete
[193] Fix | Delete
Applications will create objects of this class for use in the
[194] Fix | Delete
XMLReader.parse method and for returning from
[195] Fix | Delete
EntityResolver.resolveEntity.
[196] Fix | Delete
[197] Fix | Delete
An InputSource belongs to the application, the XMLReader is not
[198] Fix | Delete
allowed to modify InputSource objects passed to it from the
[199] Fix | Delete
application, although it may make copies and modify those."""
[200] Fix | Delete
[201] Fix | Delete
def __init__(self, system_id = None):
[202] Fix | Delete
self.__system_id = system_id
[203] Fix | Delete
self.__public_id = None
[204] Fix | Delete
self.__encoding = None
[205] Fix | Delete
self.__bytefile = None
[206] Fix | Delete
self.__charfile = None
[207] Fix | Delete
[208] Fix | Delete
def setPublicId(self, public_id):
[209] Fix | Delete
"Sets the public identifier of this InputSource."
[210] Fix | Delete
self.__public_id = public_id
[211] Fix | Delete
[212] Fix | Delete
def getPublicId(self):
[213] Fix | Delete
"Returns the public identifier of this InputSource."
[214] Fix | Delete
return self.__public_id
[215] Fix | Delete
[216] Fix | Delete
def setSystemId(self, system_id):
[217] Fix | Delete
"Sets the system identifier of this InputSource."
[218] Fix | Delete
self.__system_id = system_id
[219] Fix | Delete
[220] Fix | Delete
def getSystemId(self):
[221] Fix | Delete
"Returns the system identifier of this InputSource."
[222] Fix | Delete
return self.__system_id
[223] Fix | Delete
[224] Fix | Delete
def setEncoding(self, encoding):
[225] Fix | Delete
"""Sets the character encoding of this InputSource.
[226] Fix | Delete
[227] Fix | Delete
The encoding must be a string acceptable for an XML encoding
[228] Fix | Delete
declaration (see section 4.3.3 of the XML recommendation).
[229] Fix | Delete
[230] Fix | Delete
The encoding attribute of the InputSource is ignored if the
[231] Fix | Delete
InputSource also contains a character stream."""
[232] Fix | Delete
self.__encoding = encoding
[233] Fix | Delete
[234] Fix | Delete
def getEncoding(self):
[235] Fix | Delete
"Get the character encoding of this InputSource."
[236] Fix | Delete
return self.__encoding
[237] Fix | Delete
[238] Fix | Delete
def setByteStream(self, bytefile):
[239] Fix | Delete
"""Set the byte stream (a Python file-like object which does
[240] Fix | Delete
not perform byte-to-character conversion) for this input
[241] Fix | Delete
source.
[242] Fix | Delete
[243] Fix | Delete
The SAX parser will ignore this if there is also a character
[244] Fix | Delete
stream specified, but it will use a byte stream in preference
[245] Fix | Delete
to opening a URI connection itself.
[246] Fix | Delete
[247] Fix | Delete
If the application knows the character encoding of the byte
[248] Fix | Delete
stream, it should set it with the setEncoding method."""
[249] Fix | Delete
self.__bytefile = bytefile
[250] Fix | Delete
[251] Fix | Delete
def getByteStream(self):
[252] Fix | Delete
"""Get the byte stream for this input source.
[253] Fix | Delete
[254] Fix | Delete
The getEncoding method will return the character encoding for
[255] Fix | Delete
this byte stream, or None if unknown."""
[256] Fix | Delete
return self.__bytefile
[257] Fix | Delete
[258] Fix | Delete
def setCharacterStream(self, charfile):
[259] Fix | Delete
"""Set the character stream for this input source. (The stream
[260] Fix | Delete
must be a Python 2.0 Unicode-wrapped file-like that performs
[261] Fix | Delete
conversion to Unicode strings.)
[262] Fix | Delete
[263] Fix | Delete
If there is a character stream specified, the SAX parser will
[264] Fix | Delete
ignore any byte stream and will not attempt to open a URI
[265] Fix | Delete
connection to the system identifier."""
[266] Fix | Delete
self.__charfile = charfile
[267] Fix | Delete
[268] Fix | Delete
def getCharacterStream(self):
[269] Fix | Delete
"Get the character stream for this input source."
[270] Fix | Delete
return self.__charfile
[271] Fix | Delete
[272] Fix | Delete
# ===== ATTRIBUTESIMPL =====
[273] Fix | Delete
[274] Fix | Delete
class AttributesImpl:
[275] Fix | Delete
[276] Fix | Delete
def __init__(self, attrs):
[277] Fix | Delete
"""Non-NS-aware implementation.
[278] Fix | Delete
[279] Fix | Delete
attrs should be of the form {name : value}."""
[280] Fix | Delete
self._attrs = attrs
[281] Fix | Delete
[282] Fix | Delete
def getLength(self):
[283] Fix | Delete
return len(self._attrs)
[284] Fix | Delete
[285] Fix | Delete
def getType(self, name):
[286] Fix | Delete
return "CDATA"
[287] Fix | Delete
[288] Fix | Delete
def getValue(self, name):
[289] Fix | Delete
return self._attrs[name]
[290] Fix | Delete
[291] Fix | Delete
def getValueByQName(self, name):
[292] Fix | Delete
return self._attrs[name]
[293] Fix | Delete
[294] Fix | Delete
def getNameByQName(self, name):
[295] Fix | Delete
if not name in self._attrs:
[296] Fix | Delete
raise KeyError, name
[297] Fix | Delete
return name
[298] Fix | Delete
[299] Fix | Delete
def getQNameByName(self, name):
[300] Fix | Delete
if not name in self._attrs:
[301] Fix | Delete
raise KeyError, name
[302] Fix | Delete
return name
[303] Fix | Delete
[304] Fix | Delete
def getNames(self):
[305] Fix | Delete
return self._attrs.keys()
[306] Fix | Delete
[307] Fix | Delete
def getQNames(self):
[308] Fix | Delete
return self._attrs.keys()
[309] Fix | Delete
[310] Fix | Delete
def __len__(self):
[311] Fix | Delete
return len(self._attrs)
[312] Fix | Delete
[313] Fix | Delete
def __getitem__(self, name):
[314] Fix | Delete
return self._attrs[name]
[315] Fix | Delete
[316] Fix | Delete
def keys(self):
[317] Fix | Delete
return self._attrs.keys()
[318] Fix | Delete
[319] Fix | Delete
def has_key(self, name):
[320] Fix | Delete
return name in self._attrs
[321] Fix | Delete
[322] Fix | Delete
def __contains__(self, name):
[323] Fix | Delete
return name in self._attrs
[324] Fix | Delete
[325] Fix | Delete
def get(self, name, alternative=None):
[326] Fix | Delete
return self._attrs.get(name, alternative)
[327] Fix | Delete
[328] Fix | Delete
def copy(self):
[329] Fix | Delete
return self.__class__(self._attrs)
[330] Fix | Delete
[331] Fix | Delete
def items(self):
[332] Fix | Delete
return self._attrs.items()
[333] Fix | Delete
[334] Fix | Delete
def values(self):
[335] Fix | Delete
return self._attrs.values()
[336] Fix | Delete
[337] Fix | Delete
# ===== ATTRIBUTESNSIMPL =====
[338] Fix | Delete
[339] Fix | Delete
class AttributesNSImpl(AttributesImpl):
[340] Fix | Delete
[341] Fix | Delete
def __init__(self, attrs, qnames):
[342] Fix | Delete
"""NS-aware implementation.
[343] Fix | Delete
[344] Fix | Delete
attrs should be of the form {(ns_uri, lname): value, ...}.
[345] Fix | Delete
qnames of the form {(ns_uri, lname): qname, ...}."""
[346] Fix | Delete
self._attrs = attrs
[347] Fix | Delete
self._qnames = qnames
[348] Fix | Delete
[349] Fix | Delete
def getValueByQName(self, name):
[350] Fix | Delete
for (nsname, qname) in self._qnames.items():
[351] Fix | Delete
if qname == name:
[352] Fix | Delete
return self._attrs[nsname]
[353] Fix | Delete
[354] Fix | Delete
raise KeyError, name
[355] Fix | Delete
[356] Fix | Delete
def getNameByQName(self, name):
[357] Fix | Delete
for (nsname, qname) in self._qnames.items():
[358] Fix | Delete
if qname == name:
[359] Fix | Delete
return nsname
[360] Fix | Delete
[361] Fix | Delete
raise KeyError, name
[362] Fix | Delete
[363] Fix | Delete
def getQNameByName(self, name):
[364] Fix | Delete
return self._qnames[name]
[365] Fix | Delete
[366] Fix | Delete
def getQNames(self):
[367] Fix | Delete
return self._qnames.values()
[368] Fix | Delete
[369] Fix | Delete
def copy(self):
[370] Fix | Delete
return self.__class__(self._attrs, self._qnames)
[371] Fix | Delete
[372] Fix | Delete
[373] Fix | Delete
def _test():
[374] Fix | Delete
XMLReader()
[375] Fix | Delete
IncrementalParser()
[376] Fix | Delete
Locator()
[377] Fix | Delete
[378] Fix | Delete
if __name__ == "__main__":
[379] Fix | Delete
_test()
[380] Fix | Delete
[381] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function