@documentica/webdav
Version:
Documentica Webdav Client
120 lines (119 loc) • 5.53 kB
JavaScript
"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 Property_1 = __importDefault(require("./Property"));
class Response {
constructor(xmlNode) {
this._namespaces = {};
this.href = null;
this.status = null;
this.error = null;
this.responsedescription = null;
this.location = null;
this.propstat = null;
// Constructor logic
if (typeof xmlNode !== 'undefined') {
if ((xmlNode.namespaceURI !== 'DAV:') || xmlNode.localName !== 'response') {
throw new Exception_1.default('Node is not of type DAV:response', 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 'href':
case 'status':
case 'error':
case 'responsedescription':
// always CDATA, so just take the text
this[child.localName] = child.childNodes.item(0).nodeValue;
break;
case 'location':
this.location = child.childNodes.item(0).childNodes.item(0).nodeValue;
break;
case 'propstat': // propstat node should be parsed further
var propstatChilds = child.childNodes;
// First find status, error, responsedescription and props (as Node objects, not yet as Property objects)
var status = null;
var errors = [];
var responsedescription = null;
var props = [];
for (var j = 0; j < propstatChilds.length; j++) { // Parse the child nodes of the propstat element
var propstatChild = propstatChilds.item(j);
if ((propstatChild.nodeType !== 1) || (propstatChild.namespaceURI !== 'DAV:')) {
continue;
}
switch (propstatChild.localName) {
case 'prop':
for (var k = 0; k < propstatChild.childNodes.length; k++) {
props.push(propstatChild.childNodes.item(k));
}
break;
case 'error':
for (k = 0; k < propstatChild.childNodes.length; k++) {
errors.push(propstatChild.childNodes.item(k));
}
break;
case 'status': // always CDATA, so just take the text
let status2 = propstatChild.childNodes.item(0).nodeValue;
status = parseInt(status2.substr(status2.indexOf(' ') + 1, 3));
break;
case 'responsedescription': // always CDATA, so just take the text
responsedescription = propstatChild.childNodes.item(0).nodeValue;
break;
}
}
// Then create and add a new property for each element found in DAV:prop
for (j = 0; j < props.length; j++) {
if (props[j].nodeType === 1) {
var property = new Property_1.default(props[j], status, responsedescription || undefined, errors);
this.addProperty(property);
}
}
break;
}
}
}
}
;
addProperty(property) {
if (!(property instanceof Property_1.default)) {
throw new Exception_1.default('Response property should be instance of Property', Exception_1.default.WRONG_TYPE);
}
var { namespace } = property;
if (typeof namespace !== 'string') {
namespace = '';
}
if (this._namespaces[namespace] === undefined) {
this._namespaces[namespace] = {};
}
this._namespaces[namespace][property.tagname] = property;
return this;
}
;
getProperty(namespace, prop) {
if ((this._namespaces[namespace] === undefined) || (this._namespaces[namespace][prop] === undefined)) {
return undefined;
}
return this._namespaces[namespace][prop];
}
;
getNamespaceNames() {
return Object.keys(this._namespaces);
}
;
getPropertyNames(namespace) {
if (this._namespaces[namespace] === undefined) {
return new Array();
}
return Object.keys(this._namespaces[namespace]);
}
;
}
exports.default = Response;
;