source: ruby/trunk/lib/open_ehr/serializer.rb

Last change on this file was 413, checked in by KOBAYASHI, Shinji, 14 years ago

My X window system corrupsed.
However, only character is not so bad.

File size: 8.3 KB
Line 
1require 'rexml/document'
2require 'builder'
3
4module 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 ao = @archetype.ontology
105 ontology = 'ontology'+NL
106 ontology << INDENT + 'term_definitions = <' + NL
107 ao.term_definitions.each do |lang, items|
108 ontology << INDENT*2 + "[\"#{lang}\"] = <" + NL
109 ontology << INDENT*3 + 'items = <' + NL
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
114 end
115 ontology << INDENT*4 + '>'+NL
116 end
117 ontology << INDENT*3 + '>' + NL
118 ontology << INDENT*2 + '>' + NL
119 end
120 ontology << INDENT + '>' + NL
121 end
122
123 def merge
124 return header + NL + description + NL + definition + NL + ontology
125 end
126
127 private
128 def c_object
129 end
130
131 def existence(existence)
132 "existence matches {#{existence.lower}..#{existence.upper}}"
133 end
134 end
135
136 class XMLSerializer < BaseSerializer
137 def header
138 header = ''
139 xml = Builder::XmlMarkup.new(:indent => 2, :target => header)
140 xml.archetype_id do
141 xml.value @archetype.archetype_id.value
142 end
143 xml.concept @archetype.concept
144 xml.original_language do
145 xml.terminology_id do
146 xml.value @archetype.original_language.terminology_id.value
147 end
148 xml.code_string @archetype.original_language.code_string
149 end
150 return header
151 end
152
153 def description
154 desc = ''
155 xml = Builder::XmlMarkup.new(:indent => 2, :target => desc)
156 ad = @archetype.description
157 if ad
158 xml.description do
159 ad.original_author.each do |key,value|
160 xml.original_author(value,"id"=>key)
161 end
162 if ad.other_contributors
163 ad.other_contributors.each do |co|
164 xml.other_contributors co
165 end
166 end
167 xml.lifecycle_state ad.lifecycle_state
168 xml.details do
169 ad.details.each do |lang, item|
170 xml.language do
171 xml.terminology_id do
172 xml.value item.language.terminology_id.value
173 end
174 xml.code_string lang
175 end
176 xml.purpose item.purpose
177 if item.keywords then
178 item.keywords.each do |word|
179 xml.keywords word
180 end
181 end
182 xml.use item.use if item.use
183 xml.misuse item.misuse if item.misuse
184 xml.copyright item.copyright if item.copyright
185 if ad.other_details
186 ad.other_details.each do |key,value|
187 xml.other_details(value, "id"=>key)
188 end
189 end
190 end
191 end
192 end
193 end
194 return desc
195 end
196
197 def definition
198 definition = ''
199 ad = @archetype.definition
200 xml = Builder::XmlMarkup.new(:indent => 2, :target => definition)
201 xml.definition do
202 xml.rm_type_name ad.rm_type_name
203 xml.occurrence do
204 oc = ad.occurrences
205 xml.lower_included oc.lower_included? unless oc.lower_included?.nil?
206 xml.upper_included oc.upper_included? unless oc.upper_included?.nil?
207 xml.lower_unbounded oc.lower_unbounded?
208 xml.upper_unbounded oc.upper_unbounded?
209 xml.lower oc.lower
210 xml.upper oc.lower
211 end
212 xml.node_id ad.node_id
213 end
214 return definition
215 end
216
217 def ontology
218 ontology = ''
219 ao = @archetype.ontology
220 xml = Builder::XmlMarkup.new(:indent => 2, :target => ontology)
221 xml.ontology do
222 xml.specialisation_depth ao.specialisation_depth
223 xml.term_definitions do
224 ao.term_definitions.each do |lang, terms|
225 xml.language lang
226 xml.terms do
227 terms.each do |term|
228 xml.code term.code
229 xml.items do
230 term.items.each do |key, value|
231 xml.item do
232 xml.key key
233 xml.value value
234 end
235 end
236 end
237 end
238 end
239 end
240 end
241 end
242 end
243
244 def merge
245 archetype = "<?xml version='1.0' encoding='UTF-8'?>" + NL +
246 "<archetype xmlns=\"http://schemas.openehr.org/v1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + NL +
247 header + description + definition +
248 ontology + '</archetype>'
249 return archetype
250 end
251 end
252 end
253end
254
255class Publisher
256 def initialize(serializer)
257 @serializer = serializer
258 end
259
260 def publish(writer)
261 writer.out(@serializer.serialize)
262 end
263end
264
265class Writer
266 def initialize(target)
267 @target = target
268 end
269 def out
270 end
271end
Note: See TracBrowser for help on using the repository browser.