PageRenderTime 17ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.0.1/jsdoc-toolkit/app/frame/Dumper.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 144 lines | 97 code | 18 blank | 29 comment | 26 complexity | 840202dce50234da4ca45a1a6882c529 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. if (typeof obj == 'undefined') return 'undefined';
  32. var out;
  33. if (obj.serialize) { return obj.serialize(); }
  34. var type = this._typeof(obj);
  35. if (obj.circularReference) obj.circularReference++;
  36. switch (type) {
  37. case 'circular':
  38. out = "{ //circularReference\n}";
  39. break;
  40. case 'object':
  41. var pairs = new Array;
  42. for (var prop in obj) {
  43. if (prop != "circularReference" && obj.hasOwnProperty(prop)) { //hide inherited properties
  44. pairs.push(prop + ': ' + this._dump(obj[prop]));
  45. }
  46. }
  47. out = '{' + this._format_list(pairs) + '}';
  48. break;
  49. case 'string':
  50. for (var prop in Dumper.ESC) {
  51. if (Dumper.ESC.hasOwnProperty(prop)) {
  52. obj = obj.replace(prop, Dumper.ESC[prop]);
  53. }
  54. }
  55. // Escape UTF-8 Strings
  56. if (obj.match(/^[\x00-\x7f]*$/)) {
  57. out = '"' + obj.replace(/\"/g, "\\\"").replace(/([\n\r]+)/g, "\\$1") + '"';
  58. }
  59. else {
  60. out = "unescape('"+escape(obj)+"')";
  61. }
  62. break;
  63. case 'array':
  64. var elems = new Array;
  65. for (var i=0; i<obj.length; i++) {
  66. elems.push( this._dump(obj[i]) );
  67. }
  68. out = '[' + this._format_list(elems) + ']';
  69. break;
  70. case 'date':
  71. // firefox returns GMT strings from toUTCString()...
  72. var utc_string = obj.toUTCString().replace(/GMT/,'UTC');
  73. out = 'new Date("' + utc_string + '")';
  74. break;
  75. case 'element':
  76. // DOM element
  77. out = this._dump_dom(obj);
  78. break;
  79. default:
  80. out = obj;
  81. }
  82. out = String(out).replace(/\n/g, '\n ');
  83. out = out.replace(/\n (.*)$/,"\n$1");
  84. return out;
  85. },
  86. _format_list: function (list) {
  87. if (!list.length) return '';
  88. var nl = list.toString().length > 60 ? '\n' : ' ';
  89. return nl + list.join(',' + nl) + nl;
  90. },
  91. _typeof: function (obj) {
  92. if (obj && obj.circularReference && obj.circularReference > 1) return 'circular';
  93. if (Array.prototype.isPrototypeOf(obj)) return 'array';
  94. if (Date.prototype.isPrototypeOf(obj)) return 'date';
  95. if (typeof obj.nodeType != 'undefined') return 'element';
  96. return typeof(obj);
  97. },
  98. _dump_dom: function (obj) {
  99. return '"' + Dumper.nodeTypes[obj.nodeType] + '"';
  100. }
  101. };
  102. Dumper.ESC = {
  103. "\t": "\\t",
  104. "\n": "\\n",
  105. "\f": "\\f"
  106. };
  107. Dumper.nodeTypes = {
  108. 1: "ELEMENT_NODE",
  109. 2: "ATTRIBUTE_NODE",
  110. 3: "TEXT_NODE",
  111. 4: "CDATA_SECTION_NODE",
  112. 5: "ENTITY_REFERENCE_NODE",
  113. 6: "ENTITY_NODE",
  114. 7: "PROCESSING_INSTRUCTION_NODE",
  115. 8: "COMMENT_NODE",
  116. 9: "DOCUMENT_NODE",
  117. 10: "DOCUMENT_TYPE_NODE",
  118. 11: "DOCUMENT_FRAGMENT_NODE",
  119. 12: "NOTATION_NODE"
  120. };