pkm_level2_converter 0.2.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- fad5c8711005a03330485502117a9c5a *XML-Schema_PKM.xsd
1
+ fad5c8711005a03330485502117a9c5a *XML-Schema_PKM.xsd
@@ -1,178 +1,178 @@
1
- <?xml version="1.0" encoding="utf-8"?>
2
- <xs:schema version="1.7.1-SNAPSHOT"
3
- targetNamespace="http://vdv/ka/pkm/2"
4
- xmlns:tns="http://vdv/ka/pkm/2"
5
- xmlns:ion="http://vdv/ka/ion/2"
6
- xmlns:ct="http://vdv/ka/common/types/2"
7
- xmlns:ka="http://vdv/ka/common/2"
8
- xmlns:pkm="http://vdv/ka/pkm/2"
9
- xmlns:xs="http://www.w3.org/2001/XMLSchema"
10
- xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
11
-
12
- <xs:import namespace="http://vdv/ka/common/types/2" schemaLocation="../../../ka/common/types/2/XML-Schema_Common_Types.xsd" />
13
- <xs:import namespace="http://vdv/ka/common/2" schemaLocation="../../../ka/common/2/XML-Schema_KA_Common.xsd" />
14
- <xs:import namespace="http://vdv/ka/ion/2" schemaLocation="../../../ka/ion/2/XML-Schema_ION.xsd" />
15
- <xs:include xmlns="http://vdv/ka/pkm/2" schemaLocation="../../../ka/pkm/2/XML-Schema_PKM.xsd"/>
16
-
17
- <xs:annotation>
18
- <xs:documentation>
19
- Ersetzt XML-Schema_PKM_TX aus der Version 1.3.0 mit Namensraum http://vdv/ka/pkm/1.
20
- Fuer das neue Modul (zusammen mit KA Release 1.6.0) wird der neue Namensraum http://vdv/ka/pkm/2 verwendet.
21
- Die Hilfsdatentypen (TX_MODUL, etc.) in diesem Schema mussten aufgrund des neuen
22
- Namensraumes gedoppelt werden.
23
- </xs:documentation>
24
- </xs:annotation>
25
-
26
- <xs:simpleType name="Modulversion_NUMMER">
27
- <xs:annotation>
28
- <xs:documentation>
29
- Die Modulversion_NUMMER identifiziert einen Ergebnisstand innerhalb eines Entwicklungszyklus.
30
- Eine gueltige Modulversion_NUMMER ist groeßer als 0.
31
- </xs:documentation>
32
- </xs:annotation>
33
- <xs:restriction base="ct:ReferenceNumberFour"/>
34
- </xs:simpleType>
35
-
36
- <xs:simpleType name="Modulstatus_CODE">
37
- <xs:annotation>
38
- <xs:documentation>
39
- Der Modulstatus_CODE gibt den Bearbeitungsstatus eines Produkt- oder Kontrollmoduls an.
40
- </xs:documentation>
41
- </xs:annotation>
42
- <xs:restriction base="ct:ReferenceNumberOne">
43
- <xs:enumeration value="0">
44
- <xs:annotation>
45
- <xs:documentation>Undefiniert</xs:documentation>
46
- </xs:annotation>
47
- </xs:enumeration>
48
- <xs:enumeration value="1">
49
- <xs:annotation>
50
- <xs:documentation>Entwurf</xs:documentation>
51
- </xs:annotation>
52
- </xs:enumeration>
53
- <xs:enumeration value="2">
54
- <xs:annotation>
55
- <xs:documentation>Freigegeben</xs:documentation>
56
- </xs:annotation>
57
- </xs:enumeration>
58
- </xs:restriction>
59
- </xs:simpleType>
60
-
61
- <xs:complexType name="TX_MODUL" abstract="true">
62
- <xs:annotation>
63
- <xs:documentation>
64
- Ein TX_MODUL gilt für alle Transaktionsdatensaetze zur Verteilung von Produkt- und Kontrollmodulen.
65
- Dieser Datensatz enthaelt Auszuege aus den Modul- und Versionsdaten eines Moduls.
66
- </xs:documentation>
67
- </xs:annotation>
68
- <xs:sequence>
69
- <xs:element name="modulgueltigkeitsbeginn" type="ct:DateTimeCompact"/>
70
- <xs:element name="modulversion" type="tns:Modulversion_NUMMER"/>
71
- <xs:element name="modulstatus" type="tns:Modulstatus_CODE"/>
72
- <xs:element name="schnittstellenversion" type="ion:PrintableString"/>
73
- <xs:element name="modulnamenszusatz" type="ion:PrintableString"/>
74
- <xs:element name="bemerkung" type="ion:PrintableString"/>
75
- </xs:sequence>
76
- </xs:complexType>
77
-
78
-
79
- <xs:complexType name="TX_MODUL_Daten">
80
- <xs:annotation>
81
- <xs:documentation>
82
- Ein TX_MODUL_Daten ist die Auspraegung eines TX_MODUL,
83
- der genau dann verwendet wird, wenn der Transaktionsdatensatz
84
- zur Verteilung von Produkt- bzw. Kontrollmodul ein
85
- entsprechendes Modul enthaelt.
86
- </xs:documentation>
87
- </xs:annotation>
88
- <xs:complexContent>
89
- <xs:extension base="tns:TX_MODUL" />
90
- </xs:complexContent>
91
- </xs:complexType>
92
-
93
- <xs:complexType name="TX_MODUL_Bereitstellung">
94
- <xs:annotation>
95
- <xs:documentation>
96
- Ein TX_MODUL_Bereitstellung ist die Auspraegung eines TX_MODUL,
97
- wenn der Transaktionsdatensatz zur Verteilung von Produkt- bzw.
98
- Kontrollmodul kein Modul, sondern nur Informationen zum Download
99
- des entsprechenden Moduls enthaelt.
100
- </xs:documentation>
101
- </xs:annotation>
102
- <xs:complexContent>
103
- <xs:extension base="tns:TX_MODUL">
104
- <xs:sequence>
105
- <xs:element name="url" type="ion:PrintableString"></xs:element>
106
- <xs:element name="bereitstellungsdatum" type="ct:DateCompact"></xs:element>
107
- </xs:sequence>
108
- </xs:extension>
109
- </xs:complexContent>
110
- </xs:complexType>
111
-
112
- <xs:complexType name="TX_MODUL_Lieferung">
113
- <xs:annotation>
114
- <xs:documentation>
115
- Ein TX_MODUL_Lieferung ist die Auspraegung eines TX_MODUL,
116
- wenn der Transaktionsdatensatz zur Verteilung von Produkt- bzw.
117
- Kontrollmodul kein Modul, sondern Angaben zur anderweitigen
118
- Lieferung des entsprechenden Moduls enthaelt.
119
- </xs:documentation>
120
- </xs:annotation>
121
- <xs:complexContent>
122
- <xs:extension base="tns:TX_MODUL">
123
- <xs:sequence>
124
- <xs:element name="lieferart" type="ion:PrintableString"></xs:element>
125
- <xs:element name="sendedatum" type="ct:DateCompact"></xs:element>
126
- </xs:sequence>
127
- </xs:extension>
128
- </xs:complexContent>
129
- </xs:complexType>
130
-
131
- <xs:complexType name="TXVPVTM_Type">
132
- <xs:annotation>
133
- <xs:documentation>
134
- Verteilung von PV-Tarifmodulen, auf deren Basis die Preisberechnung
135
- fuer die ausgegebenen Berechtigungen erfolgt.
136
- Mit TXVPVTM werden Tarifmodule vom PV u.a. an das Produktclearing uebermittelt.
137
- Die Struktur enthaelt neben den eigentlichen Daten des Tarifmoduls auch
138
- ergaenzende Informationen zum Modul, zur Lieferung und Bereitstellung.
139
- Als innerer Datentyp wird
140
- </xs:documentation>
141
- </xs:annotation>
142
- <xs:complexContent>
143
- <xs:extension base="ion:TX_BASE_Type">
144
- <xs:sequence>
145
- <xs:element name="modulinformation" type="tns:TX_MODUL"/>
146
- <xs:element name="pvtarifmodul" type="xs:base64Binary" xmime:expectedContentTypes='application/gzip'>
147
- <xs:annotation>
148
- <xs:documentation>
149
- Dieses Element enthaelt die PKM Beschreibung in XML,
150
- deren Format in pkm:XML-PVTarifmodul bzw.
151
- http://vdv/ka/pkm/2:XML-PVTarifmodul definiert wird.
152
- Die Daten liegen im XML Format vor, die im gzip Format (RFC1952) komprimiert und anschliessend
153
- Base64 codiert sind.
154
- Um die PKM Beschreibungsdaten zu erhalten, muss eine Dekodierung von Base64 nach Binary
155
- stattfinden. Dieses Binaerformat ist dann gezippt und kann nach Umwandeln in eine Datei
156
- mit einer Standard-Software auf Windows oder Unix entpackt werden.
157
- Das Einbetten der XML-Struktur in gezippten Base64 bietet zum einen den Vorteil der
158
- Komprimierung von ca. 10:1. Zum anderen ist ein weiterer Vorteil, dass die XSD-Validierung nicht
159
- bei Annahme der Nachricht stattfindet sondern zu einem spaeteren Zeitpunkt
160
- durchgefuehrt werden kann. Damit kann die Annahme eines PKM-Moduls synchron erfolgen.
161
- </xs:documentation>
162
- </xs:annotation>
163
- </xs:element>
164
- </xs:sequence>
165
- </xs:extension>
166
- </xs:complexContent>
167
- </xs:complexType>
168
-
169
- <xs:element name="TXVPVTM" type="tns:TXVPVTM_Type">
170
- <xs:annotation>
171
- <xs:documentation>
172
- Element zur Verwendung im ION (WSDL). Verteilung von PV-Tarifmodulen,
173
- auf deren Basis die Preisberechnung fuer die ausgegebenen Berechtigungen erfolgt.
174
- </xs:documentation>
175
- </xs:annotation>
176
- </xs:element>
177
-
178
- </xs:schema>
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <xs:schema version="1.7.1-SNAPSHOT"
3
+ targetNamespace="http://vdv/ka/pkm/2"
4
+ xmlns:tns="http://vdv/ka/pkm/2"
5
+ xmlns:ion="http://vdv/ka/ion/2"
6
+ xmlns:ct="http://vdv/ka/common/types/2"
7
+ xmlns:ka="http://vdv/ka/common/2"
8
+ xmlns:pkm="http://vdv/ka/pkm/2"
9
+ xmlns:xs="http://www.w3.org/2001/XMLSchema"
10
+ xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
11
+
12
+ <xs:import namespace="http://vdv/ka/common/types/2" schemaLocation="../../../ka/common/types/2/XML-Schema_Common_Types.xsd" />
13
+ <xs:import namespace="http://vdv/ka/common/2" schemaLocation="../../../ka/common/2/XML-Schema_KA_Common.xsd" />
14
+ <xs:import namespace="http://vdv/ka/ion/2" schemaLocation="../../../ka/ion/2/XML-Schema_ION.xsd" />
15
+ <xs:include xmlns="http://vdv/ka/pkm/2" schemaLocation="../../../ka/pkm/2/XML-Schema_PKM.xsd"/>
16
+
17
+ <xs:annotation>
18
+ <xs:documentation>
19
+ Ersetzt XML-Schema_PKM_TX aus der Version 1.3.0 mit Namensraum http://vdv/ka/pkm/1.
20
+ Fuer das neue Modul (zusammen mit KA Release 1.6.0) wird der neue Namensraum http://vdv/ka/pkm/2 verwendet.
21
+ Die Hilfsdatentypen (TX_MODUL, etc.) in diesem Schema mussten aufgrund des neuen
22
+ Namensraumes gedoppelt werden.
23
+ </xs:documentation>
24
+ </xs:annotation>
25
+
26
+ <xs:simpleType name="Modulversion_NUMMER">
27
+ <xs:annotation>
28
+ <xs:documentation>
29
+ Die Modulversion_NUMMER identifiziert einen Ergebnisstand innerhalb eines Entwicklungszyklus.
30
+ Eine gueltige Modulversion_NUMMER ist groeßer als 0.
31
+ </xs:documentation>
32
+ </xs:annotation>
33
+ <xs:restriction base="ct:ReferenceNumberFour"/>
34
+ </xs:simpleType>
35
+
36
+ <xs:simpleType name="Modulstatus_CODE">
37
+ <xs:annotation>
38
+ <xs:documentation>
39
+ Der Modulstatus_CODE gibt den Bearbeitungsstatus eines Produkt- oder Kontrollmoduls an.
40
+ </xs:documentation>
41
+ </xs:annotation>
42
+ <xs:restriction base="ct:ReferenceNumberOne">
43
+ <xs:enumeration value="0">
44
+ <xs:annotation>
45
+ <xs:documentation>Undefiniert</xs:documentation>
46
+ </xs:annotation>
47
+ </xs:enumeration>
48
+ <xs:enumeration value="1">
49
+ <xs:annotation>
50
+ <xs:documentation>Entwurf</xs:documentation>
51
+ </xs:annotation>
52
+ </xs:enumeration>
53
+ <xs:enumeration value="2">
54
+ <xs:annotation>
55
+ <xs:documentation>Freigegeben</xs:documentation>
56
+ </xs:annotation>
57
+ </xs:enumeration>
58
+ </xs:restriction>
59
+ </xs:simpleType>
60
+
61
+ <xs:complexType name="TX_MODUL" abstract="true">
62
+ <xs:annotation>
63
+ <xs:documentation>
64
+ Ein TX_MODUL gilt für alle Transaktionsdatensaetze zur Verteilung von Produkt- und Kontrollmodulen.
65
+ Dieser Datensatz enthaelt Auszuege aus den Modul- und Versionsdaten eines Moduls.
66
+ </xs:documentation>
67
+ </xs:annotation>
68
+ <xs:sequence>
69
+ <xs:element name="modulgueltigkeitsbeginn" type="ct:DateTimeCompact"/>
70
+ <xs:element name="modulversion" type="tns:Modulversion_NUMMER"/>
71
+ <xs:element name="modulstatus" type="tns:Modulstatus_CODE"/>
72
+ <xs:element name="schnittstellenversion" type="ion:PrintableString"/>
73
+ <xs:element name="modulnamenszusatz" type="ion:PrintableString"/>
74
+ <xs:element name="bemerkung" type="ion:PrintableString"/>
75
+ </xs:sequence>
76
+ </xs:complexType>
77
+
78
+
79
+ <xs:complexType name="TX_MODUL_Daten">
80
+ <xs:annotation>
81
+ <xs:documentation>
82
+ Ein TX_MODUL_Daten ist die Auspraegung eines TX_MODUL,
83
+ der genau dann verwendet wird, wenn der Transaktionsdatensatz
84
+ zur Verteilung von Produkt- bzw. Kontrollmodul ein
85
+ entsprechendes Modul enthaelt.
86
+ </xs:documentation>
87
+ </xs:annotation>
88
+ <xs:complexContent>
89
+ <xs:extension base="tns:TX_MODUL" />
90
+ </xs:complexContent>
91
+ </xs:complexType>
92
+
93
+ <xs:complexType name="TX_MODUL_Bereitstellung">
94
+ <xs:annotation>
95
+ <xs:documentation>
96
+ Ein TX_MODUL_Bereitstellung ist die Auspraegung eines TX_MODUL,
97
+ wenn der Transaktionsdatensatz zur Verteilung von Produkt- bzw.
98
+ Kontrollmodul kein Modul, sondern nur Informationen zum Download
99
+ des entsprechenden Moduls enthaelt.
100
+ </xs:documentation>
101
+ </xs:annotation>
102
+ <xs:complexContent>
103
+ <xs:extension base="tns:TX_MODUL">
104
+ <xs:sequence>
105
+ <xs:element name="url" type="ion:PrintableString"></xs:element>
106
+ <xs:element name="bereitstellungsdatum" type="ct:DateCompact"></xs:element>
107
+ </xs:sequence>
108
+ </xs:extension>
109
+ </xs:complexContent>
110
+ </xs:complexType>
111
+
112
+ <xs:complexType name="TX_MODUL_Lieferung">
113
+ <xs:annotation>
114
+ <xs:documentation>
115
+ Ein TX_MODUL_Lieferung ist die Auspraegung eines TX_MODUL,
116
+ wenn der Transaktionsdatensatz zur Verteilung von Produkt- bzw.
117
+ Kontrollmodul kein Modul, sondern Angaben zur anderweitigen
118
+ Lieferung des entsprechenden Moduls enthaelt.
119
+ </xs:documentation>
120
+ </xs:annotation>
121
+ <xs:complexContent>
122
+ <xs:extension base="tns:TX_MODUL">
123
+ <xs:sequence>
124
+ <xs:element name="lieferart" type="ion:PrintableString"></xs:element>
125
+ <xs:element name="sendedatum" type="ct:DateCompact"></xs:element>
126
+ </xs:sequence>
127
+ </xs:extension>
128
+ </xs:complexContent>
129
+ </xs:complexType>
130
+
131
+ <xs:complexType name="TXVPVTM_Type">
132
+ <xs:annotation>
133
+ <xs:documentation>
134
+ Verteilung von PV-Tarifmodulen, auf deren Basis die Preisberechnung
135
+ fuer die ausgegebenen Berechtigungen erfolgt.
136
+ Mit TXVPVTM werden Tarifmodule vom PV u.a. an das Produktclearing uebermittelt.
137
+ Die Struktur enthaelt neben den eigentlichen Daten des Tarifmoduls auch
138
+ ergaenzende Informationen zum Modul, zur Lieferung und Bereitstellung.
139
+ Als innerer Datentyp wird
140
+ </xs:documentation>
141
+ </xs:annotation>
142
+ <xs:complexContent>
143
+ <xs:extension base="ion:TX_BASE_Type">
144
+ <xs:sequence>
145
+ <xs:element name="modulinformation" type="tns:TX_MODUL"/>
146
+ <xs:element name="pvtarifmodul" type="xs:base64Binary" xmime:expectedContentTypes='application/gzip'>
147
+ <xs:annotation>
148
+ <xs:documentation>
149
+ Dieses Element enthaelt die PKM Beschreibung in XML,
150
+ deren Format in pkm:XML-PVTarifmodul bzw.
151
+ http://vdv/ka/pkm/2:XML-PVTarifmodul definiert wird.
152
+ Die Daten liegen im XML Format vor, die im gzip Format (RFC1952) komprimiert und anschliessend
153
+ Base64 codiert sind.
154
+ Um die PKM Beschreibungsdaten zu erhalten, muss eine Dekodierung von Base64 nach Binary
155
+ stattfinden. Dieses Binaerformat ist dann gezippt und kann nach Umwandeln in eine Datei
156
+ mit einer Standard-Software auf Windows oder Unix entpackt werden.
157
+ Das Einbetten der XML-Struktur in gezippten Base64 bietet zum einen den Vorteil der
158
+ Komprimierung von ca. 10:1. Zum anderen ist ein weiterer Vorteil, dass die XSD-Validierung nicht
159
+ bei Annahme der Nachricht stattfindet sondern zu einem spaeteren Zeitpunkt
160
+ durchgefuehrt werden kann. Damit kann die Annahme eines PKM-Moduls synchron erfolgen.
161
+ </xs:documentation>
162
+ </xs:annotation>
163
+ </xs:element>
164
+ </xs:sequence>
165
+ </xs:extension>
166
+ </xs:complexContent>
167
+ </xs:complexType>
168
+
169
+ <xs:element name="TXVPVTM" type="tns:TXVPVTM_Type">
170
+ <xs:annotation>
171
+ <xs:documentation>
172
+ Element zur Verwendung im ION (WSDL). Verteilung von PV-Tarifmodulen,
173
+ auf deren Basis die Preisberechnung fuer die ausgegebenen Berechtigungen erfolgt.
174
+ </xs:documentation>
175
+ </xs:annotation>
176
+ </xs:element>
177
+
178
+ </xs:schema>
@@ -1 +1 @@
1
- d610f7403ee7f1df9c4cb1e28b711081 *XML-Schema_PKM_TX.xsd
1
+ d610f7403ee7f1df9c4cb1e28b711081 *XML-Schema_PKM_TX.xsd
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'pools'
4
+
5
+ # Main Class
6
+ class PKM
7
+ def self.parse_pool(xml, pool_symbol)
8
+ pool = PKM.pools[pool_symbol]
9
+ path_of_pool = pool[:path_of_pool]
10
+ type_of_pool = pool[:type_of_pool]
11
+ Pool.parse_pool(xml, path_of_pool, type_of_pool)
12
+ end
13
+
14
+ def self.pools
15
+ # rubocop:disable Layout/LineLength
16
+ {
17
+ ausgangskontext_pool: {
18
+ path_of_pool: '//xmlns:rntm/xmlns:ausgangskontext-pool | //xmlns:dl-km/xmlns:ausgangskontext-pool | //xmlns:pv-km/xmlns:ausgangskontext-pool',
19
+ type_of_pool: AusgangskontextPool
20
+ },
21
+ ausgangsschnittstellen_pool: {
22
+ path_of_pool: '//xmlns:rntm/xmlns:ausgangsschnittstelle-pool | //xmlns:dl-km/xmlns:ausgangsschnittstelle-pool | //xmlns:pv-km/xmlns:ausgangsschnittstelle-pool',
23
+ type_of_pool: AusgangsschnittstellenPool
24
+ },
25
+ sprache_pool: {
26
+ path_of_pool: '//xmlns:rntm/xmlns:sprache-pool | //xmlns:dl-km/xmlns:sprache-pool | //xmlns:pv-km/xmlns:sprache-pool',
27
+ type_of_pool: SprachePool
28
+ }
29
+ }
30
+ # rubocop:enable Layout/LineLength
31
+ end
32
+
33
+ def initialize(pkm_xml)
34
+ @xml_doc = pkm_xml
35
+ @sprache_pool = PKM.parse_pool(@xml_doc, :sprache_pool)
36
+ @ausgangsschnittstellen_pool = PKM.parse_pool(@xml_doc, :ausgangsschnittstellen_pool)
37
+ @ausgangskontext_pool = PKM.parse_pool(@xml_doc, :ausgangskontext_pool)
38
+ end
39
+
40
+ attr_reader :ausgangskontext_pool, :ausgangsschnittstellen_pool, :xml_doc
41
+
42
+ def cr374?
43
+ return false if @ausgangsschnittstellen_pool.nil? || @ausgangsschnittstellen_pool.empty?
44
+
45
+ @ausgangsschnittstellen_pool.cr374?
46
+ end
47
+
48
+ def ermittle_alle_cr374_ausgangskontexte
49
+ asst_pool = @ausgangsschnittstellen_pool.cr374
50
+ puts "Es wurde #{asst_pool.length} Schnittstelle(n) nach CR 374 gefunden"
51
+ ausgangskontexte = asst_pool.to_set.collect! do |asst|
52
+ puts "\nAusgangsschnittstelle #{asst.nr} (\"#{asst.name}\") mit #{asst.parameter_pool.length} Ausgangsparametern:"
53
+ asst.ermittle_alle_cr374_ausgangskontexte_zu_ausgangsparametern(ausgangskontext_pool)
54
+ end
55
+ ausgangskontexte.flatten
56
+ end
57
+ end
@@ -0,0 +1,202 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nokogiri'
4
+ require 'pathname'
5
+
6
+ # XML-Klasse zu einem PKM
7
+ class PKMXml
8
+ def self.convert_xpath_l3_id(node, x_path, parent_path)
9
+ map = node.at_xpath x_path
10
+ id = map.content.to_i
11
+ map.content = (id + 0x8000).to_s
12
+ puts "#{(parent_path + x_path).gsub('xmlns:', '')} = #{id} => #{map.text}"
13
+ end
14
+
15
+ def self.check_xsd(xsd, xml)
16
+ puts 'Check XML-Schema...'
17
+ xsd = Nokogiri::XML::Schema(xsd)
18
+ error = xsd.validate(xml)
19
+ if error.empty?
20
+ puts 'XML-Schema is valid.'
21
+ true
22
+ else
23
+ puts 'XML-Schema is invalid.'
24
+ error.each do |e|
25
+ puts e.message
26
+ end
27
+ false
28
+ end
29
+ end
30
+
31
+ def self.xml_is_valid_pkm(xml)
32
+ # xsd = File.read("./ka/pkm/1/XML-Schema_PKM.xsd")
33
+ path = ''
34
+ case xml.root.name
35
+ when 'pv-km', 'dl-km'
36
+ path = './ka/pkm/1/XML-Schema_PKM.xsd'
37
+ when 'rntm'
38
+ path = './ka/pkm/2/XML-Schema_PKM.xsd'
39
+ end
40
+
41
+ file_path = File.join(File.dirname(__FILE__), path)
42
+
43
+ xsd = File.read(file_path)
44
+ PKMXml.check_xsd(xsd, xml)
45
+ end
46
+
47
+ def initialize(file_name)
48
+ @filename = file_name
49
+ is_a_file = File.file?(@filename)
50
+ puts "File ('#{@filename}') not found." unless is_a_file
51
+ return unless is_a_file
52
+
53
+ @xml_doc = File.open(@filename) { |f| Nokogiri::XML(f) }
54
+ # Is it a valid PKM file?
55
+ xml_is_invalid_pkm = (@xml_doc.nil? || !valid_xml?)
56
+ puts 'File is not a valid PKM file' if xml_is_invalid_pkm
57
+ return if xml_is_invalid_pkm
58
+
59
+ @pkm_data = PKM.new(@xml_doc)
60
+ end
61
+
62
+ attr_reader :pkm_data
63
+
64
+ def xml_key_items
65
+ # exclude tarifmodul-pool
66
+ # exclude kontrollmodul-pool
67
+ x = '//*[not(ancestor-or-self::xmlns:kontrollmodul-pool) and not(ancestor-or-self::xmlns:tarifmodul-pool) and @key]'
68
+ @xml_doc.xpath(x)
69
+ end
70
+
71
+ def convert_file_name
72
+ pn = Pathname.new(@filename)
73
+ dir, base = File.split(pn)
74
+ f = base.split('_')
75
+ org_id_l3 = f[1].to_i
76
+ org_id_l2 = org_id_l3 + 0x8000
77
+ f[1] = org_id_l2.to_s
78
+ Pathname.new(dir).join(f.join('_'))
79
+ end
80
+
81
+ def save_as_level2
82
+ convert_org_ids_to_level2
83
+ convert_ids_cr374
84
+ save_file
85
+ end
86
+
87
+ def valid_xml?
88
+ PKMXml.xml_is_valid_pkm(@xml_doc)
89
+ end
90
+
91
+ def save_file
92
+ return unless valid_xml?
93
+
94
+ # SAVE FILE with NEW name
95
+ new_file_name = convert_file_name
96
+ output = File.open(new_file_name, 'w')
97
+ output << @xml_doc.to_xml(indent_text: '', indent: 0).gsub(">\n", '>')
98
+ output.close
99
+ puts "File saved as: #{new_file_name}"
100
+ end
101
+
102
+ def type
103
+ case @xml_doc.root.name
104
+ when 'pv-km'
105
+ :pv_km
106
+ when 'dl-km'
107
+ :dl_km
108
+ when 'rntm'
109
+ :rn_tm
110
+ else
111
+ puts("#{@xml_doc.root.name} wird nicht unterstützt.")
112
+ nil
113
+ end
114
+ end
115
+
116
+ def self.pathes
117
+ {
118
+ dl_km: {
119
+ herausgeber_xpath: '/xmlns:dl-km/xmlns:organisation/xmlns:id',
120
+ enthaltene_pvmodule_xpath: '/xmlns:dl-km/xmlns:kontrollmodul-pool/xmlns:item',
121
+ herausgeber_enthaltene_pvmodule_xpath: 'xmlns:moduldaten/xmlns:organisation/xmlns:id',
122
+ zulaessige_organisationen_xpath: 'xmlns:moduldaten/xmlns:organisation-pool/xmlns:item',
123
+ zulaessige_organisationen_org_id_xpath: 'xmlns:id'
124
+ },
125
+ pv_km: {
126
+ herausgeber_xpath: '/xmlns:pv-km/xmlns:organisation/xmlns:id',
127
+ enthaltene_pvmodule_xpath: '.',
128
+ herausgeber_enthaltene_pvmodule_xpath: nil,
129
+ zulaessige_organisationen_xpath: 'xmlns:pv-km/xmlns:organisation-pool/xmlns:item',
130
+ zulaessige_organisationen_org_id_xpath: 'xmlns:id'
131
+ },
132
+ rn_tm: {
133
+ herausgeber_xpath: '/xmlns:rntm/xmlns:herausgeber/xmlns:nr',
134
+ enthaltene_pvmodule_xpath: '/xmlns:rntm/xmlns:tarifmodul-pool/xmlns:item',
135
+ herausgeber_enthaltene_pvmodule_xpath: 'xmlns:tarifmodul/xmlns:herausgeber/xmlns:nr',
136
+ zulaessige_organisationen_xpath: 'xmlns:tarifmodul/xmlns:organisation-pool/xmlns:item',
137
+ zulaessige_organisationen_org_id_xpath: 'xmlns:nr'
138
+ }
139
+ }
140
+ end
141
+
142
+ def path_to(symbol)
143
+ PKMXml.pathes.dig type, symbol
144
+ end
145
+
146
+ def convert_orgid_at_path(node, symbol, parent_path)
147
+ path = path_to(symbol)
148
+ PKMXml.convert_xpath_l3_id(node, path, parent_path) if path
149
+ end
150
+
151
+ def iterate_on_xpath(node, symbol, &block)
152
+ iterator_path = path_to(symbol)
153
+ node.xpath(iterator_path).each { |element| block.call(element, iterator_path) }
154
+ end
155
+
156
+ def convert_org_ids_to_level2
157
+ convert_orgid_at_path(@xml_doc, :herausgeber_xpath, @xml_doc.path)
158
+ iterate_on_xpath(@xml_doc, :enthaltene_pvmodule_xpath) do |pvmodul, iterator_path|
159
+ convert_orgid_at_path(pvmodul, :herausgeber_enthaltene_pvmodule_xpath, iterator_path)
160
+ iterate_on_xpath(pvmodul, :zulaessige_organisationen_xpath) do |organisation, second_iterator_path|
161
+ concated_iterator_path = iterator_path + second_iterator_path
162
+ convert_orgid_at_path(organisation, :zulaessige_organisationen_org_id_xpath, concated_iterator_path)
163
+ end
164
+ end
165
+ end
166
+
167
+ def convert_ids_cr374
168
+ ## Ist die Ausgangsschnittstelle 3 bzw. 4 im Kontrollmodul vorhanden?
169
+ return unless @pkm_data.cr374?
170
+
171
+ ausgangskontexte = pkm_data.ermittle_alle_cr374_ausgangskontexte
172
+ puts "#{ausgangskontexte.length} Ausgangskontext(e) zur Anpassung gefunden"
173
+ # puts ausgangskontexte
174
+ ausgangskontexte.each do |ausgangskontext|
175
+ puts "Ausgangskontext \"#{ausgangskontext.name}\" - Key: #{ausgangskontext.key}"
176
+
177
+ convert_keyref_l3_id(ausgangskontext.key)
178
+ end
179
+ end
180
+
181
+ def convert_keyref_l3_id(keyref)
182
+ node = @xml_doc.at_xpath("//xmlns:text[@ref='#{keyref}']")
183
+ id = node.content.to_i
184
+ node.content = (id + 0x8000).to_s
185
+ puts "Org-ID angepasst: #{id} --> #{node.content}"
186
+ end
187
+
188
+ # def get_node_by_key(key)
189
+ # # Schließt aktuell noch die eingebetteten Module aus
190
+ # # Funktion unterstellt, dass die @keys in aufsteigender Reihenfolge im xml vorkommen. Ist das immer so?!
191
+
192
+ # previous_node = xml_key_items.first
193
+ # xml_key_items.each do |node|
194
+ # key_of_node = node.attribute('key').value.to_i
195
+ # break if key_of_node > key
196
+
197
+ # previous_node = node
198
+ # end
199
+ # child_position = 1 + key - previous_node.attribute('key').value.to_i
200
+ # previous_node.xpath("xmlns:item[#{child_position}]")
201
+ # end
202
+ end