    1   /*
    2    * Copyright 2004-2009 the Seasar Foundation and the Others.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
    13   * either express or implied. See the License for the specific language
    14   * governing permissions and limitations under the License.
    15   */
    16  package org.seasar.cubby.tags;
    17
    18  import java.io.IOException;
    19  import java.util.List;
    20
    21  import javax.servlet.jsp.JspException;
    22  import javax.servlet.jsp.tagext.BodyContent;
    23  import javax.servlet.jsp.tagext.BodyTag;
    24  import javax.servlet.jsp.tagext.JspTag;
    25  import javax.servlet.jsp.tagext.SimpleTag;
    26  import javax.servlet.jsp.tagext.Tag;
    27
    28  abstract class AbstractStandardTagTestCase extends AbstractTagTestCase {
    29
    30  	protected void setupBodyTag(BodyTag tag) {
    31  		tag.setPageContext(context);
    32  	}
    33
    34  	protected void doLifecycle(Tag tag) throws JspException, IOException {
    35  		doLifecycle(tag, null);
    36  	}
    37
    38  	protected void doLifecycle(Tag tag, ChildrenFactory childrenFactory)
    39  			throws JspException, IOException {
    40  		int reuslt = tag.doStartTag();
    41  		if (tag instanceof BodyTag) {
    42  			BodyTag bodyTag = (BodyTag) tag;
    43  			if (reuslt == BodyTag.EVAL_BODY_BUFFERED) {
    44  				BodyContent bodyContent = context.pushBody();
    45  				bodyTag.setBodyContent(bodyContent);
    46  			}
    47  			bodyTag.doInitBody();
    48  			if (childrenFactory != null) {
    49  				List<JspTag> children = childrenFactory.create();
    50  				if (children != null) {
    51  					for (JspTag child : children) {
    52  						if (child instanceof SimpleTag) {
    53  							SimpleTag simpleTag = (SimpleTag) child;
    54  							simpleTag.setJspBody(jspBody);
    55  							simpleTag.setJspContext(context);
    56  							simpleTag.setParent(tag);
    57  						} else {
    58  							throw new UnsupportedOperationException();
    59  						}
    60  						jspBody.addChild(child);
    61  					}
    62  				}
    63  			}
    64  			jspBody.invoke(context.getOut());
    65  			bodyTag.doAfterBody();
    66  			if (reuslt == BodyTag.EVAL_BODY_BUFFERED) {
    67  				context.popBody();
    68  			}
    69  		}
    70  		tag.doEndTag();
    71  	}
    72
    73  	interface ChildrenFactory {
    74  		List<JspTag> create();
    75  	}
    76
    77  }