PageRenderTime 34ms CodeModel.GetById 24ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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