UNPKG

@documentica/webdav

Version:
72 lines (71 loc) 3.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Exception_1 = __importDefault(require("./Exception")); const Response_1 = __importDefault(require("./Response")); class Multistatus { constructor(xmlNode) { this._responses = {}; this.responsedescription = null; this.value = null; this.getResponse = (name) => this._responses[name]; this.getResponseNames = () => Object.keys(this._responses); // Constructor logic if (typeof xmlNode !== 'undefined') { if ((xmlNode.namespaceURI !== 'DAV:') || (xmlNode.localName !== 'multistatus')) { throw new Exception_1.default('Node is not of type DAV:multistatus', Exception_1.default.WRONG_XML); } var data = xmlNode.childNodes; for (var i = 0; i < data.length; i++) { var child = data.item(i); if ((child.namespaceURI === null) || (child.namespaceURI !== 'DAV:')) { // Skip if not from the right namespace continue; } switch (child.localName) { case 'responsedescription': // responsedescription is always CDATA, so just take the text this.responsedescription = child.childNodes.item(0).nodeValue; break; case 'response': // response node should be parsed further var response = new Response_1.default(child); var responseChilds = child.childNodes; var hrefs = []; for (var j = 0; j < responseChilds.length; j++) { var responseChild = responseChilds.item(j); if ((responseChild.localName === 'href') && (responseChild.namespaceURI !== null) && (responseChild.namespaceURI === 'DAV:')) { // For each HREF element we create a separate response object hrefs.push(responseChild.childNodes.item(0).nodeValue); } } if (hrefs.length > 1) { // Multiple HREFs = start copying the response (this makes sure it is not parsed over and over again). No deep copying needed; there can't be a propstat for (var k = 0; k < hrefs.length; k++) { var copyResponse = new Response_1.default(); copyResponse.href = hrefs[k]; copyResponse.status = response.status; copyResponse.error = response.error; copyResponse.responsedescription = response.responsedescription; copyResponse.location = response.location; this.addResponse(copyResponse); } } else { this.addResponse(response); } break; } } } } ; addResponse(response) { // x.y.z if (!(response instanceof Response_1.default)) { throw new Exception_1.default('Response should be instance of Response', Exception_1.default.WRONG_TYPE); } var name = response.href; this._responses[name] = response; return this; } ; } exports.default = Multistatus;