use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class Expression method match.
public static final Message match(String matchString, Navajo inMessage, MappableTreeNode o, Message parent) throws SystemException {
try {
StringTokenizer tokens = new StringTokenizer(matchString, ";");
String matchSet = tokens.nextToken();
if (matchSet == null)
throw new TMLExpressionException("Invalid usage of match: match=\"[match set];[match value]\"");
String matchValue = tokens.nextToken();
if (matchValue == null)
throw new TMLExpressionException("Invalid usage of match: match=\"[match set];[match value]\"");
Operand value = evaluate(matchValue, inMessage, o, parent, null, null, null, null);
List<Property> properties;
if (parent == null)
properties = inMessage.getProperties(matchSet);
else
properties = parent.getProperties(matchSet);
for (int i = 0; i < properties.size(); i++) {
Property prop = properties.get(i);
Message parentMsg = prop.getParentMessage();
if (prop.getValue().equals(value.value))
return parentMsg;
}
} catch (NavajoException e) {
throw new SystemException(-1, e.getMessage(), e);
}
return null;
}
use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class Exists method evaluate.
@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
/**
*@todo Implement this com.dexels.navajo.parser.FunctionInterface abstract method
*/
Message arrayMessage = null;
String messagePath = null;
if (getOperand(0) instanceof Message) {
arrayMessage = (Message) getOperand(0);
} else {
messagePath = (String) getOperand(0);
}
String expression = (String) getOperand(1);
String filter = null;
if (getOperands().size() > 2) {
filter = (String) getOperand(2);
}
Message parent = getCurrentMessage();
Navajo doc = getNavajo();
// try {
try {
List<Message> arrayMsg = null;
if (arrayMessage != null) {
arrayMsg = arrayMessage.getAllMessages();
} else {
arrayMsg = (parent != null ? parent.getMessages(messagePath) : doc.getMessages(messagePath));
}
if (arrayMsg == null) {
throw new TMLExpressionException(this, "Empty or non existing array message: " + messagePath);
}
for (int i = 0; i < arrayMsg.size(); i++) {
Message current = arrayMsg.get(i);
try {
boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, current, getAccess()) : true);
if (evaluate) {
Operand result = Expression.evaluate(expression, doc, null, current);
if (result == null) {
throw new TMLExpressionException(this, "Error evaluating expression: " + expression + " in message: " + current.getFullMessageName());
}
if (result.value == null) {
throw new TMLExpressionException(this, "Error evaluating expression: " + expression + " in message: " + current.getFullMessageName());
}
String res2 = "" + result.value;
Operand result2 = Expression.evaluate(res2, doc, null, current);
if (result2 == null) {
throw new TMLExpressionException(this, "Error evaluating expression: " + res2 + " in message: " + current.getFullMessageName());
}
if (result2.value == null) {
throw new TMLExpressionException(this, "Error evaluating expression: " + res2 + " in message: " + current.getFullMessageName());
}
boolean res = ((Boolean) result2.value).booleanValue();
if (res) {
return Boolean.TRUE;
}
}
} catch (SystemException ex) {
logger.error("Error: ", ex);
}
}
} catch (NavajoException ex) {
throw new TMLExpressionException(this, "Error evaluating message path");
}
return Boolean.FALSE;
}
use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class ForAll method evaluate.
@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
/**
*@todo Implement this com.dexels.navajo.parser.FunctionInterface abstract method
*/
Message arrayMessage = null;
String messagePath = null;
if (getOperand(0) instanceof Message) {
arrayMessage = (Message) getOperand(0);
} else {
messagePath = (String) getOperand(0);
}
String expression = (String) getOperand(1);
String filter = null;
if (getOperands().size() > 2) {
filter = (String) getOperand(2);
}
Message parent = getCurrentMessage();
Navajo doc = getNavajo();
// try {
try {
List<Message> arrayMsg = null;
if (arrayMessage != null) {
arrayMsg = arrayMessage.getAllMessages();
} else {
arrayMsg = (parent != null ? parent.getMessages(messagePath) : doc.getMessages(messagePath));
}
if (arrayMsg == null) {
throw new TMLExpressionException(this, "Empty or non existing array message: " + messagePath);
}
for (int i = 0; i < arrayMsg.size(); i++) {
Message current = arrayMsg.get(i);
try {
boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, current, getAccess()) : true);
if (evaluate) {
Operand result = Expression.evaluate(expression, doc, null, current);
if (result == null) {
throw new TMLExpressionException(this, "Error evaluating expression: " + expression + " in message: " + current.getFullMessageName());
}
if (result.value == null) {
throw new TMLExpressionException(this, "Error evaluating expression: " + expression + " in message: " + current.getFullMessageName());
}
String res2 = "" + result.value;
Operand result2 = Expression.evaluate(res2, doc, null, current);
if (result2 == null) {
throw new TMLExpressionException(this, "Error evaluating expression: " + res2 + " in message: " + current.getFullMessageName());
}
if (result2.value == null) {
throw new TMLExpressionException(this, "Error evaluating expression: " + res2 + " in message: " + current.getFullMessageName());
}
boolean res = ((Boolean) result2.value).booleanValue();
if (!res) {
return Boolean.FALSE;
}
}
} catch (SystemException ex) {
logger.error("Error: ", ex);
}
}
} catch (NavajoException ex) {
throw new TMLExpressionException(this, "Error evaluating message path");
}
return Boolean.TRUE;
}
use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class TslCompiler method optimizeExpresssion.
/**
* VERY NASTY METHOD. IT TRIES ALL KINDS OF TRICKS TO TRY TO AVOID CALLING
* THE EXPRESSION.EVALUATE() METHOD IN THE GENERATED JAVA.
*
* @param ident
* @param clause
* @param className
* @return
*/
public String optimizeExpresssion(int ident, String clause, String className, String objectName) throws UserException {
boolean exact = false;
StringBuilder result = new StringBuilder();
char firstChar = ' ';
boolean functionCall = false;
StringBuilder functionNameBuffer = new StringBuilder();
String functionName = "";
String call = "";
// attribute call.
for (int i = 0; i < clause.length(); i++) {
char c = clause.charAt(i);
if (c != ' ' && firstChar == ' ') {
firstChar = c;
}
if (((firstChar > 'a' && firstChar < 'z')) || ((firstChar > 'A') && (firstChar < 'Z'))) {
functionCall = true;
}
if ((functionCall) && (c != '(')) {
functionNameBuffer.append(c);
} else if (functionCall && c == '(') {
functionName = functionNameBuffer.toString();
functionNameBuffer = new StringBuilder();
}
if (c == '$') {
// New attribute found
StringBuilder name = new StringBuilder();
i++;
c = clause.charAt(i);
while (c != '(' && i < clause.length() && c != ')') {
name.append(c);
i++;
if (i < clause.length()) {
c = clause.charAt(i);
}
}
if (name.toString().contains("..")) {
// We cannot optimize these yet
continue;
}
i++;
StringBuilder params = new StringBuilder();
if (clause.indexOf("(") != -1) {
// Determine parameters.
int endOfParams = 1;
while (endOfParams > 0 && i < clause.length()) {
c = clause.charAt(i);
if (c == '(') {
endOfParams++;
} else if (c == ')') {
endOfParams--;
} else {
params.append(c);
}
i++;
}
}
String expr = "";
if (functionName.equals("")) {
expr = (params.toString().length() > 0 ? "$" + name + "(" + params + ")" : "$" + name);
} else {
expr = functionName + "(" + (params.toString().length() > 0 ? "$" + name + "(" + params + ")" : "$" + name) + ")";
}
if (removeWhiteSpaces(expr).equals(removeWhiteSpaces(clause))) {
// Let's evaluate this directly.
exact = true;
Class expressionContextClass = null;
try {
StringBuilder objectizedParams = new StringBuilder();
StringTokenizer allParams = new StringTokenizer(params.toString(), ",");
while (allParams.hasMoreElements()) {
String param = allParams.nextToken();
// Try to evaluate expression (NOTE THAT IF
// REFERENCES ARE MADE TO EITHER NAVAJO OR MAPPABLE
// OBJECTS THIS WILL FAIL
// SINCE THESE OBJECTS ARE NOT KNOWN AT COMPILE
// TIME!!!!!!!!!!!!!!1
Operand op = Expression.evaluate(param, null);
Object v = op.value;
if (v instanceof String) {
objectizedParams.append("\"" + v + "\"");
} else if (v instanceof Integer) {
objectizedParams.append("new Integer(" + v + ")");
} else if (v instanceof Long) {
objectizedParams.append("Long.valueOf(" + v + ")");
} else if (v instanceof Float) {
objectizedParams.append("new Float(" + v + ")");
} else if (v instanceof Boolean) {
objectizedParams.append("new Boolean(" + v + ")");
} else if (v instanceof Double) {
objectizedParams.append("Double.valueOf(" + v + ")");
} else if (v == null) {
// Null support
objectizedParams.append(v);
} else {
throw new UserException(-1, "Unknown type encountered during compile time: " + v.getClass().getName() + " @clause: " + clause);
}
if (allParams.hasMoreElements()) {
objectizedParams.append(',');
}
}
try {
expressionContextClass = Class.forName(className, false, loader);
} catch (Exception e) {
throw new Exception("Could not find adapter: " + className);
}
String attrType = MappingUtils.getFieldType(expressionContextClass, name.toString());
// Try to locate class:
if (!functionName.equals("")) {
try {
Class.forName("com.dexels.navajo.functions." + functionName, false, loader);
} catch (Exception e) {
throw new Exception("Could not find Navajo function: " + functionName);
}
}
call = objectName + ".get" + (name.charAt(0) + "").toUpperCase() + name.substring(1) + "(" + objectizedParams.toString() + ")";
if (attrType.equals("int")) {
call = "new Integer(" + call + ")";
} else if (attrType.equals("float") || attrType.equals("double")) {
call = "Double.valueOf(" + call + ")";
} else if (attrType.equals("boolean")) {
call = "new Boolean(" + call + ")";
} else if (attrType.equals("long")) {
call = "Long.valueOf(" + call + ")";
}
} catch (ClassNotFoundException cnfe) {
if (expressionContextClass == null) {
throw new UserException(-1, "Error in script: Could not find adapter: " + className + " @clause: " + clause);
} else {
throw new UserException(-1, "Error in script: Could not locate function: " + functionName + " @ clause: " + clause);
}
} catch (Throwable e) {
exact = false;
}
}
}
}
// Try to evaluate clause directly (compile time).
if ((!exact) && !clause.equals("TODAY") && !clause.equals("null") && (clause.indexOf("[") == -1) && (clause.indexOf("$") == -1) && (clause.indexOf("(") == -1) && (clause.indexOf("+") == -1)) {
try {
Operand op = Expression.evaluate(clause, null);
Object v = op.value;
exact = true;
if (v instanceof String) {
call = replaceQuotesValue((String) v);
} else if (v instanceof Integer) {
call = "new Integer(" + v + ")";
} else if (v instanceof Long) {
call = "Long.valueOf(" + v + ")";
} else if (v instanceof Float) {
call = "new Float(" + v + ")";
} else if (v instanceof Boolean) {
call = "new Boolean(" + v + ")";
} else if (v instanceof Double) {
call = "Double.valueOf(" + v + ")";
} else
throw new UserException(-1, "Unknown type encountered during compile time: " + v.getClass().getName() + " @clause: " + clause);
} catch (NullPointerException | TMLExpressionException ne) {
exact = false;
} catch (SystemException se) {
exact = false;
if (clause.length() == 0 || clause.charAt(0) != '#') {
throw new UserException(-1, "Could not compile script, Invalid expression: " + clause);
}
} catch (Throwable e) {
exact = false;
}
}
if (!exact && clause.equals("null")) {
call = "null";
exact = true;
}
// Use Expression.evaluate() if expression could not be executed in an
// optimized way.
result.append(printIdent(ident) + "op = Expression.evaluate(" + replaceQuotes(clause) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg, currentSelection, null, getEvaluationParams());\n");
result.append(printIdent(ident) + "sValue = op.value;\n");
return result.toString();
}
use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.
the class CompiledScript method checkValidationRules.
/**
* Deprecated method to check validation errors. Use <validations> block
* inside webservice script instead.
*
* @param conditions
* @param inMessage
* @param outMessage
* @return
* @throws NavajoException
* @throws SystemException
* @throws UserException
*/
private final Message[] checkValidationRules(ConditionData[] conditions, Navajo inMessage, Navajo outMessage, Access a) throws SystemException, UserException {
if (conditions == null) {
return null;
}
List<Message> messages = new ArrayList<>();
int index = 0;
for (int i = 0; i < conditions.length; i++) {
ConditionData condition = conditions[i];
boolean valid = false;
try {
valid = com.dexels.navajo.parser.Condition.evaluate(condition.condition, inMessage, a);
} catch (com.dexels.navajo.expression.api.TMLExpressionException ee) {
throw new UserException(-1, "Invalid condition: " + ee.getMessage(), ee);
}
if (!valid) {
String eval = com.dexels.navajo.parser.Expression.replacePropertyValues(condition.condition, inMessage);
Message msg = NavajoFactory.getInstance().createMessage(outMessage, "failed" + (index++));
Property prop0 = NavajoFactory.getInstance().createProperty(outMessage, "Id", Property.STRING_PROPERTY, condition.id + "", 0, "", Property.DIR_OUT);
// Evaluate comment as expression.
String description = "";
try {
Operand o = Expression.evaluate(condition.comment, inMessage);
description = o.value + "";
} catch (Exception e) {
description = condition.comment;
}
Property prop1 = NavajoFactory.getInstance().createProperty(outMessage, "Description", Property.STRING_PROPERTY, description, 0, "", Property.DIR_OUT);
msg.addProperty(prop0);
msg.addProperty(prop1);
if (System.getenv("DEBUG_SCRIPTS") != null && System.getenv("DEBUG_SCRIPTS").equals("true")) {
Property prop2 = NavajoFactory.getInstance().createProperty(outMessage, "FailedExpression", Property.STRING_PROPERTY, condition.condition, 0, "", Property.DIR_OUT);
Property prop3 = NavajoFactory.getInstance().createProperty(outMessage, "EvaluatedExpression", Property.STRING_PROPERTY, eval, 0, "", Property.DIR_OUT);
msg.addProperty(prop2);
msg.addProperty(prop3);
}
messages.add(msg);
}
}
if (!messages.isEmpty()) {
Message[] msgArray = new Message[messages.size()];
messages.toArray(msgArray);
return msgArray;
} else {
return null;
}
}
Aggregations