001    package railo.runtime.tag;
002    
003    import java.io.IOException;
004    
005    import railo.commons.lang.StringUtil;
006    import railo.runtime.PageContextImpl;
007    import railo.runtime.exp.ApplicationException;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.ext.tag.TagImpl;
010    import railo.runtime.op.Caster;
011    
012    /**
013    * Writes the text specified in the text attribute to the 'head' section of a generated HTML page. 
014    *        The cfhtmlhead tag can be useful for embedding JavaScript code, or placing other HTML tags such, as 
015    *        META, LINK, TITLE, or BASE in an HTML page header.
016    *
017    *
018    *
019    **/
020    public final class HtmlHead extends TagImpl {
021    
022            /** The text to add to the 'head' area of an HTML page. Everything inside the quotation marks is 
023            **              placed in the 'head' section */
024            private String text="";
025            private String variable="cfhtmlhead";
026            private String action=null;
027    
028    
029    
030            /**
031            * @see javax.servlet.jsp.tagext.Tag#release()
032            */
033            public void release()   {
034                    super.release();
035                    text="";
036                    variable="cfhtmlhead";
037                    action=null;
038            }
039            
040            /**
041             * @param variable the variable to set
042             */
043            public void setVariable(String variable) {
044                    this.variable = variable;
045            }
046    
047    
048            /**
049             * @param action the action to set
050             */
051            public void setAction(String action) {
052                    if(!StringUtil.isEmpty(action,true))
053                            this.action = action.trim().toLowerCase();
054                    
055                    
056            }
057    
058    
059            /** set the value text
060            *  The text to add to the 'head' area of an HTML page. Everything inside the quotation marks is 
061            *               placed in the 'head' section
062            * @param text value to set
063            **/
064            public void setText(String text)        {
065                    this.text=text;
066            }
067    
068    
069            /**
070            * @see javax.servlet.jsp.tagext.Tag#doStartTag()
071            */
072            public int doStartTag() throws PageException {
073                    try {
074                            if(StringUtil.isEmpty(action,true) || action.equals("append")) actionAppend();
075                            else if(action.equals("reset")) actionReset();
076                            else if(action.equals("write")) actionWrite();
077                            else if(action.equals("read")) actionRead();
078                    else throw new ApplicationException("invalid value ["+action+"] for attribute action","values for attribute action are:append,read,reset,write");
079                    } 
080                    catch (IOException e) {
081                            throw Caster.toPageException(e);
082                    }
083                    return SKIP_BODY;
084            }
085            
086            public void actionAppend()      throws IOException, ApplicationException {
087                    required("htmlhead", "text", text);
088                    ((PageContextImpl)pageContext).getRootOut().appendHTMLHead(text); 
089            }
090            
091            public void actionWrite()       throws IOException, ApplicationException {
092                    required("htmlhead", "text", text);
093                    ((PageContextImpl)pageContext).getRootOut().writeHTMLHead(text); 
094            }
095            
096            public void actionReset() throws IOException {
097                    ((PageContextImpl)pageContext).getRootOut().resetHTMLHead(); 
098            }
099            
100            public void actionRead() throws PageException, IOException {
101                    String str=((PageContextImpl)pageContext).getRootOut().getHTMLHead(); 
102                    pageContext.setVariable(variable, str);
103            }
104            
105    
106            /**
107            * @see javax.servlet.jsp.tagext.Tag#doEndTag()
108            */
109            public int doEndTag()   {
110                    return EVAL_PAGE;
111            }
112    }