Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- loadLibrary("lf:exprParser");
- //--------------------------------------------------------------------------
- // Web service entry point
- //--------------------------------------------------------------------------
- function xtk_queryFilter_BuildJS(elemQueryDef)
- {
- var strSchema = elemQueryDef.@schema.toString().replace(/.*:/, "");
- var strRes = parseSubCond(elemQueryDef.where, strSchema);
- return strRes;
- }
- //--------------------------------------------------------------------------
- // Web service entry point
- //--------------------------------------------------------------------------
- function xtk_queryDef_BuildJS(elemWhere, strSchema)
- {
- var strRes = parseSubCond(elemWhere, strSchema.toString().replace(/.*:/,""));
- return strRes;
- }
- //--------------------------------------------------------------------------
- // Parses conditions on child elements
- //--------------------------------------------------------------------------
- function parseSubCond(elem, strBasePath)
- {
- var strRes = "";
- var strOp = " && ";
- for each (var elemCond in elem.condition)
- {
- if (elemCond.@setOperator != undefined)
- logError("Feature not supported: set operator " + elemCond.@setOperator)
- var strQueryCond = elemCond.@expr.toString();
- // Nested blocks?
- if (strQueryCond == "")
- var strCond = "(" + parseSubCond(elemCond, strBasePath) + ")";
- else
- strCond = parseCond(strQueryCond, strBasePath);
- strRes += (strRes == "" ? "" : strOp) + strCond;
- if (elemCond.@boolOperator == "OR")
- strOp = " || ";
- else if (elemCond.@boolOperator == "AND NOT")
- strOp = " && ! ";
- else
- strOp = " && ";
- }
- return strRes;
- }
- //--------------------------------------------------------------------------
- // Parses XTK expression and generates matching JS expression
- //--------------------------------------------------------------------------
- function parseCond(strExpr, strBasePath)
- {
- //logVerbose("Type: " + typeof strExpr + " value: " + strExpr);
- try {
- var tree = vgExprParser.parse(strExpr);
- return jsExpr(tree, strBasePath);
- }
- catch (e)
- {
- if (e instanceof vgExprParser.SyntaxError)
- {
- logVerbose(strExpr);
- logError("Syntax error in expression \"" + strExpr + "\": " + e.message);
- }
- if (e instanceof JsExprException)
- {
- logError("Expression limitation in \"" + strExpr + "\": " + e.strMsg);
- }
- throw e;
- }
- }
- //--------------------------------------------------------------------------
- // Expects an expression tree as input
- //--------------------------------------------------------------------------
- function jsExpr(tree, strBasePath)
- {
- var strMid = "";
- switch (tree.type)
- {
- case "=":
- return jsExpr(tree.left, strBasePath) + "==" + jsExpr(tree.right, strBasePath);
- case ">":
- case "<":
- case ">=":
- case "<=":
- return jsExpr(tree.left, strBasePath) + tree.type + jsExpr(tree.right, strBasePath);
- case "!=":
- case "<>":
- return jsExpr(tree.left, strBasePath) + "!=" + jsExpr(tree.right, strBasePath);
- case "isNull":
- return jsExpr(tree.left, strBasePath) + "==''";
- case "isNotNull":
- return jsExpr(tree.left, strBasePath) + "!=''";
- case "int":
- return tree.left;
- case "str":
- return "'" + tree.left + "'";
- case "xpath":
- return jsXpathExpr(tree, strBasePath);
- case "datetime":
- return "new Date(" + tree.left.substr(0,4) + "," + (parseInt(tree.left.substr(5,2), 10) - 1) + "," + tree.left.substr(8,2) + "," +
- tree.left.substr(11,2) + "," + tree.left.substr(14,2) + "," + tree.left.substr(17,2) + "," +
- tree.left.substr(20,3) + ")";
- case "date":
- return "new Date(" + tree.left.substr(0,4) + "," + (parseInt(tree.left.substr(5,2), 10) - 1) + "," + tree.left.substr(8,2) + ")";
- default:
- throw new JsExprException(tree, strBasePath, "Feature not supported: " + tree.type);
- }
- }
- //--------------------------------------------------------------------------
- // Translate an Xpath to a Javascript expression
- // e.g. @firstName -> recipient.firstName
- //--------------------------------------------------------------------------
- function jsXpathExpr(tree, strBasePath)
- {
- if (strBasePath == undefined)
- var strBase = "";
- else
- strBase = strBasePath + ".";
- if (typeof tree == "undefined" || tree === null)
- return null;
- var strLeft = strBase + tree.left.replace(/^@/, "").replace(/-/g, "_");
- if (typeof tree.right == "string")
- return strLeft + "." + tree.right.replace(/^@/, "").replace(/-/g, "_");
- var strRight = jsXpathExpr(tree.right, strBasePath);
- if (strRight === null)
- return strLeft;
- return strLeft + "." + strRight;
- }
- //--------------------------------------------------------------------------
- // Exception for JS expression generation
- //--------------------------------------------------------------------------
- function JsExprException(tree, strBasePath, strMsg)
- {
- this.tree = tree;
- this.strBasePath = strBasePath;
- this.strMsg = strMsg;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement