Browse Source

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

Modified Files:
 	XMLSchema.py -- added a method for grabbing a schema item
           with a (namespace, name) directly rather than via an
           XML attribute.

 ----------------------------------------------------------------------
main
Joshua Boverhof 20 years ago
parent
commit
d916e1bad9
1 changed files with 47 additions and 25 deletions
  1. +47
    -25
      XMLSchema.py

+ 47
- 25
XMLSchema.py View File

@@ -20,6 +20,16 @@ from Namespaces import XMLNS
from Utility import DOM, DOMException, Collection, SplitQName, basejoin from Utility import DOM, DOMException, Collection, SplitQName, basejoin
from StringIO import StringIO from StringIO import StringIO


#
# Collections in XMLSchema class
#
TYPES = 'types'
ATTRIBUTE_GROUPS = 'attr_groups'
ATTRIBUTES = 'attr_decl'
ELEMENTS = 'elements'
MODEL_GROUPS = 'model_groups'


def GetSchema(component): def GetSchema(component):
"""convience function for finding the parent XMLSchema instance. """convience function for finding the parent XMLSchema instance.
""" """
@@ -519,31 +529,31 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
"""attribute -- attribute with a QName value (eg. type). """attribute -- attribute with a QName value (eg. type).
collection -- check types collection in parent Schema instance collection -- check types collection in parent Schema instance
""" """
return self.getQNameAttribute('attr_decl', attribute) return self.getQNameAttribute(ATTRIBUTES, attribute)


def getAttributeGroup(self, attribute): def getAttributeGroup(self, attribute):
"""attribute -- attribute with a QName value (eg. type). """attribute -- attribute with a QName value (eg. type).
collection -- check types collection in parent Schema instance collection -- check types collection in parent Schema instance
""" """
return self.getQNameAttribute('attr_groups', attribute) return self.getQNameAttribute(ATTRIBUTE_GROUPS, attribute)


def getTypeDefinition(self, attribute): def getTypeDefinition(self, attribute):
"""attribute -- attribute with a QName value (eg. type). """attribute -- attribute with a QName value (eg. type).
collection -- check types collection in parent Schema instance collection -- check types collection in parent Schema instance
""" """
return self.getQNameAttribute('types', attribute) return self.getQNameAttribute(TYPES, attribute)


def getElementDeclaration(self, attribute): def getElementDeclaration(self, attribute):
"""attribute -- attribute with a QName value (eg. element). """attribute -- attribute with a QName value (eg. element).
collection -- check elements collection in parent Schema instance. collection -- check elements collection in parent Schema instance.
""" """
return self.getQNameAttribute('elements', attribute) return self.getQNameAttribute(ELEMENTS, attribute)


def getModelGroup(self, attribute): def getModelGroup(self, attribute):
"""attribute -- attribute with a QName value (eg. ref). """attribute -- attribute with a QName value (eg. ref).
collection -- check model_group collection in parent Schema instance. collection -- check model_group collection in parent Schema instance.
""" """
return self.getQNameAttribute('model_groups', attribute) return self.getQNameAttribute(MODEL_GROUPS, attribute)


def getQNameAttribute(self, collection, attribute): def getQNameAttribute(self, collection, attribute):
"""returns object instance representing QName --> (namespace,name), """returns object instance representing QName --> (namespace,name),
@@ -552,25 +562,35 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
collection -- collection in parent Schema instance to search. collection -- collection in parent Schema instance to search.
""" """
obj = None obj = None
tdc = self.attributes.get(attribute) #tdc = self.attributes.get(attribute)
tdc = self.getAttributeQName(attribute)
if tdc: if tdc:
parent = GetSchema(self) obj = self.getSchemaItem(collection, tdc.getTargetNamespace(), tdc.getName())
targetNamespace = tdc.getTargetNamespace() return obj
if parent.targetNamespace == targetNamespace: def getSchemaItem(self, collection, namespace, name):
item = tdc.getName() """returns object instance representing namespace, name,
try: or if does not exist return None.
obj = getattr(parent, collection)[item] namespace -- namespace item defined in.
except KeyError, ex: name -- name of item.
raise KeyError, "targetNamespace(%s) collection(%s) has no item(%s)"\ collection -- collection in parent Schema instance to search.
%(targetNamespace, collection, item) """
elif parent.imports.has_key(targetNamespace): obj = None
schema = parent.imports[targetNamespace].getSchema() parent = GetSchema(self)
item = tdc.getName() if parent.targetNamespace == namespace:
try: try:
obj = getattr(schema, collection)[item] obj = getattr(parent, collection)[name]
except KeyError, ex: except KeyError, ex:
raise KeyError, "targetNamespace(%s) collection(%s) has no item(%s)"\ raise KeyError, "targetNamespace(%s) collection(%s) has no item(%s)"\
%(targetNamespace, collection, item) %(namespace, collection, name)
elif parent.imports.has_key(namespace):
schema = parent.imports[namespace].getSchema()
try:
obj = getattr(schema, collection)[name]
except KeyError, ex:
raise KeyError, "targetNamespace(%s) collection(%s) has no item(%s)"\
%(namespace, collection, name)
return obj return obj


def getXMLNS(self, prefix=None): def getXMLNS(self, prefix=None):
@@ -608,10 +628,12 @@ class XMLSchemaComponent(XMLBase, MarkerInterface):
qname = self.getAttribute(attribute) qname = self.getAttribute(attribute)
if isinstance(qname, TypeDescriptionComponent) is True: if isinstance(qname, TypeDescriptionComponent) is True:
return qname return qname
if qname is None:
return None


prefix,ncname = SplitQName(qname) prefix,ncname = SplitQName(qname)
namespace = self.getXMLNS(prefix) namespace = self.getXMLNS(prefix)
return (namespace,ncname) return TypeDescriptionComponent((namespace,ncname))


def getAttributeName(self): def getAttributeName(self):
"""return attribute name or None """return attribute name or None
@@ -1538,7 +1560,7 @@ class AttributeGroupReference(XMLSchemaComponent,\
"""attribute -- attribute with a QName value (eg. type). """attribute -- attribute with a QName value (eg. type).
collection -- check types collection in parent Schema instance collection -- check types collection in parent Schema instance
""" """
return XMLSchemaComponent.getQNameAttribute(self, 'attr_groups', attribute) return XMLSchemaComponent.getAttributeGroup(self, attribute)


def fromDom(self, node): def fromDom(self, node):
self.setAttributes(node) self.setAttributes(node)


||||||
x
 
000:0
Loading…
Cancel
Save