From ffdcf2c8a15e70b7b948ffb7e561752ff6c4eb84 Mon Sep 17 00:00:00 2001 From: "Ivan R. Judson" Date: Tue, 1 Mar 2005 14:07:24 +0000 Subject: [PATCH] Flattening schema includes, didn't do anything to imports (yet). --- XMLSchema.py | 190 +++++++++++++++++++++++++-------------------------- 1 file changed, 92 insertions(+), 98 deletions(-) diff --git a/XMLSchema.py b/XMLSchema.py index 569d06e..146d020 100755 --- a/XMLSchema.py +++ b/XMLSchema.py @@ -203,6 +203,9 @@ class DOMAdapter(DOMAdapterInterface): self.__node = node self.__attributes = None + def getNode(self): + return self.__node + def hasattr(self, attr, ns=None): """attr -- attribute ns -- optional namespace, None means unprefixed attribute. @@ -918,6 +921,7 @@ class XMLSchema(XMLSchemaComponent): notations -- collection of notations """ + self.__node = None self.targetNamespace = None XMLSchemaComponent.__init__(self, parent) f = lambda k: k.attributes['name'] @@ -936,6 +940,12 @@ class XMLSchema(XMLSchemaComponent): self._included_schemas = {} self._base_url = None + def getNode(self): + """ + Interacting with the underlying DOM tree. + """ + return self.__node + def addImportSchema(self, schema): """for resolving import statements in Schema instance schema -- schema instance @@ -1028,6 +1038,8 @@ class XMLSchema(XMLSchemaComponent): return self.attributes.get('finalDefault') def load(self, node): + self.__node = node + pnode = node.getParentNode() if pnode: pname = SplitQName(pnode.getTagName())[1] @@ -1045,107 +1057,89 @@ class XMLSchema(XMLSchemaComponent): self.setAttributes(node) self.targetNamespace = self.getTargetNamespace() - contents = self.getContents(node) - - indx = 0 - num = len(contents) - while indx < num: - while indx < num: - node = contents[indx] - component = SplitQName(node.getTagName())[1] - - if component == 'include': - tp = self.__class__.Include(self) - tp.fromDom(node) - self.includes[tp.attributes['schemaLocation']] = tp - - schema = tp.getSchema() - if schema.targetNamespace and \ - schema.targetNamespace != self.targetNamespace: - raise SchemaError, 'included schema bad targetNamespace' - - for collection in ['imports','elements','types',\ - 'attr_decl','attr_groups','model_groups','notations']: - for k,v in getattr(schema,collection).items(): - if not getattr(self,collection).has_key(k): - v._parent = weakref.ref(self) - getattr(self,collection)[k] = v - - elif component == 'import': - tp = self.__class__.Import(self) - tp.fromDom(node) - import_ns = tp.getAttribute('namespace') - if import_ns: - if import_ns == self.targetNamespace: - raise SchemaError,\ - 'import and schema have same targetNamespace' - self.imports[import_ns] = tp - else: - self.imports[self.__class__.empty_namespace] = tp - if not self.getImportSchemas().has_key(import_ns) and\ - tp.getAttribute('schemaLocation'): - self.addImportSchema(tp.getSchema()) + for childNode in self.getContents(node): + component = SplitQName(childNode.getTagName())[1] + + if component == 'include': + tp = self.__class__.Include(self) + tp.fromDom(childNode) + + sl = tp.attributes['schemaLocation'] + schema = tp.getSchema() + + if not self.getIncludeSchemas().has_key(sl): + self.addIncludeSchema(sl, schema) + + self.includes[sl] = tp + + # This is the test code + pn = childNode.getParentNode().getNode() + pn.removeChild(childNode.getNode()) + for child in schema.getNode().getNode().childNodes: + pn.appendChild(child.cloneNode(1)) + # end of test code + + for collection in ['imports','elements','types', + 'attr_decl','attr_groups','model_groups', + 'notations']: + for k,v in getattr(schema,collection).items(): + if not getattr(self,collection).has_key(k): + v._parent = weakref.ref(self) + getattr(self,collection)[k] = v + else: + print "Warning: Not keeping schema component." - elif component == 'redefine': - #print_debug('class %s, redefine skipped' %self.__class__, 5) - pass - elif component == 'annotation': - #print_debug('class %s, annotation skipped' %self.__class__, 5) - pass - else: - break - indx += 1 + elif component == 'import': + tp = self.__class__.Import(self) + tp.fromDom(childNode) - # (attribute, attributeGroup, complexType, element, group, - # notation, simpleType)*, annotation*)* - while indx < num: - node = contents[indx] - component = SplitQName(node.getTagName())[1] + import_ns = tp.getAttribute('namespace') or \ + self.__class__.empty_namespace - if component == 'attribute': - tp = AttributeDeclaration(self) - tp.fromDom(node) - self.attr_decl[tp.getAttribute('name')] = tp - elif component == 'attributeGroup': - tp = AttributeGroupDefinition(self) - tp.fromDom(node) - self.attr_groups[tp.getAttribute('name')] = tp - elif component == 'complexType': - tp = ComplexType(self) - tp.fromDom(node) - self.types[tp.getAttribute('name')] = tp - elif component == 'element': - tp = ElementDeclaration(self) - tp.fromDom(node) - self.elements[tp.getAttribute('name')] = tp - elif component == 'group': - tp = ModelGroupDefinition(self) - tp.fromDom(node) - self.model_groups[tp.getAttribute('name')] = tp - elif component == 'notation': - tp = Notation(self) - tp.fromDom(node) - self.notations[tp.getAttribute('name')] = tp - elif component == 'simpleType': - tp = SimpleType(self) - tp.fromDom(node) - self.types[tp.getAttribute('name')] = tp - else: - break - indx += 1 - - while indx < num: - node = contents[indx] - component = SplitQName(node.getTagName())[1] - - if component == 'annotation': - #print_debug('class %s, annotation 2 skipped' %self.__class__, 5) - pass - else: - break - indx += 1 + if not self.getImportSchemas().has_key(import_ns) and \ + tp.getAttribute('schemaLocation'): + self.addImportSchema(tp.getSchema()) + + self.imports[import_ns] = tp + elif component == 'redefine': + # redefine not implemented yet + pass + elif component == 'annotation': + # annotation not implemented yet + pass + elif component == 'attribute': + tp = AttributeDeclaration(self) + tp.fromDom(childNode) + self.attr_decl[tp.getAttribute('name')] = tp + elif component == 'attributeGroup': + tp = AttributeGroupDefinition(self) + tp.fromDom(childNode) + self.attr_groups[tp.getAttribute('name')] = tp + elif component == 'element': + tp = ElementDeclaration(self) + tp.fromDom(childNode) + self.elements[tp.getAttribute('name')] = tp + elif component == 'group': + tp = ModelGroupDefinition(self) + tp.fromDom(childNode) + self.model_groups[tp.getAttribute('name')] = tp + elif component == 'notation': + tp = Notation(self) + tp.fromDom(childNode) + self.notations[tp.getAttribute('name')] = tp + elif component == 'complexType': + tp = ComplexType(self) + tp.fromDom(childNode) + self.types[tp.getAttribute('name')] = tp + elif component == 'simpleType': + tp = SimpleType(self) + tp.fromDom(childNode) + self.types[tp.getAttribute('name')] = tp + else: + break +# indx += 1 class Import(XMLSchemaComponent): """ @@ -1159,8 +1153,8 @@ class XMLSchema(XMLSchemaComponent): annotation? """ attributes = {'id':None, - 'namespace':None, - 'schemaLocation':None} + 'namespace':None, + 'schemaLocation':None} contents = {'xsd':['annotation']} tag = 'import'