Java tutorial
/* * Copyright 2011 Takumi IINO. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package jp.troter.tags.capture; import java.util.LinkedList; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.commons.lang.StringUtils; /** * ????? * <p> * <code>name</code> ???????? * <code>body</code>????JSP????? <code>value</code> ?????<br /> * contentFor?????yield?????? * </p> */ public class ContentForTag extends BodyTagSupport { private static final long serialVersionUID = 1L; /** ??? */ private String name; /** body????? */ private String value; /** body */ private String body; /** ??? */ private boolean addFirst = false; /** i:contentFor??body????value???? */ private boolean duplicate; /** * @param name ??? */ public void setName(String name) { this.name = name; } /** * @param value ?? */ public void setValue(String value) { this.value = value; } /** * ??? * @param addFirst ???true? */ public void setAddFirst(boolean addFirst) { this.addFirst = addFirst; } /** * ??? * @param duplicate */ public void setDuplicate(boolean duplicate) { this.duplicate = duplicate; } /** * */ public ContentForTag() { super(); } @Override public int doStartTag() throws JspException { // clear body content body = null; // Do we need to evaluate body ? if (value == null) { return EVAL_BODY_BUFFERED; } // Value is set, don't evaluate body. return SKIP_BODY; } @Override public int doAfterBody() throws JspException { if (bodyContent != null) { body = bodyContent.getString(); } return (SKIP_BODY); } @Override public int doEndTag() throws JspException { String name = CaptureUtil.captureName(this.name); @SuppressWarnings("unchecked") LinkedList<String> rawCurrent = (LinkedList<String>) pageContext.getAttribute(name, PageContext.REQUEST_SCOPE); LinkedList<String> current = CaptureUtil.nullToEmptyList(rawCurrent); concatToContextValue(name, current); return EVAL_PAGE; } @Override public void release() { super.release(); name = null; value = null; addFirst = false; duplicate = false; super.release(); } public void concatToContextValue(String name, LinkedList<String> current) { String value = StringUtils.isBlank(this.value) ? this.body : this.value; value = CaptureUtil.nullToEmpty(value); if (!duplicate && current.contains(value)) { return; } if (addFirst) { current.addFirst(value); } else { current.add(value); } pageContext.setAttribute(name, current, PageContext.REQUEST_SCOPE); } }