-
Notifications
You must be signed in to change notification settings - Fork 9
/
messageStructureParser.ts
423 lines (378 loc) · 18.3 KB
/
messageStructureParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
387
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
418
419
420
421
422
423
/**
* @author Roman Vottner
* @copyright 2020 Roman Vottner
* @license Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Dictionary, SegmentEntry, ElementEntry } from "../validator";
import { MessageType } from "../tracker";
import { HttpClient } from "../httpClient";
import { Parser, DomHandler } from "htmlparser2";
import { isDefined } from "../util";
export interface EdifactMessageSpecification {
readonly messageType: string;
readonly version: string;
readonly release: string;
readonly controllingAgency: string;
/**
* Contains the available segments as key and the respective elements
* a segment contains as well as the mandatory count as value
*/
readonly segmentTable: Dictionary<SegmentEntry>;
/**
* Contains the respective element ID as key and the type definition of
* the respective components as value
*/
readonly elementTable: Dictionary<ElementEntry>;
/**
* Contains the actual message structure generatedby this parser
*/
readonly messageStructureDefinition: MessageType[];
type(): string;
versionAbbr(): string;
}
export class EdifactMessageSpecificationImpl implements EdifactMessageSpecification {
messageType: string;
version: string;
release: string;
controllingAgency: string;
segmentTable: Dictionary<SegmentEntry> = new Dictionary<SegmentEntry>();
elementTable: Dictionary<ElementEntry> = new Dictionary<ElementEntry>();
messageStructureDefinition: MessageType[] = [];
constructor(messageType: string, version: string, release: string, controllingAgency: string) {
this.messageType = messageType;
this.version = version;
this.release = release;
this.controllingAgency = controllingAgency;
}
public type(): string {
return this.version + this.release + "_" + this.messageType;
}
public versionAbbr(): string {
return this.version + this.release;
}
}
enum Part {
BeforeStructureDef,
RefLink,
Pos,
Tag,
Deprecated,
Name,
AfterStructureDef
}
enum SegmentPart {
BeforeStructureDef,
Data,
AfterStructureDef
}
export type ParsingResultType = {
specObj: EdifactMessageSpecification;
promises: Promise<EdifactMessageSpecification>[];
};
export interface MessageStructureParser {
loadTypeSpec(): Promise<EdifactMessageSpecification>;
}
export class UNECEMessageStructureParser implements MessageStructureParser {
readonly version: string;
readonly type: string;
readonly httpClient: HttpClient;
constructor(version: string, type: string) {
this.version = version.toLowerCase();
this.type = type.toLowerCase();
const baseUrl: string = "https://service.unece.org/trade/untdid/" + this.version + "/trmd/" + this.type + "_c.htm";
this.httpClient = new HttpClient(baseUrl);
}
private extractTextValue(text: string, regex: RegExp, index: number = 0): string {
const arr: RegExpExecArray | null = regex.exec(text);
if (isDefined(arr)) {
return arr[index];
}
return "";
}
protected async loadPage(page: string): Promise<string> {
const data: string = await this.httpClient.get(page);
return data;
}
protected async parseSegmentDefinitionPage(segment: string, page: string, definition: EdifactMessageSpecification): Promise<EdifactMessageSpecification> {
if (definition.segmentTable.contains(segment)) {
return Promise.resolve(definition);
}
const segEntry: SegmentEntry = { "requires": 0, "elements": [] };
let state: SegmentPart = SegmentPart.BeforeStructureDef;
// only relevant for legacy UNECE segment specification pages:
let dataSection: boolean = false;
let skipAddingElement: boolean = false;
let overflowLine: string | null = null;
let complexEleId: string | null = null;
let complexEleEntry: ElementEntry | null = null;
for (let line of page.split("\n")) {
line = line.trimRight();
if (overflowLine !== null) {
line = overflowLine.trimRight() + " " + line.trim();
overflowLine = null;
}
if (state === SegmentPart.BeforeStructureDef && line.includes('<HR>')) {
dataSection = true;
} else if (
state === SegmentPart.BeforeStructureDef &&
// checking dataSection and <B> tag only relevant for legacy
// UNECE segment specification pages:
(line.includes("<H3>") || (dataSection && line.includes('<B>')))
) {
state = SegmentPart.Data;
} else if (state === SegmentPart.Data && !line.includes("<P>")) {
const regexp: RegExp = /^([\d]*)\s*?([X|\\*]?)\s*<A.*>([a-zA-Z0-9]*)<\/A>([a-zA-Z0-9 ,\-\\/&\\()]{44,})([M|C])\s*([\d]*)\s*([a-zA-Z0-9\\.]*).*$/g;
const arr: RegExpExecArray | null = regexp.exec(line);
if (isDefined(arr)) {
const segGroupId: string | undefined = arr[1] === "" ? undefined : arr[1];
// const deprecated: boolean = arr[2] === "X" ? true : false;
const id: string = arr[3];
// const name: string = arr[4].trim();
const mandatory: boolean = arr[5] === "M" ? true : false;
// const repetition: number | undefined = isDefined(arr[6]) ? parseInt(arr[6]) : undefined;
const elementDef: string | undefined = arr[7] === "" ? undefined : arr[7];
if (segGroupId) {
if (id === "") {
console.warn(`Could not determine element ID based on line ${line}`);
continue;
}
segEntry.elements.push(id);
skipAddingElement = false;
if (mandatory) {
segEntry.requires = segEntry.requires + 1;
}
if (elementDef) {
if (complexEleEntry !== null && complexEleId !== null) {
definition.elementTable.add(complexEleId, complexEleEntry);
}
complexEleId = null;
complexEleEntry = null;
if (definition.elementTable.contains(id)) {
continue;
}
const eleEntry: ElementEntry = { "requires": 0, "components": [] };
if (mandatory) {
eleEntry.requires = eleEntry.requires + 1;
}
eleEntry.components.push(elementDef);
definition.elementTable.add(id, eleEntry);
} else {
if (complexEleEntry !== null && complexEleId !== null) {
definition.elementTable.add(complexEleId, complexEleEntry);
}
if (definition.elementTable.contains(id)) {
skipAddingElement = true;
continue;
}
complexEleId = id;
complexEleEntry = { "requires": 0, "components": [] };
}
} else {
if (!skipAddingElement) {
if (complexEleEntry !== null && elementDef) {
complexEleEntry.components.push(elementDef);
complexEleEntry.requires = mandatory ? complexEleEntry.requires + 1 : complexEleEntry.requires;
} else {
// simple element definition
if (definition.elementTable.contains(id)) {
continue;
}
const eleEntry: ElementEntry = { "requires": 0, "components": [] };
if (mandatory) {
eleEntry.requires = eleEntry.requires + 1;
}
if (elementDef) {
eleEntry.components.push(elementDef);
}
definition.elementTable.add(id, eleEntry);
}
}
}
} else {
const regexpAlt: RegExp = /^([\d]*)\s*([X|\\*]?)\s*<A.*>([a-zA-Z0-9]*)<\/A>\s*([a-zA-Z0-9 \\-\\/&]*)/g;
const arrAlt: RegExpExecArray | null = regexpAlt.exec(line);
if (isDefined(arrAlt)) {
overflowLine = line;
}
}
} else if (state === SegmentPart.Data && line.includes("<P>")) {
state = SegmentPart.AfterStructureDef;
break;
}
}
if (complexEleEntry !== null && complexEleId !== null) {
definition.elementTable.add(complexEleId, complexEleEntry);
}
if (segment !== "") {
definition.segmentTable.add(segment, segEntry);
}
return Promise.resolve(definition);
}
private async parsePage(page: string): Promise<ParsingResultType> {
let definition: EdifactMessageSpecification | undefined;
const handler: DomHandler = new DomHandler();
let state: Part = Part.BeforeStructureDef;
let section: string | null = "header";
const segStack: MessageType[][] = [];
const lookupSegmentPromises: Promise<EdifactMessageSpecification>[] = [];
const nextState = () => {
if (state === Part.RefLink) {
state = Part.Pos;
} else if (state === Part.Pos) {
state = Part.Deprecated;
} else if (state === Part.Deprecated) {
state = Part.Tag;
} else if (state === Part.Tag) {
state = Part.Name;
} else if (state === Part.Name) {
state = Part.RefLink;
}
};
handler.ontext = (text: string) => {
if (text.includes("Message Type") && text.includes("Version") && text.includes("Release")) {
const messageType: string = this.extractTextValue(text, /Message Type\s*: ([A-Z]*)\s/g, 1);
const version: string = this.extractTextValue(text, /Version\s*: ([A-Z]*)\s/g, 1);
const release: string = this.extractTextValue(text, /Release\s*: ([0-9A-Z]*)\s/g, 1);
const controllingAgency: string = this.extractTextValue(text, /Contr. Agency\s*: ([0-9A-Z]*)\s/g, 1);
definition = new EdifactMessageSpecificationImpl(messageType, version, release, controllingAgency);
segStack.push(definition.messageStructureDefinition);
} else if (text.includes("Message structure")) {
state = Part.RefLink;
} else if (state !== Part.BeforeStructureDef && state !== Part.AfterStructureDef) {
if (state === Part.RefLink) {
// ignored
// console.debug(`RefLink: ${text}`);
} else if (state === Part.Pos) {
// console.debug(`Pos: ${text}`);
} else if (state === Part.Deprecated) {
if (text.includes("- Segment group")) {
const regex: RegExp = /^[\s*+-]*-* (Segment group \d*)\s*-*\s*([M|C])\s*(\d*)([-|\\+|\\|]*).*/g;
const arr: RegExpExecArray | null = regex.exec(text);
if (isDefined(arr)) {
const groupArray: MessageType[] = [];
const group: MessageType = {
content: groupArray,
mandatory: arr[2] === "M" ? true : false,
repetition: parseInt(arr[3]),
name: arr[1],
section: isDefined(section) ? section : undefined
};
section = null;
// add the group to the end of the current top segments
segStack[segStack.length - 1].push(group);
// push the array managed by this group to the end of the stack to fill it down the road
segStack.push(groupArray);
}
// no further tags available, continue on the next line with the RefLink
state = Part.RefLink;
} else {
// console.debug(`Deprecated: ${text}`);
nextState();
}
} else if (state === Part.Tag) {
// console.debug(`Tag: ${text}`);
const _section: string | undefined = section !== null ? section : undefined;
let _data: string[] | undefined;
if (definition) {
_data = text === "UNH" ? [ definition.versionAbbr(), definition.messageType ] : undefined;
}
const segment: MessageType = {
content: text,
mandatory: false,
repetition: 0,
data: _data,
section: _section
};
if (definition) {
segStack[segStack.length - 1].push(segment);
}
section = null;
} else if (state === Part.Name) {
// console.debug(`Name: ${text}`);
const regex: RegExp = /^([a-zA-Z /\\-]*)\s*?([M|C])\s*?([0-9]*?)([^0-9]*)$/g;
const arr: RegExpExecArray | null = regex.exec(text);
if (isDefined(arr)) {
// const name: string = arr[1].trim();
const sMandatory: string = arr[2];
const sRepetition: string = arr[3];
const remainder: string = arr[4];
// console.debug(`Processing segment: ${name}`);
// update the last element on the top-most stack with the respective data
const segArr: MessageType[] = segStack[segStack.length - 1];
const segData: MessageType = segArr[segArr.length - 1];
segData.mandatory = sMandatory === "M" ? true : false;
segData.repetition = parseInt(sRepetition);
// check whether the remainder contains a closing hint for a subgroup: -...-++
if (remainder.includes("-") && remainder.includes("+")) {
for (let i: number = 0; i < remainder.split("+").length - 1; i++) {
segStack.pop();
}
}
nextState();
}
if (text.includes("DETAIL SECTION")) {
section = "detail";
} else if (text.includes("SUMMARY SECTION")) {
section = "summary";
}
} else {
console.warn(`Unknown part: ${text}`);
}
}
};
handler.onopentag = (name: string, attribs: { [key: string]: string }) => {
if (name === "p" && state !== Part.BeforeStructureDef && state !== Part.AfterStructureDef) {
state = Part.AfterStructureDef;
}
if (state === Part.Tag && attribs.href !== undefined) {
if (definition) {
const end: number = attribs.href.indexOf(".htm");
const curSeg: string = attribs.href.substring(end - 3, end).toUpperCase();
// skip segments that do not point to the right segment definition page
if (curSeg !== "UNH" && curSeg !== "UNS" && curSeg !== "UNT") {
// console.debug(`Adding promise to lookup segment definition for segment ${curSeg} for URI ${attribs.href}`);
const def: EdifactMessageSpecification = definition;
lookupSegmentPromises.push(this.loadPage(attribs.href)
.then(result => this.parseSegmentDefinitionPage(curSeg, result, def))
);
}
}
}
};
handler.onclosetag = () => {
nextState();
};
const parser: Parser = new Parser(handler);
parser.write(page);
parser.end();
if (definition) {
return Promise.resolve({ specObj: definition, promises: lookupSegmentPromises });
}
return Promise.reject(new Error("Could not extract values from read page successfully"));
}
loadTypeSpec(): Promise<EdifactMessageSpecification> {
const url: string = "./" + this.type + "_c.htm";
return this.loadPage(url)
.then((page: string) => this.parsePage(page))
.then((result: ParsingResultType) =>
Promise.all(result.promises)
.then(() => result.specObj)
.catch((error: Error) => {
console.warn(`Error while processing segment definition promises: Reason ${error.message}`);
return result.specObj;
})
);
}
}