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 @Override 031 public void release() { 032 super.release(); 033 text=""; 034 variable="cfhtmlhead"; 035 action=null; 036 } 037 038 /** 039 * @param variable the variable to set 040 */ 041 public void setVariable(String variable) { 042 this.variable = variable; 043 } 044 045 046 /** 047 * @param action the action to set 048 */ 049 public void setAction(String action) { 050 if(!StringUtil.isEmpty(action,true)) 051 this.action = action.trim().toLowerCase(); 052 053 054 } 055 056 057 /** set the value text 058 * The text to add to the 'head' area of an HTML page. Everything inside the quotation marks is 059 * placed in the 'head' section 060 * @param text value to set 061 **/ 062 public void setText(String text) { 063 this.text=text; 064 } 065 066 067 @Override 068 public int doStartTag() throws PageException { 069 try { 070 if(StringUtil.isEmpty(action,true) || action.equals("append")) actionAppend(); 071 else if(action.equals("reset")) actionReset(); 072 else if(action.equals("write")) actionWrite(); 073 else if(action.equals("read")) actionRead(); 074 else throw new ApplicationException("invalid value ["+action+"] for attribute action","values for attribute action are:append,read,reset,write"); 075 } 076 catch (IOException e) { 077 throw Caster.toPageException(e); 078 } 079 return SKIP_BODY; 080 } 081 082 public void actionAppend() throws IOException, ApplicationException { 083 required("htmlhead", "text", text); 084 ((PageContextImpl)pageContext).getRootOut().appendHTMLHead(text); 085 } 086 087 public void actionWrite() throws IOException, ApplicationException { 088 required("htmlhead", "text", text); 089 ((PageContextImpl)pageContext).getRootOut().writeHTMLHead(text); 090 } 091 092 public void actionReset() throws IOException { 093 ((PageContextImpl)pageContext).getRootOut().resetHTMLHead(); 094 } 095 096 public void actionRead() throws PageException, IOException { 097 String str=((PageContextImpl)pageContext).getRootOut().getHTMLHead(); 098 pageContext.setVariable(variable, str); 099 } 100 101 102 @Override 103 public int doEndTag() { 104 return EVAL_PAGE; 105 } 106 }