UNPKG

@hpcc-js/comms

Version:
253 lines 8.22 kB
import { __extends } from "tslib"; import { SAXStackParser, Stack } from "@hpcc-js/util"; var XSDNode = /** @class */ (function () { function XSDNode(e) { this.e = e; } XSDNode.prototype.fix = function () { delete this.e; }; return XSDNode; }()); export { XSDNode }; var XSDXMLNode = /** @class */ (function (_super) { __extends(XSDXMLNode, _super); function XSDXMLNode(e) { var _this = _super.call(this, e) || this; _this.isSet = false; _this.attrs = {}; _this._children = []; return _this; } XSDXMLNode.prototype.append = function (child) { this._children.push(child); if (!this.type) { this.type = "hpcc:childDataset"; } }; XSDXMLNode.prototype.fix = function () { var _a; this.name = this.e.$["name"]; this.type = this.e.$["type"]; for (var i = this._children.length - 1; i >= 0; --i) { var row = this._children[i]; if (row.name === "Row" && row.type === undefined) { (_a = this._children).push.apply(_a, row._children); this._children.splice(i, 1); } } var setOfType = this.setOfType(); if (setOfType) { this.type = setOfType; this.isSet = true; this._children = []; } }; XSDXMLNode.prototype.children = function () { return this._children; }; XSDXMLNode.prototype.isAll = function (node) { return node.name === "All" && node.type === undefined; }; XSDXMLNode.prototype.setOfType = function () { var children = this.children(); if (this.type === undefined && children.length === 2) { if (this.isAll(children[0])) { return children[1].type; } else if (this.isAll(children[1])) { return children[0].type; } } return undefined; }; XSDXMLNode.prototype.charWidth = function () { var retVal = -1; switch (this.type) { case "xs:boolean": retVal = 5; break; case "xs:integer": retVal = 8; break; case "xs:nonNegativeInteger": retVal = 8; break; case "xs:double": retVal = 8; break; case "xs:string": retVal = 32; break; default: var numStr = "0123456789"; var underbarPos = this.type.lastIndexOf("_"); var length_1 = underbarPos > 0 ? underbarPos : this.type.length; var i = length_1 - 1; for (; i >= 0; --i) { if (numStr.indexOf(this.type.charAt(i)) === -1) break; } if (i + 1 < length_1) { retVal = parseInt(this.type.substring(i + 1, length_1), 10); } if (this.type.indexOf("data") === 0) { retVal *= 2; } break; } if (retVal < this.name.length) retVal = this.name.length; return retVal; }; return XSDXMLNode; }(XSDNode)); export { XSDXMLNode }; var XSDSimpleType = /** @class */ (function (_super) { __extends(XSDSimpleType, _super); function XSDSimpleType(e) { return _super.call(this, e) || this; } XSDSimpleType.prototype.append = function (e) { switch (e.name) { case "xs:restriction": this._restricition = e; break; case "xs:maxLength": this._maxLength = e; break; default: } }; XSDSimpleType.prototype.fix = function () { this.name = this.e.$["name"]; this.type = this._restricition.$["base"]; this.maxLength = this._maxLength ? +this._maxLength.$["value"] : undefined; delete this._restricition; delete this._maxLength; _super.prototype.fix.call(this); }; return XSDSimpleType; }(XSDNode)); export { XSDSimpleType }; var XSDSchema = /** @class */ (function () { function XSDSchema() { this.simpleTypes = {}; } XSDSchema.prototype.fields = function () { return this.root.children(); }; return XSDSchema; }()); export { XSDSchema }; var XSDParser = /** @class */ (function (_super) { __extends(XSDParser, _super); function XSDParser() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.schema = new XSDSchema(); _this.simpleTypes = {}; _this.xsdStack = new Stack(); return _this; } XSDParser.prototype.startXMLNode = function (e) { _super.prototype.startXMLNode.call(this, e); switch (e.name) { case "xs:element": var xsdXMLNode = new XSDXMLNode(e); if (!this.schema.root) { this.schema.root = xsdXMLNode; } else if (this.xsdStack.depth()) { this.xsdStack.top().append(xsdXMLNode); } this.xsdStack.push(xsdXMLNode); break; case "xs:simpleType": this.simpleType = new XSDSimpleType(e); break; default: break; } }; XSDParser.prototype.endXMLNode = function (e) { switch (e.name) { case "xs:element": var xsdXMLNode = this.xsdStack.pop(); xsdXMLNode.fix(); break; case "xs:simpleType": this.simpleType.fix(); this.simpleTypes[this.simpleType.name] = this.simpleType; delete this.simpleType; break; case "xs:appinfo": var xsdXMLNode2 = this.xsdStack.top(); for (var key in e.$) { xsdXMLNode2.attrs[key] = e.$[key]; } break; default: if (this.simpleType) { this.simpleType.append(e); } } _super.prototype.endXMLNode.call(this, e); }; return XSDParser; }(SAXStackParser)); export function parseXSD(xml) { var saxParser = new XSDParser(); saxParser.parse(xml); return saxParser.schema; } var XSDParser2 = /** @class */ (function (_super) { __extends(XSDParser2, _super); function XSDParser2(rootName) { var _this = _super.call(this) || this; _this.schema = new XSDSchema(); _this.simpleTypes = {}; _this.xsdStack = new Stack(); _this._rootName = rootName; return _this; } XSDParser2.prototype.startXMLNode = function (e) { _super.prototype.startXMLNode.call(this, e); switch (e.name) { case "xsd:element": var xsdXMLNode = new XSDXMLNode(e); if (!this.schema.root && this._rootName === e.$.name) { this.schema.root = xsdXMLNode; } if (this.xsdStack.depth()) { this.xsdStack.top().append(xsdXMLNode); } this.xsdStack.push(xsdXMLNode); break; case "xsd:simpleType": this.simpleType = new XSDSimpleType(e); break; default: break; } }; XSDParser2.prototype.endXMLNode = function (e) { switch (e.name) { case "xsd:element": var xsdXMLNode = this.xsdStack.pop(); xsdXMLNode.fix(); break; case "xsd:simpleType": break; default: break; } _super.prototype.endXMLNode.call(this, e); }; return XSDParser2; }(XSDParser)); export function parseXSD2(xml, rootName) { var saxParser = new XSDParser2(rootName); saxParser.parse(xml); return saxParser.schema; } //# sourceMappingURL=xsdParser.js.map