Browse Source

----------------------------------------------------------------------

Modified Files:
 	XMLSchema.py -- fixed a few problem with the XML namespace, mainly
            affecting the use of 'xml:lang'.  Fixed a bug identifying
            attributeGroup references vs. definitions.  Also changed the
            inheritance of MarkerInterface, and moved the classes involved
            before all Schema classes.

            Now will parse "XML Schema Part 1: Structures", and
            "XML Schema Part 2: Datatypes" XML Schema definitions.


 ----------------------------------------------------------------------
main
Joshua Boverhof 21 years ago
parent
commit
6c6678d844
1 changed files with 121 additions and 135 deletions
  1. +121
    -135
      XMLSchema.py

+ 121
- 135
XMLSchema.py View File

@@ -35,7 +35,7 @@ except ImportError:
XML = "http://www.w3.org/XML/1998/namespace" XML = "http://www.w3.org/XML/1998/namespace"
HTML = "http://www.w3.org/TR/REC-html40" HTML = "http://www.w3.org/TR/REC-html40"


from Utility import DOM, Collection
from Utility import DOM, DOMException, Collection
from StringIO import StringIO from StringIO import StringIO
try: try:
from xml.dom.ext import SplitQName from xml.dom.ext import SplitQName
@@ -280,7 +280,7 @@ class DOMAdapter(DOMAdapterInterface):
if prefix != 'xml': if prefix != 'xml':
raise SchemaError, '%s namespace not declared for %s'\ raise SchemaError, '%s namespace not declared for %s'\
%(prefix, self.__node._get_tagName()) %(prefix, self.__node._get_tagName())
namespace = XMLNS
namespace = XMLNS.XML
return namespace return namespace
def loadDocument(self, file): def loadDocument(self, file):
@@ -310,10 +310,116 @@ class XMLBase:
XMLBase.__rlock.release() XMLBase.__rlock.release()
return tmp return tmp



"""Marker Interface: can determine something about an instances properties by using
the provided convenience functions.

"""
class DefinitionMarker:
"""marker for definitions
"""
pass

class DeclarationMarker:
"""marker for declarations
"""
pass

class AttributeMarker:
"""marker for attributes
"""
pass

class AttributeGroupMarker:
"""marker for attribute groups
"""
pass

class WildCardMarker:
"""marker for wildcards
"""
pass

class ElementMarker:
"""marker for wildcards
"""
pass

class ReferenceMarker:
"""marker for references
"""
pass

class ModelGroupMarker:
"""marker for model groups
"""
pass

class ExtensionMarker:
"""marker for extensions
"""
pass

class RestrictionMarker:
"""marker for restrictions
"""
facets = ['enumeration', 'length', 'maxExclusive', 'maxInclusive',\
'maxLength', 'minExclusive', 'minInclusive', 'minLength',\
'pattern', 'fractionDigits', 'totalDigits', 'whiteSpace']

class SimpleMarker:
"""marker for simple type information
"""
pass

class ComplexMarker:
"""marker for complex type information
"""
pass


class MarkerInterface:
def isDefinition(self):
return isinstance(self, DefinitionMarker)

def isDeclaration(self):
return isinstance(self, DeclarationMarker)

def isAttribute(self):
return isinstance(self, AttributeMarker)

def isAttributeGroup(self):
return isinstance(self, AttributeGroupMarker)

def isElement(self):
return isinstance(self, ElementMarker)

def isReference(self):
return isinstance(self, ReferenceMarker)

def isWildCard(self):
return isinstance(self, WildCardMarker)

def isModelGroup(self):
return isinstance(self, ModelGroupMarker)

def isExtension(self):
return isinstance(self, ExtensionMarker)

def isRestriction(self):
return isinstance(self, RestrictionMarker)

def isSimple(self):
return isinstance(self, SimpleMarker)

def isComplex(self):
return isinstance(self, ComplexMarker)


