[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
|
---|
| 104 | end
|
---|
| 105 |
|
---|
| 106 | def envelope
|
---|
| 107 | end
|
---|
| 108 |
|
---|
| 109 | def merge
|
---|
[406] | 110 | return header + description + definition + ontology
|
---|
[405] | 111 | end
|
---|
[409] | 112 |
|
---|
| 113 | private
|
---|
| 114 | def c_object
|
---|
| 115 | end
|
---|
| 116 |
|
---|
| 117 | def existence(existence)
|
---|
| 118 | "existence matches {#{existence.lower}..#{existence.upper}}"
|
---|
| 119 | end
|
---|
[405] | 120 | end
|
---|
| 121 |
|
---|
| 122 | class XMLSerializer < BaseSerializer
|
---|
[406] | 123 |
|
---|
| 124 | def header
|
---|
| 125 | header = ''
|
---|
| 126 | xml = Builder::XmlMarkup.new(:indent => 2, :target => header)
|
---|
| 127 | xml.archetype_id do
|
---|
| 128 | xml.value @archetype.archetype_id.value
|
---|
| 129 | end
|
---|
| 130 | xml.concept @archetype.concept
|
---|
[407] | 131 | xml.original_language do
|
---|
| 132 | xml.terminology_id do
|
---|
| 133 | xml.value @archetype.original_language.terminology_id.value
|
---|
| 134 | end
|
---|
[408] | 135 | xml.code_string @archetype.original_language.code_string
|
---|
[407] | 136 | end
|
---|
[406] | 137 | return header
|
---|
| 138 | end
|
---|
| 139 |
|
---|
[405] | 140 | def description
|
---|
[408] | 141 | desc = ''
|
---|
| 142 | xml = Builder::XmlMarkup.new(:indent => 2, :target => desc)
|
---|
| 143 | ad = @archetype.description
|
---|
| 144 | if ad
|
---|
| 145 | xml.description do
|
---|
| 146 | ad.original_author.each do |key,value|
|
---|
| 147 | xml.original_author(value,"id"=>key)
|
---|
| 148 | end
|
---|
| 149 | if ad.other_contributors
|
---|
| 150 | ad.other_contributors.each do |co|
|
---|
| 151 | xml.other_contributors co
|
---|
| 152 | end
|
---|
| 153 | end
|
---|
| 154 | xml.lifecycle_state ad.lifecycle_state
|
---|
| 155 | xml.details do
|
---|
| 156 | ad.details.each do |lang, item|
|
---|
| 157 | xml.language do
|
---|
| 158 | xml.terminology_id do
|
---|
| 159 | xml.value item.language.terminology_id.value
|
---|
| 160 | end
|
---|
| 161 | xml.code_string lang
|
---|
| 162 | end
|
---|
| 163 | xml.purpose item.purpose
|
---|
[409] | 164 | if item.keywords then
|
---|
| 165 | item.keywords.each do |word|
|
---|
| 166 | xml.keywords word
|
---|
| 167 | end
|
---|
| 168 | end
|
---|
[408] | 169 | xml.use item.use if item.use
|
---|
| 170 | xml.misuse item.misuse if item.misuse
|
---|
| 171 | xml.copyright item.copyright if item.copyright
|
---|
| 172 | if ad.other_details
|
---|
| 173 | ad.other_details.each do |key,value|
|
---|
| 174 | xml.other_details(value, "id"=>key)
|
---|
| 175 | end
|
---|
| 176 | end
|
---|
| 177 | end
|
---|
| 178 | end
|
---|
| 179 | end
|
---|
| 180 | end
|
---|
| 181 | return desc
|
---|
[405] | 182 | end
|
---|
[406] | 183 |
|
---|
[410] | 184 | def definition
|
---|
| 185 | definition = ''
|
---|
| 186 | ad = @archetype.definition
|
---|
| 187 | xml = Builder::XmlMarkup.new(:indent => 2, :target => definition)
|
---|
| 188 | xml.definition do
|
---|
| 189 | xml.rm_type_name ad.rm_type_name
|
---|
| 190 | xml.occurrence do
|
---|
| 191 | oc = ad.occurrences
|
---|
| 192 | xml.lower_included oc.lower_included? unless oc.lower_included?.nil?
|
---|
| 193 | xml.upper_included oc.upper_included? unless oc.upper_included?.nil?
|
---|
| 194 | xml.lower_unbounded oc.lower_unbounded?
|
---|
| 195 | xml.upper_unbounded oc.upper_unbounded?
|
---|
| 196 | xml.lower oc.lower
|
---|
| 197 | xml.upper oc.lower
|
---|
| 198 | end
|
---|
| 199 | xml.node_id ad.node_id
|
---|
| 200 | end
|
---|
| 201 | end
|
---|
| 202 |
|
---|
[406] | 203 | def merge
|
---|
| 204 |
|
---|
| 205 | end
|
---|
[405] | 206 | end
|
---|
[404] | 207 | end
|
---|
[405] | 208 | end
|
---|
[404] | 209 |
|
---|
[405] | 210 | class Publisher
|
---|
| 211 | def initialize(serializer)
|
---|
| 212 | @serializer = serializer
|
---|
| 213 | end
|
---|
[404] | 214 |
|
---|
[405] | 215 | def publish(writer)
|
---|
| 216 | writer.out(@serializer.serialize)
|
---|
[404] | 217 | end
|
---|
| 218 | end
|
---|
[405] | 219 |
|
---|
| 220 | class Writer
|
---|
[406] | 221 | def initialize(target)
|
---|
| 222 | @target = target
|
---|
| 223 | end
|
---|
| 224 | def out
|
---|
| 225 | end
|
---|
[405] | 226 | end
|
---|