org.apache.velocity.runtime.parser.node.SimpleNode Java Examples

The following examples show how to use org.apache.velocity.runtime.parser.node.SimpleNode. 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 Project: sunbird-lms-service   Author: project-sunbird   File: OTPService.java    License: MIT License 6 votes vote down vote up
public static String getSmsBody(String templateFile, Map<String, String> smsTemplate) {
  try {
    String sms = getOTPSMSTemplate(templateFile);
    RuntimeServices rs = RuntimeSingleton.getRuntimeServices();
    SimpleNode sn = rs.parse(sms, "Sms Information");
    Template t = new Template();
    t.setRuntimeServices(rs);
    t.setData(sn);
    t.initDocument();
    VelocityContext context = new VelocityContext();
    context.put(JsonKey.OTP, smsTemplate.get(JsonKey.OTP));
    context.put(
        JsonKey.OTP_EXPIRATION_IN_MINUTES, smsTemplate.get(JsonKey.OTP_EXPIRATION_IN_MINUTES));
    context.put(
        JsonKey.INSTALLATION_NAME,
        ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME));

    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    return writer.toString();
  } catch (Exception ex) {
    ProjectLogger.log(
        "OTPService:getSmsBody: Exception occurred with error message = " + ex.getMessage(), ex);
  }
  return "";
}
 
Example #2
Source Project: pippo   Author: pippo-java   File: VelocityTemplateEngine.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void renderString(String templateContent, Map<String, Object> model, Writer writer) {
    // create the velocity context
    VelocityContext context = createVelocityContext(model);

    // merge the template
    try {
        RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
        StringReader reader = new StringReader(templateContent);
        SimpleNode node = runtimeServices.parse(reader, "StringTemplate");
        Template template = new Template();
        template.setRuntimeServices(runtimeServices);
        template.setData(node);
        template.initDocument();
        template.merge(context, writer);
    } catch (Exception e) {
        throw new PippoRuntimeException(e);
    }
}
 
Example #3
Source Project: velocity-engine   Author: apache   File: LogContext.java    License: Apache License 2.0 6 votes vote down vote up
public void pushLogContext(SimpleNode src, Info info)
{
    if (!trackLocation)
    {
        return;
    }
    Deque<StackElement> stack = contextStack.get();
    StackElement last = stack.peek();
    if (last != null && last.src == src)
    {
        ++last.count;
    }
    else
    {
        stack.push(new StackElement(src, info));
        setLogContext(info);
    }
}
 
Example #4
Source Project: velocity-engine   Author: apache   File: VelocimacroManager.java    License: Apache License 2.0 6 votes vote down vote up
private MacroEntry(final String vmName, final Node macro,
           List<Macro.MacroArg> macroArgs, final String sourceTemplate,
           RuntimeServices rsvc)
{
    this.vmName = vmName;
    this.macroArgs = macroArgs;
    this.nodeTree = (SimpleNode)macro;
    this.sourceTemplate = sourceTemplate;

    vp = new VelocimacroProxy();
    vp.init(rsvc);
    vp.setName(this.vmName);
    vp.setMacroArgs(this.macroArgs);
    vp.setNodeTree(this.nodeTree);
    vp.setLocation(macro.getLine(), macro.getColumn(), macro.getTemplate());
}
 
Example #5
Source Project: AuTe-Framework   Author: BSC-RUS   File: VelocityTransformer.java    License: Apache License 2.0 5 votes vote down vote up
private String render(final String stringTemplate) throws ParseException {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    runtimeServices.setProperty("userdirective", GroovyDirective.class.getName());
    StringReader reader = new StringReader(stringTemplate);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    Template template = new Template();
    template.setEncoding("UTF-8");
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();

    StringWriter writer = new StringWriter();
    template.merge(velocityContext, writer);
    return String.valueOf(writer.getBuffer());
}
 
Example #6
Source Project: AuTe-Framework   Author: BSC-RUS   File: CustomVelocityResponseTransformer.java    License: Apache License 2.0 5 votes vote down vote up
private String getRenderedBodyFromFile(final ResponseDefinition response) throws ParseException {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(response.getBody());
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    Template template = new Template();
    template.setEncoding("UTF-8");
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();

    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    return String.valueOf(writer.getBuffer());
}
 
