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