Module: HQ::Engine::LibXmlRubyMixin

Included in:
Engine, Transformer
Defined in:
lib/hq/engine/libxmlruby-mixin.rb

Instance Method Summary collapse

Instance Method Details

#field_to_json(schemas, schema_elem, fields_elem, elem, value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 74

def field_to_json schemas, schema_elem, fields_elem, elem, value

  fields_elem.find(
    "* [name() != 'option']
  ").each do
    |field_elem|

    field_name =
      field_elem["name"]

    case field_elem.name

      when "text"

        value[field_name] =
          elem[field_name]

      when "int", "ts-update"

        temp = elem.attributes[field_name]

        value[field_name] = temp.empty? ? nil : temp.to_i

      when "list"

        value[field_name] = []

        elem.find("* [ name () = #{xp field_name} ]") \
          .each do |child_elem|

          prop = {}

          field_to_json \
            schemas,
            schema_elem,
            field_elem,
            child_elem,
            prop

          value[field_name] << prop
        end

      when "struct"

        prop = {}

        child_elem =
          elem.find_first \
            "* [ name () = #{xp field_name} ]"

        if child_elem
          field_to_json \
            schemas,
            schema_elem,
            field_elem,
            child_elem,
            prop
        end

        value[field_name] = prop unless prop.empty?

      when "xml"

        value[field_name] = ""

        elem.find("* [ name () = #{xp field_name} ] / *") \
          .each do |prop|

          value[field_name] += prop.to_s
        end

      when "bool"

        value[field_name] = \
          elem.attributes[field_name] == "yes"

      when "bigtext"

        value[field_name] =
          elem.find_first("* [ name () = #{xp field_name} ]") \
            .content

      else

        raise "unexpected element #{field_elem.name} found in " \
          "field list for schema " \
          "#{schema_elem.attributes["name"]}"

    end
  end

  content = []

  elem.find("*").each do |child_elem|

    option_elem =
      fields_elem.find_first \
        "option [ @name = #{xp child_elem.name} ]"

    next unless option_elem

    option_ref =
      option_elem.attributes["ref"]

    schema_option_elem =
      schemas["schema-opion/#{option_ref}"]

    raise "Error" \
      unless schema_option_elem

    schema_option_props_elem =
      schema_option_elem.find_first "props"

    prop = {}

    field_to_json \
      schemas,
      schema_elem,
      schema_option_props_elem,
      child_elem,
      prop

    content << {
      "type" => child_elem.name,
      "value" => prop,
    }

  end

  value["content"] = content \
    unless content.empty?

end

#field_to_xml(schemas, fields_elem, value, elem) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 253

def field_to_xml schemas, fields_elem, value, elem

  unless value.is_a? Hash
    value = {}
  end

  fields_elem.find("
    * [name() != 'option']
  ").each do
    |field_elem|

    field_name =
      field_elem["name"]

    case field_elem.name

    when "text", "int", "ts-update", "enum"
      elem[field_name] = value[field_name].to_s

    when "bigtext"
      prop = XML::Node.new field_name
      prop << value[field_name]
      elem << prop

    when "bool"
      elem[field_name] = "yes" if value[field_name]

    when "list"

      items = value[field_name]

      if items.is_a? Array

        items.each do
          |item|

          prop =
            XML::Node.new field_name

          field_to_xml \
            schemas,
            field_elem,
            item,
            prop

          elem << prop

        end

      end

    when "struct"

      prop =
        XML::Node.new field_name

      field_to_xml \
        schemas,
        field_elem,
        value[field_name],
        prop

      if prop.attributes.length + prop.children.size > 0
        elem << prop
      end

    when "xml"

      prop =
        XML::Node.new field_name

      temp_doc =
        XML::Document.string \
          "<xml>#{value[field_name]}</xml>",
          :options =>XML::Parser::Options::NOBLANKS

      temp_doc.root.each do
        |temp_elem|
        prop << temp_elem.copy(true)
      end

      elem << prop

    else

      raise "unexpected element #{field_elem.name} found in field "
        "list for schema #{schema_elem["name"]}"

    end

  end

  if value["content"].is_a? Array

    value["content"].each do
      |item|

      item_type = item["type"]
      item_value = item["value"]

      option_elem =
        fields_elem.find_first "
          option [@name = '#{item_type}']
        "

      next unless option_elem

      option_ref =
        option_elem["ref"]

      schema_option_elem =
        schemas["schema-option/#{option_ref}"]

      next unless schema_option_elem

      schema_option_props_elem =
        schema_option_elem.find_first "props"

      prop =
        XML::Node.new item_type

      field_to_xml \
        schemas,
        schema_option_props_elem,
        item_value,
        prop

      elem << prop

    end

  end

end

#get_record_id_long(schemas, record_elem) ⇒ Object



419
420
421
422
423
424
425
426
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 419

def get_record_id_long schemas, record_elem

  return "%s/%s" % [
    record_elem.name,
    get_record_id_short(schemas, record_elem),
  ]

end

#get_record_id_short(schemas, record_elem) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 388

def get_record_id_short schemas, record_elem

  schema_elem =
    schemas["schema/#{record_elem.name}"]

  unless schema_elem
    raise "No schema for #{record_elem.name}"
  end

  id_parts =
    schema_elem.find("id/*").to_a.map do
      |id_elem|

      part =
        record_elem[id_elem["name"]]

      unless part
        raise "No #{id_elem["name"]} for #{record_elem.name}"
      end

      part

    end

  id =
    id_parts.join "/"

  return id

end

#js_to_xml(schemas, type, value) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 208

def js_to_xml schemas, type, value

  schema_elem =
    schemas["schema/#{type}"]

  elem =
    XML::Node.new type

  field_to_xml \
    schemas,
    schema_elem.find_first("id"),
    value,
    elem

  field_to_xml \
    schemas,
    schema_elem.find_first("fields"),
    value,
    elem

  return elem

end

#load_data_file(filename) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 8

def load_data_file filename

  ret = []

  doc =
    XML::Document.file \
      filename,
      :options =>XML::Parser::Options::NOBLANKS

  return doc.find("//data/*").to_a

end

#load_data_string(string) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 21

def load_data_string string

  ret = []

  doc =
    XML::Document.string \
      string,
      :options =>XML::Parser::Options::NOBLANKS

  return doc.find("//data/*").to_a

end

#load_schema_file(filename) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 51

def load_schema_file filename

  schema_doc =
    XML::Document.file filename

  schema =
    Hash[
      schema_doc.find("*").map do
        |schema_elem|
        [
          "%s/%s" % [
            schema_elem.name,
            schema_elem["name"],
          ],
          schema_elem,
        ]
      end
    ]

  return schema

end

#to_xml_string(elem) ⇒ Object



428
429
430
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 428

def to_xml_string elem
  return elem.to_s
end

#write_data_file(filename, data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 34

def write_data_file filename, data

  doc = XML::Document.new
  doc.root = XML::Node.new "data"

  data.each do
    |item|
    doc.root << doc.import(item)
  end

  File.open filename, "w" do |f|
    f.print doc.to_s
  end

end

#xml_to_json(schemas, schema_elem, elem) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/hq/engine/libxmlruby-mixin.rb', line 232

def xml_to_json schemas, schema_elem, elem

  value = {}

  field_to_json \
    schemas,
    schema_elem,
    schema_elem.find_first("id"),
    elem,
    value

  field_to_json \
    schemas,
    schema_elem,
    schema_elem.find_first("fields"),
    elem,
    value

  return value
end