1 | require 'rexml/document'
|
---|
2 | require 'builder'
|
---|
3 |
|
---|
4 | module OpenEHR
|
---|
5 | module Serializer
|
---|
6 | NL = "\r\n"
|
---|
7 | INDENT = ' '
|
---|
8 |
|
---|
9 | class BaseSerializer
|
---|
10 | def initialize(archetype)
|
---|
11 | @archetype = archetype
|
---|
12 | end
|
---|
13 |
|
---|
14 | def serialize
|
---|
15 | return self.merge
|
---|
16 | end
|
---|
17 | end
|
---|
18 |
|
---|
19 | class ADLSerializer < BaseSerializer
|
---|
20 | def header
|
---|
21 | hd = 'archetype'
|
---|
22 | unless @archetype.adl_version.nil?
|
---|
23 | hd << " (adl_version = #{@archetype.adl_version})"
|
---|
24 | end
|
---|
25 | hd << NL+INDENT + "#{@archetype.archetype_id.value}"+NL*2
|
---|
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
|
---|
31 | end
|
---|
32 |
|
---|
33 | def description
|
---|
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
|
---|
80 | end
|
---|
81 |
|
---|
82 | def definition
|
---|
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
|
---|
101 | end
|
---|
102 |
|
---|
103 | def ontology
|
---|
104 | end
|
---|
105 |
|
---|
106 | def envelope
|
---|
107 | end
|
---|
108 |
|
---|
109 | def merge
|
---|
110 | return header + description + definition + ontology
|
---|
111 | end
|
---|
112 |
|
---|
113 | private
|
---|
114 | def c_object
|
---|
115 | end
|
---|
116 |
|
---|
117 | def existence(existence)
|
---|
118 | "existence matches {#{existence.lower}..#{existence.upper}}"
|
---|
119 | end
|
---|
120 | end
|
---|
121 |
|
---|
122 | class XMLSerializer < BaseSerializer
|
---|
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
|
---|
131 | xml.original_language do
|
---|
132 | xml.terminology_id do
|
---|
133 | xml.value @archetype.original_language.terminology_id.value
|
---|
134 | end
|
---|
135 | xml.code_string @archetype.original_language.code_string
|
---|
136 | end
|
---|
137 | return header
|
---|
138 | end
|
---|
139 |
|
---|
140 | def description
|
---|
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
|
---|
164 | if item.keywords then
|
---|
165 | item.keywords.each do |word|
|
---|
166 | xml.keywords word
|
---|
167 | end
|
---|
168 | end
|
---|
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
|
---|
182 | end
|
---|
183 |
|
---|
184 | def merge
|
---|
185 |
|
---|
186 | end
|
---|
187 | end
|
---|
188 | end
|
---|
189 | end
|
---|
190 |
|
---|
191 | class Publisher
|
---|
192 | def initialize(serializer)
|
---|
193 | @serializer = serializer
|
---|
194 | end
|
---|
195 |
|
---|
196 | def publish(writer)
|
---|
197 | writer.out(@serializer.serialize)
|
---|
198 | end
|
---|
199 | end
|
---|
200 |
|
---|
201 | class Writer
|
---|
202 | def initialize(target)
|
---|
203 | @target = target
|
---|
204 | end
|
---|
205 | def out
|
---|
206 | end
|
---|
207 | end
|
---|