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.codec.GetlastmodifiedCodec is already defined, we have a namespace clash!
 22 if (nl.sara.webdav.codec.GetlastmodifiedCodec !== undefined) {
 23   throw new nl.sara.webdav.Exception('Namespace nl.sara.webdav.codec.GetlastmodifiedCodec already taken, could not load JavaScript library for WebDAV connectivity.', nl.sara.webdav.Exception.NAMESPACE_TAKEN);
 24 }
 25 
 26 /**
 27  * @class Adds a codec that converts DAV: getlastmodified to a Date object
 28  * @augments nl.sara.webdav.Codec
 29  */
 30 nl.sara.webdav.codec.GetlastmodifiedCodec = new nl.sara.webdav.Codec();
 31 nl.sara.webdav.codec.GetlastmodifiedCodec.namespace = 'DAV:';
 32 nl.sara.webdav.codec.GetlastmodifiedCodec.tagname = 'getlastmodified';
 33 
 34 nl.sara.webdav.codec.GetlastmodifiedCodec.fromXML = function(nodelist) {
 35   var node = nodelist.item(0);
 36   if ((node.nodeType === 3) || (node.nodeType === 4)) { // Make sure text and CDATA content is stored
 37     return new Date(node.nodeValue);
 38   }else{ // If the node is not text or CDATA, then we don't parse a value at all
 39     return null;
 40   }
 41 };
 42 
 43 nl.sara.webdav.codec.GetlastmodifiedCodec.toXML = function(value, xmlDoc){
 44   function pad(text) {
 45     text = text.toString();
 46     while (text.length < 2) {
 47       text = '0' + text;
 48     }
 49     return text;
 50   }
 51   // I will parse this myself, because the Date object doesn't have a method which is specified to always give a rfc1123-date (defined in Section 3.3.1 of [RFC2616]). Even though Date.toUTCString() often does so, it doesn't have to.
 52   var wkday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
 53   var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
 54   var date1 = pad(value.getUTCDate()) + ' ' + month[value.getUTCMonth()] + ' ' + value.getUTCFullYear();
 55   var time = pad(value.getUTCHours()) + ':' + pad(value.getUTCMinutes()) + ':' + pad(value.getUTCSeconds());
 56   var cdata = xmlDoc.createCDATASection(wkday[value.getUTCDay()] + ', ' + date1 + ' ' + time + ' GMT');
 57   xmlDoc.documentElement.appendChild(cdata);
 58   return xmlDoc;
 59 };
 60 
 61 nl.sara.webdav.Property.addCodec(nl.sara.webdav.codec.GetlastmodifiedCodec);
 62 
 63 // End of file