########################################################## ##########################################################
# Schema Components # Schema Components
######################################################### #########################################################
class XMLSchemaComponent(XMLBase):
class XMLSchemaComponent(XMLBase, MarkerInterface):
""" """
class variables: class variables:
required -- list of required attributes required -- list of required attributes
@@ -391,6 +497,8 @@ class XMLSchemaComponent(XMLBase):
def getXMLNS(self, prefix=None): def getXMLNS(self, prefix=None):
"""deference prefix or by default xmlns, returns namespace. """deference prefix or by default xmlns, returns namespace.
""" """
if prefix == XMLSchemaComponent.xml:
return XMLNS.XML
parent = self parent = self
ns = self.attributes[XMLSchemaComponent.xmlns].get(prefix or\ ns = self.attributes[XMLSchemaComponent.xmlns].get(prefix or\
XMLSchemaComponent.xmlns_key) XMLSchemaComponent.xmlns_key)
@@ -490,9 +598,8 @@ class XMLSchemaComponent(XMLBase):
raise SchemaError,\ raise SchemaError,\
'class instance %s, missing required attribute %s'\ 'class instance %s, missing required attribute %s'\
%(self.__class__, a) %(self.__class__, a)
for a in self.attributes.keys(): for a in self.attributes.keys():
if (a != XMLSchemaComponent.xmlns) and\
if (a not in (XMLSchemaComponent.xmlns, XMLNS.XML)) and\
(a not in self.__class__.attributes.keys()) and not\ (a not in self.__class__.attributes.keys()) and not\
(self.isAttribute() and self.isReference()): (self.isAttribute() and self.isReference()):
raise SchemaError, '%s, unknown attribute' %a raise SchemaError, '%s, unknown attribute' %a
@@ -513,109 +620,6 @@ class WSDLToolsAdapter(XMLSchemaComponent):
""" """
return self._parent().types return self._parent().types


"""Marker Interface: can determine something about an instances properties by using
the provided convenience functions.

"""
class DefinitionMarker:
"""marker for definitions
"""
pass

class DeclarationMarker:
"""marker for declarations
"""
pass

class AttributeMarker:
"""marker for attributes
"""
pass

class AttributeGroupMarker:
"""marker for attribute groups
"""
pass

class WildCardMarker:
"""marker for wildcards
"""
pass

class ElementMarker:
"""marker for wildcards
"""
pass

class ReferenceMarker:
"""marker for references
"""
pass

class ModelGroupMarker:
"""marker for model groups
"""
pass

class ExtensionMarker:
"""marker for extensions
"""
pass

class RestrictionMarker:
"""marker for restrictions
"""
facets = ['enumeration', 'length', 'maxExclusive', 'maxInclusive',\
'maxLength', 'minExclusive', 'minInclusive', 'minLength',\
'pattern', 'fractionDigits', 'totalDigits', 'whiteSpace']

class SimpleMarker:
"""marker for simple type information
"""
pass

class ComplexMarker:
"""marker for complex type information
"""
pass

class MarkerInterface:
def isDefinition(self):
return isinstance(self, DefinitionMarker)

def isDeclaration(self):
return isinstance(self, DeclarationMarker)

def isAttribute(self):
return isinstance(self, AttributeMarker)

def isAttributeGroup(self):
return isinstance(self, AttributeGroupMarker)

def isElement(self):
return isinstance(self, ElementMarker)

def isReference(self):
return isinstance(self, ReferenceMarker)

def isWildCard(self):
return isinstance(self, WildCardMarker)

def isModelGroup(self):
return isinstance(self, ModelGroupMarker)

def isExtension(self):
return isinstance(self, ExtensionMarker)

def isRestriction(self):
return isinstance(self, RestrictionMarker)

def isSimple(self):
return isinstance(self, SimpleMarker)

def isComplex(self):
return isinstance(self, ComplexMarker)



class Notation(XMLSchemaComponent): class Notation(XMLSchemaComponent):
"""<notation> """<notation>
@@ -1059,7 +1063,7 @@ class XMLSchema(XMLSchemaComponent):
indx += 1 indx += 1




class Import(XMLSchemaComponent, MarkerInterface):
class Import(XMLSchemaComponent):
"""<import> """<import>
parent: parent:
schema schema
@@ -1117,7 +1121,7 @@ class XMLSchema(XMLSchemaComponent):
return self._schema or schema return self._schema or schema




class Include(XMLSchemaComponent, MarkerInterface):
class Include(XMLSchemaComponent):
"""<include schemaLocation> """<include schemaLocation>
parent: parent:
schema schema
@@ -1172,7 +1176,6 @@ class XMLSchema(XMLSchemaComponent):




