Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../xml/dom
File: pulldom.py
import xml.sax
[0] Fix | Delete
import xml.sax.handler
[1] Fix | Delete
import types
[2] Fix | Delete
[3] Fix | Delete
try:
[4] Fix | Delete
_StringTypes = [types.StringType, types.UnicodeType]
[5] Fix | Delete
except AttributeError:
[6] Fix | Delete
_StringTypes = [types.StringType]
[7] Fix | Delete
[8] Fix | Delete
START_ELEMENT = "START_ELEMENT"
[9] Fix | Delete
END_ELEMENT = "END_ELEMENT"
[10] Fix | Delete
COMMENT = "COMMENT"
[11] Fix | Delete
START_DOCUMENT = "START_DOCUMENT"
[12] Fix | Delete
END_DOCUMENT = "END_DOCUMENT"
[13] Fix | Delete
PROCESSING_INSTRUCTION = "PROCESSING_INSTRUCTION"
[14] Fix | Delete
IGNORABLE_WHITESPACE = "IGNORABLE_WHITESPACE"
[15] Fix | Delete
CHARACTERS = "CHARACTERS"
[16] Fix | Delete
[17] Fix | Delete
class PullDOM(xml.sax.ContentHandler):
[18] Fix | Delete
_locator = None
[19] Fix | Delete
document = None
[20] Fix | Delete
[21] Fix | Delete
def __init__(self, documentFactory=None):
[22] Fix | Delete
from xml.dom import XML_NAMESPACE
[23] Fix | Delete
self.documentFactory = documentFactory
[24] Fix | Delete
self.firstEvent = [None, None]
[25] Fix | Delete
self.lastEvent = self.firstEvent
[26] Fix | Delete
self.elementStack = []
[27] Fix | Delete
self.push = self.elementStack.append
[28] Fix | Delete
try:
[29] Fix | Delete
self.pop = self.elementStack.pop
[30] Fix | Delete
except AttributeError:
[31] Fix | Delete
# use class' pop instead
[32] Fix | Delete
pass
[33] Fix | Delete
self._ns_contexts = [{XML_NAMESPACE:'xml'}] # contains uri -> prefix dicts
[34] Fix | Delete
self._current_context = self._ns_contexts[-1]
[35] Fix | Delete
self.pending_events = []
[36] Fix | Delete
[37] Fix | Delete
def pop(self):
[38] Fix | Delete
result = self.elementStack[-1]
[39] Fix | Delete
del self.elementStack[-1]
[40] Fix | Delete
return result
[41] Fix | Delete
[42] Fix | Delete
def setDocumentLocator(self, locator):
[43] Fix | Delete
self._locator = locator
[44] Fix | Delete
[45] Fix | Delete
def startPrefixMapping(self, prefix, uri):
[46] Fix | Delete
if not hasattr(self, '_xmlns_attrs'):
[47] Fix | Delete
self._xmlns_attrs = []
[48] Fix | Delete
self._xmlns_attrs.append((prefix or 'xmlns', uri))
[49] Fix | Delete
self._ns_contexts.append(self._current_context.copy())
[50] Fix | Delete
self._current_context[uri] = prefix or None
[51] Fix | Delete
[52] Fix | Delete
def endPrefixMapping(self, prefix):
[53] Fix | Delete
self._current_context = self._ns_contexts.pop()
[54] Fix | Delete
[55] Fix | Delete
def startElementNS(self, name, tagName , attrs):
[56] Fix | Delete
# Retrieve xml namespace declaration attributes.
[57] Fix | Delete
xmlns_uri = 'http://www.w3.org/2000/xmlns/'
[58] Fix | Delete
xmlns_attrs = getattr(self, '_xmlns_attrs', None)
[59] Fix | Delete
if xmlns_attrs is not None:
[60] Fix | Delete
for aname, value in xmlns_attrs:
[61] Fix | Delete
attrs._attrs[(xmlns_uri, aname)] = value
[62] Fix | Delete
self._xmlns_attrs = []
[63] Fix | Delete
uri, localname = name
[64] Fix | Delete
if uri:
[65] Fix | Delete
# When using namespaces, the reader may or may not
[66] Fix | Delete
# provide us with the original name. If not, create
[67] Fix | Delete
# *a* valid tagName from the current context.
[68] Fix | Delete
if tagName is None:
[69] Fix | Delete
prefix = self._current_context[uri]
[70] Fix | Delete
if prefix:
[71] Fix | Delete
tagName = prefix + ":" + localname
[72] Fix | Delete
else:
[73] Fix | Delete
tagName = localname
[74] Fix | Delete
if self.document:
[75] Fix | Delete
node = self.document.createElementNS(uri, tagName)
[76] Fix | Delete
else:
[77] Fix | Delete
node = self.buildDocument(uri, tagName)
[78] Fix | Delete
else:
[79] Fix | Delete
# When the tagname is not prefixed, it just appears as
[80] Fix | Delete
# localname
[81] Fix | Delete
if self.document:
[82] Fix | Delete
node = self.document.createElement(localname)
[83] Fix | Delete
else:
[84] Fix | Delete
node = self.buildDocument(None, localname)
[85] Fix | Delete
[86] Fix | Delete
for aname,value in attrs.items():
[87] Fix | Delete
a_uri, a_localname = aname
[88] Fix | Delete
if a_uri == xmlns_uri:
[89] Fix | Delete
if a_localname == 'xmlns':
[90] Fix | Delete
qname = a_localname
[91] Fix | Delete
else:
[92] Fix | Delete
qname = 'xmlns:' + a_localname
[93] Fix | Delete
attr = self.document.createAttributeNS(a_uri, qname)
[94] Fix | Delete
node.setAttributeNodeNS(attr)
[95] Fix | Delete
elif a_uri:
[96] Fix | Delete
prefix = self._current_context[a_uri]
[97] Fix | Delete
if prefix:
[98] Fix | Delete
qname = prefix + ":" + a_localname
[99] Fix | Delete
else:
[100] Fix | Delete
qname = a_localname
[101] Fix | Delete
attr = self.document.createAttributeNS(a_uri, qname)
[102] Fix | Delete
node.setAttributeNodeNS(attr)
[103] Fix | Delete
else:
[104] Fix | Delete
attr = self.document.createAttribute(a_localname)
[105] Fix | Delete
node.setAttributeNode(attr)
[106] Fix | Delete
attr.value = value
[107] Fix | Delete
[108] Fix | Delete
self.lastEvent[1] = [(START_ELEMENT, node), None]
[109] Fix | Delete
self.lastEvent = self.lastEvent[1]
[110] Fix | Delete
self.push(node)
[111] Fix | Delete
[112] Fix | Delete
def endElementNS(self, name, tagName):
[113] Fix | Delete
self.lastEvent[1] = [(END_ELEMENT, self.pop()), None]
[114] Fix | Delete
self.lastEvent = self.lastEvent[1]
[115] Fix | Delete
[116] Fix | Delete
def startElement(self, name, attrs):
[117] Fix | Delete
if self.document:
[118] Fix | Delete
node = self.document.createElement(name)
[119] Fix | Delete
else:
[120] Fix | Delete
node = self.buildDocument(None, name)
[121] Fix | Delete
[122] Fix | Delete
for aname,value in attrs.items():
[123] Fix | Delete
attr = self.document.createAttribute(aname)
[124] Fix | Delete
attr.value = value
[125] Fix | Delete
node.setAttributeNode(attr)
[126] Fix | Delete
[127] Fix | Delete
self.lastEvent[1] = [(START_ELEMENT, node), None]
[128] Fix | Delete
self.lastEvent = self.lastEvent[1]
[129] Fix | Delete
self.push(node)
[130] Fix | Delete
[131] Fix | Delete
def endElement(self, name):
[132] Fix | Delete
self.lastEvent[1] = [(END_ELEMENT, self.pop()), None]
[133] Fix | Delete
self.lastEvent = self.lastEvent[1]
[134] Fix | Delete
[135] Fix | Delete
def comment(self, s):
[136] Fix | Delete
if self.document:
[137] Fix | Delete
node = self.document.createComment(s)
[138] Fix | Delete
self.lastEvent[1] = [(COMMENT, node), None]
[139] Fix | Delete
self.lastEvent = self.lastEvent[1]
[140] Fix | Delete
else:
[141] Fix | Delete
event = [(COMMENT, s), None]
[142] Fix | Delete
self.pending_events.append(event)
[143] Fix | Delete
[144] Fix | Delete
def processingInstruction(self, target, data):
[145] Fix | Delete
if self.document:
[146] Fix | Delete
node = self.document.createProcessingInstruction(target, data)
[147] Fix | Delete
self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
[148] Fix | Delete
self.lastEvent = self.lastEvent[1]
[149] Fix | Delete
else:
[150] Fix | Delete
event = [(PROCESSING_INSTRUCTION, target, data), None]
[151] Fix | Delete
self.pending_events.append(event)
[152] Fix | Delete
[153] Fix | Delete
def ignorableWhitespace(self, chars):
[154] Fix | Delete
node = self.document.createTextNode(chars)
[155] Fix | Delete
self.lastEvent[1] = [(IGNORABLE_WHITESPACE, node), None]
[156] Fix | Delete
self.lastEvent = self.lastEvent[1]
[157] Fix | Delete
[158] Fix | Delete
def characters(self, chars):
[159] Fix | Delete
node = self.document.createTextNode(chars)
[160] Fix | Delete
self.lastEvent[1] = [(CHARACTERS, node), None]
[161] Fix | Delete
self.lastEvent = self.lastEvent[1]
[162] Fix | Delete
[163] Fix | Delete
def startDocument(self):
[164] Fix | Delete
if self.documentFactory is None:
[165] Fix | Delete
import xml.dom.minidom
[166] Fix | Delete
self.documentFactory = xml.dom.minidom.Document.implementation
[167] Fix | Delete
[168] Fix | Delete
def buildDocument(self, uri, tagname):
[169] Fix | Delete
# Can't do that in startDocument, since we need the tagname
[170] Fix | Delete
# XXX: obtain DocumentType
[171] Fix | Delete
node = self.documentFactory.createDocument(uri, tagname, None)
[172] Fix | Delete
self.document = node
[173] Fix | Delete
self.lastEvent[1] = [(START_DOCUMENT, node), None]
[174] Fix | Delete
self.lastEvent = self.lastEvent[1]
[175] Fix | Delete
self.push(node)
[176] Fix | Delete
# Put everything we have seen so far into the document
[177] Fix | Delete
for e in self.pending_events:
[178] Fix | Delete
if e[0][0] == PROCESSING_INSTRUCTION:
[179] Fix | Delete
_,target,data = e[0]
[180] Fix | Delete
n = self.document.createProcessingInstruction(target, data)
[181] Fix | Delete
e[0] = (PROCESSING_INSTRUCTION, n)
[182] Fix | Delete
elif e[0][0] == COMMENT:
[183] Fix | Delete
n = self.document.createComment(e[0][1])
[184] Fix | Delete
e[0] = (COMMENT, n)
[185] Fix | Delete
else:
[186] Fix | Delete
raise AssertionError("Unknown pending event ",e[0][0])
[187] Fix | Delete
self.lastEvent[1] = e
[188] Fix | Delete
self.lastEvent = e
[189] Fix | Delete
self.pending_events = None
[190] Fix | Delete
return node.firstChild
[191] Fix | Delete
[192] Fix | Delete
def endDocument(self):
[193] Fix | Delete
self.lastEvent[1] = [(END_DOCUMENT, self.document), None]
[194] Fix | Delete
self.pop()
[195] Fix | Delete
[196] Fix | Delete
def clear(self):
[197] Fix | Delete
"clear(): Explicitly release parsing structures"
[198] Fix | Delete
self.document = None
[199] Fix | Delete
[200] Fix | Delete
class ErrorHandler:
[201] Fix | Delete
def warning(self, exception):
[202] Fix | Delete
print exception
[203] Fix | Delete
def error(self, exception):
[204] Fix | Delete
raise exception
[205] Fix | Delete
def fatalError(self, exception):
[206] Fix | Delete
raise exception
[207] Fix | Delete
[208] Fix | Delete
class DOMEventStream:
[209] Fix | Delete
def __init__(self, stream, parser, bufsize):
[210] Fix | Delete
self.stream = stream
[211] Fix | Delete
self.parser = parser
[212] Fix | Delete
self.bufsize = bufsize
[213] Fix | Delete
if not hasattr(self.parser, 'feed'):
[214] Fix | Delete
self.getEvent = self._slurp
[215] Fix | Delete
self.reset()
[216] Fix | Delete
[217] Fix | Delete
def reset(self):
[218] Fix | Delete
self.pulldom = PullDOM()
[219] Fix | Delete
# This content handler relies on namespace support
[220] Fix | Delete
self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
[221] Fix | Delete
self.parser.setContentHandler(self.pulldom)
[222] Fix | Delete
[223] Fix | Delete
def __getitem__(self, pos):
[224] Fix | Delete
rc = self.getEvent()
[225] Fix | Delete
if rc:
[226] Fix | Delete
return rc
[227] Fix | Delete
raise IndexError
[228] Fix | Delete
[229] Fix | Delete
def next(self):
[230] Fix | Delete
rc = self.getEvent()
[231] Fix | Delete
if rc:
[232] Fix | Delete
return rc
[233] Fix | Delete
raise StopIteration
[234] Fix | Delete
[235] Fix | Delete
def __iter__(self):
[236] Fix | Delete
return self
[237] Fix | Delete
[238] Fix | Delete
def expandNode(self, node):
[239] Fix | Delete
event = self.getEvent()
[240] Fix | Delete
parents = [node]
[241] Fix | Delete
while event:
[242] Fix | Delete
token, cur_node = event
[243] Fix | Delete
if cur_node is node:
[244] Fix | Delete
return
[245] Fix | Delete
if token != END_ELEMENT:
[246] Fix | Delete
parents[-1].appendChild(cur_node)
[247] Fix | Delete
if token == START_ELEMENT:
[248] Fix | Delete
parents.append(cur_node)
[249] Fix | Delete
elif token == END_ELEMENT:
[250] Fix | Delete
del parents[-1]
[251] Fix | Delete
event = self.getEvent()
[252] Fix | Delete
[253] Fix | Delete
def getEvent(self):
[254] Fix | Delete
# use IncrementalParser interface, so we get the desired
[255] Fix | Delete
# pull effect
[256] Fix | Delete
if not self.pulldom.firstEvent[1]:
[257] Fix | Delete
self.pulldom.lastEvent = self.pulldom.firstEvent
[258] Fix | Delete
while not self.pulldom.firstEvent[1]:
[259] Fix | Delete
buf = self.stream.read(self.bufsize)
[260] Fix | Delete
if not buf:
[261] Fix | Delete
self.parser.close()
[262] Fix | Delete
return None
[263] Fix | Delete
self.parser.feed(buf)
[264] Fix | Delete
rc = self.pulldom.firstEvent[1][0]
[265] Fix | Delete
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
[266] Fix | Delete
return rc
[267] Fix | Delete
[268] Fix | Delete
def _slurp(self):
[269] Fix | Delete
""" Fallback replacement for getEvent() using the
[270] Fix | Delete
standard SAX2 interface, which means we slurp the
[271] Fix | Delete
SAX events into memory (no performance gain, but
[272] Fix | Delete
we are compatible to all SAX parsers).
[273] Fix | Delete
"""
[274] Fix | Delete
self.parser.parse(self.stream)
[275] Fix | Delete
self.getEvent = self._emit
[276] Fix | Delete
return self._emit()
[277] Fix | Delete
[278] Fix | Delete
def _emit(self):
[279] Fix | Delete
""" Fallback replacement for getEvent() that emits
[280] Fix | Delete
the events that _slurp() read previously.
[281] Fix | Delete
"""
[282] Fix | Delete
rc = self.pulldom.firstEvent[1][0]
[283] Fix | Delete
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
[284] Fix | Delete
return rc
[285] Fix | Delete
[286] Fix | Delete
def clear(self):
[287] Fix | Delete
"""clear(): Explicitly release parsing objects"""
[288] Fix | Delete
self.pulldom.clear()
[289] Fix | Delete
del self.pulldom
[290] Fix | Delete
self.parser = None
[291] Fix | Delete
self.stream = None
[292] Fix | Delete
[293] Fix | Delete
class SAX2DOM(PullDOM):
[294] Fix | Delete
[295] Fix | Delete
def startElementNS(self, name, tagName , attrs):
[296] Fix | Delete
PullDOM.startElementNS(self, name, tagName, attrs)
[297] Fix | Delete
curNode = self.elementStack[-1]
[298] Fix | Delete
parentNode = self.elementStack[-2]
[299] Fix | Delete
parentNode.appendChild(curNode)
[300] Fix | Delete
[301] Fix | Delete
def startElement(self, name, attrs):
[302] Fix | Delete
PullDOM.startElement(self, name, attrs)
[303] Fix | Delete
curNode = self.elementStack[-1]
[304] Fix | Delete
parentNode = self.elementStack[-2]
[305] Fix | Delete
parentNode.appendChild(curNode)
[306] Fix | Delete
[307] Fix | Delete
def processingInstruction(self, target, data):
[308] Fix | Delete
PullDOM.processingInstruction(self, target, data)
[309] Fix | Delete
node = self.lastEvent[0][1]
[310] Fix | Delete
parentNode = self.elementStack[-1]
[311] Fix | Delete
parentNode.appendChild(node)
[312] Fix | Delete
[313] Fix | Delete
def ignorableWhitespace(self, chars):
[314] Fix | Delete
PullDOM.ignorableWhitespace(self, chars)
[315] Fix | Delete
node = self.lastEvent[0][1]
[316] Fix | Delete
parentNode = self.elementStack[-1]
[317] Fix | Delete
parentNode.appendChild(node)
[318] Fix | Delete
[319] Fix | Delete
def characters(self, chars):
[320] Fix | Delete
PullDOM.characters(self, chars)
[321] Fix | Delete
node = self.lastEvent[0][1]
[322] Fix | Delete
parentNode = self.elementStack[-1]
[323] Fix | Delete
parentNode.appendChild(node)
[324] Fix | Delete
[325] Fix | Delete
[326] Fix | Delete
default_bufsize = (2 ** 14) - 20
[327] Fix | Delete
[328] Fix | Delete
def parse(stream_or_string, parser=None, bufsize=None):
[329] Fix | Delete
if bufsize is None:
[330] Fix | Delete
bufsize = default_bufsize
[331] Fix | Delete
if type(stream_or_string) in _StringTypes:
[332] Fix | Delete
stream = open(stream_or_string)
[333] Fix | Delete
else:
[334] Fix | Delete
stream = stream_or_string
[335] Fix | Delete
if not parser:
[336] Fix | Delete
parser = xml.sax.make_parser()
[337] Fix | Delete
return DOMEventStream(stream, parser, bufsize)
[338] Fix | Delete
[339] Fix | Delete
def parseString(string, parser=None):
[340] Fix | Delete
try:
[341] Fix | Delete
from cStringIO import StringIO
[342] Fix | Delete
except ImportError:
[343] Fix | Delete
from StringIO import StringIO
[344] Fix | Delete
[345] Fix | Delete
bufsize = len(string)
[346] Fix | Delete
buf = StringIO(string)
[347] Fix | Delete
if not parser:
[348] Fix | Delete
parser = xml.sax.make_parser()
[349] Fix | Delete
return DOMEventStream(buf, parser, bufsize)
[350] Fix | Delete
[351] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function