dynarex-import 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/dynarex-import.rb +123 -0
  2. metadata +55 -0
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: dynarex-import.rb
4
+
5
+ require 'nokogiri'
6
+
7
+ class DynarexImport
8
+
9
+ attr_reader :to_xml
10
+
11
+ def initialize(options={})
12
+ o = {xml: '', foreign_schema: '', schema: ''}.merge(options)
13
+ @xsl = build_xsl(o[:foreign_schema], o[:schema])
14
+ @to_xml = transform(xsl, o[:xml])
15
+ end
16
+
17
+ private
18
+
19
+ def build_xsl(foreign_schema, native_schema)
20
+
21
+ a1 = capture_fields(native_schema)
22
+ a2 = capture_fields(foreign_schema)
23
+
24
+ h = {
25
+ summary: [
26
+ a1[0][0], a2[0][0], # root name, and xpath
27
+ Hash[a1[0][-1].zip(a2[0][-1])] # summary fields
28
+ ],
29
+ records: [
30
+ a1[-1][0].to_sym, a2[-1][0], # record name, and relative record name
31
+ Hash[a1[-1][-1].zip(a2[-1][-1])] # record fields
32
+ ]
33
+ }
34
+
35
+ xsl = "<?xml version='1.0' encoding='UTF-8'?>
36
+ <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
37
+ <xsl:output method='xml' indent='yes' />
38
+
39
+ "
40
+ xsl << "
41
+ <xsl:template match='#{h[:summary][1]}'>
42
+ <xsl:element name='#{h[:summary][0]}'>
43
+ <xsl:element name='summary'>
44
+ "
45
+
46
+ h[:summary][-1].each do |key,value|
47
+ xsl << "
48
+ <xsl:element name='#{key}'>
49
+ <xsl:value-of select='#{value}'/>
50
+ </xsl:element>
51
+ "
52
+ end
53
+
54
+ xsl << "
55
+ <recordx_type>dynarex</recordx_type>
56
+ <schema>#{native_schema}</schema>
57
+ </xsl:element>
58
+
59
+ <xsl:element name='records'>
60
+ <xsl:apply-templates select='#{h[:records][0]}'/>
61
+ </xsl:element>
62
+
63
+ </xsl:element>
64
+
65
+ </xsl:template>
66
+
67
+ <xsl:template match='#{h[:records][1]}'>
68
+ <xsl:element name='#{h[:records][0]}'>
69
+ "
70
+
71
+ h[:records][-1].values.each do |value|
72
+ xsl << "
73
+ <xsl:apply-templates select='#{value}'/>"
74
+ end
75
+
76
+ xsl << "
77
+
78
+ </xsl:element>
79
+ <xsl:text>
80
+ </xsl:text>
81
+ "
82
+
83
+ xsl << "
84
+ </xsl:template>
85
+ "
86
+
87
+ h[:records][-1].each do |key,value|
88
+ xsl << "
89
+ <xsl:template match='#{value}'>
90
+ <xsl:element name='#{key}'>
91
+ <xsl:value-of select='.'/>
92
+ </xsl:element>
93
+ </xsl:template>
94
+ "
95
+ end
96
+
97
+ xsl << "
98
+ </xsl:stylesheet>
99
+ "
100
+ xsl
101
+
102
+ end
103
+
104
+ def capture_fields(schema)
105
+ rec_name, raw_rec_fields = schema.match(/(\w+)\(([^\)]+)\)$/).captures
106
+ rec_fields = raw_rec_fields.split(',').map(&:strip)
107
+
108
+ summary_name, raw_summary_fields = ($`).match(/([^\[]+)(\[([^\]]+)\]\/)?$/)\
109
+ .captures.values_at(0, -1)
110
+ summary_fields = raw_summary_fields.split(',').map(&:strip)
111
+
112
+ [[summary_name, summary_fields], [rec_name, rec_fields]]
113
+ end
114
+
115
+ def transform(xsl, xml)
116
+ doc = Nokogiri::XML(xml)
117
+ xslt = Nokogiri::XSLT(xsl)
118
+ xslt.transform(doc).to_xml
119
+ end
120
+
121
+ end
122
+
123
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynarex-import
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2011-02-01 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/dynarex-import.rb
26
+ has_rdoc: true
27
+ homepage:
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: dynarex-import
54
+ test_files: []
55
+