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

The following examples show how to use org.apache.velocity.runtime.parser.node.ASTBlock. 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: JspUtilsTest.java    From velocity-tools with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #2
Source File: JspUtilsTest.java    From velocity-tools with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: ClassDirective.java    From sundrio with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #4
Source File: MethodDirective.java    From sundrio with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #5
Source File: TrimExtDirective.java    From mycollab with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #6
Source File: VelocityJspFragment.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #7
Source File: VelocityBodyContent.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #8
Source File: JspUtils.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #9
Source File: VelocityJspFragmentTest.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #10
Source File: VelocityJspFragmentTest.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #11
Source File: VelocityBodyContentTest.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #12
Source File: JspUtils.java    From velocity-tools with Apache License 2.0 3 votes vote down vote up
/**
 * 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();
}