Java源码示例:org.apache.velocity.context.InternalContextAdapter
示例1
/**
* Render the AST of this block into the writer using the context.
* @param context
* @param writer
* @return success status
*/
public boolean render(InternalContextAdapter context, Writer writer)
{
depth++;
if (depth > parent.maxDepth)
{
/* this is only a debug message, as recursion can
* happen in quasi-innocent situations and is relatively
* harmless due to how we handle it here.
* this is more to help anyone nuts enough to intentionally
* use recursive block definitions and having problems
* pulling it off properly.
*/
parent.log.debug("Max recursion depth reached for {} at {}", parent.id(context), StringUtils.formatFileString(parent));
depth--;
return false;
}
else
{
parent.render(context, writer);
depth--;
return true;
}
}
示例2
public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
//render content
StringWriter content = new StringWriter();
node.jjtGetChild(0).render(context, content);
//compress
try {
writer.write(htmlCompressor.compress(content.toString()));
} catch (Exception e) {
writer.write(content.toString());
String msg = "Failed to compress content: "+content.toString();
log.error(msg, e);
throw new RuntimeException(msg, e);
}
return true;
}
示例3
public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
//render content
StringWriter content = new StringWriter();
node.jjtGetChild(0).render(context, content);
//compress
try {
writer.write(xmlCompressor.compress(content.toString()));
} catch (Exception e) {
writer.write(content.toString());
String msg = "Failed to compress content: "+content.toString();
log.error(msg, e);
throw new RuntimeException(msg, e);
}
return true;
}
示例4
/**
* Test method for {@link org.apache.velocity.tools.view.jsp.jspimpl.JspUtils#executeSimpleTag(org.apache.velocity.context.InternalContextAdapter, org.apache.velocity.runtime.parser.node.Node, javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.SimpleTag)}.
* @throws IOException If something goes wrong.
* @throws JspException If something goes wrong.
*/
@Test
public void testExecuteSimpleTag() throws JspException, IOException
{
InternalContextAdapter context = createMock(InternalContextAdapter.class);
Node node = createMock(Node.class);
PageContext pageContext = createMock(PageContext.class);
SimpleTag tag = createMock(SimpleTag.class);
ASTBlock block = createMock(ASTBlock.class);
tag.setJspBody(isA(VelocityJspFragment.class));
expect(node.jjtGetChild(1)).andReturn(block);
tag.doTag();
replay(context, node, pageContext, block, tag);
JspUtils.executeSimpleTag(context, node, pageContext, tag);
verify(context, node, pageContext, block, tag);
}
示例5
/**
* Test method for {@link org.apache.velocity.tools.view.jsp.jspimpl.JspUtils#executeTag(org.apache.velocity.context.InternalContextAdapter, org.apache.velocity.runtime.parser.node.Node, javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)}.
* @throws JspException If something goes wrong.
* @throws IOException If something goes wrong.
* @throws ParseErrorException If something goes wrong.
* @throws ResourceNotFoundException If something goes wrong.
* @throws MethodInvocationException If something goes wrong.
*/
@Test
public void testExecuteTag() throws JspException, MethodInvocationException, ResourceNotFoundException, ParseErrorException, IOException
{
InternalContextAdapter context = createMock(InternalContextAdapter.class);
Node node = createMock(Node.class);
PageContext pageContext = createMock(PageContext.class);
BodyTag tag = createMock(BodyTag.class);
ASTBlock block = createMock(ASTBlock.class);
JspWriter writer = createMock(JspWriter.class);
expect(tag.doStartTag()).andReturn(BodyTag.EVAL_BODY_BUFFERED);
tag.setBodyContent(isA(VelocityBodyContent.class));
tag.doInitBody();
expect(node.jjtGetChild(1)).andReturn(block);
expect(tag.doAfterBody()).andReturn(BodyTag.SKIP_BODY);
expect(tag.doEndTag()).andReturn(BodyTag.EVAL_PAGE);
expect(pageContext.getOut()).andReturn(writer);
expect(block.render(eq(context), isA(StringWriter.class))).andReturn(true);
replay(context, node, pageContext, block, tag, writer);
JspUtils.executeTag(context, node, pageContext, tag);
verify(context, node, pageContext, block, tag, writer);
}
示例6
/**
* the logical or :
* <pre>
* left || null -> left
* null || right -> right
* null || null -> false
* left || right -> left || right
* </pre>
* @param context
* @return The evaluation result.
* @throws MethodInvocationException
*/
public boolean evaluate( InternalContextAdapter context)
throws MethodInvocationException
{
Node left = jjtGetChild(0);
Node right = jjtGetChild(1);
/*
* if the left is not null and true, then true
*/
if (left != null && left.evaluate( context ) )
return true;
/*
* same for right
*/
return right != null && right.evaluate(context);
}
示例7
/**
* @see org.apache.velocity.runtime.parser.node.SimpleNode#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
*/
public Object init( InternalContextAdapter context, Object data)
throws TemplateInitException
{
Token t = getFirstToken();
String text = t.image;
// t.image is in format: #[[ <string> ]]#
// we must strip away the hash tags
text = text.substring(START.length(), text.length() - END.length());
ctext = text.toCharArray();
cleanupParserAndTokens();
return data;
}
示例8
/**
* simple init - don't do anything that is context specific.
* just get what we need from the AST, which is static.
* @param context
* @param data
* @return The data object.
* @throws TemplateInitException
*/
public Object init(InternalContextAdapter context, Object data)
throws TemplateInitException
{
super.init(context, data);
identifier = rsvc.useStringInterning() ? getFirstToken().image.intern() : getFirstToken().image;
uberInfo = new Info(getTemplateName(), getLine(), getColumn());
strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
saveTokenImages();
cleanupParserAndTokens();
return data;
}
示例9
@Override
protected Object handleSpecial(Object left, Object right, InternalContextAdapter context)
{
// check for strings, but don't coerce
String lstr = DuckType.asString(left, false);
String rstr = DuckType.asString(right, false);
if (lstr != null || rstr != null)
{
if (lstr == null)
{
lstr = left != null ? left.toString() : jjtGetChild(0).literal();
}
else if (rstr == null)
{
rstr = right != null ? right.toString() : jjtGetChild(1).literal();
}
return lstr.concat(rstr);
}
return null;
}
示例10
/**
* Computes boolean value of this reference
* Returns the actual value of reference return type
* boolean, and 'true' if value is not null
*
* @param context context to compute value with
* @return True if evaluation was ok.
* @throws MethodInvocationException
*/
public boolean evaluate(InternalContextAdapter context)
throws MethodInvocationException
{
Object value = execute(this, context); // non-null object as first parameter by convention for 'evaluate'
if (value == null)
{
return false;
}
try
{
rsvc.getLogContext().pushLogContext(this, uberInfo);
return DuckType.asBoolean(value, checkEmpty);
}
catch(Exception e)
{
throw new VelocityException("Reference evaluation threw an exception at "
+ StringUtils.formatFileString(this), e, rsvc.getLogContext().getStackTrace());
}
finally
{
rsvc.getLogContext().popLogContext();
}
}
示例11
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException
{
Object c = context.get(counterName);
context.put(counterName, counterInitialValue);
try
{
return super.render(context, writer, node);
}
finally
{
if (c != null)
{
context.put(counterName, c);
}
else
{
context.remove(counterName);
}
}
}
示例12
/**
* @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
*/
public boolean render( InternalContextAdapter context, Writer writer)
throws IOException, MethodInvocationException,
ResourceNotFoundException, ParseErrorException
{
SpaceGobbling spaceGobbling = rsvc.getSpaceGobbling();
if (spaceGobbling == SpaceGobbling.NONE)
{
writer.write(prefix);
}
int i, k = jjtGetNumChildren();
for (i = 0; i < k; i++)
jjtGetChild(i).render(context, writer);
if (morePostfix.length() > 0 || spaceGobbling.compareTo(SpaceGobbling.LINES) < 0)
{
writer.write(postfix);
}
writer.write(morePostfix);
return true;
}
示例13
/**
* @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter)
*/
public Object value( InternalContextAdapter context)
throws MethodInvocationException
{
int size = jjtGetNumChildren();
// since we know the amount of elements, initialize arraylist with proper size
List objectArray = new ArrayList(size);
for (int i = 0; i < size; i++)
{
objectArray.add(jjtGetChild(i).value(context));
}
return objectArray;
}
示例14
/**
* We need to make sure we catch any of the dreaded MORE tokens.
* @param context
* @param data
* @return The data object.
*/
public Object init(InternalContextAdapter context, Object data)
{
Token t = getFirstToken();
int loc1 = t.image.indexOf(parser.lineComment());
int loc2 = t.image.indexOf(parser.blockComment());
if (loc1 == -1 && loc2 == -1)
{
carr = ZILCH;
}
else
{
carr = t.image.substring(0, (loc1 == -1) ? loc2 : loc1).toCharArray();
}
cleanupParserAndTokens();
return data;
}
示例15
/**
* @see org.apache.velocity.runtime.parser.node.SimpleNode#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
*/
public Object init( InternalContextAdapter context, Object data)
throws TemplateInitException
{
StringBuilder builder = new StringBuilder();
Token t = getFirstToken();
for (; t != getLastToken(); t = t.next)
{
builder.append(NodeUtils.tokenLiteral(parser, t));
}
builder.append(NodeUtils.tokenLiteral(parser, t));
ctext = builder.toString();
cleanupParserAndTokens();
return data;
}
示例16
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node ) throws IOException {
String variableName = node.jjtGetChild(0).getFirstToken().image.substring(1);
String xmlDocument = String.valueOf(node.jjtGetChild(1).value(context));
String xpathString = String.valueOf(node.jjtGetChild(2).value(context));
XPath xpath = XPathFactory.newInstance().newXPath();
try {
String evaluatedValue = xpath.evaluate(xpathString, new InputSource(new StringReader(xmlDocument)));
context.put(variableName, evaluatedValue);
} catch (XPathExpressionException e) {
throw new IOException("cannot evaluate xpath: " + e.getMessage(), e);
}
return true;
}
示例17
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException,
MethodInvocationException {
List placeList=(List) context.get("placeList");
if(placeList==null){
return true;
}
Object value=node.jjtGetChild(0).value(context);
placeList.add(value);
writer.write("?");
return true;
}
示例18
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String clazz = (String) node.jjtGetChild(0).value(context);
if (context.containsKey(clazz)) {
String packagePath = context.get(clazz).toString();
packagePath = new StringBuilder("").append(packagePath).toString();
writer.write(packagePath);
return true;
}
return false;
}
示例19
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String value = (String) node.jjtGetChild(0).value(context);
if (StringUtils.isBlank(value)) {
writer.write(value);
return true;
}
String result = value.replaceFirst(value.substring(0, 1), value.substring(0, 1).toLowerCase());
writer.write(result);
return true;
}
示例20
/**
* @see org.apache.velocity.runtime.parser.node.Node#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
*/
public boolean render( InternalContextAdapter context, Writer writer)
throws IOException, MethodInvocationException, ParseErrorException, ResourceNotFoundException
{
int i, k = jjtGetNumChildren();
for (i = 0; i < k; i++)
jjtGetChild(i).render(context, writer);
return true;
}
示例21
/**
* This creates and places the scope control for this directive
* into the context (if scope provision is turned on).
* @param context
*/
protected void preRender(InternalContextAdapter context)
{
if (isScopeProvided())
{
String name = getScopeName();
Object previous = context.get(name);
context.put(name, makeScope(previous));
}
}
示例22
/**
* This method helps to implement the "render literal if null" functionality.
*
* VelocimacroProxy saves references to macro arguments (AST nodes) so that if we have a macro
* #foobar($a $b) then there is key "$a.literal" which points to the literal presentation of the
* argument provided to variable $a. If the value of $a is null, we render the string that was
* provided as the argument.
*
* @param context
* @return
*/
private String getNullString(InternalContextAdapter context)
{
String ret = nullString;
if (lookupAlternateLiteral)
{
Deque<String> alternateLiteralsStack = (Deque<String>)context.get(alternateNullStringKey);
if (alternateLiteralsStack != null && alternateLiteralsStack.size() > 0)
{
ret = alternateLiteralsStack.peekFirst();
}
}
return ret;
}
示例23
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String value = (String) node.jjtGetChild(0).value(context);
String result = value.replaceFirst(value.substring(0, 1), value.substring(0, 1).toUpperCase());
writer.write(result);
return true;
}
示例24
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException,
MethodInvocationException {
List placeList=(List) context.get("placeList");
if(placeList==null){
return true;
}
Object value=node.jjtGetChild(0).value(context);
placeList.add(value);
writer.write("?");
return true;
}
示例25
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException {
String word = null;
if (node.jjtGetChild(0) != null) {
word = String.valueOf(node.jjtGetChild(0).value(context));
}
//truncate and write result to writer
writer.write(Singularize.FUNCTION.apply(word));
return true;
}
示例26
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException {
String block = "";
Method method = null;
Boolean isInterface = false;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (node.jjtGetChild(i) != null) {
if (!(node.jjtGetChild(i) instanceof ASTBlock)) {
//reading and casting inline parameters
if (i == 0) {
method = (Method) node.jjtGetChild(i).value(context);
} else if (i == 1) {
isInterface = (Boolean) node.jjtGetChild(i).value(context);
} else {
break;
}
} else {
//reading block content and rendering it
StringWriter blockContent = new StringWriter();
node.jjtGetChild(i).render(context, blockContent);
block = blockContent.toString();
break;
}
}
}
writeMethod(writer, method, block, isInterface);
return true;
}
示例27
/**
* renders block directive
* @param context
* @param writer
* @return success status
*/
public boolean render(InternalContextAdapter context, Writer writer)
{
preRender(context);
try
{
return block.render(context, writer);
}
catch (IOException e)
{
String msg = "Failed to render " + id(context) + " to writer at " +
StringUtils.formatFileString(this);
log.error(msg, e);
throw new RuntimeException(msg, e);
}
catch (StopCommand stop)
{
if (!stop.isFor(this))
{
throw stop;
}
return true;
}
finally
{
postRender(context);
}
}
示例28
@Override
public final boolean render(InternalContextAdapter ica, Writer writer, Node node) throws IOException,
ResourceNotFoundException, ParseErrorException, MethodInvocationException {
Params p = getParams(ica, node);
if (p == null) {
return false;
} else {
return render(p, writer);
}
}
示例29
/**
* @throws TemplateInitException
* @see org.apache.velocity.runtime.parser.node.Node#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
*/
public Object init( InternalContextAdapter context, Object data) throws TemplateInitException
{
Object obj = super.init(context, data);
cleanupParserAndTokens(); // drop reference to Parser and all JavaCC Tokens
return obj;
}
示例30
/**
* @throws TemplateInitException
* @see org.apache.velocity.runtime.parser.node.Node#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
*/
public Object init( InternalContextAdapter context, Object data) throws TemplateInitException
{
Object obj = super.init(context, data);
cleanupParserAndTokens(); // drop reference to Parser and all JavaCC Tokens
return obj;
}