@@ -94,7 +94,8 @@ class SchemaReader:
"""
for schemaLocation, val in schema.includes.items():
if schemaLocation in self._includes:
schema.addIncludeSchema(schemaLocation, self._imports[schemaLocation])
schema.addIncludeSchema(
schemaLocation, self._imports[schemaLocation])
def addSchemaByLocation(self, location, schema):
"""provide reader with schema document for a location.
@@ -303,7 +304,8 @@ class DOMAdapter(DOMAdapterInterface):
namespace = DOM.findNamespaceURI(prefix, self.__node)
except DOMException as ex:
if prefix != 'xml':
raise SchemaError('%s namespace not declared for %s' % (prefix, self.__node._get_tagName()))
raise SchemaError('%s namespace not declared for %s' % (
prefix, self.__node._get_tagName()))
namespace = XMLNS.XML
return namespace
@@ -565,7 +567,8 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
and not (type(self.__class__.required) == type(XMLSchemaComponent.required)
and type(self.__class__.attributes) == type(XMLSchemaComponent.attributes)
and type(self.__class__.contents) == type(XMLSchemaComponent.contents)):
raise RuntimeError('Bad type for a class variable in %s' % self.__class__)
raise RuntimeError(
'Bad type for a class variable in %s' % self.__class__)
def getItemTrace(self):
"""Returns a node trace up to the <schema> item.
@@ -642,11 +645,13 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
if not tdc:
return
obj = self.getSchemaItem(collection, tdc.getTargetNamespace(), tdc.getName())
obj = self.getSchemaItem(
collection, tdc.getTargetNamespace(), tdc.getName())
if obj:
return obj
# raise SchemaError, 'No schema item "%s" in collection %s' %(tdc, collection)
# raise SchemaError, 'No schema item "%s" in collection %s' %(tdc,
# collection)
return
def getSchemaItem(self, collection, namespace, name):
@@ -663,14 +668,16 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
try:
obj = getattr(parent, collection)[name]
except KeyError as ex:
raise KeyError('targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name))
raise KeyError('targetNamespace(%s) collection(%s) has no item(%s)' % (
namespace, collection, name))
return obj
if not namespace in parent.imports:
if namespace in BUILT_IN_NAMESPACES:
# built-in just return
# WARNING: expecting import if "redefine" or add to built-in namespace.
# WARNING: expecting import if "redefine" or add to built-in
# namespace.
return
raise SchemaError('schema "%s" does not import namespace "%s"' % (
@@ -688,7 +695,8 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
# built-in just return
return
raise SchemaError('no schema instance for imported namespace (%s).' % (namespace))
raise SchemaError(
'no schema instance for imported namespace (%s).' % (namespace))
if not isinstance(schema, XMLSchema):
raise TypeError('expecting XMLSchema instance not "%r"' % schema)
@@ -696,7 +704,8 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
try:
obj = getattr(schema, collection)[name]
except KeyError as ex:
raise KeyError('targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name))
raise KeyError('targetNamespace(%s) collection(%s) has no item(%s)' % (
namespace, collection, name))
return obj
@@ -723,7 +732,8 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
"""
if type(attribute) in (list, tuple):
if len(attribute) != 2:
raise LookupError('To access attributes must use name or (namespace,name)')
raise LookupError(
'To access attributes must use name or (namespace,name)')
ns_dict = self.attributes.get(attribute[0])
if ns_dict is None:
@@ -768,20 +778,24 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
for k, v in node.getAttributeDictionary().items():
prefix, value = SplitQName(k)
if value == XMLSchemaComponent.xmlns:
self.attributes[value][prefix or XMLSchemaComponent.xmlns_key] = v
self.attributes[value][
prefix or XMLSchemaComponent.xmlns_key] = v
elif prefix:
ns = node.getNamespace(prefix)
if not ns:
raise SchemaError('no namespace for attribute prefix %s' % prefix)
raise SchemaError(
'no namespace for attribute prefix %s' % prefix)
if ns not in self.attributes:
self.attributes[ns] = {}
elif value in self.attributes[ns]:
raise SchemaError('attribute %s declared multiple times in %s' % (value, ns))
raise SchemaError(
'attribute %s declared multiple times in %s' % (value, ns))
self.attributes[ns][value] = v
elif not value in self.attributes:
self.attributes[value] = v
else:
raise SchemaError('attribute %s declared multiple times' % value)
raise SchemaError(
'attribute %s declared multiple times' % value)
if not isinstance(self, WSDLToolsAdapter):
self.__checkAttributes()
@@ -830,7 +844,8 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
"""
for a in self.__class__.required:
if not a in self.attributes:
raise SchemaError('class instance %s, missing required attribute %s' % (self.__class__, a))
raise SchemaError(
'class instance %s, missing required attribute %s' % (self.__class__, a))
for a, v in self.attributes.items():
# attribute #other, ie. not in empty namespace
if type(v) is dict:
@@ -842,7 +857,8 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
if (a not in self.__class__.attributes.keys()) and not\
(self.isAttribute() and self.isReference()):
raise SchemaError('%s, unknown attribute(%s, %s)' % (self.getItemTrace(), a, self.attributes[a]))
raise SchemaError('%s, unknown attribute(%s, %s)' % (
self.getItemTrace(), a, self.attributes[a]))
class WSDLToolsAdapter(XMLSchemaComponent):
@@ -968,7 +984,8 @@ class Annotation(XMLSchemaComponent):
#print_debug('class %s, any skipped' %self.__class__, 5)
continue
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
self.content = tuple(content)
class Appinfo(XMLSchemaComponent):
@@ -1003,7 +1020,8 @@ class Annotation(XMLSchemaComponent):
#print_debug('class %s, any skipped' %self.__class__, 5)
continue
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
self.content = tuple(content)
@@ -1386,7 +1404,8 @@ class XMLSchema(XMLSchemaComponent):
self.annotation = Annotation(self)
self.annotation.fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
def getSchema(self):
"""if schema is not defined, first look for a Schema class instance
@@ -1420,7 +1439,8 @@ class XMLSchema(XMLSchemaComponent):
self._schema = schema
if 'schemaLocation' not in self.attributes:
raise NoSchemaLocationWarning('no schemaLocation attribute in import')
raise NoSchemaLocationWarning(
'no schemaLocation attribute in import')
reader.loadFromURL(self.attributes.get('schemaLocation'), schema)
@@ -1456,7 +1476,8 @@ class XMLSchema(XMLSchemaComponent):
self.annotation = Annotation(self)
self.annotation.fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
def getSchema(self):
"""if schema is not defined, first look for a Schema class instance
@@ -1465,7 +1486,8 @@ class XMLSchema(XMLSchemaComponent):
"""
if not self._schema:
schema = self._parent()
self._schema = schema.getIncludeSchemas().get(self.attributes['schemaLocation'])
self._schema = schema.getIncludeSchemas().get(
self.attributes['schemaLocation'])
if not self._schema:
url = self.attributes['schemaLocation']
reader = SchemaReader(base_url=schema.getBaseUrl())
@@ -1678,7 +1700,8 @@ class AttributeGroupDefinition(XMLSchemaComponent,
required = ['name']
attributes = {'id': None,
'name': None}
contents = {'xsd': ['annotation', 'attribute', 'attributeGroup', 'anyAttribute']}
contents = {'xsd': ['annotation', 'attribute',
'attributeGroup', 'anyAttribute']}
tag = 'attributeGroup'
def __init__(self, parent):
@@ -1712,11 +1735,13 @@ class AttributeGroupDefinition(XMLSchemaComponent,
content[-1].fromDom(contents[indx])
elif component == 'anyAttribute':
if len(contents) != indx + 1:
raise SchemaError('anyAttribute is out of order in %s' % self.getItemTrace())
raise SchemaError(
'anyAttribute is out of order in %s' % self.getItemTrace())
content.append(AttributeWildCard(self))
content[-1].fromDom(contents[indx])
else:
raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName()))
raise SchemaError('Unknown component (%s)' %
(contents[indx].getTagName()))
self.attr_content = tuple(content)
@@ -1802,7 +1827,8 @@ class IdentityConstrants(XMLSchemaComponent):
fields[-1].fromDom(i)
continue
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
self.fields = tuple(fields)
@@ -1824,9 +1850,11 @@ class IdentityConstrants(XMLSchemaComponent):
self.annotation = Annotation(self)
self.annotation.fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError(
'Unknown component (%s)' % (i.getTagName()))
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
class Selector(Constraint):
@@ -2023,7 +2051,8 @@ class ElementDeclaration(XMLSchemaComponent,
def setConstraints(self, constraints):
self._constraints = tuple(constraints)
constraints = property(getConstraints, setConstraints, None, "tuple of key, keyref, unique constraints")
constraints = property(getConstraints, setConstraints,
None, "tuple of key, keyref, unique constraints")
def fromDom(self, node):
self.setAttributes(node)
@@ -2051,7 +2080,8 @@ class ElementDeclaration(XMLSchemaComponent,
constraints.append(Unique(self))
constraints[-1].fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
@@ -2104,7 +2134,8 @@ Local elements can be qualified or unqualifed according
return True
if form == 'unqualified':
return False
raise SchemaError('Bad form (%s) for element: %s' % (form, self.getItemTrace()))
raise SchemaError('Bad form (%s) for element: %s' %
(form, self.getItemTrace()))
class ElementReference(XMLSchemaComponent,
@@ -2153,7 +2184,8 @@ class ElementReference(XMLSchemaComponent,
self.annotation = Annotation(self)
self.annotation.fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
class ElementWildCard(LocalElementDeclaration, WildCardMarker):
@@ -2209,7 +2241,8 @@ class ElementWildCard(LocalElementDeclaration, WildCardMarker):
self.annotation = Annotation(self)
self.annotation.fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
######################################################
@@ -2267,7 +2300,8 @@ class Sequence(XMLSchemaComponent,
elif component == 'any':
content.append(ElementWildCard(self))
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
content[-1].fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
@@ -2317,7 +2351,8 @@ class All(XMLSchemaComponent,
else:
content.append(LocalElementDeclaration(self))
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
content[-1].fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
@@ -2376,7 +2411,8 @@ class Choice(XMLSchemaComponent,
elif component == 'any':
content.append(ElementWildCard(self))
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
content[-1].fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
@@ -2426,7 +2462,8 @@ class ModelGroupDefinition(XMLSchemaComponent,
elif component == 'sequence' and not self.content:
self.content = Sequence(self)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
self.content.fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
@@ -2474,7 +2511,8 @@ class ModelGroupReference(XMLSchemaComponent,
self.annotation = Annotation(self)
self.annotation.fromDom(i)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
@@ -2526,7 +2564,8 @@ class ComplexType(XMLSchemaComponent,
if m in ('true', '1'):
return True
raise SchemaError('invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace()))
raise SchemaError(
'invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace()))
def getAttributeContent(self):
return self.attr_content
@@ -2582,13 +2621,15 @@ class ComplexType(XMLSchemaComponent,
if contents[indx].hasattr('ref'):
self.attr_content.append(AttributeReference(self))
else:
self.attr_content.append(LocalAttributeDeclaration(self))
self.attr_content.append(
LocalAttributeDeclaration(self))
elif component == 'attributeGroup':
self.attr_content.append(AttributeGroupReference(self))
elif component == 'anyAttribute':
self.attr_content.append(AttributeWildCard(self))
else:
raise SchemaError('Unknown component (%s): %s' % (contents[indx].getTagName(), self.getItemTrace()))
raise SchemaError('Unknown component (%s): %s' % (
contents[indx].getTagName(), self.getItemTrace()))
self.attr_content[-1].fromDom(contents[indx])
indx += 1
@@ -2617,9 +2658,11 @@ class ComplexType(XMLSchemaComponent,
elif component == 'extension' and not self.derivation:
self.derivation = self.__class__.Extension(self)
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError(
'Unknown component (%s)' % (i.getTagName()))
else:
raise SchemaError('Unknown component (%s)' % (i.getTagName()))
raise SchemaError('Unknown component (%s)' %
(i.getTagName()))
self.derivation.fromDom(i)
self.content = self.derivation
@@ -2650,7 +2693,8 @@ class ComplexType(XMLSchemaComponent,
return False
if m in ('true', '1'):
return True
raise SchemaError('invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace()))
raise SchemaError(
'invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace()))
class _DerivationBase(XMLSchemaComponent):
@@ -2722,16 +2766,20 @@ class ComplexType(XMLSchemaComponent,
if contents[indx].hasattr('ref'):
self.attr_content.append(AttributeReference(self))
else:
self.attr_content.append(LocalAttributeDeclaration(self))
self.attr_content.append(
LocalAttributeDeclaration(self))
elif component == 'attributeGroup':
if contents[indx].hasattr('ref'):
self.attr_content.append(AttributeGroupReference(self))
self.attr_content.append(
AttributeGroupReference(self))
else:
self.attr_content.append(AttributeGroupDefinition(self))
self.attr_content.append(
AttributeGroupDefinition(self))
elif component == 'anyAttribute':
self.attr_content.append(AttributeWildCard(self))
else:
raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName()))
raise SchemaError('Unknown component (%s)' %
(contents[indx].getTagName()))
self.attr_content[-1].fromDom(contents[indx])
indx += 1
@@ -2839,7 +2887,8 @@ class ComplexType(XMLSchemaComponent,
elif component == 'anyAttribute':
content.append(AttributeWildCard(self))
else:
raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName()))
raise SchemaError('Unknown component (%s)' %
(contents[indx].getTagName()))
content[-1].fromDom(contents[indx])
indx += 1
self.attr_content = tuple(content)
@@ -2907,7 +2956,8 @@ class ComplexType(XMLSchemaComponent,
self.content.append(AnonymousSimpleType(self))
self.content[-1].fromDom(contents[indx])
else:
raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName()))
raise SchemaError('Unknown component (%s)' %
(contents[indx].getTagName()))
content[-1].fromDom(contents[indx])
indx += 1
self.attr_content = tuple(content)
@@ -3007,7 +3057,8 @@ class SimpleType(XMLSchemaComponent,
"""
attributes = {'id': None,
'base': None}
contents = {'xsd': ['annotation', 'simpleType'] + RestrictionMarker.facets}
contents = {'xsd': ['annotation', 'simpleType'] +
RestrictionMarker.facets}
tag = 'restriction'
def __init__(self, parent):
@@ -3046,7 +3097,8 @@ class SimpleType(XMLSchemaComponent,
elif component in RestrictionMarker.facets:
self.facets.append(contents[indx])
else:
raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName()))
raise SchemaError('Unknown component (%s)' %
(contents[indx].getTagName()))
self.content = tuple(content)
class Union(XMLSchemaComponent,
@@ -3086,7 +3138,8 @@ class SimpleType(XMLSchemaComponent,
content.append(AnonymousSimpleType(self))
content[-1].fromDom(contents[indx])
else:
raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName()))
raise SchemaError('Unknown component (%s)' %
(contents[indx].getTagName()))
self.content = tuple(content)
class List(XMLSchemaComponent,
@@ -3139,7 +3192,8 @@ class SimpleType(XMLSchemaComponent,
self.content.fromDom(contents[indx])
break
else:
raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName()))
raise SchemaError('Unknown component (%s)' %
(contents[indx].getTagName()))
class AnonymousSimpleType(SimpleType,