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 String style;
041    
042      public int doEndTag() throws JspException {
043    
044        FacesContext facesContext = FacesContext.getCurrentInstance();
045        UIPage page = ComponentUtil.findPage(facesContext);
046        if (page == null) {
047          throw new JspException("The StyleTag cannot find the UIPage. "
048              + "Check you have defined the StyleTag inside of the PageTag!");
049        }
050    
051        if (style != null) {
052          page.getStyleFiles().add(ComponentUtil.getValueFromEl(style));
053        }
054    
055        if (bodyContent != null) {
056          String classes = bodyContent.getString();
057          bodyContent.clearBody();
058          page.getStyleBlocks().add(ComponentUtil.getValueFromEl(classes));
059        }
060    
061        return EVAL_PAGE;
062      }
063    
064      public int doStartTag() throws JspException {
065        return EVAL_BODY_BUFFERED;
066      }
067    
068      public void release() {
069        super.release();
070        style = null;
071      }
072    
073      public String getStyle() {
074        return style;
075      }
076    
077      /**
078       * Name of the stylsheet file to add to page.
079       */
080      @TagAttribute
081      public void setStyle(String style) {
082        this.style = style;
083      }
084    }
085