class AttributeDeclaration(XMLSchemaComponent,\ class AttributeDeclaration(XMLSchemaComponent,\
MarkerInterface,\
AttributeMarker,\ AttributeMarker,\
DeclarationMarker): DeclarationMarker):
"""<attribute name> """<attribute name>
@@ -1219,7 +1222,6 @@ class AttributeDeclaration(XMLSchemaComponent,\




class LocalAttributeDeclaration(AttributeDeclaration,\ class LocalAttributeDeclaration(AttributeDeclaration,\
MarkerInterface,\
AttributeMarker,\ AttributeMarker,\
DeclarationMarker): DeclarationMarker):
"""<attribute name> """<attribute name>
@@ -1268,7 +1270,6 @@ class LocalAttributeDeclaration(AttributeDeclaration,\




class AttributeWildCard(XMLSchemaComponent,\ class AttributeWildCard(XMLSchemaComponent,\
MarkerInterface,\
AttributeMarker,\ AttributeMarker,\
DeclarationMarker,\ DeclarationMarker,\
WildCardMarker): WildCardMarker):
@@ -1306,7 +1307,6 @@ class AttributeWildCard(XMLSchemaComponent,\




class AttributeReference(XMLSchemaComponent,\ class AttributeReference(XMLSchemaComponent,\
MarkerInterface,\
AttributeMarker,\ AttributeMarker,\
ReferenceMarker): ReferenceMarker):
"""<attribute ref> """<attribute ref>
@@ -1347,7 +1347,6 @@ class AttributeReference(XMLSchemaComponent,\




class AttributeGroupDefinition(XMLSchemaComponent,\ class AttributeGroupDefinition(XMLSchemaComponent,\
MarkerInterface,\
AttributeGroupMarker,\ AttributeGroupMarker,\
DefinitionMarker): DefinitionMarker):
"""<attributeGroup name> """<attributeGroup name>
@@ -1399,7 +1398,6 @@ class AttributeGroupDefinition(XMLSchemaComponent,\
self.attr_content = tuple(content) self.attr_content = tuple(content)


class AttributeGroupReference(XMLSchemaComponent,\ class AttributeGroupReference(XMLSchemaComponent,\
MarkerInterface,\
AttributeGroupMarker,\ AttributeGroupMarker,\
ReferenceMarker): ReferenceMarker):
"""<attributeGroup ref> """<attributeGroup ref>
@@ -1584,7 +1582,6 @@ class KeyRef(IdentityConstrants):




class ElementDeclaration(XMLSchemaComponent,\ class ElementDeclaration(XMLSchemaComponent,\
MarkerInterface,\
ElementMarker,\ ElementMarker,\
DeclarationMarker): DeclarationMarker):
"""<element name> """<element name>
@@ -1695,7 +1692,6 @@ class LocalElementDeclaration(ElementDeclaration):




class ElementReference(XMLSchemaComponent,\ class ElementReference(XMLSchemaComponent,\
MarkerInterface,\
ElementMarker,\ ElementMarker,\
ReferenceMarker): ReferenceMarker):
"""<element ref> """<element ref>
@@ -1777,7 +1773,6 @@ class ElementWildCard(LocalElementDeclaration,\
# Model Groups # Model Groups
##################################################### #####################################################
class Sequence(XMLSchemaComponent,\ class Sequence(XMLSchemaComponent,\
MarkerInterface,\
ModelGroupMarker): ModelGroupMarker):
"""<sequence> """<sequence>
parents: parents:
@@ -1835,7 +1830,6 @@ class Sequence(XMLSchemaComponent,\




class All(XMLSchemaComponent,\ class All(XMLSchemaComponent,\
MarkerInterface,\
ModelGroupMarker): ModelGroupMarker):
"""<all> """<all>
parents: parents:
@@ -1884,7 +1878,6 @@ class All(XMLSchemaComponent,\




class Choice(XMLSchemaComponent,\ class Choice(XMLSchemaComponent,\
MarkerInterface,\
ModelGroupMarker): ModelGroupMarker):
"""<choice> """<choice>
parents: parents:
@@ -1942,7 +1935,6 @@ class Choice(XMLSchemaComponent,\




class ModelGroupDefinition(XMLSchemaComponent,\ class ModelGroupDefinition(XMLSchemaComponent,\
MarkerInterface,\
ModelGroupMarker,\ ModelGroupMarker,\
DefinitionMarker): DefinitionMarker):
"""<group name> """<group name>
@@ -1973,7 +1965,7 @@ class ModelGroupDefinition(XMLSchemaComponent,\
component = SplitQName(i.getTagName())[1] component = SplitQName(i.getTagName())[1]
if component in self.__class__.contents['xsd']: if component in self.__class__.contents['xsd']:
if component == 'annotation' and not self.annotation: if component == 'annotation' and not self.annotation:
self.annotation = Annotation()
self.annotation = Annotation(self)
self.annotation.fromDom(i) self.annotation.fromDom(i)
continue continue
elif component == 'all' and not self.content: elif component == 'all' and not self.content:
@@ -1990,7 +1982,6 @@ class ModelGroupDefinition(XMLSchemaComponent,\




class ModelGroupReference(XMLSchemaComponent,\ class ModelGroupReference(XMLSchemaComponent,\
MarkerInterface,\
ModelGroupMarker,\ ModelGroupMarker,\
ReferenceMarker): ReferenceMarker):
"""<group ref> """<group ref>
@@ -2034,7 +2025,6 @@ class ModelGroupReference(XMLSchemaComponent,\




class ComplexType(XMLSchemaComponent,\ class ComplexType(XMLSchemaComponent,\
MarkerInterface,\
DefinitionMarker,\ DefinitionMarker,\
ComplexMarker): ComplexMarker):
"""<complexType name> """<complexType name>
@@ -2151,7 +2141,6 @@ class ComplexType(XMLSchemaComponent,\
self.derivation.fromDom(i) self.derivation.fromDom(i)


class ComplexContent(_DerivedType,\ class ComplexContent(_DerivedType,\
MarkerInterface,\
ComplexMarker): ComplexMarker):
"""<complexContent> """<complexContent>
parents: parents:
@@ -2229,7 +2218,10 @@ class ComplexType(XMLSchemaComponent,\
else: else:
self.attr_content.append(LocalAttributeDeclaration(self)) self.attr_content.append(LocalAttributeDeclaration(self))
elif component == 'attributeGroup': elif component == 'attributeGroup':
self.attr_content.append(AttributeGroupDefinition(self))
if contents[indx].hasattr('ref'):
self.attr_content.append(AttributeGroupReference(self))
else:
self.attr_content.append(AttributeGroupDefinition(self))
elif component == 'anyAttribute': elif component == 'anyAttribute':
self.attr_content.append(AttributeWildCard(self)) self.attr_content.append(AttributeWildCard(self))
else: else:
@@ -2237,7 +2229,8 @@ class ComplexType(XMLSchemaComponent,\
self.attr_content[-1].fromDom(contents[indx]) self.attr_content[-1].fromDom(contents[indx])
indx += 1 indx += 1


class Extension(_DerivationBase, MarkerInterface, ExtensionMarker):
class Extension(_DerivationBase,
ExtensionMarker):
"""<extension base> """<extension base>
parents: parents:
complexContent complexContent
@@ -2252,7 +2245,6 @@ class ComplexType(XMLSchemaComponent,\
pass pass


class Restriction(_DerivationBase,\ class Restriction(_DerivationBase,\
MarkerInterface,\
RestrictionMarker): RestrictionMarker):
"""<restriction base> """<restriction base>
parents: parents:
@@ -2269,7 +2261,6 @@ class ComplexType(XMLSchemaComponent,\




class SimpleContent(_DerivedType,\ class SimpleContent(_DerivedType,\
MarkerInterface,\
SimpleMarker): SimpleMarker):
"""<simpleContent> """<simpleContent>
parents: parents:
@@ -2284,7 +2275,6 @@ class ComplexType(XMLSchemaComponent,\
contents = {'xsd':['annotation', 'restriction', 'extension']} contents = {'xsd':['annotation', 'restriction', 'extension']}


class Extension(XMLSchemaComponent,\ class Extension(XMLSchemaComponent,\
MarkerInterface,\
ExtensionMarker): ExtensionMarker):
"""<extension base> """<extension base>
parents: parents:
@@ -2341,7 +2331,6 @@ class ComplexType(XMLSchemaComponent,\




class Restriction(XMLSchemaComponent,\ class Restriction(XMLSchemaComponent,\
MarkerInterface,\
RestrictionMarker): RestrictionMarker):
"""<restriction base> """<restriction base>
parents: parents:
@@ -2382,7 +2371,6 @@ class LocalComplexType(ComplexType):


class SimpleType(XMLSchemaComponent,\ class SimpleType(XMLSchemaComponent,\
MarkerInterface,\
DefinitionMarker,\ DefinitionMarker,\
SimpleMarker): SimpleMarker):
"""<simpleType name> """<simpleType name>
@@ -2432,7 +2420,6 @@ class SimpleType(XMLSchemaComponent,\
self.content.fromDom(child) self.content.fromDom(child)


class Restriction(XMLSchemaComponent,\ class Restriction(XMLSchemaComponent,\
MarkerInterface,\
RestrictionMarker): RestrictionMarker):
"""<restriction base> """<restriction base>
parents: parents:
@@ -2558,7 +2545,6 @@ class SimpleType(XMLSchemaComponent,\


class AnonymousSimpleType(SimpleType,\ class AnonymousSimpleType(SimpleType,\
MarkerInterface,\
SimpleMarker): SimpleMarker):
"""<simpleType> """<simpleType>
parents: parents:


Loading…
Cancel
Save