Class: PKMXml

Inherits:
Object
  • Object
show all
Defined in:
lib/pkm_level2_converter/pkm_xml.rb

Overview

XML-Klasse zu einem PKM

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ PKMXml

Returns a new instance of PKMXml.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 47

def initialize(file_name)
  @filename = file_name
  is_a_file = File.file?(@filename)
  puts "File ('#{@filename}') not found." unless is_a_file
  return unless is_a_file

  @xml_doc = File.open(@filename) { |f| Nokogiri::XML(f) }
  # Is it a valid PKM file?
  xml_is_invalid_pkm = (@xml_doc.nil? || !valid_xml?)
  puts 'File is not a valid PKM file' if xml_is_invalid_pkm
  return if xml_is_invalid_pkm

  @pkm_data = PKM.new(@xml_doc)
end

Instance Attribute Details

#pkm_dataObject (readonly)

Returns the value of attribute pkm_data.



62
63
64
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 62

def pkm_data
  @pkm_data
end

Class Method Details

.check_xsd(xsd, xml) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 15

def self.check_xsd(xsd, xml)
  puts 'Check XML-Schema...'
  xsd = Nokogiri::XML::Schema(xsd)
  error = xsd.validate(xml)
  if error.empty?
    puts 'XML-Schema is valid.'
    true
  else
    puts 'XML-Schema is invalid.'
    error.each do |e|
      puts e.message
    end
    false
  end
end

.convert_xpath_l3_id(node, x_path, parent_path) ⇒ Object



8
9
10
11
12
13
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 8

def self.convert_xpath_l3_id(node, x_path, parent_path)
  map = node.at_xpath x_path
  id = map.content.to_i
  map.content = (id + 0x8000).to_s
  puts "#{(parent_path + x_path).gsub('xmlns:', '')} = #{id} => #{map.text}"
end

.pathesObject



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
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 116

def self.pathes
  {
    dl_km: {
      herausgeber_xpath: '/xmlns:dl-km/xmlns:organisation/xmlns:id',
      enthaltene_pvmodule_xpath: '/xmlns:dl-km/xmlns:kontrollmodul-pool/xmlns:item',
      herausgeber_enthaltene_pvmodule_xpath: 'xmlns:moduldaten/xmlns:organisation/xmlns:id',
      zulaessige_organisationen_xpath: 'xmlns:moduldaten/xmlns:organisation-pool/xmlns:item',
      zulaessige_organisationen_org_id_xpath: 'xmlns:id'
    },
    pv_km: {
      herausgeber_xpath: '/xmlns:pv-km/xmlns:organisation/xmlns:id',
      enthaltene_pvmodule_xpath: '.',
      herausgeber_enthaltene_pvmodule_xpath: nil,
      zulaessige_organisationen_xpath: 'xmlns:pv-km/xmlns:organisation-pool/xmlns:item',
      zulaessige_organisationen_org_id_xpath: 'xmlns:id'
    },
    rn_tm: {
      herausgeber_xpath: '/xmlns:rntm/xmlns:herausgeber/xmlns:nr',
      enthaltene_pvmodule_xpath: '/xmlns:rntm/xmlns:tarifmodul-pool/xmlns:item',
      herausgeber_enthaltene_pvmodule_xpath: 'xmlns:tarifmodul/xmlns:herausgeber/xmlns:nr',
      zulaessige_organisationen_xpath: 'xmlns:tarifmodul/xmlns:organisation-pool/xmlns:item',
      zulaessige_organisationen_org_id_xpath: 'xmlns:nr'
    }
  }
end

.xml_is_valid_pkm(xml) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 31

def self.xml_is_valid_pkm(xml)
  # xsd =  File.read("./ka/pkm/1/XML-Schema_PKM.xsd")
  path = ''
  case xml.root.name
  when 'pv-km', 'dl-km'
    path = './ka/pkm/1/XML-Schema_PKM.xsd'
  when 'rntm'
    path = './ka/pkm/2/XML-Schema_PKM.xsd'
  end

  file_path = File.join(File.dirname(__FILE__), path)

  xsd = File.read(file_path)
  PKMXml.check_xsd(xsd, xml)
end

Instance Method Details

#convert_file_nameObject



71
72
73
74
75
76
77
78
79
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 71

