/branches/jsdoc_tk_gui/setup/jsDoc/Dumper.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 142 lines · 95 code · 18 blank · 29 comment · 23 complexity · 02fb603653a0e8316401f710517ed95a MD5 · raw file

  1. /**
  2. * @class
  3. <pre>
  4. This is a lightly modified version of Kevin Jones' JavaScript
  5. library Data.Dump. To download the original visit:
  6. <a href="http://openjsan.org/doc/k/ke/kevinj/Data/Dump/">http://openjsan.org/doc/k/ke/kevinj/Data/Dump/</a>
  7. AUTHORS
  8. The Data.Dump JavaScript module is written by Kevin Jones
  9. (kevinj@cpan.org), based on Data::Dump by Gisle Aas (gisle@aas.no),
  10. based on Data::Dumper by Gurusamy Sarathy (gsar@umich.edu).
  11. COPYRIGHT
  12. Copyright 2007 Kevin Jones. Copyright 1998-2000,2003-2004 Gisle Aas.
  13. Copyright 1996-1998 Gurusamy Sarathy.
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the Perl Artistic License
  16. See http://www.perl.com/perl/misc/Artistic.html
  17. </pre>
  18. * @static
  19. */
  20. Dumper = {
  21. /** @param [...] The objects to dump. */
  22. dump: function () {
  23. if (arguments.length > 1)
  24. return this._dump(arguments);
  25. else if (arguments.length == 1)
  26. return this._dump(arguments[0]);
  27. else
  28. return "()";
  29. },
  30. _dump: function (obj) {
  31. var out;
  32. var type = this._typeof(obj);
  33. if (obj.circularReference) obj.circularReference++;
  34. switch (type) {
  35. case 'circular':
  36. out = '{/*circularReference*/}';
  37. break;
  38. case 'object':
  39. var pairs = new Array;
  40. for (var prop in obj) {
  41. if (prop != "circularReference" && obj.hasOwnProperty(prop)) { //hide inherited properties
  42. pairs.push(prop + ': ' + this._dump(obj[prop]));
  43. }
  44. }
  45. out = '{' + this._format_list(pairs) + '}';
  46. break;
  47. case 'string':
  48. for (var prop in Dumper.ESC) {
  49. if (Dumper.ESC.hasOwnProperty(prop)) {
  50. obj = obj.replace(prop, Dumper.ESC[prop]);
  51. }
  52. }
  53. // Escape UTF-8 Strings
  54. if (obj.match(/^[\x00-\x7f]*$/)) {
  55. out = '"' + obj + '"';
  56. }
  57. else {
  58. out = "unescape('"+escape(obj)+"')";
  59. }
  60. break;
  61. case 'array':
  62. var elems = new Array;
  63. for (var i=0; i<obj.length; i++) {
  64. elems.push( this._dump(obj[i]) );
  65. }
  66. out = '[' + this._format_list(elems) + ']';
  67. break;
  68. case 'date':
  69. // firefox returns GMT strings from toUTCString()...
  70. var utc_string = obj.toUTCString().replace(/GMT/,'UTC');
  71. out = 'new Date("' + utc_string + '")';
  72. break;
  73. case 'element':
  74. // DOM element
  75. out = this._dump_dom(obj);
  76. break;
  77. default:
  78. out = obj;
  79. }
  80. out = String(out).replace(/\n/g, '\n ');
  81. out = out.replace(/\n (.*)$/,"\n$1");
  82. return out;
  83. },
  84. _format_list: function (list) {
  85. if (!list.length) return '';
  86. var nl = list.toString().length > 60 ? '\n' : ' ';
  87. return nl + list.join(',' + nl) + nl;
  88. },
  89. _typeof: function (obj) {
  90. if (obj && obj.circularReference && obj.circularReference > 1) return 'circular';
  91. if (Array.prototype.isPrototypeOf(obj)) return 'array';
  92. if (Date.prototype.isPrototypeOf(obj)) return 'date';
  93. if (typeof(obj.nodeType) != 'undefined') return 'element';
  94. return typeof(obj);
  95. },
  96. _dump_dom: function (obj) {
  97. return '"' + Dumper.nodeTypes[obj.nodeType] + '"';
  98. }
  99. };
  100. Dumper.ESC = {
  101. "\t": "\\t",
  102. "\n": "\\n",
  103. "\f": "\\f"
  104. };
  105. Dumper.nodeTypes = {
  106. 1: "ELEMENT_NODE",
  107. 2: "ATTRIBUTE_NODE",
  108. 3: "TEXT_NODE",
  109. 4: "CDATA_SECTION_NODE",
  110. 5: "ENTITY_REFERENCE_NODE",
  111. 6: "ENTITY_NODE",
  112. 7: "PROCESSING_INSTRUCTION_NODE",
  113. 8: "COMMENT_NODE",
  114. 9: "DOCUMENT_NODE",
  115. 10: "DOCUMENT_TYPE_NODE",
  116. 11: "DOCUMENT_FRAGMENT_NODE",
  117. 12: "NOTATION_NODE"
  118. };