Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. loadLibrary("lf:exprParser");
  2.  
  3.  
  4. //--------------------------------------------------------------------------
  5. // Web service entry point
  6. //--------------------------------------------------------------------------
  7. function xtk_queryFilter_BuildJS(elemQueryDef)
  8. {
  9. var strSchema = elemQueryDef.@schema.toString().replace(/.*:/, "");
  10. var strRes = parseSubCond(elemQueryDef.where, strSchema);
  11. return strRes;
  12. }
  13.  
  14. //--------------------------------------------------------------------------
  15. // Web service entry point
  16. //--------------------------------------------------------------------------
  17. function xtk_queryDef_BuildJS(elemWhere, strSchema)
  18. {
  19. var strRes = parseSubCond(elemWhere, strSchema.toString().replace(/.*:/,""));
  20. return strRes;
  21. }
  22.  
  23.  
  24. //--------------------------------------------------------------------------
  25. // Parses conditions on child elements
  26. //--------------------------------------------------------------------------
  27. function parseSubCond(elem, strBasePath)
  28. {
  29. var strRes = "";
  30. var strOp = " && ";
  31.  
  32. for each (var elemCond in elem.condition)
  33. {
  34. if (elemCond.@setOperator != undefined)
  35. logError("Feature not supported: set operator " + elemCond.@setOperator)
  36.  
  37. var strQueryCond = elemCond.@expr.toString();
  38.  
  39. // Nested blocks?
  40. if (strQueryCond == "")
  41. var strCond = "(" + parseSubCond(elemCond, strBasePath) + ")";
  42. else
  43. strCond = parseCond(strQueryCond, strBasePath);
  44.  
  45. strRes += (strRes == "" ? "" : strOp) + strCond;
  46. if (elemCond.@boolOperator == "OR")
  47. strOp = " || ";
  48. else if (elemCond.@boolOperator == "AND NOT")
  49. strOp = " && ! ";
  50. else
  51. strOp = " && ";
  52. }
  53.  
  54. return strRes;
  55. }
  56.  
  57. //--------------------------------------------------------------------------
  58. // Parses XTK expression and generates matching JS expression
  59. //--------------------------------------------------------------------------
  60. function parseCond(strExpr, strBasePath)
  61. {
  62. //logVerbose("Type: " + typeof strExpr + " value: " + strExpr);
  63. try {
  64. var tree = vgExprParser.parse(strExpr);
  65. return jsExpr(tree, strBasePath);
  66. }
  67. catch (e)
  68. {
  69. if (e instanceof vgExprParser.SyntaxError)
  70. {
  71. logVerbose(strExpr);
  72. logError("Syntax error in expression \"" + strExpr + "\": " + e.message);
  73. }
  74. if (e instanceof JsExprException)
  75. {
  76. logError("Expression limitation in \"" + strExpr + "\": " + e.strMsg);
  77. }
  78. throw e;
  79. }
  80. }
  81.  
  82. //--------------------------------------------------------------------------
  83. // Expects an expression tree as input
  84. //--------------------------------------------------------------------------
  85. function jsExpr(tree, strBasePath)
  86. {
  87. var strMid = "";
  88. switch (tree.type)
  89. {
  90. case "=":
  91. return jsExpr(tree.left, strBasePath) + "==" + jsExpr(tree.right, strBasePath);
  92.  
  93. case ">":
  94. case "<":
  95. case ">=":
  96. case "<=":
  97. return jsExpr(tree.left, strBasePath) + tree.type + jsExpr(tree.right, strBasePath);
  98.  
  99. case "!=":
  100. case "<>":
  101. return jsExpr(tree.left, strBasePath) + "!=" + jsExpr(tree.right, strBasePath);
  102.  
  103. case "isNull":
  104. return jsExpr(tree.left, strBasePath) + "==''";
  105.  
  106. case "isNotNull":
  107. return jsExpr(tree.left, strBasePath) + "!=''";
  108.  
  109. case "int":
  110. return tree.left;
  111.  
  112. case "str":
  113. return "'" + tree.left + "'";
  114.  
  115. case "xpath":
  116. return jsXpathExpr(tree, strBasePath);
  117.  
  118. case "datetime":
  119. return "new Date(" + tree.left.substr(0,4) + "," + (parseInt(tree.left.substr(5,2), 10) - 1) + "," + tree.left.substr(8,2) + "," +
  120. tree.left.substr(11,2) + "," + tree.left.substr(14,2) + "," + tree.left.substr(17,2) + "," +
  121. tree.left.substr(20,3) + ")";
  122.  
  123. case "date":
  124. return "new Date(" + tree.left.substr(0,4) + "," + (parseInt(tree.left.substr(5,2), 10) - 1) + "," + tree.left.substr(8,2) + ")";
  125.  
  126. default:
  127. throw new JsExprException(tree, strBasePath, "Feature not supported: " + tree.type);
  128. }
  129. }
  130.  
  131. //--------------------------------------------------------------------------
  132. // Translate an Xpath to a Javascript expression
  133. // e.g. @firstName -> recipient.firstName
  134. //--------------------------------------------------------------------------
  135. function jsXpathExpr(tree, strBasePath)
  136. {
  137. if (strBasePath == undefined)
  138. var strBase = "";
  139. else
  140. strBase = strBasePath + ".";
  141.  
  142. if (typeof tree == "undefined" || tree === null)
  143. return null;
  144.  
  145. var strLeft = strBase + tree.left.replace(/^@/, "").replace(/-/g, "_");
  146.  
  147. if (typeof tree.right == "string")
  148. return strLeft + "." + tree.right.replace(/^@/, "").replace(/-/g, "_");
  149.  
  150. var strRight = jsXpathExpr(tree.right, strBasePath);
  151. if (strRight === null)
  152. return strLeft;
  153.  
  154. return strLeft + "." + strRight;
  155. }
  156.  
  157. //--------------------------------------------------------------------------
  158. // Exception for JS expression generation
  159. //--------------------------------------------------------------------------
  160. function JsExprException(tree, strBasePath, strMsg)
  161. {
  162. this.tree = tree;
  163. this.strBasePath = strBasePath;
  164. this.strMsg = strMsg;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement