org.apache.velocity.tools.generic.ResourceTool Java Examples

The following examples show how to use org.apache.velocity.tools.generic.ResourceTool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GenericToolsTests.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
public @Test void testResourceTool() {
    ResourceTool textTool = (ResourceTool)toolbox.get("text");
    assertNotNull(textTool);

    List<String> keys = textTool.getKeys();
    assertTrue(keys.contains("foo"));
    assertTrue(keys.contains("hello.whoever"));
    assertTrue(keys.contains("world"));

    keys = textTool.get("hello").getKeys();
    assertTrue(keys.contains("whoever"));
    assertFalse(keys.contains("foo"));

    ResourceTool.Key foo = textTool.get("foo");
    assertStringEquals("bar", foo);

    ResourceTool.Key frenchFoo = foo.locale(Locale.FRENCH);
    assertStringEquals("barre", frenchFoo);

    ResourceTool.Key otherFoo = foo.bundle("resources2");
    assertStringEquals("woogie", otherFoo);

    ResourceTool.Key helloWhoever = textTool.get("hello").get("whoever");
    assertStringEquals("Hello {0}!", helloWhoever);

    ResourceTool.Key helloWorld = helloWhoever.insert(textTool.get("world"));
    assertStringEquals("Hello World!", helloWorld);

    ResourceTool.Key halfFrenchHelloWorld = helloWorld.locale(Locale.FRENCH);
    assertStringEquals("Bonjour World!", halfFrenchHelloWorld);

    ResourceTool.Key frenchTool = textTool.locale("fr");
    ResourceTool.Key frenchHelloWorld =
        frenchTool.get("hello.whoever").insert(frenchTool.get("world"));
    assertStringEquals("Bonjour Monde!", frenchHelloWorld);
}
 
Example #2
Source File: ConfigTests.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
protected FactoryConfiguration getBaseConfig()
{
    FactoryConfiguration base = new FactoryConfiguration();

    Data datum = new Data();
        datum.setKey("version");
        datum.setType("number");
        datum.setValue("2.0");
    base.addData(datum);

    ToolboxConfiguration toolbox = new ToolboxConfiguration();
    toolbox.setScope(Scope.REQUEST);
    toolbox.setProperty("locale", Locale.US);
        ToolConfiguration tool = new ToolConfiguration();
            tool.setClass(ResourceTool.class);
        toolbox.addTool(tool);
    base.addToolbox(toolbox);

    toolbox = new ToolboxConfiguration();
    toolbox.setScope(Scope.APPLICATION);
        tool = new ToolConfiguration();
            tool.setKey("calc");
            tool.setClass(MathTool.class);
        toolbox.addTool(tool);

        tool = new ToolConfiguration();
            tool.setClass(NumberTool.class);
            tool.setProperty("locale", Locale.FRENCH);
        toolbox.addTool(tool);
    base.addToolbox(toolbox);

    return base;
}
 
Example #3
Source File: ConfigTests.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
public @Test void testEasyConfig()
{
    EasyFactoryConfiguration easy = new EasyFactoryConfiguration();
    easy.number("version", 2.0);
    easy.toolbox("request")
            .property("locale", Locale.US)
            .tool(ResourceTool.class);
    easy.toolbox("application")
            .tool("calc", MathTool.class)
            .tool(NumberTool.class)
                .property("locale", Locale.FRENCH);

    assertValid(easy);
    assertConfigEquals(getBaseConfig(), easy);
}
 
Example #4
Source File: tools.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
public static FactoryConfiguration getConfiguration()
{
    EasyFactoryConfiguration easy = new EasyFactoryConfiguration();
    easy.number("version", 2.0);
    easy.toolbox("request")
            .property("locale", Locale.US)
            .tool(ResourceTool.class);
    easy.toolbox("application")
            .tool("calc", MathTool.class)
            .tool(NumberTool.class)
                .property("locale", Locale.FRENCH);
    return easy;
}
 
Example #5
Source File: EmailSenderImp.java    From swellrt with Apache License 2.0 3 votes vote down vote up
@Override
public String getTemplateMessage(Template template, String messageBundleName,
    Map<String, Object> params, Locale locale) {

  Map<String, Object> ctx = new HashMap<String, Object>();

  if (locale == null) {
    locale = Locale.getDefault();
  }
  ctx.put("locale", locale);

  ctx.put(ResourceTool.BUNDLES_KEY, getDecoupledBundleName(messageBundleName));

  ctx.put(CustomResourceTool.CLASS_LOADER_KEY, propertyClassloader);

  Context context = manager.createContext(ctx);

  Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();

  while (it.hasNext()) {
    Entry<String, Object> p = it.next();
    context.put(p.getKey(), p.getValue());
  }

  context.put("esc", new EscapeTool());

  StringWriter sw = new StringWriter();

  template.merge(context, sw);

  sw.flush();

  return sw.toString();

}