@makakwastaken/ts-edifact
Version:
Edifact parser library
67 lines • 2.07 kB
JavaScript
;
/**
* @author Stefan Partheymüller
* @copyright 2021 Stefan Partheymüller
* @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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.UNECEPageParser = void 0;
const tsm_1 = require("@initics/tsm");
const htmlparser2_1 = require("htmlparser2");
const util_1 = require("../util");
const uneceDomHandler_1 = require("./uneceDomHandler");
class UNECEPageParser {
sm;
_spec;
constructor(smdef) {
this.sm = new tsm_1.StateMachine(smdef);
}
get spec() {
if (!this._spec) {
throw new Error('EdifactMessageSpecification not defined');
}
return this._spec;
}
parse(page) {
const parser = new htmlparser2_1.Parser(this.setupHandler());
parser.write(page);
parser.end();
}
setupHandler() {
return new (class extends uneceDomHandler_1.UNECEDomHandler {
onOpenTag() {
//
}
onText() {
//
}
})();
}
extractTextValue(text, regex, index = 0) {
const arr = regex.exec(text);
if ((0, util_1.isDefined)(arr)) {
return arr[index];
}
return '';
}
throwInvalidParserState(state) {
throw new Error(`Invalid parser state: ${state}`);
}
throwCouldNotParsePage() {
throw new Error('Could not parse page');
}
}
exports.UNECEPageParser = UNECEPageParser;
//# sourceMappingURL=unecePageParser.js.map