Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2..../xml/dom
File: expatbuilder.py
"""Facility to use the Expat parser to load a minidom instance
[0] Fix | Delete
from a string or file.
[1] Fix | Delete
[2] Fix | Delete
This avoids all the overhead of SAX and pulldom to gain performance.
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
# Warning!
[6] Fix | Delete
#
[7] Fix | Delete
# This module is tightly bound to the implementation details of the
[8] Fix | Delete
# minidom DOM and can't be used with other DOM implementations. This
[9] Fix | Delete
# is due, in part, to a lack of appropriate methods in the DOM (there is
[10] Fix | Delete
# no way to create Entity and Notation nodes via the DOM Level 2
[11] Fix | Delete
# interface), and for performance. The latter is the cause of some fairly
[12] Fix | Delete
# cryptic code.
[13] Fix | Delete
#
[14] Fix | Delete
# Performance hacks:
[15] Fix | Delete
#
[16] Fix | Delete
# - .character_data_handler() has an extra case in which continuing
[17] Fix | Delete
# data is appended to an existing Text node; this can be a
[18] Fix | Delete
# speedup since pyexpat can break up character data into multiple
[19] Fix | Delete
# callbacks even though we set the buffer_text attribute on the
[20] Fix | Delete
# parser. This also gives us the advantage that we don't need a
[21] Fix | Delete
# separate normalization pass.
[22] Fix | Delete
#
[23] Fix | Delete
# - Determining that a node exists is done using an identity comparison
[24] Fix | Delete
# with None rather than a truth test; this avoids searching for and
[25] Fix | Delete
# calling any methods on the node object if it exists. (A rather
[26] Fix | Delete
# nice speedup is achieved this way as well!)
[27] Fix | Delete
[28] Fix | Delete
from xml.dom import xmlbuilder, minidom, Node
[29] Fix | Delete
from xml.dom import EMPTY_NAMESPACE, EMPTY_PREFIX, XMLNS_NAMESPACE
[30] Fix | Delete
from xml.parsers import expat
[31] Fix | Delete
from xml.dom.minidom import _append_child, _set_attribute_node
[32] Fix | Delete
from xml.dom.NodeFilter import NodeFilter
[33] Fix | Delete
[34] Fix | Delete
from xml.dom.minicompat import *
[35] Fix | Delete
[36] Fix | Delete
TEXT_NODE = Node.TEXT_NODE
[37] Fix | Delete
CDATA_SECTION_NODE = Node.CDATA_SECTION_NODE
[38] Fix | Delete
DOCUMENT_NODE = Node.DOCUMENT_NODE
[39] Fix | Delete
[40] Fix | Delete
FILTER_ACCEPT = xmlbuilder.DOMBuilderFilter.FILTER_ACCEPT
[41] Fix | Delete
FILTER_REJECT = xmlbuilder.DOMBuilderFilter.FILTER_REJECT
[42] Fix | Delete
FILTER_SKIP = xmlbuilder.DOMBuilderFilter.FILTER_SKIP
[43] Fix | Delete
FILTER_INTERRUPT = xmlbuilder.DOMBuilderFilter.FILTER_INTERRUPT
[44] Fix | Delete
[45] Fix | Delete
theDOMImplementation = minidom.getDOMImplementation()
[46] Fix | Delete
[47] Fix | Delete
# Expat typename -> TypeInfo
[48] Fix | Delete
_typeinfo_map = {
[49] Fix | Delete
"CDATA": minidom.TypeInfo(None, "cdata"),
[50] Fix | Delete
"ENUM": minidom.TypeInfo(None, "enumeration"),
[51] Fix | Delete
"ENTITY": minidom.TypeInfo(None, "entity"),
[52] Fix | Delete
"ENTITIES": minidom.TypeInfo(None, "entities"),
[53] Fix | Delete
"ID": minidom.TypeInfo(None, "id"),
[54] Fix | Delete
"IDREF": minidom.TypeInfo(None, "idref"),
[55] Fix | Delete
"IDREFS": minidom.TypeInfo(None, "idrefs"),
[56] Fix | Delete
"NMTOKEN": minidom.TypeInfo(None, "nmtoken"),
[57] Fix | Delete
"NMTOKENS": minidom.TypeInfo(None, "nmtokens"),
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
class ElementInfo(object):
[61] Fix | Delete
__slots__ = '_attr_info', '_model', 'tagName'
[62] Fix | Delete
[63] Fix | Delete
def __init__(self, tagName, model=None):
[64] Fix | Delete
self.tagName = tagName
[65] Fix | Delete
self._attr_info = []
[66] Fix | Delete
self._model = model
[67] Fix | Delete
[68] Fix | Delete
def __getstate__(self):
[69] Fix | Delete
return self._attr_info, self._model, self.tagName
[70] Fix | Delete
[71] Fix | Delete
def __setstate__(self, state):
[72] Fix | Delete
self._attr_info, self._model, self.tagName = state
[73] Fix | Delete
[74] Fix | Delete
def getAttributeType(self, aname):
[75] Fix | Delete
for info in self._attr_info:
[76] Fix | Delete
if info[1] == aname:
[77] Fix | Delete
t = info[-2]
[78] Fix | Delete
if t[0] == "(":
[79] Fix | Delete
return _typeinfo_map["ENUM"]
[80] Fix | Delete
else:
[81] Fix | Delete
return _typeinfo_map[info[-2]]
[82] Fix | Delete
return minidom._no_type
[83] Fix | Delete
[84] Fix | Delete
def getAttributeTypeNS(self, namespaceURI, localName):
[85] Fix | Delete
return minidom._no_type
[86] Fix | Delete
[87] Fix | Delete
def isElementContent(self):
[88] Fix | Delete
if self._model:
[89] Fix | Delete
type = self._model[0]
[90] Fix | Delete
return type not in (expat.model.XML_CTYPE_ANY,
[91] Fix | Delete
expat.model.XML_CTYPE_MIXED)
[92] Fix | Delete
else:
[93] Fix | Delete
return False
[94] Fix | Delete
[95] Fix | Delete
def isEmpty(self):
[96] Fix | Delete
if self._model:
[97] Fix | Delete
return self._model[0] == expat.model.XML_CTYPE_EMPTY
[98] Fix | Delete
else:
[99] Fix | Delete
return False
[100] Fix | Delete
[101] Fix | Delete
def isId(self, aname):
[102] Fix | Delete
for info in self._attr_info:
[103] Fix | Delete
if info[1] == aname:
[104] Fix | Delete
return info[-2] == "ID"
[105] Fix | Delete
return False
[106] Fix | Delete
[107] Fix | Delete
def isIdNS(self, euri, ename, auri, aname):
[108] Fix | Delete
# not sure this is meaningful
[109] Fix | Delete
return self.isId((auri, aname))
[110] Fix | Delete
[111] Fix | Delete
def _intern(builder, s):
[112] Fix | Delete
return builder._intern_setdefault(s, s)
[113] Fix | Delete
[114] Fix | Delete
def _parse_ns_name(builder, name):
[115] Fix | Delete
assert ' ' in name
[116] Fix | Delete
parts = name.split(' ')
[117] Fix | Delete
intern = builder._intern_setdefault
[118] Fix | Delete
if len(parts) == 3:
[119] Fix | Delete
uri, localname, prefix = parts
[120] Fix | Delete
prefix = intern(prefix, prefix)
[121] Fix | Delete
qname = "%s:%s" % (prefix, localname)
[122] Fix | Delete
qname = intern(qname, qname)
[123] Fix | Delete
localname = intern(localname, localname)
[124] Fix | Delete
else:
[125] Fix | Delete
uri, localname = parts
[126] Fix | Delete
prefix = EMPTY_PREFIX
[127] Fix | Delete
qname = localname = intern(localname, localname)
[128] Fix | Delete
return intern(uri, uri), localname, prefix, qname
[129] Fix | Delete
[130] Fix | Delete
[131] Fix | Delete
class ExpatBuilder:
[132] Fix | Delete
"""Document builder that uses Expat to build a ParsedXML.DOM document
[133] Fix | Delete
instance."""
[134] Fix | Delete
[135] Fix | Delete
def __init__(self, options=None):
[136] Fix | Delete
if options is None:
[137] Fix | Delete
options = xmlbuilder.Options()
[138] Fix | Delete
self._options = options
[139] Fix | Delete
if self._options.filter is not None:
[140] Fix | Delete
self._filter = FilterVisibilityController(self._options.filter)
[141] Fix | Delete
else:
[142] Fix | Delete
self._filter = None
[143] Fix | Delete
# This *really* doesn't do anything in this case, so
[144] Fix | Delete
# override it with something fast & minimal.
[145] Fix | Delete
self._finish_start_element = id
[146] Fix | Delete
self._parser = None
[147] Fix | Delete
self.reset()
[148] Fix | Delete
[149] Fix | Delete
def createParser(self):
[150] Fix | Delete
"""Create a new parser object."""
[151] Fix | Delete
return expat.ParserCreate()
[152] Fix | Delete
[153] Fix | Delete
def getParser(self):
[154] Fix | Delete
"""Return the parser object, creating a new one if needed."""
[155] Fix | Delete
if not self._parser:
[156] Fix | Delete
self._parser = self.createParser()
[157] Fix | Delete
self._intern_setdefault = self._parser.intern.setdefault
[158] Fix | Delete
self._parser.buffer_text = True
[159] Fix | Delete
self._parser.ordered_attributes = True
[160] Fix | Delete
self._parser.specified_attributes = True
[161] Fix | Delete
self.install(self._parser)
[162] Fix | Delete
return self._parser
[163] Fix | Delete
[164] Fix | Delete
def reset(self):
[165] Fix | Delete
"""Free all data structures used during DOM construction."""
[166] Fix | Delete
self.document = theDOMImplementation.createDocument(
[167] Fix | Delete
EMPTY_NAMESPACE, None, None)
[168] Fix | Delete
self.curNode = self.document
[169] Fix | Delete
self._elem_info = self.document._elem_info
[170] Fix | Delete
self._cdata = False
[171] Fix | Delete
[172] Fix | Delete
def install(self, parser):
[173] Fix | Delete
"""Install the callbacks needed to build the DOM into the parser."""
[174] Fix | Delete
# This creates circular references!
[175] Fix | Delete
parser.StartDoctypeDeclHandler = self.start_doctype_decl_handler
[176] Fix | Delete
parser.StartElementHandler = self.first_element_handler
[177] Fix | Delete
parser.EndElementHandler = self.end_element_handler
[178] Fix | Delete
parser.ProcessingInstructionHandler = self.pi_handler
[179] Fix | Delete
if self._options.entities:
[180] Fix | Delete
parser.EntityDeclHandler = self.entity_decl_handler
[181] Fix | Delete
parser.NotationDeclHandler = self.notation_decl_handler
[182] Fix | Delete
if self._options.comments:
[183] Fix | Delete
parser.CommentHandler = self.comment_handler
[184] Fix | Delete
if self._options.cdata_sections:
[185] Fix | Delete
parser.StartCdataSectionHandler = self.start_cdata_section_handler
[186] Fix | Delete
parser.EndCdataSectionHandler = self.end_cdata_section_handler
[187] Fix | Delete
parser.CharacterDataHandler = self.character_data_handler_cdata
[188] Fix | Delete
else:
[189] Fix | Delete
parser.CharacterDataHandler = self.character_data_handler
[190] Fix | Delete
parser.ExternalEntityRefHandler = self.external_entity_ref_handler
[191] Fix | Delete
parser.XmlDeclHandler = self.xml_decl_handler
[192] Fix | Delete
parser.ElementDeclHandler = self.element_decl_handler
[193] Fix | Delete
parser.AttlistDeclHandler = self.attlist_decl_handler
[194] Fix | Delete
[195] Fix | Delete
def parseFile(self, file):
[196] Fix | Delete
"""Parse a document from a file object, returning the document
[197] Fix | Delete
node."""
[198] Fix | Delete
parser = self.getParser()
[199] Fix | Delete
first_buffer = True
[200] Fix | Delete
try:
[201] Fix | Delete
while 1:
[202] Fix | Delete
buffer = file.read(16*1024)
[203] Fix | Delete
if not buffer:
[204] Fix | Delete
break
[205] Fix | Delete
parser.Parse(buffer, 0)
[206] Fix | Delete
if first_buffer and self.document.documentElement:
[207] Fix | Delete
self._setup_subset(buffer)
[208] Fix | Delete
first_buffer = False
[209] Fix | Delete
parser.Parse("", True)
[210] Fix | Delete
except ParseEscape:
[211] Fix | Delete
pass
[212] Fix | Delete
doc = self.document
[213] Fix | Delete
self.reset()
[214] Fix | Delete
self._parser = None
[215] Fix | Delete
return doc
[216] Fix | Delete
[217] Fix | Delete
def parseString(self, string):
[218] Fix | Delete
"""Parse a document from a string, returning the document node."""
[219] Fix | Delete
parser = self.getParser()
[220] Fix | Delete
try:
[221] Fix | Delete
parser.Parse(string, True)
[222] Fix | Delete
self._setup_subset(string)
[223] Fix | Delete
except ParseEscape:
[224] Fix | Delete
pass
[225] Fix | Delete
doc = self.document
[226] Fix | Delete
self.reset()
[227] Fix | Delete
self._parser = None
[228] Fix | Delete
return doc
[229] Fix | Delete
[230] Fix | Delete
def _setup_subset(self, buffer):
[231] Fix | Delete
"""Load the internal subset if there might be one."""
[232] Fix | Delete
if self.document.doctype:
[233] Fix | Delete
extractor = InternalSubsetExtractor()
[234] Fix | Delete
extractor.parseString(buffer)
[235] Fix | Delete
subset = extractor.getSubset()
[236] Fix | Delete
self.document.doctype.internalSubset = subset
[237] Fix | Delete
[238] Fix | Delete
def start_doctype_decl_handler(self, doctypeName, systemId, publicId,
[239] Fix | Delete
has_internal_subset):
[240] Fix | Delete
doctype = self.document.implementation.createDocumentType(
[241] Fix | Delete
doctypeName, publicId, systemId)
[242] Fix | Delete
doctype.ownerDocument = self.document
[243] Fix | Delete
_append_child(self.document, doctype)
[244] Fix | Delete
self.document.doctype = doctype
[245] Fix | Delete
if self._filter and self._filter.acceptNode(doctype) == FILTER_REJECT:
[246] Fix | Delete
self.document.doctype = None
[247] Fix | Delete
del self.document.childNodes[-1]
[248] Fix | Delete
doctype = None
[249] Fix | Delete
self._parser.EntityDeclHandler = None
[250] Fix | Delete
self._parser.NotationDeclHandler = None
[251] Fix | Delete
if has_internal_subset:
[252] Fix | Delete
if doctype is not None:
[253] Fix | Delete
doctype.entities._seq = []
[254] Fix | Delete
doctype.notations._seq = []
[255] Fix | Delete
self._parser.CommentHandler = None
[256] Fix | Delete
self._parser.ProcessingInstructionHandler = None
[257] Fix | Delete
self._parser.EndDoctypeDeclHandler = self.end_doctype_decl_handler
[258] Fix | Delete
[259] Fix | Delete
def end_doctype_decl_handler(self):
[260] Fix | Delete
if self._options.comments:
[261] Fix | Delete
self._parser.CommentHandler = self.comment_handler
[262] Fix | Delete
self._parser.ProcessingInstructionHandler = self.pi_handler
[263] Fix | Delete
if not (self._elem_info or self._filter):
[264] Fix | Delete
self._finish_end_element = id
[265] Fix | Delete
[266] Fix | Delete
def pi_handler(self, target, data):
[267] Fix | Delete
node = self.document.createProcessingInstruction(target, data)
[268] Fix | Delete
_append_child(self.curNode, node)
[269] Fix | Delete
if self._filter and self._filter.acceptNode(node) == FILTER_REJECT:
[270] Fix | Delete
self.curNode.removeChild(node)
[271] Fix | Delete
[272] Fix | Delete
def character_data_handler_cdata(self, data):
[273] Fix | Delete
childNodes = self.curNode.childNodes
[274] Fix | Delete
if self._cdata:
[275] Fix | Delete
if ( self._cdata_continue
[276] Fix | Delete
and childNodes[-1].nodeType == CDATA_SECTION_NODE):
[277] Fix | Delete
childNodes[-1].appendData(data)
[278] Fix | Delete
return
[279] Fix | Delete
node = self.document.createCDATASection(data)
[280] Fix | Delete
self._cdata_continue = True
[281] Fix | Delete
elif childNodes and childNodes[-1].nodeType == TEXT_NODE:
[282] Fix | Delete
node = childNodes[-1]
[283] Fix | Delete
value = node.data + data
[284] Fix | Delete
d = node.__dict__
[285] Fix | Delete
d['data'] = d['nodeValue'] = value
[286] Fix | Delete
return
[287] Fix | Delete
else:
[288] Fix | Delete
node = minidom.Text()
[289] Fix | Delete
d = node.__dict__
[290] Fix | Delete
d['data'] = d['nodeValue'] = data
[291] Fix | Delete
d['ownerDocument'] = self.document
[292] Fix | Delete
_append_child(self.curNode, node)
[293] Fix | Delete
[294] Fix | Delete
def character_data_handler(self, data):
[295] Fix | Delete
childNodes = self.curNode.childNodes
[296] Fix | Delete
if childNodes and childNodes[-1].nodeType == TEXT_NODE:
[297] Fix | Delete
node = childNodes[-1]
[298] Fix | Delete
d = node.__dict__
[299] Fix | Delete
d['data'] = d['nodeValue'] = node.data + data
[300] Fix | Delete
return
[301] Fix | Delete
node = minidom.Text()
[302] Fix | Delete
d = node.__dict__
[303] Fix | Delete
d['data'] = d['nodeValue'] = node.data + data
[304] Fix | Delete
d['ownerDocument'] = self.document
[305] Fix | Delete
_append_child(self.curNode, node)
[306] Fix | Delete
[307] Fix | Delete
def entity_decl_handler(self, entityName, is_parameter_entity, value,
[308] Fix | Delete
base, systemId, publicId, notationName):
[309] Fix | Delete
if is_parameter_entity:
[310] Fix | Delete
# we don't care about parameter entities for the DOM
[311] Fix | Delete
return
[312] Fix | Delete
if not self._options.entities:
[313] Fix | Delete
return
[314] Fix | Delete
node = self.document._create_entity(entityName, publicId,
[315] Fix | Delete
systemId, notationName)
[316] Fix | Delete
if value is not None:
[317] Fix | Delete
# internal entity
[318] Fix | Delete
# node *should* be readonly, but we'll cheat
[319] Fix | Delete
child = self.document.createTextNode(value)
[320] Fix | Delete
node.childNodes.append(child)
[321] Fix | Delete
self.document.doctype.entities._seq.append(node)
[322] Fix | Delete
if self._filter and self._filter.acceptNode(node) == FILTER_REJECT:
[323] Fix | Delete
del self.document.doctype.entities._seq[-1]
[324] Fix | Delete
[325] Fix | Delete
def notation_decl_handler(self, notationName, base, systemId, publicId):
[326] Fix | Delete
node = self.document._create_notation(notationName, publicId, systemId)
[327] Fix | Delete
self.document.doctype.notations._seq.append(node)
[328] Fix | Delete
if self._filter and self._filter.acceptNode(node) == FILTER_ACCEPT:
[329] Fix | Delete
del self.document.doctype.notations._seq[-1]
[330] Fix | Delete
[331] Fix | Delete
def comment_handler(self, data):
[332] Fix | Delete
node = self.document.createComment(data)
[333] Fix | Delete
_append_child(self.curNode, node)
[334] Fix | Delete
if self._filter and self._filter.acceptNode(node) == FILTER_REJECT:
[335] Fix | Delete
self.curNode.removeChild(node)
[336] Fix | Delete
[337] Fix | Delete
def start_cdata_section_handler(self):
[338] Fix | Delete
self._cdata = True
[339] Fix | Delete
self._cdata_continue = False
[340] Fix | Delete
[341] Fix | Delete
def end_cdata_section_handler(self):
[342] Fix | Delete
self._cdata = False
[343] Fix | Delete
self._cdata_continue = False
[344] Fix | Delete
[345] Fix | Delete
def external_entity_ref_handler(self, context, base, systemId, publicId):
[346] Fix | Delete
return 1
[347] Fix | Delete
[348] Fix | Delete
def first_element_handler(self, name, attributes):
[349] Fix | Delete
if self._filter is None and not self._elem_info:
[350] Fix | Delete
self._finish_end_element = id
[351] Fix | Delete
self.getParser().StartElementHandler = self.start_element_handler
[352] Fix | Delete
self.start_element_handler(name, attributes)
[353] Fix | Delete
[354] Fix | Delete
def start_element_handler(self, name, attributes):
[355] Fix | Delete
node = self.document.createElement(name)
[356] Fix | Delete
_append_child(self.curNode, node)
[357] Fix | Delete
self.curNode = node
[358] Fix | Delete
[359] Fix | Delete
if attributes:
[360] Fix | Delete
for i in range(0, len(attributes), 2):
[361] Fix | Delete
a = minidom.Attr(attributes[i], EMPTY_NAMESPACE,
[362] Fix | Delete
None, EMPTY_PREFIX)
[363] Fix | Delete
value = attributes[i+1]
[364] Fix | Delete
d = a.childNodes[0].__dict__
[365] Fix | Delete
d['data'] = d['nodeValue'] = value
[366] Fix | Delete
d = a.__dict__
[367] Fix | Delete
d['value'] = d['nodeValue'] = value
[368] Fix | Delete
d['ownerDocument'] = self.document
[369] Fix | Delete
_set_attribute_node(node, a)
[370] Fix | Delete
[371] Fix | Delete
if node is not self.document.documentElement:
[372] Fix | Delete
self._finish_start_element(node)
[373] Fix | Delete
[374] Fix | Delete
def _finish_start_element(self, node):
[375] Fix | Delete
if self._filter:
[376] Fix | Delete
# To be general, we'd have to call isSameNode(), but this
[377] Fix | Delete
# is sufficient for minidom:
[378] Fix | Delete
if node is self.document.documentElement:
[379] Fix | Delete
return
[380] Fix | Delete
filt = self._filter.startContainer(node)
[381] Fix | Delete
if filt == FILTER_REJECT:
[382] Fix | Delete
# ignore this node & all descendents
[383] Fix | Delete
Rejecter(self)
[384] Fix | Delete
elif filt == FILTER_SKIP:
[385] Fix | Delete
# ignore this node, but make it's children become
[386] Fix | Delete
# children of the parent node
[387] Fix | Delete
Skipper(self)
[388] Fix | Delete
else:
[389] Fix | Delete
return
[390] Fix | Delete
self.curNode = node.parentNode
[391] Fix | Delete
node.parentNode.removeChild(node)
[392] Fix | Delete
node.unlink()
[393] Fix | Delete
[394] Fix | Delete
# If this ever changes, Namespaces.end_element_handler() needs to
[395] Fix | Delete
# be changed to match.
[396] Fix | Delete
#
[397] Fix | Delete
def end_element_handler(self, name):
[398] Fix | Delete
curNode = self.curNode
[399] Fix | Delete
self.curNode = curNode.parentNode
[400] Fix | Delete
self._finish_end_element(curNode)
[401] Fix | Delete
[402] Fix | Delete
def _finish_end_element(self, curNode):
[403] Fix | Delete
info = self._elem_info.get(curNode.tagName)
[404] Fix | Delete
if info:
[405] Fix | Delete
self._handle_white_text_nodes(curNode, info)
[406] Fix | Delete
if self._filter:
[407] Fix | Delete
if curNode is self.document.documentElement:
[408] Fix | Delete
return
[409] Fix | Delete
if self._filter.acceptNode(curNode) == FILTER_REJECT:
[410] Fix | Delete
self.curNode.removeChild(curNode)
[411] Fix | Delete
curNode.unlink()
[412] Fix | Delete
[413] Fix | Delete
def _handle_white_text_nodes(self, node, info):
[414] Fix | Delete
if (self._options.whitespace_in_element_content
[415] Fix | Delete
or not info.isElementContent()):
[416] Fix | Delete
return
[417] Fix | Delete
[418] Fix | Delete
# We have element type information and should remove ignorable
[419] Fix | Delete
# whitespace; identify for text nodes which contain only
[420] Fix | Delete
# whitespace.
[421] Fix | Delete
L = []
[422] Fix | Delete
for child in node.childNodes:
[423] Fix | Delete
if child.nodeType == TEXT_NODE and not child.data.strip():
[424] Fix | Delete
L.append(child)
[425] Fix | Delete
[426] Fix | Delete
# Remove ignorable whitespace from the tree.
[427] Fix | Delete
for child in L:
[428] Fix | Delete
node.removeChild(child)
[429] Fix | Delete
[430] Fix | Delete
def element_decl_handler(self, name, model):
[431] Fix | Delete
info = self._elem_info.get(name)
[432] Fix | Delete
if info is None:
[433] Fix | Delete
self._elem_info[name] = ElementInfo(name, model)
[434] Fix | Delete
else:
[435] Fix | Delete
assert info._model is None
[436] Fix | Delete
info._model = model
[437] Fix | Delete
[438] Fix | Delete
def attlist_decl_handler(self, elem, name, type, default, required):
[439] Fix | Delete
info = self._elem_info.get(elem)
[440] Fix | Delete
if info is None:
[441] Fix | Delete
info = ElementInfo(elem)
[442] Fix | Delete
self._elem_info[elem] = info
[443] Fix | Delete
info._attr_info.append(
[444] Fix | Delete
[None, name, None, None, default, 0, type, required])
[445] Fix | Delete
[446] Fix | Delete
def xml_decl_handler(self, version, encoding, standalone):
[447] Fix | Delete
self.document.version = version
[448] Fix | Delete
self.document.encoding = encoding
[449] Fix | Delete
# This is still a little ugly, thanks to the pyexpat API. ;-(
[450] Fix | Delete
if standalone >= 0:
[451] Fix | Delete
if standalone:
[452] Fix | Delete
self.document.standalone = True
[453] Fix | Delete
else:
[454] Fix | Delete
self.document.standalone = False
[455] Fix | Delete
[456] Fix | Delete
[457] Fix | Delete
# Don't include FILTER_INTERRUPT, since that's checked separately
[458] Fix | Delete
# where allowed.
[459] Fix | Delete
_ALLOWED_FILTER_RETURNS = (FILTER_ACCEPT, FILTER_REJECT, FILTER_SKIP)
[460] Fix | Delete
[461] Fix | Delete
class FilterVisibilityController(object):
[462] Fix | Delete
"""Wrapper around a DOMBuilderFilter which implements the checks
[463] Fix | Delete
to make the whatToShow filter attribute work."""
[464] Fix | Delete
[465] Fix | Delete
__slots__ = 'filter',
[466] Fix | Delete
[467] Fix | Delete
def __init__(self, filter):
[468] Fix | Delete
self.filter = filter
[469] Fix | Delete
[470] Fix | Delete
def startContainer(self, node):
[471] Fix | Delete
mask = self._nodetype_mask[node.nodeType]
[472] Fix | Delete
if self.filter.whatToShow & mask:
[473] Fix | Delete
val = self.filter.startContainer(node)
[474] Fix | Delete
if val == FILTER_INTERRUPT:
[475] Fix | Delete
raise ParseEscape
[476] Fix | Delete
if val not in _ALLOWED_FILTER_RETURNS:
[477] Fix | Delete
raise ValueError, \
[478] Fix | Delete
"startContainer() returned illegal value: " + repr(val)
[479] Fix | Delete
return val
[480] Fix | Delete
else:
[481] Fix | Delete
return FILTER_ACCEPT
[482] Fix | Delete
[483] Fix | Delete
def acceptNode(self, node):
[484] Fix | Delete
mask = self._nodetype_mask[node.nodeType]
[485] Fix | Delete
if self.filter.whatToShow & mask:
[486] Fix | Delete
val = self.filter.acceptNode(node)
[487] Fix | Delete
if val == FILTER_INTERRUPT:
[488] Fix | Delete
raise ParseEscape
[489] Fix | Delete
if val == FILTER_SKIP:
[490] Fix | Delete
# move all child nodes to the parent, and remove this node
[491] Fix | Delete
parent = node.parentNode
[492] Fix | Delete
for child in node.childNodes[:]:
[493] Fix | Delete
parent.appendChild(child)
[494] Fix | Delete
# node is handled by the caller
[495] Fix | Delete
return FILTER_REJECT
[496] Fix | Delete
if val not in _ALLOWED_FILTER_RETURNS:
[497] Fix | Delete
raise ValueError, \
[498] Fix | Delete
"acceptNode() returned illegal value: " + repr(val)
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function