1 /** 2 * @fileoverview A class representing operations on union expressions. 3 */ 4 5 goog.provide('xrx.xpath.UnionExpr'); 6 7 goog.require('goog.array'); 8 goog.require('xrx.xpath.DataType'); 9 goog.require('xrx.xpath.Expr'); 10 11 12 13 /** 14 * Constructor for UnionExpr. 15 * 16 * @param {!Array.<!xrx.xpath.Expr>} paths The paths in the union. 17 * @extends {xrx.xpath.Expr} 18 * @constructor 19 */ 20 xrx.xpath.UnionExpr = function(paths) { 21 xrx.xpath.Expr.call(this, xrx.xpath.DataType.NODESET); 22 23 /** 24 * @type {!Array.<!xrx.xpath.Expr>} 25 * @private 26 */ 27 this.paths_ = paths; 28 this.setNeedContextPosition(goog.array.some(this.paths_, function(p) { 29 return p.doesNeedContextPosition(); 30 })); 31 this.setNeedContextNode(goog.array.some(this.paths_, function(p) { 32 return p.doesNeedContextNode(); 33 })); 34 }; 35 goog.inherits(xrx.xpath.UnionExpr, xrx.xpath.Expr); 36 37 38 /** 39 * @override 40 * @return {!xrx.xpath.NodeSet} The nodeset result. 41 */ 42 xrx.xpath.UnionExpr.prototype.evaluate = function(ctx) { 43 var nodeset = new xrx.xpath.NodeSet(); 44 goog.array.forEach(this.paths_, function(p) { 45 var result = p.evaluate(ctx); 46 if (!(result instanceof xrx.xpath.NodeSet)) { 47 throw Error('Path expression must evaluate to NodeSet.'); 48 } 49 nodeset = xrx.xpath.NodeSet.merge(nodeset, result); 50 }); 51 return nodeset; 52 }; 53 54 55 /** 56 * @override 57 */ 58 xrx.xpath.UnionExpr.prototype.toString = function() { 59 return goog.array.reduce(this.paths_, function(prev, curr) { 60 return prev + xrx.xpath.Expr.indent(curr); 61 }, 'Union Expression:'); 62 }; 63