Commit 0c854e6b authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[xsd gen] Rework a bit XSD generation

* case without content_type + concept as target value should not occurs

* drop XXX about target.duplicated: handle it at once with proper comments

* introduce fill_element for the former 'duplicated' case and the case where
  element has a content type
parent 137e27f7a58c
......@@ -164,7 +164,6 @@ class SEDA2XSDExport(EntityAdapter):
card_entity=card_entity)
def dispatch_occ(self, profile_element, occ, target_value, to_process, card_entity):
occ.target.duplicated = isinstance(occ, dict) # XXX iirk
callback = getattr(self, 'element_' + occ.target.__class__.__name__.lower())
callback(occ, profile_element, target_value, to_process, card_entity)
......@@ -207,40 +206,45 @@ class SEDA2XSDExport(EntityAdapter):
if xselement.local_name == 'Signature':
attrs['type'] = 'OpenType'
self._target_element(xselement, profile_element, attrs)
return
target_element = self._target_element(xselement, profile_element, attrs)
content_type = self.xsd_content_type(xselement.textual_content_type)
if content_type:
if not xselement.duplicated:
elif isinstance(occ, dict): # fake occurence introduced for some elements'content
# target element has already been introduced: it is now given as profile_element
target_element = profile_element
extension_element = target_element[0][0][0]
self.fill_element(xselement, target_element, extension_element,
target_value, card_entity)
else:
target_element = self._target_element(xselement, profile_element, attrs)
content_type = self.xsd_content_type(xselement.textual_content_type)
if content_type:
type_element = self.element('xsd:complexType', target_element)
content_element = self.element('xsd:simpleContent', parent=type_element)
extension_element = self.element('xsd:extension', parent=content_element,
attributes={'base': content_type})
self.fill_element(xselement, target_element, extension_element,
target_value, card_entity)
else:
extension_element = target_element[0][0][0]
if getattr(target_value, 'cw_etype', None) == 'Concept':
self.concept_scheme_attribute(xselement, extension_element, target_value.scheme)
elif (xselement.local_name == 'KeywordReference' and card_entity.scheme):
self.concept_scheme_attribute(xselement, extension_element, card_entity.scheme)
fixed_value = xsd_serialize(target_value)
if fixed_value is not None:
attr = 'default' if hasattr(target_value, 'id') else 'fixed'
target_element.attrib[attr] = fixed_value
else:
type_element = self.element('xsd:complexType', target_element)
seq_element = self.element('xsd:sequence', type_element)
# target is a complex element
type_element = self.element('xsd:complexType', target_element)
seq_element = self.element('xsd:sequence', type_element)
# target is a complex element
if getattr(target_value, 'eid', None): # value is an entity
if target_value.cw_etype == 'Agent':
self.fill_organization_element(seq_element, target_value)
elif xselement.local_name in ('ArchivalAgency', 'TransferringAgency'):
self.fill_organization_element(seq_element, None)
elif target_value is not None:
assert False, (xselement, target_value)
if getattr(target_value, 'eid', None): # value is an entity
if target_value.cw_etype == 'Concept':
self.fill_concept_element(xselement, type_element, target_element, target_value)
elif target_value.cw_etype == 'Agent':
self.fill_organization_element(seq_element, target_value)
elif xselement.local_name in ('ArchivalAgency', 'TransferringAgency'):
self.fill_organization_element(seq_element, None)
elif target_value is not None:
assert False, (xselement, target_value)
if getattr(target_value, 'eid', None): # value is an entity
to_process[xselement].append((target_value, target_element))
to_process[xselement].append((target_value, target_element))
def fill_element(self, xselement, target_element, extension_element, value, card_entity):
if getattr(value, 'cw_etype', None) == 'Concept':
self.concept_scheme_attribute(xselement, extension_element, value.scheme)
elif (xselement.local_name == 'KeywordReference' and card_entity.scheme):
self.concept_scheme_attribute(xselement, extension_element, card_entity.scheme)
fixed_value = xsd_serialize(value)
if fixed_value is not None:
attr = 'default' if hasattr(value, 'id') else 'fixed'
target_element.attrib[attr] = fixed_value
def concept_scheme_attribute(self, xselement, type_element, scheme):
try:
......@@ -253,10 +257,6 @@ class SEDA2XSDExport(EntityAdapter):
'fixed': scheme.absolute_url()})
type_element.append(scheme_attr)
def fill_concept_element(self, xselement, type_element, target_element, target_value):
self.concept_scheme_attribute(xselement, type_element, target_value)
target_element.attrib['fixed'] = xsd_serialize(target_value)
def fill_organization_element(self, parent_element, value):
target_element = self.element('xsd:element', parent_element, {'name': 'Identifier'})
type_element = self.element('xsd:simpleType', target_element)
......@@ -277,8 +277,6 @@ class SEDA2XSDExport(EntityAdapter):
return parent
def _target_element(self, xselement, profile_element, attrs):
if xselement.duplicated:
return profile_element
parent_element = self._parent_element(profile_element)
attrs['name'] = xselement.local_name
return self.element('xsd:element', parent_element, attrs)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment