Class: OpenEHR::Serializer::XMLSerializer

Inherits:
BaseSerializer show all
Includes:
AM::Archetype::ConstraintModel
Defined in:
lib/openehr/serializer/xml_serializer.rb

Overview

Emits the canonical openEHR ITS-XML AOM 1.4 shape for the parts that have a verifiable ground truth: (plural), xsi:type dispatch on /, nested terminology_id/code_list, and real ASSERTION expression trees. This gem's own OPTParser (lib/openehr/parser/opt_parser.rb) reads exactly this shape for the constraint tree - verified against real Ocean Template Designer-generated .opt fixtures in spec/lib/openehr/opt_parser/ - so the two no longer contradict each other. 's attribute-style term_definitions follows the same confirmed-from-.opt convention; term_bindings/ constraint_bindings and the header extras (uid/translations/etc.) have no directly-comparable real fixture in this repo, so they follow the same attribute-style spirit for internal consistency - XMLArchetypeParser (lib/openehr/parser/xml_archetype_parser.rb) is the authority for reading them back.

Constant Summary collapse

Primitive =
OpenEHR::AM::Archetype::ConstraintModel::Primitive
AssertionModel =
OpenEHR::AM::Archetype::Assertion
OpenEHRProfile =
OpenEHR::AM::OpenEHRProfile
PRIMITIVE_XSI_TYPES =
{
  Primitive::CBoolean => 'C_BOOLEAN',
  Primitive::CString => 'C_STRING',
  Primitive::CInteger => 'C_INTEGER',
  Primitive::CReal => 'C_REAL',
  Primitive::CDate => 'C_DATE',
  Primitive::CDateTime => 'C_DATE_TIME',
  Primitive::CTime => 'C_TIME',
  Primitive::CDuration => 'C_DURATION'
}.freeze

Instance Method Summary collapse

Methods inherited from BaseSerializer

#initialize, #serialize

Constructor Details

This class inherits a constructor from OpenEHR::Serializer::BaseSerializer

Instance Method Details

#definitionObject



62
63
64
65
66
67
# File 'lib/openehr/serializer/xml_serializer.rb', line 62

def definition
  definition = +''
  xml = Builder::XmlMarkup.new(:indent => 2, :target => definition)
  xml.definition { emit_c_object_body(xml, @archetype.definition) }
  return definition
end

#descriptionObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openehr/serializer/xml_serializer.rb', line 41

def description
  desc = +''
  xml = Builder::XmlMarkup.new(:indent => 2, :target => desc)
  ad = @archetype.description
  if ad
    xml.description do
      ad.original_author.each { |key, value| xml.original_author(value, 'id' => key) }
      (ad.other_contributors || []).each { |co| xml.other_contributors co }
      xml.lifecycle_state ad.lifecycle_state
      xml.details do
        # Each language's fields are wrapped in its own <detail
        # language="..."> - without this, multiple languages would
        # flatten into one <details> with no way to tell which
        # purpose/keywords/etc. belong to which language.
        ad.details.each { |lang, item| xml.tag!('detail', 'language' => lang) { emit_description_detail(xml, lang, item) } }
      end
    end
  end
  return desc
end

#headerObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openehr/serializer/xml_serializer.rb', line 25

def header
  header = +''
  xml = Builder::XmlMarkup.new(:indent => 2, :target => header)
  xml.archetype_id { xml.value @archetype.archetype_id.value }
  xml.uid { xml.value @archetype.uid.value } if @archetype.uid
  xml.adl_version @archetype.adl_version if @archetype.adl_version
  xml.parent_archetype_id { xml.value @archetype.parent_archetype_id.value } if @archetype.parent_archetype_id
  xml.concept @archetype.concept
  xml.original_language do
    xml.terminology_id { xml.value @archetype.original_language.terminology_id.value }
    xml.code_string @archetype.original_language.code_string
  end
  emit_translations(xml, @archetype.translations)
  return header
end

#mergeObject



85
86
87
88
89
90
# File 'lib/openehr/serializer/xml_serializer.rb', line 85

def merge
  archetype = "<?xml version='1.0' encoding='UTF-8'?>" + NL +
    "<archetype xmlns=\"http://schemas.openehr.org/v1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + NL +
    header + description + definition + emit_invariants + ontology + '</archetype>'
  return archetype
end

#ontologyObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/openehr/serializer/xml_serializer.rb', line 69

def ontology
  ontology = +''
  ao = @archetype.ontology
  xml = Builder::XmlMarkup.new(:indent => 2, :target => ontology)
  xml.ontology do
    xml.primary_language ao.primary_language if ao.primary_language
    xml.specialisation_depth ao.specialisation_depth
    (ao.languages_available || []).each { |lang| xml.languages_available lang }
    (ao.terminologies_available || []).each { |t| xml.terminologies_available t }
    emit_term_definitions(xml, 'term_definitions', ao.term_definitions)
    emit_term_definitions(xml, 'constraint_definitions', ao.constraint_definitions) if ao.constraint_definitions
    emit_bindings(xml, 'term_bindings', ao.term_bindings) { |cp| "#{cp.terminology_id.value}::#{cp.code_string}" }
    emit_bindings(xml, 'constraint_bindings', ao.constraint_bindings) { |uri| uri.value }
  end
end