Here are the examples of the java api class bboss.org.apache.velocity.runtime.parser.node.SimpleNode taken from open source projects.
1. RuntimeInstance#evaluate()
Project: bboss
File: RuntimeInstance.java
File: RuntimeInstance.java
/** * Renders the input reader using the context into the output writer. * To be used when a template is dynamically constructed, or want to * use Velocity as a token replacer. * * @param context context to use in rendering input string * @param writer Writer in which to render the output * @param logTag string to be used as the template name for log messages * in case of error * @param reader Reader containing the VTL to be rendered * * @return true if successful, false otherwise. If false, see * Velocity runtime log * @throws ParseErrorException The template could not be parsed. * @throws MethodInvocationException A method on a context object could not be invoked. * @throws ResourceNotFoundException A referenced resource could not be loaded. * @since Velocity 1.6 */ public boolean evaluate(Context context, Writer writer, String logTag, Reader reader) { if (logTag == null) { throw new NullPointerException("logTag (i.e. template name) cannot be null, you must provide an identifier for the content being evaluated"); } SimpleNode nodeTree = null; try { nodeTree = parse(reader, logTag); } catch (ParseException pex) { throw new ParseErrorException(pex, null); } catch (TemplateInitException pex) { throw new ParseErrorException(pex, null); } if (nodeTree == null) { return false; } else { return render(context, writer, logTag, nodeTree); } }
2. Foreach#init()
Project: bboss
File: Foreach.java
File: Foreach.java
/** * simple init - init the tree and get the elementKey from * the AST * @param rs * @param context * @param node * @throws TemplateInitException */ public void init(RuntimeServices rs, InternalContextAdapter context, Node node) throws TemplateInitException { super.init(rs, context, node); // handle deprecated config settings counterName = rsvc.getString(RuntimeConstants.COUNTER_NAME); hasNextName = rsvc.getString(RuntimeConstants.HAS_NEXT_NAME); counterInitialValue = rsvc.getInt(RuntimeConstants.COUNTER_INITIAL_VALUE); // only warn once per instance... if (!warned && rsvc.getLog().isWarnEnabled()) { warned = true; // ...and only if they customize these settings if (!"velocityCount".equals(counterName)) { rsvc.getLog().warn("The " + RuntimeConstants.COUNTER_NAME + " property has been deprecated. It will be removed" + " (along with $velocityCount itself) in Velocity 2.0. " + " Instead, please use $foreach.count to access" + " the loop counter."); } if (!"velocityHasNext".equals(hasNextName)) { rsvc.getLog().warn("The " + RuntimeConstants.HAS_NEXT_NAME + " property has been deprecated. It will be removed" + " (along with $velocityHasNext itself ) in Velocity 2.0. " + " Instead, please use $foreach.hasNext to access" + " this value from now on."); } if (counterInitialValue != 1) { rsvc.getLog().warn("The " + RuntimeConstants.COUNTER_INITIAL_VALUE + " property has been deprecated. It will be removed" + " (along with $velocityCount itself) in Velocity 2.0. " + " Instead, please use $foreach.index to access" + " the 0-based loop index and $foreach.count" + " to access the 1-based loop counter."); } } maxNbrLoops = rsvc.getInt(RuntimeConstants.MAX_NUMBER_LOOPS, Integer.MAX_VALUE); if (maxNbrLoops < 1) { maxNbrLoops = Integer.MAX_VALUE; } skipInvalidIterator = rsvc.getBoolean(RuntimeConstants.SKIP_INVALID_ITERATOR, true); if (rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false)) { // If we are in strict mode then the default for skipInvalidItarator // is true. However, if the property is explicitly set, then honor the setting. skipInvalidIterator = rsvc.getBoolean(RuntimeConstants.SKIP_INVALID_ITERATOR, false); } /* * this is really the only thing we can do here as everything * else is context sensitive */ SimpleNode sn = (SimpleNode) node.jjtGetChild(0); if (sn instanceof ASTReference) { elementKey = ((ASTReference) sn).getRootString(); } else { /* * the default, error-prone way which we'll remove * TODO : remove if all goes well */ elementKey = sn.getFirstToken().image.substring(1); } /* * make an uberinfo - saves new's later on */ uberInfo = new Info(this.getTemplateName(), getLine(), getColumn()); }
3. Evaluate#render()
Project: bboss
File: Evaluate.java
File: Evaluate.java
/** * Evaluate the argument, convert to a String, and evaluate again * (with the same context). * @param context * @param writer * @param node * @return True if the directive rendered successfully. * @throws IOException * @throws ResourceNotFoundException * @throws ParseErrorException * @throws MethodInvocationException */ public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException { /* * Evaluate the string with the current context. We know there is * exactly one argument and it is a string or reference. */ Object value = node.jjtGetChild(0).value(context); String sourceText; if (value != null) { sourceText = value.toString(); } else { sourceText = ""; } /* * The new string needs to be parsed since the text has been dynamically generated. */ String templateName = context.getCurrentTemplateName(); SimpleNode nodeTree = null; try { nodeTree = rsvc.parse(new StringReader(sourceText), templateName, false); } catch (ParseException pex) { Info info = new Info(templateName, node.getLine(), node.getColumn()); throw new ParseErrorException(pex.getMessage(), info); } catch (TemplateInitException pex) { Info info = new Info(templateName, node.getLine(), node.getColumn()); throw new ParseErrorException(pex.getMessage(), info); } if (nodeTree != null) { InternalContextAdapter ica = new EvaluateContext(context, rsvc); ica.pushCurrentTemplateName(templateName); try { try { nodeTree.init(ica, rsvc); } catch (TemplateInitException pex) { Info info = new Info(templateName, node.getLine(), node.getColumn()); throw new ParseErrorException(pex.getMessage(), info); } try { preRender(ica); /* * now render, and let any exceptions fly */ nodeTree.render(ica, writer); } catch (StopCommand stop) { if (!stop.isFor(this)) { throw stop; } else if (rsvc.getLog().isDebugEnabled()) { rsvc.getLog().debug(stop.getMessage()); } } catch (ParseErrorException pex) { Info info = new Info(templateName, node.getLine(), node.getColumn()); throw new ParseErrorException(pex.getMessage(), info); } } finally { ica.popCurrentTemplateName(); postRender(ica); } return true; } return false; }