Search in sources :

Example 41 with TMLExpressionException

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

the class SetMimeType method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    // application/vnd.ms-excel
    Object o = getOperand(0);
    if (!(o instanceof Binary)) {
        throw new TMLExpressionException(this, "Mime type can only be set for binaries.");
    }
    Object mt = getOperand(1);
    if (!(mt instanceof String)) {
        throw new TMLExpressionException(this, "Mime type should be string expression.");
    }
    Binary b = (Binary) o;
    b.setMimeType((String) mt);
    return b;
}
Also used : Binary(com.dexels.navajo.document.types.Binary) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 42 with TMLExpressionException

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

the class StringFunction method evaluate.

/**
 * NOTE: THIS FUNCTION DOES NOT SUPPORT STRING METHODS THAT CONTAIN
 * PRIMITIVE TYPE ARGUMENTS LIKE int, boolean float, etc. EXAMPLE OF A NOT
 * SUPPORTED METHOD IS: substring(int).
 *
 * @return
 * @throws TMLExpressionException
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    String methodName = (String) getOperand(0);
    if (methodName == null) {
        throw new TMLExpressionException("Could not evaluate StringFunction because method name is null");
    }
    String object = (String) getOperand(1);
    if (object == null) {
        throw new TMLExpressionException("Could not evaluate StringFunction because string is null");
    }
    ArrayList parameters = new ArrayList();
    for (int i = 2; i < getOperands().size(); i++) {
        parameters.add(getOperand(i));
    }
    // logger.warn("String function: " + methodName + " object: " + object);
    // logger.warn("Params: " + parameters);
    // boolean containsInteger = false;
    Class[] classTypes = null;
    if (parameters.size() > 0) {
        classTypes = new Class[parameters.size()];
        for (int i = 0; i < parameters.size(); i++) {
            Class c = parameters.get(i).getClass();
            // System.err.println(i + " c = " + c.getName());
            if (c.getName().equals("java.lang.Integer")) {
                c = java.lang.Integer.TYPE;
            }
            if (c.getName().equals("java.lang.Character")) {
                c = java.lang.Character.TYPE;
            }
            classTypes[i] = c;
        }
    }
    Object returnValue = null;
    try {
        // fix for contains method (see issue #135 GitHub).
        if ("contains".equals(methodName)) {
            // replace java.lang.String for CharSequence...
            classTypes = new Class[1];
            classTypes[0] = java.lang.CharSequence.class;
        }
        Method m = java.lang.String.class.getMethod(methodName, classTypes);
        if (m == null && classTypes != null) {
            String parameterList = "";
            if (parameters.size() > 0) {
                parameterList = parameters.get(0) + " (" + classTypes[0] + ")";
            }
            for (int i = 1; i < parameters.size(); i++) {
                parameterList += ", " + parameters.get(i) + "(" + classTypes[i] + ")";
            }
            throw new TMLExpressionException("Could not evaluate: " + object + "." + methodName + "(" + parameterList + ")");
        }
        if (m == null) {
            throw new TMLExpressionException("No method found: " + methodName);
        }
        if (parameters.size() > 0) {
            returnValue = m.invoke(object, parameters.toArray());
        } else {
            returnValue = m.invoke(object, (Object[]) null);
        }
    } catch (Exception e) {
        throw new TMLExpressionException(e.getMessage(), e);
    }
    return returnValue;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 43 with TMLExpressionException

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

the class StripTime method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object o = getOperand(0);
    java.util.Date date = null;
    if (o instanceof java.util.Date) {
        date = (java.util.Date) o;
    } else if (o instanceof String) {
        String format = (String) this.getOperands().get(1);
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format);
        try {
            date = formatter.parse((String) o);
        } catch (ParseException ex) {
            throw new TMLExpressionException(this, "Invalid date format: " + (String) o + " for given date pattern: " + format);
        }
    } else {
        throw new TMLExpressionException("Invalid date: " + o);
    }
    if (date != null) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // Set time fields to zero
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        date = cal.getTime();
    }
    return date;
}
Also used : Calendar(java.util.Calendar) ParseException(java.text.ParseException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 44 with TMLExpressionException

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

the class SumExpressions method evaluate.

/* (non-Javadoc)
	 * @see com.dexels.navajo.parser.FunctionInterface#evaluate()
	 */
