Search in sources :

Example 16 with SimpleScalar

use of freemarker.template.SimpleScalar in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatSingleKeyWithMissingValueReturnsDefaultKey.

@Test
public void testThatSingleKeyWithMissingValueReturnsDefaultKey() throws Exception {
    Optional<Result> resultOptional = Optional.of(result);
    Mockito.when(messages.get("my.message.key", context, resultOptional)).thenReturn(Optional.<String>empty());
    List args = new ArrayList();
    args.add(new SimpleScalar("my.message.key"));
    TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
    assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("my.message.key"));
    // There must have been logged something because we did not find
    // the value for the key...
    Mockito.verify(mockAppender).doAppend(Matchers.anyObject());
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TemplateModel(freemarker.template.TemplateModel) SimpleScalar(freemarker.template.SimpleScalar) Result(ninja.Result) Test(org.junit.Test)

Example 17 with SimpleScalar

use of freemarker.template.SimpleScalar in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatKeyWithPlaceholderWorks.

@Test
public void testThatKeyWithPlaceholderWorks() throws Exception {
    Optional<Result> resultOptional = Optional.of(result);
    List args = new ArrayList();
    args.add(new SimpleScalar("my.message.key"));
    args.add(new SimpleScalar("1000"));
    Mockito.when(messages.get(Matchers.eq("my.message.key"), Matchers.eq(context), Matchers.eq(resultOptional), Matchers.any(Object.class))).thenReturn(Optional.of("This simulates the translated message number 1000!"));
    TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
    assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("This simulates the translated message number 1000!"));
    Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject());
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TemplateModel(freemarker.template.TemplateModel) SimpleScalar(freemarker.template.SimpleScalar) Result(ninja.Result) Test(org.junit.Test)

Example 18 with SimpleScalar

use of freemarker.template.SimpleScalar in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethod method exec.

public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() == 1 && args.get(0) instanceof StringModel && ((StringModel) args.get(0)).getWrappedObject() instanceof ConstraintViolation) {
        ConstraintViolation violation = (ConstraintViolation) ((StringModel) args.get(0)).getWrappedObject();
        String messageValue = messages.get(violation.getMessageKey(), context, result, violation.getMessageParams()).orElse(violation.getDefaultMessage());
        return new SimpleScalar(messageValue);
    } else if (args.size() == 1) {
        String messageKey = ((SimpleScalar) args.get(0)).getAsString();
        String messageValue = messages.get(messageKey, context, result).orElse(messageKey);
        logIfMessageKeyIsMissing(messageKey, messageValue);
        return new SimpleScalar(messageValue);
    } else if (args.size() > 1) {
        List<String> strings = Lists.newArrayList();
        for (Object o : args) {
            // We currently allow only numbers and strings as arguments
            if (o instanceof SimpleScalar) {
                strings.add(((SimpleScalar) o).getAsString());
            } else if (o instanceof SimpleNumber) {
                strings.add(((SimpleNumber) o).toString());
            }
        }
        String messageKey = strings.get(0);
        String messageValue = messages.get(messageKey, context, result, strings.subList(1, strings.size()).toArray()).orElse(messageKey);
        logIfMessageKeyIsMissing(messageKey, messageValue);
        return new SimpleScalar(messageValue);
    } else {
        throw new TemplateModelException("Using i18n without any key is not possible.");
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) ConstraintViolation(ninja.validation.ConstraintViolation) StringModel(freemarker.ext.beans.StringModel) SimpleScalar(freemarker.template.SimpleScalar)

Example 19 with SimpleScalar

use of freemarker.template.SimpleScalar in project ninja by ninjaframework.

the class TemplateEngineFreemarkerReverseRouteHelper method computeReverseRoute.

public TemplateModel computeReverseRoute(List args) throws TemplateModelException {
    if (args.size() < 2) {
        throw new TemplateModelException("Please specify at least classname and controller (2 parameters).");
    } else {
        List<String> strings = new ArrayList<>(args.size());
        for (Object o : args) {
            // We currently allow only numbers and strings as arguments
            if (o instanceof String) {
                strings.add((String) o);
            }
            if (o instanceof SimpleScalar) {
                strings.add(((SimpleScalar) o).getAsString());
            } else if (o instanceof SimpleNumber) {
                strings.add(((SimpleNumber) o).toString());
            }
        }
        try {
            Class<?> clazz = Class.forName(strings.get(0));
            Object[] parameterMap = strings.subList(2, strings.size()).toArray();
            String reverseRoute = router.getReverseRoute(clazz, strings.get(1), parameterMap);
            return new SimpleScalar(reverseRoute);
        } catch (ClassNotFoundException ex) {
            throw new TemplateModelException("Error. Cannot find class for String: " + strings.get(0));
        }
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) ArrayList(java.util.ArrayList) SimpleScalar(freemarker.template.SimpleScalar)

Example 20 with SimpleScalar

use of freemarker.template.SimpleScalar in project android by JetBrains.

the class FmEscapePropertyValueMethodTest method check.

@SuppressWarnings("rawtypes")
private static void check(String expected, String s) throws TemplateModelException {
    FmEscapePropertyValueMethod method = new FmEscapePropertyValueMethod();
    List list = Collections.singletonList(new SimpleScalar(s));
    assertEquals(expected, ((SimpleScalar) method.exec(list)).getAsString());
}
Also used : List(java.util.List) SimpleScalar(freemarker.template.SimpleScalar)

Aggregations

SimpleScalar (freemarker.template.SimpleScalar)23 List (java.util.List)17 SimpleNumber (freemarker.template.SimpleNumber)5 ArrayList (java.util.ArrayList)5 TemplateModel (freemarker.template.TemplateModel)4 Result (ninja.Result)4 Test (org.junit.Test)4 TemplateModelException (freemarker.template.TemplateModelException)3 StringModel (freemarker.ext.beans.StringModel)2 SimpleDate (freemarker.template.SimpleDate)2 ContainerUtil.newArrayList (com.intellij.util.containers.ContainerUtil.newArrayList)1 Template (freemarker.template.Template)1 Date (java.util.Date)1 ConstraintViolation (ninja.validation.ConstraintViolation)1