Example #7
Source Project: iotplatform   Author: osswangxining   File: VelocityUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Template create(String source, String templateName) throws ParseException {
  RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
  StringReader reader = new StringReader(source);
  SimpleNode node = runtimeServices.parse(reader, templateName);
  Template template = new Template();
  template.setRuntimeServices(runtimeServices);
  template.setData(node);
  template.initDocument();
  return template;
}
 
Example #8
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVars.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the result of substituting the variables defined by the fields of this class
 * (a concrete subclass of TemplateVars) into the template returned by {@link #parsedTemplate()}.
 */
String toText() {
  VelocityContext velocityContext = toVelocityContext();
  StringWriter writer = new StringWriter();
  SimpleNode parsedTemplate = parsedTemplate();
  boolean rendered = velocityRuntimeInstance.render(
      velocityContext, writer, parsedTemplate.getTemplateName(), parsedTemplate);
  if (!rendered) {
    // I don't know when this happens. Usually you get an exception during rendering.
    throw new IllegalArgumentException("Template rendering failed");
  }
  return writer.toString();
}
 
Example #9
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVars.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForResource(String templateStr, String resourceName) {
  try {
    return velocityRuntimeInstance.parse(templateStr, resourceName);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example #10
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVarsTest.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForString(String string) {
  try {
    Reader reader = new StringReader(string);
    return RuntimeSingleton.parse(reader, string);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example #11
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVars.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the result of substituting the variables defined by the fields of this class
 * (a concrete subclass of TemplateVars) into the template returned by {@link #parsedTemplate()}.
 */
String toText() {
  VelocityContext velocityContext = toVelocityContext();
  StringWriter writer = new StringWriter();
  SimpleNode parsedTemplate = parsedTemplate();
  boolean rendered = velocityRuntimeInstance.render(
      velocityContext, writer, parsedTemplate.getTemplateName(), parsedTemplate);
  if (!rendered) {
    // I don't know when this happens. Usually you get an exception during rendering.
    throw new IllegalArgumentException("Template rendering failed");
  }
  return writer.toString();
}
 
Example #12
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVars.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForResource(String templateStr, String resourceName) {
  try {
    return velocityRuntimeInstance.parse(templateStr, resourceName);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example #13
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVarsTest.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForString(String string) {
  try {
    Reader reader = new StringReader(string);
    return RuntimeSingleton.parse(reader, string);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example #14
Source Project: mybaties   Author: shurun19851206   File: VelocitySqlSource.java    License: Apache License 2.0 5 votes vote down vote up
public VelocitySqlSource(Configuration configuration, String scriptText) {
  this.configuration = configuration;
  try {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(scriptText);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    script = new Template();
    script.setRuntimeServices(runtimeServices);
    script.setData(node);
    script.initDocument();
  } catch (Exception ex) {
    throw new BuilderException("Error parsing velocity script", ex);
  }
}
 
Example #15
Source Project: SimpleWeibo   Author: 8tory   File: TemplateVars.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the result of substituting the variables defined by the fields of this class
 * (a concrete subclass of TemplateVars) into the template returned by {@link #parsedTemplate()}.
 */
String toText() {
  VelocityContext velocityContext = toVelocityContext();
  StringWriter writer = new StringWriter();
  SimpleNode parsedTemplate = parsedTemplate();
  boolean rendered = velocityRuntimeInstance.render(
      velocityContext, writer, parsedTemplate.getTemplateName(), parsedTemplate);
  if (!rendered) {
    // I don't know when this happens. Usually you get an exception during rendering.
    throw new IllegalArgumentException("Template rendering failed");
  }
  return writer.toString();
}
 
Example #16
Source Project: SimpleWeibo   Author: 8tory   File: TemplateVars.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForResource(String templateStr, String resourceName) {
  try {
    return velocityRuntimeInstance.parse(templateStr, resourceName);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example #17
Source Project: SimpleWeibo   Author: 8tory   File: TemplateVarsTest.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForString(String string) {
  try {
    Reader reader = new StringReader(string);
    return RuntimeSingleton.parse(reader, string);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example #18
Source Project: mybatis   Author: tuguangquan   File: VelocitySqlSource.java    License: Apache License 2.0 5 votes vote down vote up
public VelocitySqlSource(Configuration configuration, String scriptText) {
  this.configuration = configuration;
  try {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(scriptText);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    script = new Template();
    script.setRuntimeServices(runtimeServices);
    script.setData(node);
    script.initDocument();
  } catch (Exception ex) {
    throw new BuilderException("Error parsing velocity script", ex);
  }
}
 
Example #19
Source Project: velocity-engine   Author: apache   File: RuntimeInstance.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Parse the input and return the root of
 * AST node structure.
 * <br><br>
 *  In the event that it runs out of parsers in the
 *  pool, it will create and let them be GC'd
 *  dynamically, logging that it has to do that.  This
 *  is considered an exceptional condition.  It is
 *  expected that the user will set the
 *  PARSER_POOL_SIZE property appropriately for their
 *  application.  We will revisit this.
 *
 * @param reader Reader retrieved by a resource loader
 * @param template template being parsed
 * @return A root node representing the template as an AST tree.
 * @throws ParseException When the template could not be parsed.
 */
public SimpleNode parse(Reader reader, Template template)
    throws ParseException
{
    requireInitialization();

    Parser parser = parserPool.get();
    boolean keepParser = true;
    if (parser == null)
    {
        /*
         *  if we couldn't get a parser from the pool make one and log it.
         */
        log.info("Runtime: ran out of parsers. Creating a new one. "
                 + " Please increment the parser.pool.size property."
                 + " The current value is too small.");
        parser = createNewParser();
        keepParser = false;
    }

    try
    {
        return parser.parse(reader, template);
    }
    finally
    {
        if (keepParser)
        {
            /* drop the parser Template reference to allow garbage collection */
            parser.resetCurrentTemplate();
            parserPool.put(parser);
        }

    }
}
 
Example #20
Source Project: velocity-engine   Author: apache   File: Template.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *  initializes the document.  init() is not longer
 *  dependant upon context, but we need to let the
 *  init() carry the template name down through for VM
 *  namespace features
 * @throws TemplateInitException When a problem occurs during the document initialization.
 */
public void initDocument()
throws TemplateInitException
{
    /*
     *  send an empty InternalContextAdapter down into the AST to initialize it
     */

    InternalContextAdapterImpl ica = new InternalContextAdapterImpl(  new VelocityContext() );

    try
    {
        /*
         *  put the current template name on the stack
         */

        ica.pushCurrentTemplateName( name );
        ica.setCurrentResource( this );

        /*
         *  init the AST
         */

        ((SimpleNode)data).init( ica, rsvc);

        provideScope = rsvc.isScopeControlEnabled(scopeName);
    }
    finally
    {
        /*
         *  in case something blows up...
         *  pull it off for completeness
         */

        ica.popCurrentTemplateName();
        ica.setCurrentResource( null );
    }

}
 
Example #21
Source Project: RetroFacebook   Author: yongjhih   File: RetroFacebookTemplateVars.java    License: Apache License 2.0 4 votes vote down vote up
@Override
SimpleNode parsedTemplate() {
  return TEMPLATE;
}
 
Example #22
Source Project: RetroFacebook   Author: yongjhih   File: GwtSerialization.java    License: Apache License 2.0 4 votes vote down vote up
@Override
SimpleNode parsedTemplate() {
  return TEMPLATE;
}
 
Example #23
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVarsTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  return parsedTemplateForString("integer=$integer string=$string list=$list");
}
 
Example #24
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVarsTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  throw new UnsupportedOperationException();
}
 
Example #25
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVarsTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  throw new UnsupportedOperationException();
}
 
Example #26
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVarsTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  throw new UnsupportedOperationException();
}
 
Example #27
Source Project: RetroFacebook   Author: yongjhih   File: RetroFacebookTemplateVars.java    License: Apache License 2.0 4 votes vote down vote up
@Override
SimpleNode parsedTemplate() {
  return TEMPLATE;
}
 
Example #28
Source Project: RetroFacebook   Author: yongjhih   File: GwtSerialization.java    License: Apache License 2.0 4 votes vote down vote up
@Override
SimpleNode parsedTemplate() {
  return TEMPLATE;
}
 
Example #29
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVarsTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  return parsedTemplateForString("integer=$integer string=$string list=$list");
}
 
Example #30
Source Project: RetroFacebook   Author: yongjhih   File: TemplateVarsTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  throw new UnsupportedOperationException();
}