Search in sources :

Example 6 with AviatorString

use of com.googlecode.aviator.runtime.type.AviatorString in project aviatorscript by killme2008.

the class StringJoinFunction method join.

private AviatorObject join(final Map<String, Object> env, final AviatorObject arg1, final Object target, final String split) {
    Class<?> clazz = target.getClass();
    StringBuilder sb = new StringBuilder(50);
    if (Collection.class.isAssignableFrom(clazz)) {
        boolean wasFirst = true;
        for (Object obj : (Collection<?>) target) {
            wasFirst = append(sb, split, wasFirst, obj);
        }
    } else if (clazz.isArray()) {
        int length = ArrayUtils.getLength(target);
        boolean wasFirst = true;
        for (int i = 0; i < length; i++) {
            Object obj = ArrayUtils.get(target, i);
            wasFirst = append(sb, split, wasFirst, obj);
        }
    } else {
        throw new IllegalArgumentException(arg1.desc(env) + " is not a seq");
    }
    return new AviatorString(sb.toString());
}
Also used : AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Collection(java.util.Collection) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 7 with AviatorString

use of com.googlecode.aviator.runtime.type.AviatorString in project aviatorscript by killme2008.

the class Date2StringFunction method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
    Date date = (Date) arg1.getValue(env);
    String format = FunctionUtils.getStringValue(arg2, env);
    SimpleDateFormat dateFormat = DateFormatCache.getOrCreateDateFormat(format);
    return new AviatorString(dateFormat.format(date));
}
Also used : AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 8 with AviatorString

use of com.googlecode.aviator.runtime.type.AviatorString in project aviatorscript by killme2008.

the class PrintlnFunctionUnitTest method testCall_WithTwoArgument.

@Test
public void testCall_WithTwoArgument() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("out", out);
    this.fun.call(env, new AviatorJavaType("out"), new AviatorString("hello"));
    out.flush();
    out.close();
    String lineSeparator = System.getProperty("line.separator");
    byte[] data = out.toByteArray();
    assertEquals("hello" + lineSeparator, new String(data));
}
Also used : HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 9 with AviatorString

use of com.googlecode.aviator.runtime.type.AviatorString in project aviatorscript by killme2008.

the class String2DateFunctionUnitTest method testCall_NotDate.

@Test(expected = ClassCastException.class)
public void testCall_NotDate() {
    assertEquals("string_to_date", function.getName());
    function.call(null, AviatorRuntimeJavaType.valueOf(1), new AviatorString("yyyyMMdd"));
}
Also used : AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 10 with AviatorString

use of com.googlecode.aviator.runtime.type.AviatorString in project aviatorscript by killme2008.

the class PrintFunctionUnitTest method testCall_WithTwoArgument.

@Test
public void testCall_WithTwoArgument() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("out", out);
    this.fun.call(env, new AviatorJavaType("out"), new AviatorString("hello"));
    out.flush();
    out.close();
    byte[] data = out.toByteArray();
    assertEquals("hello", new String(data));
}
Also used : HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Aggregations

AviatorString (com.googlecode.aviator.runtime.type.AviatorString)33 Test (org.junit.Test)19 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)14 HashMap (java.util.HashMap)11 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)3 Date (java.util.Date)3 List (java.util.List)3 Map (java.util.Map)3 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)2 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)2 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)2 PrintStream (java.io.PrintStream)2 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 ID (cn.devezhao.persist4j.engine.ID)1 AviatorEvaluator (com.googlecode.aviator.AviatorEvaluator)1 AviatorNil (com.googlecode.aviator.runtime.type.AviatorNil)1 AviatorNumber (com.googlecode.aviator.runtime.type.AviatorNumber)1