下面列出了怎么用org.apache.velocity.runtime.parser.node.ASTBlock的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
/**
* 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);
}
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException {
String block = "";
TypeDef clazz = null;
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) {
clazz = (TypeDef) node.jjtGetChild(i).value(context);
} else {
break;
}
} else {
//reading block content and rendering it
try {
StringWriter blockContent = new StringWriter();
node.jjtGetChild(i).render(context, blockContent);
block = blockContent.toString();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
writeClazz(writer, clazz, block);
return true;
}
@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;
}
protected Params getParams(final InternalContextAdapter context, final Node node) throws IOException,
ResourceNotFoundException, ParseErrorException, MethodInvocationException {
final Params params = new Params();
final int nodes = node.jjtGetNumChildren();
for (int i = 0; i < nodes; i++) {
Node child = node.jjtGetChild(i);
if (child != null) {
if (!(child instanceof ASTBlock)) {
if (i == 0) {
params.setPrefix(String.valueOf(child.value(context)));
} else if (i == 1) {
params.setPrefixOverrides(String.valueOf(child.value(context)).toUpperCase(Locale.ENGLISH));
} else if (i == 2) {
params.setSuffix(String.valueOf(child.value(context)));
} else if (i == 3) {
params.setSuffixOverrides(String.valueOf(child.value(context)).toUpperCase(Locale.ENGLISH));
} else {
break;
}
} else {
StringWriter blockContent = new StringWriter();
child.render(context, blockContent);
if ("".equals(blockContent.toString().trim())) {
return null;
}
params.setBody(blockContent.toString().trim());
break;
}
}
}
return params;
}
/**
* Constructor.
*
* @param pageContext The page context to use.
* @param block The block to wrap.
* @param context The directive context.
*/
public VelocityJspFragment(PageContext pageContext, ASTBlock block,
InternalContextAdapter context)
{
this.pageContext = pageContext;
this.block = block;
this.context = context;
}
/**
* Constructor.
*
* @param jspWriter The JSP writer to be used by default.
* @param block The block to wrap.
* @param context The directive context.
*/
public VelocityBodyContent(JspWriter jspWriter, ASTBlock block,
InternalContextAdapter context)
{
super(jspWriter);
this.block = block;
this.context = context;
}
/**
* Executes a {@link Tag}.
*
* @param context The directive context.
* @param node The main node of the directive.
* @param pageContext The page context.
* @param tag The tag to execute.
* @throws JspException If something goes wrong.
*/
public static void executeTag(InternalContextAdapter context, Node node,
PageContext pageContext, Tag tag) throws JspException
{
int result = tag.doStartTag();
if (tag instanceof BodyTag)
{
BodyTag bodyTag = (BodyTag) tag;
BodyContent bodyContent = new VelocityBodyContent(
pageContext.getOut(), (ASTBlock) node.jjtGetChild(1),
context);
switch (result)
{
case BodyTag.EVAL_BODY_BUFFERED:
bodyTag.setBodyContent(bodyContent);
bodyTag.doInitBody();
case BodyTag.EVAL_BODY_INCLUDE:
bodyContent.getString();
default:
break;
}
while (bodyTag.doAfterBody() == BodyTag.EVAL_BODY_AGAIN) {
bodyContent.getString();
}
}
tag.doEndTag();
}
/**
* Test method for {@link org.apache.velocity.tools.view.jsp.jspimpl.VelocityJspFragment#invoke(java.io.Writer)}.
* @throws IOException If something goes wrong.
* @throws ParseErrorException If something goes wrong.
* @throws ResourceNotFoundException If something goes wrong.
* @throws MethodInvocationException If something goes wrong.
* @throws JspException If something goes wrong.
*/
@Test
public void testInvokeWriter() throws MethodInvocationException, ResourceNotFoundException, ParseErrorException, IOException, JspException
{
PageContext pageContext = createMock(PageContext.class);
ASTBlock block = createMock(ASTBlock.class);
InternalContextAdapter context = createMock(InternalContextAdapter.class);
Writer writer = createMock(Writer.class);
expect(block.render(context, writer)).andReturn(true);
replay(pageContext, block, context, writer);
VelocityJspFragment fragment = new VelocityJspFragment(pageContext, block, context);
fragment.invoke(writer);
verify(pageContext, block, context, writer);
}
/**
* Test method for {@link org.apache.velocity.tools.view.jsp.jspimpl.VelocityJspFragment#getJspContext()}.
*/
@Test
public void testGetJspContext()
{
PageContext pageContext = createMock(PageContext.class);
ASTBlock block = createMock(ASTBlock.class);
InternalContextAdapter context = createMock(InternalContextAdapter.class);
replay(pageContext, block, context);
VelocityJspFragment fragment = new VelocityJspFragment(pageContext, block, context);
assertSame(pageContext, fragment.getJspContext());
verify(pageContext, block, context);
}
/**
* Sets up the test.
*
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
{
jspWriter = createMock(JspWriter.class);
block = createMock(ASTBlock.class);
context = createMock(InternalContextAdapter.class);
content = new VelocityBodyContent(jspWriter, block, context);
}
/**
* Executes a {@link SimpleTag}.
*
* @param context The directive context.
* @param node The main node of the directive.
* @param pageContext The page context.
* @param tag The tag to execute.
* @throws JspException If something goes wrong.
* @throws IOException If something goes wrong.
*/
public static void executeSimpleTag(InternalContextAdapter context,
Node node, PageContext pageContext, SimpleTag tag)
throws JspException, IOException
{
tag.setJspBody(new VelocityJspFragment(pageContext, (ASTBlock) node
.jjtGetChild(1), context));
tag.doTag();
}