Search in sources :

Example 31 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class GetContextResource method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    java.io.File contextRoot = DispatcherFactory.getInstance().getNavajoConfig().getContextRoot();
    java.io.File res = new java.io.File(contextRoot, "resources");
    // input (ArrayList, Object).
    if (this.getOperands().size() != 1)
        throw new TMLExpressionException("GetContextResource(filename) expected");
    Object a = this.getOperands().get(0);
    if (!(a instanceof String)) {
        throw new TMLExpressionException("GetContextResource(filename) expected");
    }
    java.io.File result = new java.io.File(res, (String) a);
    // Security might be compromised if supplied with path like: ../../../root/somethingsecret
    try {
        return new Binary(result);
    } catch (IOException e) {
        throw new TMLExpressionException("Error constructing binary", e);
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 32 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException 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;
}
Also used : Message(com.dexels.navajo.document.Message) SystemException(com.dexels.navajo.script.api.SystemException) Operand(com.dexels.navajo.document.Operand) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 33 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class FindElement method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Message in = null;
    String propertyName = (String) getOperand(0);
    Object propertyValue = getOperand(1);
    if (getOperands().size() == 2) {
        in = getCurrentMessage();
        if (in == null) {
            throw new TMLExpressionException("Can not FindElement: No supplied message and no currentMessage");
        }
    } else {
        in = (Message) getOperand(2);
    }
    if (!Message.MSG_TYPE_ARRAY.equals(in.getType())) {
        in = in.getArrayParentMessage();
    }
    if (in == null) {
        throw new TMLExpressionException("Can not FindElement: Supplied message is not an array message or array element");
    }
    String type = in.getType();
    if (!Message.MSG_TYPE_ARRAY.equals(type)) {
        throw new TMLExpressionException("FindElement resolved message is still no array message (?)");
    }
    for (Message element : in.getAllMessages()) {
        Property p = element.getProperty(propertyName);
        if (p != null) {
            if (propertyValue.equals(p.getTypedValue())) {
                return element;
            }
        }
    }
    logger.debug("No property found in message. Property name: " + propertyName + " value: " + propertyValue);
    return null;
}
Also used : Message(com.dexels.navajo.document.Message) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 34 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class GetPropertyAttribute method getAttribute.

public Object getAttribute(Object operand, String attribute) throws com.dexels.navajo.expression.api.TMLExpressionException {
    Property p = null;
    if (operand instanceof Property) {
        p = (Property) operand;
    } else {
        String propertyName = operand.toString();
        p = (getCurrentMessage() != null ? getCurrentMessage().getProperty(propertyName) : this.getNavajo().getProperty(propertyName));
    }
    if (p == null) {
        throw new TMLExpressionException(this, "Property " + operand.toString() + " not found");
    }
    if (attribute.equals("direction")) {
        return p.getDirection();
    }
    if (attribute.equals("type")) {
        return p.getType();
    }
    if (attribute.equals("cardinality")) {
        return p.getCardinality();
    }
    try {
        throw new TMLExpressionException(this, "attribute " + attribute + " not found for property " + p.getFullPropertyName());
    } catch (NavajoException e) {
        throw new TMLExpressionException(this, "attribute " + attribute + " not found for unknown property ");
    }
}
Also used : NavajoException(com.dexels.navajo.document.NavajoException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 35 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class GetPropertyValue method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() != 2) {
        throw new TMLExpressionException(this, "Invalid function call");
    }
    Object m = operand(0).value;
    ;
    if (m == null) {
        throw new TMLExpressionException(this, "Message or navajo argument expected. This one is null");
    }
    // Object o = getOperand(1);
    // if (!(o instanceof String)) {
    // throw new TMLExpressionException(this, "String argument expected");
    // }
    String propertyName = getStringOperand(1);
    if (!(m instanceof Message) && !(m instanceof Navajo)) {
        throw new TMLExpressionException(this, "Message or navajo argument expected");
    }
    if (m instanceof Navajo) {
        Navajo n = (Navajo) m;
        return n.getProperty(propertyName).getTypedValue();
    }
    if (m instanceof Message) {
        Message message = (Message) m;
        return message.getProperty(propertyName).getTypedValue();
    }
    return null;
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Aggregations

TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)129 Message (com.dexels.navajo.document.Message)26 Navajo (com.dexels.navajo.document.Navajo)26 Binary (com.dexels.navajo.document.types.Binary)23 Property (com.dexels.navajo.document.Property)17 ArrayList (java.util.ArrayList)16 Operand (com.dexels.navajo.document.Operand)15 List (java.util.List)13 NavajoException (com.dexels.navajo.document.NavajoException)12 IOException (java.io.IOException)12 Selection (com.dexels.navajo.document.Selection)11 Access (com.dexels.navajo.script.api.Access)10 Calendar (java.util.Calendar)10 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)9 Test (org.junit.Test)9 Money (com.dexels.navajo.document.types.Money)8 SystemException (com.dexels.navajo.script.api.SystemException)8 StringTokenizer (java.util.StringTokenizer)8 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)7 ClockTime (com.dexels.navajo.document.types.ClockTime)7