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