@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            System.err.println("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(getOperand(0) instanceof String && getOperand(1) instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + getOperand(0).getClass() + " and " + getOperand(1).getClass());
    }
    String messageName = (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 {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        String sumType = "int";
        double sum = 0;
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                Operand o = Expression.evaluate(expression, m.getRootDoc(), null, m);
                if (o.value == null) {
                    throw new TMLExpressionException(this, "Null value encountered");
                }
                if (o.value instanceof Integer) {
                    sum += ((Integer) o.value).doubleValue();
                } else if (o.value instanceof Double) {
                    sum += ((Double) o.value).doubleValue();
                } else {
                    throw new TMLExpressionException(this, "Incompatible type while summing: " + o.value.getClass().getName());
                }
            }
        }
        if (sumType.equals("int")) {
            return Integer.valueOf((int) sum);
        } else if (sumType.equals("money")) {
            return new Money(sum);
        } else if (sumType.equals("percentage")) {
            return new Percentage(sum);
        } else {
            return Double.valueOf(sum);
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) Percentage(com.dexels.navajo.document.types.Percentage) 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) Money(com.dexels.navajo.document.types.Money) SystemException(com.dexels.navajo.script.api.SystemException)

Example 45 with TMLExpressionException

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

the class SumProperties method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            logger.info("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(getOperand(0) instanceof String && getOperand(1) instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + getOperand(0).getClass() + " and " + getOperand(1).getClass());
    }
    String messageName = (String) getOperand(0);
    String propertyName = (String) getOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = (String) getOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    try {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        String sumType = "int";
        double sum = 0;
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            Property p = m.getProperty(propertyName);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                if (p != null) {
                    Object o = p.getTypedValue();
                    if (o == null) {
                        continue;
                    }
                    if (!(o instanceof Integer || o instanceof Double || o instanceof Float || o instanceof Money || o instanceof Percentage || o instanceof Boolean || o instanceof String)) {
                        throw new TMLExpressionException(this, "Only numbers are supported a sum. Not: " + (o.getClass().toString()) + " value: " + o);
                    }
                    if (o instanceof String) {
                        if ("".equals(o)) {
                        // ignore
                        } else {
                            logger.error("Only numbers are supported a sum. Not strings. Value:  " + o);
                            throw new TMLExpressionException(this, "Only numbers are supported a sum. Not strings. Value:  " + o + (o.getClass().toString()));
                        }
                    }
                    if (o instanceof Integer) {
                        sumType = "int";
                        sum += ((Integer) o).doubleValue();
                    } else if (o instanceof Double) {
                        // if (!((Double)o).equals(Double.valueOf(Double.NaN))) {
                        sumType = "float";
                        sum += ((Double) o).doubleValue();
                    // }
                    } else if (o instanceof Float) {
                        // if (!((Float)o).equals(new Float(Float.NaN))) {
                        sumType = "float";
                        sum += ((Float) o).doubleValue();
                    // }
                    } else if (o instanceof Money) {
                        // if (!Double.valueOf(((Money)o).doubleValue()).equals(Double.valueOf(Float.NaN))) {
                        sumType = "money";
                        sum += ((Money) o).doubleValue();
                    // }
                    } else if (o instanceof Percentage) {
                        // if (!Double.valueOf(((Money)o).doubleValue()).equals(Double.valueOf(Float.NaN))) {
                        sumType = "percentage";
                        sum += ((Percentage) o).doubleValue();
                    // }
                    } else if (o instanceof Boolean) {
                        sumType = "int";
                        sum += ((Boolean) o).booleanValue() ? 1 : 0;
                    }
                } else {
                    throw new TMLExpressionException(this, "Property does not exist: " + propertyName);
                }
            }
        }
        if (sumType.equals("int")) {
            return Integer.valueOf((int) sum);
        } else if (sumType.equals("money")) {
            return new Money(sum);
        } else if (sumType.equals("percentage")) {
            return new Percentage(sum);
        } else {
            return Double.valueOf(sum);
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) Percentage(com.dexels.navajo.document.types.Percentage) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Money(com.dexels.navajo.document.types.Money) SystemException(com.dexels.navajo.script.api.SystemException) Property(com.dexels.navajo.document.Property)

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