Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../xml/dom
File: xmlbuilder.py
"""Implementation of the DOM Level 3 'LS-Load' feature."""
[0] Fix | Delete
[1] Fix | Delete
import copy
[2] Fix | Delete
import xml.dom
[3] Fix | Delete
[4] Fix | Delete
from xml.dom.NodeFilter import NodeFilter
[5] Fix | Delete
[6] Fix | Delete
[7] Fix | Delete
__all__ = ["DOMBuilder", "DOMEntityResolver", "DOMInputSource"]
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
class Options:
[11] Fix | Delete
"""Features object that has variables set for each DOMBuilder feature.
[12] Fix | Delete
[13] Fix | Delete
The DOMBuilder class uses an instance of this class to pass settings to
[14] Fix | Delete
the ExpatBuilder class.
[15] Fix | Delete
"""
[16] Fix | Delete
[17] Fix | Delete
# Note that the DOMBuilder class in LoadSave constrains which of these
[18] Fix | Delete
# values can be set using the DOM Level 3 LoadSave feature.
[19] Fix | Delete
[20] Fix | Delete
namespaces = 1
[21] Fix | Delete
namespace_declarations = True
[22] Fix | Delete
validation = False
[23] Fix | Delete
external_parameter_entities = True
[24] Fix | Delete
external_general_entities = True
[25] Fix | Delete
external_dtd_subset = True
[26] Fix | Delete
validate_if_schema = False
[27] Fix | Delete
validate = False
[28] Fix | Delete
datatype_normalization = False
[29] Fix | Delete
create_entity_ref_nodes = True
[30] Fix | Delete
entities = True
[31] Fix | Delete
whitespace_in_element_content = True
[32] Fix | Delete
cdata_sections = True
[33] Fix | Delete
comments = True
[34] Fix | Delete
charset_overrides_xml_encoding = True
[35] Fix | Delete
infoset = False
[36] Fix | Delete
supported_mediatypes_only = False
[37] Fix | Delete
[38] Fix | Delete
errorHandler = None
[39] Fix | Delete
filter = None
[40] Fix | Delete
[41] Fix | Delete
[42] Fix | Delete
class DOMBuilder:
[43] Fix | Delete
entityResolver = None
[44] Fix | Delete
errorHandler = None
[45] Fix | Delete
filter = None
[46] Fix | Delete
[47] Fix | Delete
ACTION_REPLACE = 1
[48] Fix | Delete
ACTION_APPEND_AS_CHILDREN = 2
[49] Fix | Delete
ACTION_INSERT_AFTER = 3
[50] Fix | Delete
ACTION_INSERT_BEFORE = 4
[51] Fix | Delete
[52] Fix | Delete
_legal_actions = (ACTION_REPLACE, ACTION_APPEND_AS_CHILDREN,
[53] Fix | Delete
ACTION_INSERT_AFTER, ACTION_INSERT_BEFORE)
[54] Fix | Delete
[55] Fix | Delete
def __init__(self):
[56] Fix | Delete
self._options = Options()
[57] Fix | Delete
[58] Fix | Delete
def _get_entityResolver(self):
[59] Fix | Delete
return self.entityResolver
[60] Fix | Delete
def _set_entityResolver(self, entityResolver):
[61] Fix | Delete
self.entityResolver = entityResolver
[62] Fix | Delete
[63] Fix | Delete
def _get_errorHandler(self):
[64] Fix | Delete
return self.errorHandler
[65] Fix | Delete
def _set_errorHandler(self, errorHandler):
[66] Fix | Delete
self.errorHandler = errorHandler
[67] Fix | Delete
[68] Fix | Delete
def _get_filter(self):
[69] Fix | Delete
return self.filter
[70] Fix | Delete
def _set_filter(self, filter):
[71] Fix | Delete
self.filter = filter
[72] Fix | Delete
[73] Fix | Delete
def setFeature(self, name, state):
[74] Fix | Delete
if self.supportsFeature(name):
[75] Fix | Delete
state = state and 1 or 0
[76] Fix | Delete
try:
[77] Fix | Delete
settings = self._settings[(_name_xform(name), state)]
[78] Fix | Delete
except KeyError:
[79] Fix | Delete
raise xml.dom.NotSupportedErr(
[80] Fix | Delete
"unsupported feature: %r" % (name,))
[81] Fix | Delete
else:
[82] Fix | Delete
for name, value in settings:
[83] Fix | Delete
setattr(self._options, name, value)
[84] Fix | Delete
else:
[85] Fix | Delete
raise xml.dom.NotFoundErr("unknown feature: " + repr(name))
[86] Fix | Delete
[87] Fix | Delete
def supportsFeature(self, name):
[88] Fix | Delete
return hasattr(self._options, _name_xform(name))
[89] Fix | Delete
[90] Fix | Delete
def canSetFeature(self, name, state):
[91] Fix | Delete
key = (_name_xform(name), state and 1 or 0)
[92] Fix | Delete
return key in self._settings
[93] Fix | Delete
[94] Fix | Delete
# This dictionary maps from (feature,value) to a list of
[95] Fix | Delete
# (option,value) pairs that should be set on the Options object.
[96] Fix | Delete
# If a (feature,value) setting is not in this dictionary, it is
[97] Fix | Delete
# not supported by the DOMBuilder.
[98] Fix | Delete
#
[99] Fix | Delete
_settings = {
[100] Fix | Delete
("namespace_declarations", 0): [
[101] Fix | Delete
("namespace_declarations", 0)],
[102] Fix | Delete
("namespace_declarations", 1): [
[103] Fix | Delete
("namespace_declarations", 1)],
[104] Fix | Delete
("validation", 0): [
[105] Fix | Delete
("validation", 0)],
[106] Fix | Delete
("external_general_entities", 0): [
[107] Fix | Delete
("external_general_entities", 0)],
[108] Fix | Delete
("external_general_entities", 1): [
[109] Fix | Delete
("external_general_entities", 1)],
[110] Fix | Delete
("external_parameter_entities", 0): [
[111] Fix | Delete
("external_parameter_entities", 0)],
[112] Fix | Delete
("external_parameter_entities", 1): [
[113] Fix | Delete
("external_parameter_entities", 1)],
[114] Fix | Delete
("validate_if_schema", 0): [
[115] Fix | Delete
("validate_if_schema", 0)],
[116] Fix | Delete
("create_entity_ref_nodes", 0): [
[117] Fix | Delete
("create_entity_ref_nodes", 0)],
[118] Fix | Delete
("create_entity_ref_nodes", 1): [
[119] Fix | Delete
("create_entity_ref_nodes", 1)],
[120] Fix | Delete
("entities", 0): [
[121] Fix | Delete
("create_entity_ref_nodes", 0),
[122] Fix | Delete
("entities", 0)],
[123] Fix | Delete
("entities", 1): [
[124] Fix | Delete
("entities", 1)],
[125] Fix | Delete
("whitespace_in_element_content", 0): [
[126] Fix | Delete
("whitespace_in_element_content", 0)],
[127] Fix | Delete
("whitespace_in_element_content", 1): [
[128] Fix | Delete
("whitespace_in_element_content", 1)],
[129] Fix | Delete
("cdata_sections", 0): [
[130] Fix | Delete
("cdata_sections", 0)],
[131] Fix | Delete
("cdata_sections", 1): [
[132] Fix | Delete
("cdata_sections", 1)],
[133] Fix | Delete
("comments", 0): [
[134] Fix | Delete
("comments", 0)],
[135] Fix | Delete
("comments", 1): [
[136] Fix | Delete
("comments", 1)],
[137] Fix | Delete
("charset_overrides_xml_encoding", 0): [
[138] Fix | Delete
("charset_overrides_xml_encoding", 0)],
[139] Fix | Delete
("charset_overrides_xml_encoding", 1): [
[140] Fix | Delete
("charset_overrides_xml_encoding", 1)],
[141] Fix | Delete
("infoset", 0): [],
[142] Fix | Delete
("infoset", 1): [
[143] Fix | Delete
("namespace_declarations", 0),
[144] Fix | Delete
("validate_if_schema", 0),
[145] Fix | Delete
("create_entity_ref_nodes", 0),
[146] Fix | Delete
("entities", 0),
[147] Fix | Delete
("cdata_sections", 0),
[148] Fix | Delete
("datatype_normalization", 1),
[149] Fix | Delete
("whitespace_in_element_content", 1),
[150] Fix | Delete
("comments", 1),
[151] Fix | Delete
("charset_overrides_xml_encoding", 1)],
[152] Fix | Delete
("supported_mediatypes_only", 0): [
[153] Fix | Delete
("supported_mediatypes_only", 0)],
[154] Fix | Delete
("namespaces", 0): [
[155] Fix | Delete
("namespaces", 0)],
[156] Fix | Delete
("namespaces", 1): [
[157] Fix | Delete
("namespaces", 1)],
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
def getFeature(self, name):
[161] Fix | Delete
xname = _name_xform(name)
[162] Fix | Delete
try:
[163] Fix | Delete
return getattr(self._options, xname)
[164] Fix | Delete
except AttributeError:
[165] Fix | Delete
if name == "infoset":
[166] Fix | Delete
options = self._options
[167] Fix | Delete
return (options.datatype_normalization
[168] Fix | Delete
and options.whitespace_in_element_content
[169] Fix | Delete
and options.comments
[170] Fix | Delete
and options.charset_overrides_xml_encoding
[171] Fix | Delete
and not (options.namespace_declarations
[172] Fix | Delete
or options.validate_if_schema
[173] Fix | Delete
or options.create_entity_ref_nodes
[174] Fix | Delete
or options.entities
[175] Fix | Delete
or options.cdata_sections))
[176] Fix | Delete
raise xml.dom.NotFoundErr("feature %s not known" % repr(name))
[177] Fix | Delete
[178] Fix | Delete
def parseURI(self, uri):
[179] Fix | Delete
if self.entityResolver:
[180] Fix | Delete
input = self.entityResolver.resolveEntity(None, uri)
[181] Fix | Delete
else:
[182] Fix | Delete
input = DOMEntityResolver().resolveEntity(None, uri)
[183] Fix | Delete
return self.parse(input)
[184] Fix | Delete
[185] Fix | Delete
def parse(self, input):
[186] Fix | Delete
options = copy.copy(self._options)
[187] Fix | Delete
options.filter = self.filter
[188] Fix | Delete
options.errorHandler = self.errorHandler
[189] Fix | Delete
fp = input.byteStream
[190] Fix | Delete
if fp is None and options.systemId:
[191] Fix | Delete
import urllib2
[192] Fix | Delete
fp = urllib2.urlopen(input.systemId)
[193] Fix | Delete
return self._parse_bytestream(fp, options)
[194] Fix | Delete
[195] Fix | Delete
def parseWithContext(self, input, cnode, action):
[196] Fix | Delete
if action not in self._legal_actions:
[197] Fix | Delete
raise ValueError("not a legal action")
[198] Fix | Delete
raise NotImplementedError("Haven't written this yet...")
[199] Fix | Delete
[200] Fix | Delete
def _parse_bytestream(self, stream, options):
[201] Fix | Delete
import xml.dom.expatbuilder
[202] Fix | Delete
builder = xml.dom.expatbuilder.makeBuilder(options)
[203] Fix | Delete
return builder.parseFile(stream)
[204] Fix | Delete
[205] Fix | Delete
[206] Fix | Delete
def _name_xform(name):
[207] Fix | Delete
return name.lower().replace('-', '_')
[208] Fix | Delete
[209] Fix | Delete
[210] Fix | Delete
class DOMEntityResolver(object):
[211] Fix | Delete
__slots__ = '_opener',
[212] Fix | Delete
[213] Fix | Delete
def resolveEntity(self, publicId, systemId):
[214] Fix | Delete
assert systemId is not None
[215] Fix | Delete
source = DOMInputSource()
[216] Fix | Delete
source.publicId = publicId
[217] Fix | Delete
source.systemId = systemId
[218] Fix | Delete
source.byteStream = self._get_opener().open(systemId)
[219] Fix | Delete
[220] Fix | Delete
# determine the encoding if the transport provided it
[221] Fix | Delete
source.encoding = self._guess_media_encoding(source)
[222] Fix | Delete
[223] Fix | Delete
# determine the base URI is we can
[224] Fix | Delete
import posixpath, urlparse
[225] Fix | Delete
parts = urlparse.urlparse(systemId)
[226] Fix | Delete
scheme, netloc, path, params, query, fragment = parts
[227] Fix | Delete
# XXX should we check the scheme here as well?
[228] Fix | Delete
if path and not path.endswith("/"):
[229] Fix | Delete
path = posixpath.dirname(path) + "/"
[230] Fix | Delete
parts = scheme, netloc, path, params, query, fragment
[231] Fix | Delete
source.baseURI = urlparse.urlunparse(parts)
[232] Fix | Delete
[233] Fix | Delete
return source
[234] Fix | Delete
[235] Fix | Delete
def _get_opener(self):
[236] Fix | Delete
try:
[237] Fix | Delete
return self._opener
[238] Fix | Delete
except AttributeError:
[239] Fix | Delete
self._opener = self._create_opener()
[240] Fix | Delete
return self._opener
[241] Fix | Delete
[242] Fix | Delete
def _create_opener(self):
[243] Fix | Delete
import urllib2
[244] Fix | Delete
return urllib2.build_opener()
[245] Fix | Delete
[246] Fix | Delete
def _guess_media_encoding(self, source):
[247] Fix | Delete
info = source.byteStream.info()
[248] Fix | Delete
if "Content-Type" in info:
[249] Fix | Delete
for param in info.getplist():
[250] Fix | Delete
if param.startswith("charset="):
[251] Fix | Delete
return param.split("=", 1)[1].lower()
[252] Fix | Delete
[253] Fix | Delete
[254] Fix | Delete
class DOMInputSource(object):
[255] Fix | Delete
__slots__ = ('byteStream', 'characterStream', 'stringData',
[256] Fix | Delete
'encoding', 'publicId', 'systemId', 'baseURI')
[257] Fix | Delete
[258] Fix | Delete
def __init__(self):
[259] Fix | Delete
self.byteStream = None
[260] Fix | Delete
self.characterStream = None
[261] Fix | Delete
self.stringData = None
[262] Fix | Delete
self.encoding = None
[263] Fix | Delete
self.publicId = None
[264] Fix | Delete
self.systemId = None
[265] Fix | Delete
self.baseURI = None
[266] Fix | Delete
[267] Fix | Delete
def _get_byteStream(self):
[268] Fix | Delete
return self.byteStream
[269] Fix | Delete
def _set_byteStream(self, byteStream):
[270] Fix | Delete
self.byteStream = byteStream
[271] Fix | Delete
[272] Fix | Delete
def _get_characterStream(self):
[273] Fix | Delete
return self.characterStream
[274] Fix | Delete
def _set_characterStream(self, characterStream):
[275] Fix | Delete
self.characterStream = characterStream
[276] Fix | Delete
[277] Fix | Delete
def _get_stringData(self):
[278] Fix | Delete
return self.stringData
[279] Fix | Delete
def _set_stringData(self, data):
[280] Fix | Delete
self.stringData = data
[281] Fix | Delete
[282] Fix | Delete
def _get_encoding(self):
[283] Fix | Delete
return self.encoding
[284] Fix | Delete
def _set_encoding(self, encoding):
[285] Fix | Delete
self.encoding = encoding
[286] Fix | Delete
[287] Fix | Delete
def _get_publicId(self):
[288] Fix | Delete
return self.publicId
[289] Fix | Delete
def _set_publicId(self, publicId):
[290] Fix | Delete
self.publicId = publicId
[291] Fix | Delete
[292] Fix | Delete
def _get_systemId(self):
[293] Fix | Delete
return self.systemId
[294] Fix | Delete
def _set_systemId(self, systemId):
[295] Fix | Delete
self.systemId = systemId
[296] Fix | Delete
[297] Fix | Delete
def _get_baseURI(self):
[298] Fix | Delete
return self.baseURI
[299] Fix | Delete
def _set_baseURI(self, uri):
[300] Fix | Delete
self.baseURI = uri
[301] Fix | Delete
[302] Fix | Delete
[303] Fix | Delete
class DOMBuilderFilter:
[304] Fix | Delete
"""Element filter which can be used to tailor construction of
[305] Fix | Delete
a DOM instance.
[306] Fix | Delete
"""
[307] Fix | Delete
[308] Fix | Delete
# There's really no need for this class; concrete implementations
[309] Fix | Delete
# should just implement the endElement() and startElement()
[310] Fix | Delete
# methods as appropriate. Using this makes it easy to only
[311] Fix | Delete
# implement one of them.
[312] Fix | Delete
[313] Fix | Delete
FILTER_ACCEPT = 1
[314] Fix | Delete
FILTER_REJECT = 2
[315] Fix | Delete
FILTER_SKIP = 3
[316] Fix | Delete
FILTER_INTERRUPT = 4
[317] Fix | Delete
[318] Fix | Delete
whatToShow = NodeFilter.SHOW_ALL
[319] Fix | Delete
[320] Fix | Delete
def _get_whatToShow(self):
[321] Fix | Delete
return self.whatToShow
[322] Fix | Delete
[323] Fix | Delete
def acceptNode(self, element):
[324] Fix | Delete
return self.FILTER_ACCEPT
[325] Fix | Delete
[326] Fix | Delete
def startContainer(self, element):
[327] Fix | Delete
return self.FILTER_ACCEPT
[328] Fix | Delete
[329] Fix | Delete
del NodeFilter
[330] Fix | Delete
[331] Fix | Delete
[332] Fix | Delete
class DocumentLS:
[333] Fix | Delete
"""Mixin to create documents that conform to the load/save spec."""
[334] Fix | Delete
[335] Fix | Delete
async = False
[336] Fix | Delete
[337] Fix | Delete
def _get_async(self):
[338] Fix | Delete
return False
[339] Fix | Delete
def _set_async(self, async):
[340] Fix | Delete
if async:
[341] Fix | Delete
raise xml.dom.NotSupportedErr(
[342] Fix | Delete
"asynchronous document loading is not supported")
[343] Fix | Delete
[344] Fix | Delete
def abort(self):
[345] Fix | Delete
# What does it mean to "clear" a document? Does the
[346] Fix | Delete
# documentElement disappear?
[347] Fix | Delete
raise NotImplementedError(
[348] Fix | Delete
"haven't figured out what this means yet")
[349] Fix | Delete
[350] Fix | Delete
def load(self, uri):
[351] Fix | Delete
raise NotImplementedError("haven't written this yet")
[352] Fix | Delete
[353] Fix | Delete
def loadXML(self, source):
[354] Fix | Delete
raise NotImplementedError("haven't written this yet")
[355] Fix | Delete
[356] Fix | Delete
def saveXML(self, snode):
[357] Fix | Delete
if snode is None:
[358] Fix | Delete
snode = self
[359] Fix | Delete
elif snode.ownerDocument is not self:
[360] Fix | Delete
raise xml.dom.WrongDocumentErr()
[361] Fix | Delete
return snode.toxml()
[362] Fix | Delete
[363] Fix | Delete
[364] Fix | Delete
class DOMImplementationLS:
[365] Fix | Delete
MODE_SYNCHRONOUS = 1
[366] Fix | Delete
MODE_ASYNCHRONOUS = 2
[367] Fix | Delete
[368] Fix | Delete
def createDOMBuilder(self, mode, schemaType):
[369] Fix | Delete
if schemaType is not None:
[370] Fix | Delete
raise xml.dom.NotSupportedErr(
[371] Fix | Delete
"schemaType not yet supported")
[372] Fix | Delete
if mode == self.MODE_SYNCHRONOUS:
[373] Fix | Delete
return DOMBuilder()
[374] Fix | Delete
if mode == self.MODE_ASYNCHRONOUS:
[375] Fix | Delete
raise xml.dom.NotSupportedErr(
[376] Fix | Delete
"asynchronous builders are not supported")
[377] Fix | Delete
raise ValueError("unknown value for mode")
[378] Fix | Delete
[379] Fix | Delete
def createDOMWriter(self):
[380] Fix | Delete
raise NotImplementedError(
[381] Fix | Delete
"the writer interface hasn't been written yet!")
[382] Fix | Delete
[383] Fix | Delete
def createDOMInputSource(self):
[384] Fix | Delete
return DOMInputSource()
[385] Fix | Delete
[386] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function