"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/plist/lib/plist.js" (8 Mar 2017, 9588 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 ;(function (exports, DOMParser, xmlbuilder) {
    2     // Checks if running in a non-browser environment
    3 
    4     var inNode = typeof process !== "undefined" && process.versions && !!process.versions.node
    5           , utf8_to_b64
    6         , b64_to_utf8;
    7 
    8 
    9 
   10     // this library runs in browsers and nodejs, set up functions accordingly
   11     if (inNode) {
   12         exports.parseFile = function (filename, callback) {
   13             console.warn('parseFile is deprecated. Please use parseFileSync instead.');
   14             var fs = require('fs');
   15             var inxml = fs.readFileSync(filename, 'utf8');
   16             exports.parseString(inxml, callback);
   17         }
   18 
   19         exports.parseFileSync = function (filename) {
   20             var fs = require('fs');
   21             var inxml = fs.readFileSync(filename, 'utf8');
   22             return exports.parseStringSync(inxml);
   23         }
   24 
   25         // set up base64 encode/decode functions
   26         utf8_to_b64= function ( str ) {
   27             return new Buffer(str).toString('base64');
   28         }
   29         b64_to_utf8 = function ( str ) {
   30             return new Buffer(str, 'base64').toString('utf8');
   31         }
   32     } else {
   33         // we're in a browser context, find the DOMParser
   34         if (typeof window.DOMParser != 'undefined') {
   35             DOMParser = function(xmlStr) {
   36                 return ( new window.DOMParser() ).parseFromString(xmlStr, 'text/xml');
   37             };
   38         } else if (typeof window.ActiveXObject != 'undefined' &&
   39                new window.ActiveXObject('Microsoft.XMLDOM')) {
   40             DOMParser = function(xmlStr) {
   41                 var xmlDoc = new window.ActiveXObject('Microsoft.XMLDOM');
   42                 xmlDoc.async = 'false';
   43                 xmlDoc.loadXML(xmlStr);
   44                 return xmlDoc;
   45             };
   46         } else {
   47             throw new Error('No XML parser found');
   48         }
   49 
   50         // set up base64 encode/decode functions
   51         if (typeof window.btoa == 'undefined') {
   52             throw new Error('No base64 support found');
   53             // TODO: shim btoa and atob if not present (ie < 10)
   54             //http://stackoverflow.com/questions/246801/how-can-you-encode-to-base64-using-javascript/247261#247261
   55         } else {
   56             utf8_to_b64= function ( str ) {
   57                 return window.btoa(unescape(encodeURIComponent( str )));
   58             }
   59 
   60             b64_to_utf8 = function ( str ) {
   61                 return decodeURIComponent(escape(window.atob( str )));
   62             }
   63         }
   64 
   65     }
   66 
   67     exports.parseString = function (xml, callback) {
   68         console.warn('parseString is deprecated. Please use parseStringSync instead.');
   69         var doc, error, plist;
   70         try {
   71             doc = buildDOMParser().parseFromString(xml);
   72             plist = parsePlistXML(doc.documentElement);
   73         } catch(e) {
   74             error = e;
   75         }
   76         callback(error, plist);
   77     }
   78 
   79     exports.parseStringSync = function (xml) {
   80         var doc = buildDOMParser().parseFromString(xml);
   81     var plist;
   82         if (doc.documentElement.nodeName !== 'plist') {
   83             throw new Error('malformed document. First element should be <plist>');
   84         }
   85         plist = parsePlistXML(doc.documentElement);
   86 
   87         // if the plist is an array with 1 element, pull it out of the array
   88         if(isArray(plist) && plist.length == 1) {
   89             plist = plist[0];
   90         }
   91         return plist;
   92     }
   93 
   94     /**
   95      * convert an XML based plist document into a JSON representation
   96      *
   97      * @param object xml_node current XML node in the plist
   98      * @return built up JSON object
   99      */
  100     function parsePlistXML(node) {
  101         var i, new_obj, key, val, new_arr;
  102         if (!node)
  103             return null;
  104 
  105         if (node.nodeName === 'plist') {
  106             new_arr = [];
  107             for (i=0;i < node.childNodes.length;i++) {
  108                 // ignore comment nodes (text)
  109                 if (node.childNodes[i].nodeType !== 3) {
  110                     new_arr.push( parsePlistXML(node.childNodes[i]));
  111                 }
  112             }
  113             return new_arr;
  114         }
  115         else if(node.nodeName === 'dict') {
  116             new_obj = {};
  117             key = null;
  118             for (i=0;i < node.childNodes.length;i++) {
  119                 // ignore comment nodes (text)
  120                 if (node.childNodes[i].nodeType !== 3) {
  121                     if (key === null) {
  122                         key = parsePlistXML(node.childNodes[i]);
  123                     } else {
  124                         new_obj[key] = parsePlistXML(node.childNodes[i]);
  125                         key = null;
  126                     }
  127                 }
  128             }
  129             return new_obj;
  130         }
  131         else if(node.nodeName === 'array') {
  132             new_arr = [];
  133             for (i=0;i < node.childNodes.length;i++) {
  134                 // ignore comment nodes (text)
  135                 if (node.childNodes[i].nodeType !== 3) {
  136                     res = parsePlistXML(node.childNodes[i]);
  137                     if (res) new_arr.push( res );
  138                 }
  139             }
  140             return new_arr;
  141         }
  142         else if(node.nodeName === '#text') {
  143             // TODO: what should we do with text types? (CDATA sections)
  144         }
  145         else if(node.nodeName === 'key') {
  146             return node.childNodes[0].nodeValue;
  147         }
  148         else if(node.nodeName === 'string') {
  149             var res = '';
  150             for (var d=0; d < node.childNodes.length; d++)
  151             {
  152                 res += node.childNodes[d].nodeValue;
  153             }
  154             return res;
  155         }
  156         else if(node.nodeName === 'integer') {
  157             // parse as base 10 integer
  158             return parseInt(node.childNodes[0].nodeValue, 10);
  159         }
  160         else if(node.nodeName === 'real') {
  161             var res = '';
  162             for (var d=0; d < node.childNodes.length; d++)
  163             {
  164                 if(node.childNodes[d].nodeType === 3) {
  165                     res += node.childNodes[d].nodeValue;
  166                 }
  167             }
  168             return parseFloat(res);
  169         }
  170         else if(node.nodeName === 'data') {
  171             var res = '';
  172             for (var d=0; d < node.childNodes.length; d++)
  173             {
  174                 if(node.childNodes[d].nodeType === 3) {
  175                     res += node.childNodes[d].nodeValue;
  176                 }
  177             }
  178 
  179             // validate that the string is encoded as base64
  180             var base64Matcher = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
  181             if (!base64Matcher.test(res.replace(/\s/g,'')) ) {
  182                 throw new Error('malformed document. <data> element is not base64 encoded');
  183             }
  184 
  185             // decode base64 data as utf8 string
  186             return b64_to_utf8(res);
  187         }
  188         else if(node.nodeName === 'date') {
  189             return new Date(node.childNodes[0].nodeValue);
  190         }
  191         else if(node.nodeName === 'true') {
  192             return true;
  193         }
  194         else if(node.nodeName === 'false') {
  195             return false;
  196         }
  197     }
  198 
  199 
  200   function ISODateString(d){
  201     function pad(n){return n<10 ? '0'+n : n}
  202     return d.getUTCFullYear()+'-'
  203         + pad(d.getUTCMonth()+1)+'-'
  204         + pad(d.getUTCDate())+'T'
  205         + pad(d.getUTCHours())+':'
  206         + pad(d.getUTCMinutes())+':'
  207         + pad(d.getUTCSeconds())+'Z'
  208   }
  209 
  210   // instanceof is horribly unreliable so we use these hackish but safer checks
  211   // http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray
  212   function isArray(obj) {
  213     return Object.prototype.toString.call(obj) === '[object Array]';
  214   }
  215 
  216   function isDate(obj) {
  217     return Object.prototype.toString.call(obj) === '[object Date]';
  218   }
  219 
  220   function isBoolean(obj) {
  221     return (obj === true || obj === false || toString.call(obj) == '[object Boolean]');
  222   }
  223 
  224   function isNumber(obj) {
  225     return Object.prototype.toString.call(obj) === '[object Number]';
  226   }
  227 
  228   function isObject(obj) {
  229     return Object.prototype.toString.call(obj) === '[object Object]';
  230   }
  231 
  232   function isString(obj) {
  233     return Object.prototype.toString.call(obj) === '[object String]';
  234   }
  235 
  236   /**
  237    * generate an XML plist string from the input object
  238    *
  239    * @param object obj the object to convert
  240    * @return string converted plist
  241    */
  242   exports.build = function(obj) {
  243     var XMLHDR = { 'version': '1.0','encoding': 'UTF-8'}
  244         , XMLDTD = { 'ext': 'PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"'}
  245         , doc = xmlbuilder.create()
  246         , child = doc.begin('plist', XMLHDR, XMLDTD).att('version', '1.0');
  247 
  248     walk_obj(obj, child);
  249     return child.end({pretty: true });
  250   }
  251 
  252   // depth first, recursive traversal of a javascript object. when complete,
  253   // next_child contains a reference to the build XML object.
  254   function walk_obj(next, next_child) {
  255     var tag_type, i, prop;
  256 
  257     if(isArray(next)) {
  258       next_child = next_child.ele('array');
  259       for(i=0 ;i < next.length;i++) {
  260         walk_obj(next[i], next_child);
  261       }
  262     }
  263     else if (isObject(next)) {
  264       if (inNode && next instanceof Buffer) {
  265         next_child.ele('data').raw(next.toString('base64'));
  266       } else {
  267         next_child = next_child.ele('dict');
  268         for(prop in next) {
  269           if(next.hasOwnProperty(prop)) {
  270             next_child.ele('key').txt(prop);
  271             walk_obj(next[prop], next_child);
  272           }
  273         }
  274       }
  275     }
  276     else if(isNumber(next)) {
  277       // detect if this is an integer or real
  278       tag_type =(next % 1 === 0) ? 'integer' : 'real';
  279       next_child.ele(tag_type).txt(next.toString());
  280     }
  281     else if(isDate(next)) {
  282       next_child.ele('date').raw(ISODateString(new Date(next)));
  283     }
  284     else if(isBoolean(next)) {
  285       val = next ? 'true' : 'false';
  286       next_child.ele(val);
  287     }
  288     else if(isString(next)) {
  289       //if (str!=obj || str.indexOf("\n")>=0) str = "<![CDATA["+str+"]]>";
  290       var base64Matcher = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
  291       if (!inNode && base64Matcher.test(next.replace(/\s/g,''))) {
  292         // data is base 64 encoded so assume it's a <data> node
  293         next_child.ele('data').raw(utf8_to_b64(next));
  294       } else {
  295         // it's not base 64 encoded, assume it's a <string> node
  296         next_child.ele('string').raw(next);
  297       }
  298     }
  299   };
  300 
  301     function buildDOMParser() {
  302         // prevent the parser from logging non-fatel errors
  303         return new DOMParser({errorHandler: function() {}});
  304     }
  305 
  306 })(typeof exports === 'undefined' ? plist = {} : exports, typeof require !== 'undefined' ? require('xmldom').DOMParser : null, typeof require !== 'undefined' ? require('xmlbuilder') : xmlbuilder)
  307 // the above line checks for exports (defined in node) and uses it, or creates
  308 // a global variable and exports to that. also, if in node, require DOMParser
  309 // node-style, in browser it should already be present