001    package org.apache.myfaces.tobago.taglib.component;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    /*
021     * Created on: 15.02.2002, 16:19:49
022     * $Id: TobagoBodyTag.java 601107 2007-12-04 22:12:14Z bommel $
023     */
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    import javax.faces.component.UIComponent;
029    import javax.servlet.jsp.JspException;
030    import javax.servlet.jsp.tagext.BodyContent;
031    import javax.servlet.jsp.tagext.BodyTag;
032    
033    public abstract class TobagoBodyTag extends TobagoTag implements TobagoBodyTagDeclaration {
034    
035      private static final Log LOG = LogFactory.getLog(TobagoBodyTag.class);
036    
037      private BodyContent bodyContent;
038    
039      public int doAfterBody() throws JspException {
040        return getDoAfterBodyValue();
041      }
042    
043      public void doInitBody() throws JspException {
044      }
045    
046    
047      public int doEndTag() throws JspException {
048        if (LOG.isWarnEnabled()) {
049          UIComponent component = getComponentInstance();
050          if (component != null && component.getRendersChildren() && !isBodyContentEmpty()) {
051            LOG.warn("BodyContent should be empty. Component with id " + component.getId()
052                + " class " + component.getClass().getName() + " content " + bodyContent.getString()
053                + "  Please use the f:verbatim tag for nested content!");
054          }
055        }
056        return super.doEndTag();
057      }
058    
059      protected boolean isBodyContentEmpty() {
060        if (bodyContent != null) {
061          String content = bodyContent.getString();
062          //bodyContent.clearBody();
063          String tmp = content.replace('\n', ' ');
064          if (tmp.trim().length() > 0) { // if there are only whitespaces: drop bodyContent
065            return false;
066          }
067        }
068        return true;
069      }
070    
071      protected int getDoStartValue() throws JspException {
072        return BodyTag.EVAL_BODY_BUFFERED;
073      }
074    
075      protected int getDoAfterBodyValue() throws JspException {
076        return BodyTag.SKIP_BODY;
077      }
078    
079      public void release() {
080        super.release();
081        bodyContent = null;
082      }
083    
084      public void setBodyContent(BodyContent bodyContent) {
085        this.bodyContent = bodyContent;
086      }
087    }
088