@documentica/webdav
Version:
Documentica Webdav Client
163 lines (162 loc) • 7.66 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 AclCodec_1 = __importDefault(require("./codecs/AclCodec"));
const CheckOutCodec_1 = __importDefault(require("./codecs/CheckOutCodec"));
const CreationDateCodec_1 = __importDefault(require("./codecs/CreationDateCodec"));
const GetLastModifiedCodec_1 = __importDefault(require("./codecs/GetLastModifiedCodec"));
const IntCodec_1 = __importDefault(require("./codecs/IntCodec"));
const LockDiscoveryCodec_1 = __importDefault(require("./codecs/LockDiscoveryCodec"));
const OwnerCodec_1 = __importDefault(require("./codecs/OwnerCodec"));
const PrincipalCollectionCodec_1 = __importDefault(require("./codecs/PrincipalCollectionCodec"));
const ResourceTypeCodec_1 = __importDefault(require("./codecs/ResourceTypeCodec"));
const SupportedPrivilegeSetCodec_1 = __importDefault(require("./codecs/SupportedPrivilegeSetCodec"));
const CurrentUserPrivilegeCodec_1 = __importDefault(require("./codecs/CurrentUserPrivilegeCodec"));
exports.codecs = {};
exports.registerCodec = ({ tagname, namespace, codec }) => {
if (typeof namespace !== 'string') {
throw new Exception_1.default('addCodec: namespace must be a String', Exception_1.default.WRONG_TYPE);
}
if (typeof tagname !== 'string') {
throw new Exception_1.default('addCodec: tagname must be a String', Exception_1.default.WRONG_TYPE);
}
if (exports.codecs[namespace] === undefined) {
exports.codecs[namespace] = {};
}
exports.codecs[namespace][tagname] = codec;
};
exports.registerCodec({ namespace: 'DAV:', tagname: 'acl', codec: AclCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'checked-out', codec: CheckOutCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'creationdate', codec: CreationDateCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'getlastmodified', codec: GetLastModifiedCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'getcontentlength', codec: IntCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'lockdiscovery', codec: LockDiscoveryCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'owner', codec: OwnerCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'principal-collection-set', codec: PrincipalCollectionCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'resourcetype', codec: ResourceTypeCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'supported-privilege-set', codec: SupportedPrivilegeSetCodec_1.default });
exports.registerCodec({ namespace: 'DAV:', tagname: 'current-user-privilege-set', codec: CurrentUserPrivilegeCodec_1.default });
class Property {
constructor(xmlNode, status, responsedescription, errors) {
this._xmlvalue = undefined;
this._errors = [];
this.namespace = '';
this.tagname = '';
this.status = null;
this.responsedescription = undefined;
if ((typeof xmlNode !== 'undefined') && (xmlNode.nodeType === 1)) {
this.namespace = xmlNode.namespaceURI;
this.tagname = xmlNode.localName;
this.xmlvalue = xmlNode.childNodes;
}
if (status !== undefined) {
this.status = status;
}
if (responsedescription !== undefined) {
this.responsedescription = responsedescription;
}
if (errors instanceof Array) {
for (var i = 0; i < errors.length; i++) {
this.addError(errors[i]);
}
}
}
;
set xmlvalue(value) {
if (value === null) {
this._xmlvalue = undefined;
return;
}
if (!(value instanceof NodeList)) {
throw new Exception_1.default('xmlvalue must be an instance of NodeList', Exception_1.default.WRONG_TYPE);
}
this._xmlvalue = value;
}
;
get xmlvalue() {
return this._xmlvalue;
}
;
setValueAndRebuildXml(value) {
// Call codec to automatically create correct 'xmlvalue'
var xmlDoc = document.implementation.createDocument(this.namespace, this.tagname, null);
const tagname = this.tagname.indexOf(".") !== -1 ? this.tagname.split(".")[0] : this.tagname;
if ((exports.codecs[this.namespace] === undefined) ||
(exports.codecs[this.namespace][tagname] === undefined) ||
(exports.codecs[this.namespace][tagname]['toXML'] === undefined)) {
// No 'toXML' function set, so create a NodeList with just one CDATA node
if (value !== null) { // If the value is NULL, then we should add anything to the NodeList
var cdata = xmlDoc.createCDATASection(value);
xmlDoc.documentElement.appendChild(cdata);
}
this._xmlvalue = xmlDoc.documentElement.childNodes;
}
else {
const codec = exports.codecs[this.namespace][tagname];
if (codec.toXML)
this._xmlvalue = codec.toXML(value, xmlDoc).documentElement.childNodes;
}
}
;
getParsedValue() {
// Call codec to automatically create correct 'value'
if (!this._xmlvalue)
return undefined;
if (this._xmlvalue.length > 0) {
const tagname = this.tagname.indexOf(".") !== -1 ? this.tagname.split(".")[0] : this.tagname;
if ((exports.codecs[this.namespace] === undefined) ||
(exports.codecs[this.namespace][tagname] === undefined) ||
(exports.codecs[this.namespace][tagname]['fromXML'] === undefined)) {
// No 'fromXML' function set, so try to create a text value based on TextNodes and CDATA nodes. If other nodes are present, set 'value' to null
let parsedValue = '';
for (var i = 0; i < this._xmlvalue.length; i++) {
var node = this._xmlvalue.item(i);
if ((node.nodeType === 3) || (node.nodeType === 4)) { // Make sure text and CDATA content is stored
parsedValue += node.nodeValue;
}
else { // If even one of the nodes is not text or CDATA, then we don't parse a text value at all
parsedValue = undefined;
break;
}
}
return parsedValue;
}
else {
const res = exports.codecs[this.namespace][tagname]['fromXML'](this._xmlvalue);
return res.getParsedValue ? res.getParsedValue() : res;
}
}
return null;
}
;
addError(error) {
if (!(error instanceof Node)) {
throw new Exception_1.default('Error must be an instance of Node', Exception_1.default.WRONG_TYPE);
}
this._errors.push(error);
return this;
}
;
getErrors() {
return this._errors;
}
;
toString() {
const value = this.getParsedValue();
return typeof (value) == "object" ? JSON.stringify(value) : value;
}
;
static NewProperty(namespace, tagname, value) {
const p = new Property();
p.namespace = namespace;
p.tagname = tagname;
if (value !== undefined)
p.setValueAndRebuildXml(value);
return p;
}
}
exports.default = Property;
;