[405] | 1 | require 'rexml/document'
|
---|
[406] | 2 | require 'builder'
|
---|
[405] | 3 |
|
---|
[404] | 4 | module OpenEHR
|
---|
[405] | 5 | module Serializer
|
---|
[407] | 6 | NL = "\r\n"
|
---|
| 7 | INDENT = ' '
|
---|
| 8 |
|
---|
[405] | 9 | class BaseSerializer
|
---|
| 10 | def initialize(archetype)
|
---|
| 11 | @archetype = archetype
|
---|
| 12 | end
|
---|
[404] | 13 |
|
---|
[405] | 14 | def serialize
|
---|
| 15 | return self.merge
|
---|
| 16 | end
|
---|
| 17 | end
|
---|
[404] | 18 |
|
---|
[405] | 19 | class ADLSerializer < BaseSerializer
|
---|
[406] | 20 | def header
|
---|
[407] | 21 | hd = 'archetype'
|
---|
| 22 | unless @archetype.adl_version.nil?
|
---|
| 23 | hd << " (adl_version = #{@archetype.adl_version})"
|
---|
| 24 | end
|
---|
[408] | 25 | hd << NL+INDENT + "#{@archetype.archetype_id.value}"+NL*2
|
---|
[407] | 26 | hd << 'concept'+NL+ INDENT+"[#{@archetype.concept}]"+NL
|
---|
| 27 | hd << NL+'language'+NL+INDENT+'original_language = <['+
|
---|
| 28 | @archetype.original_language.terminology_id.value+'::'+
|
---|
| 29 | @archetype.original_language.code_string+']>'+NL
|
---|
| 30 | return hd
|
---|
[405] | 31 | end
|
---|
| 32 |
|
---|
[406] | 33 | def description
|
---|
[408] | 34 | desc = ''
|
---|
| 35 | if @archetype.description
|
---|
| 36 | ad = @archetype.description
|
---|
| 37 | desc << 'description' + NL
|
---|
| 38 | desc << INDENT + 'original_author = <' + NL
|
---|
| 39 | ad.original_author.each do |k,v|
|
---|
| 40 | desc << INDENT+INDENT+'["'+k+'"] = <"'+v+'">'+NL
|
---|
| 41 | end
|
---|
| 42 | desc << INDENT+'>'+NL
|
---|
| 43 | desc << INDENT+'lifecycle_state = <"'+ad.lifecycle_state+'">'+NL
|
---|
| 44 | desc << INDENT+'details = <'+NL
|
---|
| 45 | ad.details.each do |lang,item|
|
---|
| 46 | desc << INDENT*2+'["'+lang+'"] = <'+NL
|
---|
| 47 | desc << INDENT*3+'language = <['+
|
---|
| 48 | item.language.terminology_id.value+'::'+
|
---|
| 49 | item.language.code_string+']>'+NL
|
---|
| 50 | desc << INDENT*3+'purpose = <"'+item.purpose+'">'+NL
|
---|
| 51 | if item.keywords then
|
---|
| 52 | desc << INDENT*3+'keywords = <'
|
---|
| 53 | item.keywords.each do |word|
|
---|
| 54 | desc << '"'+word+'",'
|
---|
| 55 | end
|
---|
| 56 | desc.chop! << '>'+NL
|
---|
| 57 | end
|
---|
| 58 | desc << INDENT*3+'use = <"'+item.use+'">'+NL if item.use
|
---|
| 59 | desc << INDENT*3+'misuse = <"'+item.misuse+'">'+NL if item.misuse
|
---|
| 60 | desc << INDENT*3+'copyright = <"'+item.copyright+'">'+NL if item.copyright
|
---|
| 61 | if item.original_resource_uri
|
---|
| 62 | desc << INDENT*3 + 'original_resource_uri = <'
|
---|
| 63 | item.original_resource_uri.each do |k,v|
|
---|
| 64 | desc << INDENT*4+'["'+k+'"] = <"'+v+'">'+NL
|
---|
| 65 | end
|
---|
| 66 | desc << INDENT*3+'>'+NL
|
---|
| 67 | end
|
---|
| 68 | if item.other_details
|
---|
| 69 | desc << INDENT*3 + 'other_details = <'
|
---|
| 70 | item.original_resource_uri.each do |k,v|
|
---|
| 71 | desc << INDENT*4+'["'+k+'"] = <"'+v+'">'+NL
|
---|
| 72 | end
|
---|
| 73 | desc << INDENT*3+'>'+NL
|
---|
| 74 | end
|
---|
| 75 | desc << INDENT*2+'>'+NL
|
---|
| 76 | end
|
---|
| 77 | desc << INDENT+'>'+NL
|
---|
| 78 | end
|
---|
| 79 | return desc
|
---|
[406] | 80 | end
|
---|
| 81 |
|
---|
[405] | 82 | def definition
|
---|
[409] | 83 | ad = @archetype.definition
|
---|
| 84 | definition = 'definition'+NL
|
---|
| 85 | definition << INDENT+ad.rm_type_name+"[#{ad.node_id}] matches {"
|
---|
| 86 | if ad.any_allowed?
|
---|
| 87 | definition << '*}'+NL
|
---|
| 88 | else
|
---|
| 89 | definition << NL
|
---|
| 90 | if ad.attributes
|
---|
| 91 | attributes = ad.attributes
|
---|
| 92 | indents = 2
|
---|
| 93 | while attributes
|
---|
| 94 | definition << INDENT*indents+attributes.rm_type_name
|
---|
| 95 | definition << "[#{attributes.node_id}] "
|
---|
| 96 | definition << existence(attributes.existence)
|
---|
| 97 | definition << " matches {"
|
---|
| 98 | end
|
---|
| 99 | end
|
---|
| 100 | end
|
---|
[405] | 101 | end
|
---|
| 102 |
|
---|
| 103 | def ontology
|
---|
[411] | 104 | ao = @archetype.ontology
|
---|
| 105 | ontology = 'ontology'+NL
|
---|
| 106 | ontology << INDENT + 'term_definitions = <' + NL
|
---|
[412] | 107 | ao.term_definitions.each do |lang, items|
|
---|
| 108 | ontology << INDENT*2 + "[\"#{lang}\"] = <" + NL
|
---|
[411] | 109 | ontology << INDENT*3 + 'items = <' + NL
|
---|
[412] | 110 | items.each do |item|
|
---|
| 111 | ontology << INDENT*4 + "[\"#{item.code}\"] = <" + NL
|
---|
| 112 | item.items.each do |name, desc|
|
---|
| 113 | ontology << INDENT*5 + "#{name} = <\"#{desc}\">" +NL
|
---|
[411] | 114 | end
|
---|
| 115 | ontology << INDENT*4 + '>'+NL
|
---|
| 116 | end
|
---|
[412] | 117 | ontology << INDENT*3 + '>' + NL
|
---|
[411] | 118 | ontology << INDENT*2 + '>' + NL
|
---|
| 119 | end
|
---|
| 120 | ontology << INDENT + '>' + NL
|
---|
[405] | 121 | end
|
---|
| 122 |
|
---|
| 123 | def envelope
|
---|
| 124 | end
|
---|
| 125 |
|
---|
| 126 | def merge
|
---|
[406] | 127 | return header + description + definition + ontology
|
---|
[405] | 128 | end
|
---|
[409] | 129 |
|
---|
| 130 | private
|
---|
| 131 | def c_object
|
---|
| 132 | end
|
---|
| 133 |
|
---|
| 134 | def existence(existence)
|
---|
| 135 | "existence matches {#{existence.lower}..#{existence.upper}}"
|
---|
| 136 | end
|
---|
[405] | 137 | end
|
---|
| 138 |
|
---|
| 139 | class XMLSerializer < BaseSerializer
|
---|
[406] | 140 |
|
---|
| 141 | def header
|
---|
| 142 | header = ''
|
---|
| 143 | xml = Builder::XmlMarkup.new(:indent => 2, :target => header)
|
---|
| 144 | xml.archetype_id do
|
---|
| 145 | xml.value @archetype.archetype_id.value
|
---|
| 146 | end
|
---|
| 147 | xml.concept @archetype.concept
|
---|
[407] | 148 | xml.original_language do
|
---|
| 149 | xml.terminology_id do
|
---|
| 150 | xml.value @archetype.original_language.terminology_id.value
|
---|
| 151 | end
|
---|
[408] | 152 | xml.code_string @archetype.original_language.code_string
|
---|
[407] | 153 | end
|
---|
[406] | 154 | return header
|
---|
| 155 | end
|
---|
| 156 |
|
---|
[405] | 157 | def description
|
---|
[408] | 158 | desc = ''
|
---|
| 159 | xml = Builder::XmlMarkup.new(:indent => 2, :target => desc)
|
---|
| 160 | ad = @archetype.description
|
---|
| 161 | if ad
|
---|
| 162 | xml.description do
|
---|
| 163 | ad.original_author.each do |key,value|
|
---|
| 164 | xml.original_author(value,"id"=>key)
|
---|
| 165 | end
|
---|
| 166 | if ad.other_contributors
|
---|
| 167 | ad.other_contributors.each do |co|
|
---|
| 168 | xml.other_contributors co
|
---|
| 169 | end
|
---|
| 170 | end
|
---|
| 171 | xml.lifecycle_state ad.lifecycle_state
|
---|
| 172 | xml.details do
|
---|
| 173 | ad.details.each do |lang, item|
|
---|
| 174 | xml.language do
|
---|
| 175 | xml.terminology_id do
|
---|
| 176 | xml.value item.language.terminology_id.value
|
---|
| 177 | end
|
---|
| 178 | xml.code_string lang
|
---|
| 179 | end
|
---|
| 180 | xml.purpose item.purpose
|
---|
[409] | 181 | if item.keywords then
|
---|
| 182 | item.keywords.each do |word|
|
---|
| 183 | xml.keywords word
|
---|
| 184 | end
|
---|
| 185 | end
|
---|
[408] | 186 | xml.use item.use if item.use
|
---|
| 187 | xml.misuse item.misuse if item.misuse
|
---|
| 188 | xml.copyright item.copyright if item.copyright
|
---|
| 189 | if ad.other_details
|
---|
| 190 | ad.other_details.each do |key,value|
|
---|
| 191 | xml.other_details(value, "id"=>key)
|
---|
| 192 | end
|
---|
| 193 | end
|
---|
| 194 | end
|
---|
| 195 | end
|
---|
| 196 | end
|
---|
| 197 | end
|
---|
| 198 | return desc
|
---|
[405] | 199 | end
|
---|
[406] | 200 |
|
---|
[410] | 201 | def definition
|
---|
| 202 | definition = ''
|
---|
| 203 | ad = @archetype.definition
|
---|
| 204 | xml = Builder::XmlMarkup.new(:indent => 2, :target => definition)
|
---|
| 205 | xml.definition do
|
---|
| 206 | xml.rm_type_name ad.rm_type_name
|
---|
| 207 | xml.occurrence do
|
---|
| 208 | oc = ad.occurrences
|
---|
| 209 | xml.lower_included oc.lower_included? unless oc.lower_included?.nil?
|
---|
| 210 | xml.upper_included oc.upper_included? unless oc.upper_included?.nil?
|
---|
| 211 | xml.lower_unbounded oc.lower_unbounded?
|
---|
| 212 | xml.upper_unbounded oc.upper_unbounded?
|
---|
| 213 | xml.lower oc.lower
|
---|
| 214 | xml.upper oc.lower
|
---|
| 215 | end
|
---|
| 216 | xml.node_id ad.node_id
|
---|
| 217 | end
|
---|
| 218 | end
|
---|
| 219 |
|
---|
[406] | 220 | def merge
|
---|
| 221 |
|
---|
| 222 | end
|
---|
[405] | 223 | end
|
---|
[404] | 224 | end
|
---|
[405] | 225 | end
|
---|
[404] | 226 |
|
---|
[405] | 227 | class Publisher
|
---|
| 228 | def initialize(serializer)
|
---|
| 229 | @serializer = serializer
|
---|
| 230 | end
|
---|
[404] | 231 |
|
---|
[405] | 232 | def publish(writer)
|
---|
| 233 | writer.out(@serializer.serialize)
|
---|
[404] | 234 | end
|
---|
| 235 | end
|
---|
[405] | 236 |
|
---|
| 237 | class Writer
|
---|
[406] | 238 | def initialize(target)
|
---|
| 239 | @target = target
|
---|
| 240 | end
|
---|
| 241 | def out
|
---|
| 242 | end
|
---|
[405] | 243 | end
|
---|