Search in sources :

Example 91 with TMLExpressionException

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

the class Date method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    String arg = (String) this.getOperands().get(0);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
    java.util.Date date = null;
    try {
        date = format.parse(arg);
    } catch (Exception e) {
        throw new TMLExpressionException("Invalid date format: " + arg);
    }
    return date;
}
Also used : TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 92 with TMLExpressionException

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

the class DateSubtract method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (this.getOperands().size() != 2)
        throw new TMLExpressionException("DateSubtract(Date1, Date2) expected. Wrong no. of args.");
    java.util.Date date1 = (java.util.Date) this.getOperands().get(0);
    java.util.Date date2 = (java.util.Date) this.getOperands().get(1);
    if (date1 == null || date2 == null) {
        return null;
    }
    long diff = date1.getTime() - date2.getTime();
    int hours = (int) (diff / 3600000);
    int millis = (int) (diff % 3600000);
    Calendar cc = Calendar.getInstance();
    cc.clear();
    cc.add(Calendar.HOUR_OF_DAY, hours);
    cc.add(Calendar.MILLISECOND, millis);
    return cc.getTime();
}
Also used : Calendar(java.util.Calendar) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 93 with TMLExpressionException

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

the class DayOfWeek method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object o = this.getOperand(0);
    Object f = this.getOperand(1);
    if (!(o instanceof Integer))
        throw new TMLExpressionException("Invalid operand type, Integer expected");
    if (!(f instanceof Boolean))
        throw new TMLExpressionException("Invalid operand type, Boolean expected");
    Integer weekday = (Integer) o;
    Boolean past = (Boolean) f;
    Calendar today = Calendar.getInstance();
    if (today.get(Calendar.DAY_OF_WEEK) == weekday.intValue())
        return reset(today, past.booleanValue());
    int factor = (past.booleanValue() ? -1 : 1);
    for (int i = 1; i <= 8; i++) {
        today.add(Calendar.DAY_OF_WEEK, factor);
        if (today.get(Calendar.DAY_OF_WEEK) == weekday.intValue()) {
            return reset(today, past.booleanValue());
        }
    }
    return null;
}
Also used : Calendar(java.util.Calendar) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 94 with TMLExpressionException

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

the class FormatDecimal method evaluate.

/* (non-Javadoc)
     * @see com.dexels.navajo.parser.FunctionInterface#evaluate()
     */
@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() != 2) {
        throw new TMLExpressionException(this, "Invalid number of arguments");
    }
    Object o1 = getOperand(0);
    Object o2 = getOperand(1);
    if (!(o2 instanceof String)) {
        throw new TMLExpressionException(this, "Invalid argument: " + o2);
    }
    DecimalFormat df = new DecimalFormat(o2.toString());
    return df.format(o1);
}
Also used : DecimalFormat(java.text.DecimalFormat) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 95 with TMLExpressionException

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

the class FormatStringList method evaluate.

@Override
@SuppressWarnings("rawtypes")
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object a = this.getOperands().get(0);
    Object b = this.getOperands().get(1);
    if (a instanceof String)
        return a;
    if (!(a instanceof List))
        throw new TMLExpressionException("FormatStringList: invalid operand: " + a.getClass().getName());
    if (!(b instanceof String))
        throw new TMLExpressionException("FormatStringList: invalid operand: " + a.getClass().getName());
    List strings = (List) a;
    String sep = (String) b;
    StringBuffer result = new StringBuffer(20 * strings.size());
    for (int i = 0; i < strings.size(); i++) {
        String el = (String) strings.get(i);
        result.append(el);
        if (i < (strings.size() - 1))
            result.append(sep);
    }
    return result.toString();
}
Also used : List(java.util.List) 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