1 /*
  2  * Copyright ©2012 SARA bv, The Netherlands
  3  *
  4  * This file is part of js-webdav-client.
  5  *
  6  * js-webdav-client is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published
  8  * by the Free Software Foundation, either version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * js-webdav-client is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Lesser General Public License
 17  * along with js-webdav-client.  If not, see <http://www.gnu.org/licenses/>.
 18  */
 19 "use strict";
 20 
 21 // If nl.sara.webdav.Multistatus is already defined, we have a namespace clash!
 22 if (nl.sara.webdav.Multistatus !== undefined) {
 23   throw new nl.sara.webdav.Exception('Namespace nl.sara.webdav.Multistatus already taken, could not load JavaScript library for WebDAV connectivity.', nl.sara.webdav.Exception.NAMESPACE_TAKEN);
 24 }
 25 
 26 /**
 27  * @class WebDAV Multistatus response body
 28  *
 29  * @param  {Node}  [xmlNode]  Optional; the xmlNode describing the multistatus object (should be compliant with RFC 4918)
 30  * @property  {String}  responsedescription  The response description
 31  */
 32 nl.sara.webdav.Multistatus = function(xmlNode) {
 33   // First define private attributes
 34   Object.defineProperty(this, '_responses', {
 35     'value': {},
 36     'enumerable': false,
 37     'configurable': false,
 38     'writable': true
 39   });
 40   // Second define public attributes
 41   Object.defineProperty(this, 'responsedescription', {
 42     'value': null,
 43     'enumerable': true,
 44     'configurable': false,
 45     'writable': true
 46   });
 47 
 48   // Constructor logic
 49   if (typeof xmlNode !== 'undefined') {
 50     if ((xmlNode.namespaceURI !== 'DAV:') || (nl.sara.webdav.Ie.getLocalName(xmlNode) !== 'multistatus')) {
 51       throw new nl.sara.webdav.Exception('Node is not of type DAV:multistatus', nl.sara.webdav.Exception.WRONG_XML);
 52     }
 53     var data = xmlNode.childNodes;
 54     for (var i = 0; i < data.length; i++) {
 55       var child = data.item(i);
 56       if ((child.namespaceURI === null) || (child.namespaceURI !== 'DAV:')) { // Skip if not from the right namespace
 57         continue;
 58       }
 59       switch (nl.sara.webdav.Ie.getLocalName(child)) {
 60         case 'responsedescription': // responsedescription is always CDATA, so just take the text
 61           this.responsedescription = child.childNodes.item(0).nodeValue;
 62           break;
 63         case 'response': // response node should be parsed further
 64           var response = new nl.sara.webdav.Response(child);
 65           var responseChilds = child.childNodes;
 66           var hrefs = [];
 67           for (var j = 0; j < responseChilds.length; j++) {
 68             var responseChild = responseChilds.item(j);
 69             if ((nl.sara.webdav.Ie.getLocalName(responseChild) === 'href') && (responseChild.namespaceURI !== null) && (responseChild.namespaceURI === 'DAV:')) { // For each HREF element we create a separate response object
 70               hrefs.push(responseChild.childNodes.item(0).nodeValue);
 71             }
 72           }
 73           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
 74             for (var k = 0; k < hrefs.length; k++) {
 75               var copyResponse = new nl.sara.webdav.Response();
 76               copyResponse.href = hrefs[k];
 77               copyResponse.status = response.status;
 78               copyResponse.error = response.error;
 79               copyResponse.responsedescription = response.responsedescription;
 80               copyResponse.location = response.location;
 81               this.addResponse(copyResponse);
 82             }
 83           }else{
 84             this.addResponse(response);
 85           }
 86         break;
 87       }
 88     }
 89   }
 90 };
 91 
 92 //########################## DEFINE PUBLIC METHODS #############################
 93 /**
 94  * Adds a Response
 95  *
 96  * @param    {nl.sara.webdav.Response}     response  The response
 97  * @returns  {nl.sara.webdav.Multistatus}            The multistatus itself for chaining methods
 98  */
 99 nl.sara.webdav.Multistatus.prototype.addResponse = function(response) {
100   if (!nl.sara.webdav.Ie.isIE && !(response instanceof nl.sara.webdav.Response)) {
101     throw new nl.sara.webdav.Exception('Response should be instance of Response', nl.sara.webdav.Exception.WRONG_TYPE);
102   }
103   var name = response.href;
104   this._responses[name] = response;
105   return this;
106 };
107 
108 /**
109  * Gets a Response
110  *
111  * @param    {String}    name           The name of the response to get
112  * @returns  {nl.sara.webdav.Response}  The value of the WebDAV property or undefined if the WebDAV property doesn't exist
113  */
114 nl.sara.webdav.Multistatus.prototype.getResponse = function(name) {
115   return this._responses[name];
116 };
117 
118 /**
119  * Gets the response names
120  *
121  * @returns  {String[]}  The names of the different responses
122  */
123 nl.sara.webdav.Multistatus.prototype.getResponseNames = function() {
124   return Object.keys(this._responses);
125 };
126 
127 // End of library
128