1 /**
  2  * @fileoverview A class which implements and extends the xrx.xdm 
  3  * interface.
  4  */
  5 
  6 goog.provide('xrx.node');
  7 
  8 
  9 
 10 goog.require('xrx.model');
 11 goog.require('xrx.token');
 12 goog.require('xrx.xdm');
 13 
 14 
 15 
 16 /**
 17  * A class which implements and extends the xrx.xdm interface.
 18  * 
 19  * @constructor
 20  * @implements {xrx.xdm}
 21  */
 22 xrx.node = function(pilot, type, token, instance) {
 23   goog.base(this, pilot, xrx.node.ELEMENT, token);
 24 
 25   /**
 26    * @private
 27    */
 28   this.pilot_ = pilot;
 29 
 30   /**
 31    * @private
 32    */
 33   this.token_ = token;
 34 
 35   /**
 36    * @private
 37    */
 38   this.type_ = type;
 39 
 40   /**
 41    * @private
 42    */
 43   this.instance_ = instance;
 44 };
 45 goog.inherits(xrx.node, xrx.xdm);
 46 
 47 
 48 
 49 /** @const */ xrx.node.DOCUMENT = 0;
 50 /** @const */ xrx.node.ELEMENT = 1;
 51 /** @const */ xrx.node.ATTRIBUTE = 2;
 52 /** @const */ xrx.node.NAMESPACE = 3;
 53 /** @const */ xrx.node.PI = 4;
 54 /** @const */ xrx.node.COMMENT = 5;
 55 /** @const */ xrx.node.TEXT = 6;
 56 /** @const */ xrx.node.NODE = 7;
 57 
 58 
 59 
 60 /**
 61  * @return
 62  */
 63 xrx.node.prototype.pilot = function() {
 64   return this.pilot_;
 65 };
 66 
 67 
 68 
 69 /**
 70  * @return
 71  */
 72 xrx.node.prototype.stream = function() {
 73   return this.pilot_.stream();
 74 };
 75 
 76 
 77 
 78 /**
 79  * @return
 80  */
 81 xrx.node.prototype.token = function() {
 82   return this.token_;
 83 };
 84 
 85 
 86 
 87 /**
 88  * @return
 89  */
 90 xrx.node.prototype.type = function() {
 91   return this.type_;
 92 };
 93 
 94 
 95 
 96 /**
 97  * @return
 98  */
 99 xrx.node.prototype.label = function() {
100   return this.token_.label();
101 };
102 
103 
104 
105 /**
106  * @return
107  */
108 xrx.node.prototype.offset = function() {
109   return this.token_.offset();
110 };
111 
112 
113 
114 /**
115  * @return
116  */
117 xrx.node.prototype.instance = function() {
118   return this.instance_;
119 };
120 
121 
122 
123 /**
124  * Returns the string-value of the required type from a node.
125  *
126  * @param {!xrx.node} node The node to get value from.
127  * @return {string} The value required.
128  */
129 xrx.node.getValueAsString = function(node) {
130   return node.stringValue();
131 };
132 
133 
134 
135 /**
136  * Returns the string-value of the required type from a node, casted to number.
137  *
138  * @param {!xrx.node} node The node to get value from.
139  * @return {number} The value required.
140  */
141 xrx.node.getValueAsNumber = function(node) {
142   return +xrx.node.getValueAsString(node);
143 };
144 
145 
146 
147 /**
148  * Returns the string-value of the required type from a node, casted to boolean.
149  *
150  * @param {!xrx.node} node The node to get value from.
151  * @return {boolean} The value required.
152  */
153 xrx.node.getValueAsBool = function(node) {
154   return !!xrx.node.getValueAsString(node);
155 };
156 
157 
158 
159 /**
160  * Returns whether two nodes are the same.
161  *
162  * @param {xrx.node} node The node to test against.
163  * @return {boolean} Whether the nodes are the same.
164  */
165 xrx.node.prototype.sameAs = function(node) {
166   return this.type() === node.type() && this.label().sameAs(
167       node.label()) && this.instance_ === node.instance_;
168 };
169 
170 
171 
172 /**
173  * @return
174  */
175 xrx.node.prototype.isBefore = function(node) {
176   return this.type_ <= node.type() && this.label().isBefore(
177       node.label());
178 };
179 
180 
181 
182 /**
183  * @return
184  */
185 xrx.node.compareOrder = function(node1, node2) {
186   
187   if (node1.sameAs(node2)) {
188     return 0;
189   } else if (node1.isBefore(node2)) {
190     return -1;
191   } else {
192     return 1;
193   }
194 };
195 
196 
197 
198 /**
199  * @override
200  */
201 xrx.node.prototype.expandedName = function() { return ''; };
202 
203 
204 /**
205  * @override
206  */
207 xrx.node.prototype.namespaceUri = function() { return undefined; };
208 
209 
210 
211 /**
212  * @override
213  */
214 xrx.node.prototype.getAncestorNodes = function(test) {
215 
216   return this.find(test, xrx.label.prototype.isDescendantOf, true);
217 };
218 
219 
220 /**
221  * @override
222  */
223 xrx.node.prototype.getChildNodes = function(test) {
224 
225   return this.find(test, xrx.label.prototype.isParentOf);
226 };
227 
228 
229 /**
230  * Returns the descendants of a node.
231  *
232  * @private
233  * @param {!xrx.xpath.NodeTest} test A NodeTest for matching nodes.
234  * @param {!xrx.node} node The node to get descendants from.
235  * @param {?string} opt_attrName The attribute name to match, if any.
236  * @param {?string} opt_attrValue The attribute value to match, if any.
237  * @return {!xrx.xpath.NodeSet} The node-set with descendants.
238  */
239 xrx.node.prototype.getDescendantNodes = function(test) {
240 
241   return this.find(test, xrx.label.prototype.isAncestorOf);
242 };
243 
244 
245 /**
246  * @override
247  */
248 xrx.node.prototype.getFollowingSiblingNodes = function(test) {
249 
250   return this.find(test, xrx.label.prototype.isPrecedingSiblingOf);
251 };
252 
253 
254 /**
255  * @override
256  */
257 xrx.node.prototype.getFollowingNodes = function(test) {
258 
259   return this.find(test, xrx.label.prototype.isBefore);
260 };
261 
262 
263 
264 /**
265  * @override
266  */
267 xrx.node.prototype.getParentNodes = function(test) {
268 
269   return this.find(test, xrx.label.prototype.isChildOf, true);
270 };
271 
272 
273