Search in sources :

Example 46 with TMLExpressionException

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

the class Trunc method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    java.util.Date date = getDateOperand(0);
    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");
    try {
        date = formatter.parse(formatter.format(date));
    } catch (ParseException ex) {
        throw new TMLExpressionException(this, "Invalid date format: " + date + " for given date pattern: " + "yyyy-MM-dd");
    }
    return date;
}
Also used : ParseException(java.text.ParseException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 47 with TMLExpressionException

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

the class WeekDay method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object o = null;
    try {
        o = getOperand(0);
    } catch (Exception e) {
        o = new java.util.Date();
    }
    java.util.Date day = null;
    if (o == null) {
        // take today
        day = Calendar.getInstance(locale).getTime();
    } else if (o instanceof java.util.Date) {
        day = (java.util.Date) o;
    } else if (o instanceof String) {
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        try {
            day = format.parse((String) o);
        } catch (Exception e) {
            throw new TMLExpressionException("Invalid date format: " + (String) o);
        }
    } else {
        throw new TMLExpressionException("Invalid date: " + o);
    }
    return new SimpleDateFormat("EEE", locale).format(day).toUpperCase();
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) SimpleDateFormat(java.text.SimpleDateFormat) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 48 with TMLExpressionException

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

the class URLEncode method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    String stringToEncode = getStringOperand(0);
    boolean space_as_plus = false;
    if (getOperands().size() > 1) {
        space_as_plus = getBooleanOperand(1);
    }
    String encoded;
    try {
        encoded = URLEncoder.encode(stringToEncode, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new TMLExpressionException(this, "Can not parse URL: " + stringToEncode, e);
    }
    if (!space_as_plus) {
        encoded = encoded.replace("+", "%20");
    }
    return encoded;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 49 with TMLExpressionException

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

the class ToPattern method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    String s = (String) this.getOperands().get(0);
    if (s == null)
        return null;
    int options = 0;
    if (this.getOperands().size() > 1) {
        String soptions = (String) this.getOperands().get(1);
        if (soptions.indexOf('i') > -1) {
            options = options | Pattern.CASE_INSENSITIVE;
        }
    }
    try {
        return Pattern.compile(s, options);
    } catch (PatternSyntaxException e) {
        throw new TMLExpressionException("Invalid regex!");
    }
}
Also used : TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 50 with TMLExpressionException

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

the class Round method evaluate.

@Override
public final Object evaluate() throws TMLExpressionException {
    Object a = getOperands().get(0);
    Object b = getOperands().get(1);
    try {
        Double value = (Double) a;
        Integer digits = (Integer) b;
        value = (int) Math.signum(value) * ((int) (0.5 + Math.abs(value) * Math.pow(10.0, digits))) / Math.pow(10.0, digits);
        return Double.valueOf(value);
    } catch (Exception e) {
        throw new TMLExpressionException(this, "Illegal type specified in Round() function: " + e.getMessage());
    }
}
Also used : TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) 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