com.gantzgulch.taglibs.html.BodyTagRunner.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.taglibs.html.BodyTagRunner.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzHtmlTaglibs.
 *
 * GantzHtmlTaglibs is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GantzHtmlTagLibs is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GantzHtmlTagLibs.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.gantzgulch.taglibs.html;

import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTag;

import org.springframework.mock.web.MockBodyContent;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockPageContext;
import org.springframework.mock.web.MockServletContext;

public class BodyTagRunner {

    private TagState state = null;

    private MockPageContext pageContext;
    private MockServletContext servletContext;
    private MockHttpServletRequest servletRequest;
    private MockHttpServletResponse servletResponse;

    private BodyTag bodyTag;
    private String bodyContentValue;
    private MockBodyContent mockBodyContent;

    public BodyTagRunner(BodyTag bodyTag, String contextPath) throws JspException {

        servletRequest = new MockHttpServletRequest();
        servletResponse = new MockHttpServletResponse();
        servletContext = new MockServletContext();
        servletContext.setContextPath(contextPath);
        pageContext = new MockPageContext(servletContext, servletRequest, servletResponse);

        this.bodyTag = bodyTag;
        state = doInitializeState.execute();
    }

    public void execute(String bodyContentValue) throws JspException {
        this.bodyContentValue = bodyContentValue;
        this.mockBodyContent = new MockBodyContent(this.bodyContentValue, new StringWriter());

        while (state != endState) {
            state = state.execute();
        }
    }

    public interface TagState {
        TagState execute() throws JspException;
    }

    public TagState doInitializeState = new TagState() {
        @Override
        public TagState execute() throws JspException {
            System.out.println("Setting page context.");
            bodyTag.setPageContext(pageContext);
            return doStartTagState;
        }
    };

    public TagState doStartTagState = new TagState() {

        public TagState execute() throws JspException {
            int returnCode = bodyTag.doStartTag();
            switch (returnCode) {
            case BodyTag.EVAL_BODY_BUFFERED:
                bodyTag.setBodyContent(mockBodyContent);
                bodyTag.doInitBody();
                return doEvaluateBodyState;
            default:
                throw new RuntimeException("Unsupported doStartTag return value : " + returnCode);
            }
        }
    };

    public TagState doEvaluateBodyState = new TagState() {

        public TagState execute() throws JspException {
            return doAfterBodyState;
        }
    };

    public TagState doAfterBodyState = new TagState() {

        public TagState execute() throws JspException {
            int returnCode = bodyTag.doAfterBody();
            switch (returnCode) {
            case BodyTag.EVAL_BODY_AGAIN:
                return doEvaluateBodyState;
            case BodyTag.SKIP_BODY:
                return doEndTagState;
            default:
                throw new RuntimeException("Unsupported doAfterBody return value : " + returnCode);
            }
        }
    };

    public TagState doEndTagState = new TagState() {

        public TagState execute() throws JspException {
            int returnCode = bodyTag.doEndTag();
            switch (returnCode) {
            case BodyTag.EVAL_PAGE:
            case BodyTag.SKIP_PAGE:
                break;
            default:
                throw new RuntimeException("Unsupported doEndTag return value : " + returnCode);
            }
            return endState;
        }
    };

    public TagState endState = new TagState() {

        @Override
        public TagState execute() throws JspException {
            return null;
        }

    };

    public String getOutput() {
        try {
            return servletResponse.getContentAsString();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}