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    import org.apache.myfaces.tobago.apt.annotation.BodyContent;
021    import org.apache.myfaces.tobago.apt.annotation.BodyContentDescription;
022    import org.apache.myfaces.tobago.apt.annotation.Tag;
023    import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
024    import org.apache.myfaces.tobago.component.ComponentUtil;
025    import org.apache.myfaces.tobago.component.UIPage;
026    import org.apache.myfaces.tobago.taglib.decl.HasId;
027    
028    import javax.faces.context.FacesContext;
029    import javax.servlet.jsp.JspException;
030    import javax.servlet.jsp.tagext.BodyTagSupport;
031    
032    /**
033     * Add a style tag.
034     * Collected bodyContent is rendered as content into a style tag.
035     */
036    @Tag(name = "style", bodyContent = BodyContent.TAGDEPENDENT)
037    @BodyContentDescription(contentType = "css")
038    public class StyleTag extends BodyTagSupport implements HasId {
039    
040      private static final long serialVersionUID = -2201525304632479403L;
041    
042      private String style;
043    
044      public int doEndTag() throws JspException {
045    
046        FacesContext facesContext = FacesContext.getCurrentInstance();
047        UIPage page = ComponentUtil.findPage(facesContext);
048        if (page == null) {
049          throw new JspException("The StyleTag cannot find the UIPage. "
050              + "Check you have defined the StyleTag inside of the PageTag!");
051        }
052    
053        if (style != null) {
054          page.getStyleFiles().add(ComponentUtil.getValueFromEl(style));
055        }
056    
057        if (bodyContent != null) {
058          String classes = bodyContent.getString();
059          bodyContent.clearBody();
060          page.getStyleBlocks().add(ComponentUtil.getValueFromEl(classes));
061        }
062    
063        return EVAL_PAGE;
064      }
065    
066      public int doStartTag() throws JspException {
067        return EVAL_BODY_BUFFERED;
068      }
069    
070      public void release() {
071        super.release();
072        style = null;
073      }
074    
075      public String getStyle() {
076        return style;
077      }
078    
079      /**
080       * Name of the stylsheet file to add to page.
081       */
082      @TagAttribute
083      public void setStyle(String style) {
084        this.style = style;
085      }
086    }
087