def convert_file_name
  pn = Pathname.new(@filename)
  dir, base = File.split(pn)
  f = base.split('_')
  org_id_l3 = f[1].to_i
  org_id_l2 = org_id_l3 + 0x8000
  f[1] = org_id_l2.to_s
  Pathname.new(dir).join(f.join('_'))
end

#convert_ids_cr374Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 167

def convert_ids_cr374
  ## Ist die Ausgangsschnittstelle 3 bzw. 4 im Kontrollmodul vorhanden?
  return unless @pkm_data.cr374?

  ausgangskontexte = pkm_data.ermittle_alle_cr374_ausgangskontexte
  puts "#{ausgangskontexte.length} Ausgangskontext(e) zur Anpassung gefunden"
  # puts ausgangskontexte
  ausgangskontexte.each do |ausgangskontext|
    puts "Ausgangskontext \"#{ausgangskontext.name}\" - Key: #{ausgangskontext.key}"

    convert_keyref_l3_id(ausgangskontext.key)
  end
end

#convert_keyref_l3_id(keyref) ⇒ Object



181
182
183
184
185
186
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 181

def convert_keyref_l3_id(keyref)
  node = @xml_doc.at_xpath("//xmlns:text[@ref='#{keyref}']")
  id = node.content.to_i
  node.content = (id + 0x8000).to_s
  puts "Org-ID angepasst: #{id} --> #{node.content}"
end

#convert_org_ids_to_level2Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 156

def convert_org_ids_to_level2
  convert_orgid_at_path(@xml_doc, :herausgeber_xpath, @xml_doc.path)
  iterate_on_xpath(@xml_doc, :enthaltene_pvmodule_xpath) do |pvmodul, iterator_path|
    convert_orgid_at_path(pvmodul, :herausgeber_enthaltene_pvmodule_xpath, iterator_path)
    iterate_on_xpath(pvmodul, :zulaessige_organisationen_xpath) do |organisation, second_iterator_path|
      concated_iterator_path = iterator_path + second_iterator_path
      convert_orgid_at_path(organisation, :zulaessige_organisationen_org_id_xpath, concated_iterator_path)
    end
  end
end

#convert_orgid_at_path(node, symbol, parent_path) ⇒ Object



146
147
148
149
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 146

def convert_orgid_at_path(node, symbol, parent_path)
  path = path_to(symbol)
  PKMXml.convert_xpath_l3_id(node, path, parent_path) if path
end

#iterate_on_xpath(node, symbol, &block) ⇒ Object



151
152
153
154
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 151

def iterate_on_xpath(node, symbol, &block)
  iterator_path = path_to(symbol)
  node.xpath(iterator_path).each { |element| block.call(element, iterator_path) }
end

#path_to(symbol) ⇒ Object



142
143
144
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 142

def path_to(symbol)
  PKMXml.pathes.dig type, symbol
end

#save_as_level2Object



81
82
83
84
85
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 81

def save_as_level2
  convert_org_ids_to_level2
  convert_ids_cr374
  save_file
end

#save_fileObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 91

def save_file
  return unless valid_xml?

  # SAVE FILE with NEW name
  new_file_name = convert_file_name
  output = File.open(new_file_name, 'w')
  output << @xml_doc.to_xml(indent_text: '', indent: 0).gsub(">\n", '>')
  output.close
  puts "File saved as: #{new_file_name}"
end

#typeObject



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 102

def type
  case @xml_doc.root.name
  when 'pv-km'
    :pv_km
  when 'dl-km'
    :dl_km
  when 'rntm'
    :rn_tm
  else
    puts("#{@xml_doc.root.name} wird nicht unterstützt.")
    nil
  end
end

#valid_xml?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 87

def valid_xml?
  PKMXml.xml_is_valid_pkm(@xml_doc)
end

#xml_key_itemsObject



64
65
66
67
68
69
# File 'lib/pkm_level2_converter/pkm_xml.rb', line 64

def xml_key_items
  # exclude tarifmodul-pool
  # exclude kontrollmodul-pool
  x = '//*[not(ancestor-or-self::xmlns:kontrollmodul-pool) and not(ancestor-or-self::xmlns:tarifmodul-pool) and @key]'
  @xml_doc.xpath(x)
end