/tags/jsdoc_toolkit-2.2.1/jsdoc-toolkit/app/handlers/XMLDOC/DomReader.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 159 lines · 75 code · 26 blank · 58 comment · 10 complexity · e6b50bf71f74a3b6ca1df7aab947df4b MD5 · raw file

  1. LOG.inform("XMLDOC.DomReader loaded");
  2. XMLDOC.DomReader = function(root) {
  3. this.dom = root;
  4. /**
  5. * The current node the reader is on
  6. */
  7. this.node = root;
  8. /**
  9. * Get the current node the reader is on
  10. * @type XMLDOC.Parser.node
  11. */
  12. XMLDOC.DomReader.prototype.getNode = function() {
  13. return this.node;
  14. };
  15. /**
  16. * Set the node the reader should be positioned on.
  17. * @param node {XMLDOC.Parser.node}
  18. */
  19. XMLDOC.DomReader.prototype.setNode = function(node) {
  20. this.node = node;
  21. };
  22. /**
  23. * A helper method to make sure the current node will
  24. * never return null, unless null is passed as the root.
  25. * @param step {String} An expression to evaluate - should return a node or null
  26. */
  27. XMLDOC.DomReader.prototype.navigate = function(step) {
  28. var n;
  29. if ((n = step) != null)
  30. {
  31. this.node = n;
  32. return this.node;
  33. }
  34. return null;
  35. };
  36. /**
  37. * Get the root node of the current node's document.
  38. */
  39. XMLDOC.DomReader.prototype.root = function() {
  40. this.navigate(this.dom);
  41. };
  42. /**
  43. * Get the parent of the current node.
  44. */
  45. XMLDOC.DomReader.prototype.parent = function() {
  46. return this.navigate(this.node.parentNode());
  47. };
  48. /**
  49. * Get the first child of the current node.
  50. */
  51. XMLDOC.DomReader.prototype.firstChild = function() {
  52. return this.navigate(this.node.firstChild());
  53. };
  54. /**
  55. * Get the last child of the current node.
  56. */
  57. XMLDOC.DomReader.prototype.lastChild = function() {
  58. return this.navigate(this.node.lastChild());
  59. };
  60. /**
  61. * Get the next sibling of the current node.
  62. */
  63. XMLDOC.DomReader.prototype.nextSibling = function() {
  64. return this.navigate(this.node.nextSibling());
  65. };
  66. /**
  67. * Get the previous sibling of the current node.
  68. */
  69. XMLDOC.DomReader.prototype.prevSibling = function() {
  70. return this.navigate(this.node.prevSibling());
  71. };
  72. //===============================================================================================
  73. // Support methods
  74. /**
  75. * Walk the tree starting with the current node, calling the plug-in for
  76. * each node visited. Each time the plug-in is called, the DomReader
  77. * is passed as the only parameter. Use the {@link XMLDOC.DomReader#getNode} method
  78. * to access the current node. <i>This method uses a depth first traversal pattern.</i>
  79. *
  80. * @param srcFile {String} The source file being evaluated
  81. */
  82. XMLDOC.DomReader.prototype.getSymbols = function(srcFile)
  83. {
  84. XMLDOC.DomReader.symbols = [];
  85. XMLDOC.DomReader.currentFile = srcFile;
  86. JSDOC.Symbol.srcFile = (srcFile || "");
  87. if (defined(JSDOC.PluginManager)) {
  88. JSDOC.PluginManager.run("onDomGetSymbols", this);
  89. }
  90. return XMLDOC.DomReader.symbols;
  91. };
  92. /**
  93. * Find the node with the given name using a depth first traversal.
  94. * Does not modify the DomReader's current node.
  95. *
  96. * @param name {String} The name of the node to find
  97. * @return the node that was found, or null if not found
  98. */
  99. XMLDOC.DomReader.prototype.findNode = function(name)
  100. {
  101. var findNode = null;
  102. // Start at the current node and move into the subtree,
  103. // looking for the node with the given name
  104. function deeper(node, find)
  105. {
  106. var look = null;
  107. if (node) {
  108. if (node.name == find)
  109. {
  110. return node;
  111. }
  112. if (node.firstChild())
  113. {
  114. look = deeper(node.firstChild(), find);
  115. }
  116. if (!look && node.nextSibling())
  117. {
  118. look = deeper(node.nextSibling(), find);
  119. }
  120. }
  121. return look;
  122. }
  123. return deeper(this.getNode().firstChild(), name);
  124. };
  125. /**
  126. * Find the next node with the given name using a depth first traversal.
  127. *
  128. * @param name {String} The name of the node to find
  129. */
  130. XMLDOC.DomReader.prototype.findPreviousNode = function(name)
  131. {
  132. };
  133. };