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 File: CustomVelocityResponseTransformer.java    From AuTe-Framework with Apache License 2.0 6 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 #2
Source File: OTPService.java    From sunbird-lms-service with 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 #3
Source File: VelocimacroManager.java    From velocity-engine with 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 #4
Source File: LogContext.java    From velocity-engine with 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 #5
Source File: VelocityTemplateEngine.java    From pippo with 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 #6
Source File: Template.java    From velocity-engine with 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 #7
Source File: VelocityTransformer.java    From AuTe-Framework with 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 #8
Source File: RuntimeInstance.java    From velocity-engine with 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 #9
Source File: VelocitySqlSource.java    From mybatis with 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 #10
Source File: TemplateVarsTest.java    From SimpleWeibo with 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 File: TemplateVars.java    From SimpleWeibo with 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 #12
Source File: TemplateVars.java    From SimpleWeibo with 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 #13
Source File: VelocitySqlSource.java    From mybaties with 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 #14
Source File: TemplateVarsTest.java    From RetroFacebook with 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 #15
Source File: TemplateVars.java    From RetroFacebook with 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 #16
Source File: TemplateVars.java    From RetroFacebook with 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 #17
Source File: VelocityUtils.java    From iotplatform with 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 #18
Source File: TemplateVars.java    From RetroFacebook with 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 #19
Source File: TemplateVars.java    From RetroFacebook with 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 #20
Source File: TemplateVarsTest.java    From RetroFacebook with 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 #21
Source File: TemplateVarsTest.java    From RetroFacebook with Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  return parsedTemplateForString("integer=$integer string=$string list=$list");
}
 
Example #22
Source File: RetroFacebookTemplateVars.java    From RetroFacebook with Apache License 2.0 4 votes vote down vote up
@Override
SimpleNode parsedTemplate() {
  return TEMPLATE;
}
 
Example #23
Source File: VelocimacroProxy.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * @param tree
 */
public void setNodeTree(SimpleNode tree)
{
    nodeTree = tree;
}
 
Example #24
Source File: GwtSerialization.java    From RetroFacebook with Apache License 2.0 4 votes vote down vote up
@Override
SimpleNode parsedTemplate() {
  return TEMPLATE;
}
 
Example #25
Source File: LogContext.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
protected StackElement(SimpleNode src, Info info)
{
    this.src = src;
    this.info = info;
}
 
Example #26
Source File: VelocityWrapper.java    From consulo with Apache License 2.0 4 votes vote down vote up
static SimpleNode parse(Reader reader, String templateName) throws ParseException {
  Template template = new Template();
  template.setName(templateName);
  return RuntimeSingleton.parse(reader, template);
}
 
Example #27
Source File: TemplateVarsTest.java    From SimpleWeibo with Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  throw new UnsupportedOperationException();
}
 
Example #28
Source File: TemplateVarsTest.java    From SimpleWeibo with Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  throw new UnsupportedOperationException();
}
 
Example #29
Source File: TemplateVarsTest.java    From SimpleWeibo with Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  throw new UnsupportedOperationException();
}
 
Example #30
Source File: TemplateVarsTest.java    From SimpleWeibo with Apache License 2.0 4 votes vote down vote up
@Override SimpleNode parsedTemplate() {
  return parsedTemplateForString("integer=$integer string=$string list=$list");
}