1 38 package com.gargoylesoftware.htmlunit.jelly; 39 40 import org.apache.commons.jelly.JellyTagException; 41 import org.apache.commons.jelly.MissingAttributeException; 42 import org.apache.commons.jelly.TagSupport; 43 44 import com.gargoylesoftware.htmlunit.Page; 45 import com.gargoylesoftware.htmlunit.WebClient; 46 import com.gargoylesoftware.htmlunit.html.HtmlPage; 47 48 54 public abstract class HtmlUnitTagSupport extends TagSupport { 55 56 private String var_; 57 private String page_; 58 59 60 63 public HtmlUnitTagSupport() { 64 } 65 66 67 71 public void setPage( final String page ) { 72 page_ = page; 73 } 74 75 76 81 protected final Page getPage() throws JellyTagException { 82 if( page_ == null ) { 83 throw new JellyTagException("page is a mandatory attribute"); 84 } 85 86 final Object object = getContext().getVariable(page_); 87 try { 88 return (Page)object; 89 } 90 catch( final ClassCastException e ) { 91 throw new JellyTagException("Expected page object in variable ["+page_+"] but found ["+object+"]"); 92 } 93 } 94 95 96 102 protected final HtmlPage getHtmlPage() throws JellyTagException { 103 final Page page = getPage(); 104 if( page instanceof HtmlPage ) { 105 return (HtmlPage)page; 106 } 107 else { 108 throw new JellyTagException("Page isn't an instance of HtmlPage: "+page.getClass().getName()); 109 } 110 } 111 112 113 117 public void setVar( final String var ) { 118 var_ = var; 119 } 120 121 122 127 public String getVarValueOrDie() throws MissingAttributeException { 128 if( var_ == null ) { 129 throw new MissingAttributeException("var"); 130 } 131 return var_; 132 } 133 134 135 140 public String getVarValueOrNull() throws MissingAttributeException { 141 return var_; 142 } 143 144 145 150 public WebClient getWebClient() throws JellyTagException { 151 final WebClientTag webClientTag = (WebClientTag) findAncestorWithClass(WebClientTag.class); 152 if( webClientTag == null ) { 153 throw new JellyTagException("Unable to determine webClient"); 154 } 155 else { 156 return webClientTag.getWebClient(); 157 } 158 } 159 } 160 | Popular Tags |