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;
}
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;
}
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;
}
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());
}
}
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());
}
}
Aggregations