1 5 package com.opensymphony.webwork.views.jsp.vui; 6 7 import com.opensymphony.webwork.components.Include; 8 import com.opensymphony.webwork.config.Configuration; 9 import com.opensymphony.webwork.util.ContainUtil; 10 import com.opensymphony.webwork.views.jsp.ParamTag; 11 import com.opensymphony.webwork.views.jsp.WebWorkBodyTagSupport; 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 import javax.servlet.jsp.JspException ; 18 import javax.servlet.jsp.JspTagException ; 19 import javax.servlet.jsp.JspWriter ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 24 30 public abstract class AbstractVUITag extends WebWorkBodyTagSupport implements ParamTag.Parametric { 31 33 private static Log log = LogFactory.getLog(AbstractVUITag.class); 35 36 38 protected Map params = new HashMap (); 39 protected String templateFooterAttr; 40 protected String templateHeaderAttr; 41 protected String theme; 42 protected String themeAttr; 43 44 46 public abstract String getFooterTemplate(); 47 48 public abstract String getHeaderTemplate(); 50 51 public String getBrowserUserAgent() { 52 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 53 String ua = request.getHeader("User-Agent"); 54 55 if (ua == null) { 56 ua = request.getHeader("user-agent"); 57 } 58 59 return ((ua == null) ? "" : ua); 60 } 61 62 public Map getParameters() { 63 return params; 64 } 65 66 public void setTemplateFooter(String aName) { 67 templateFooterAttr = aName; 68 } 69 70 public void setTemplateHeader(String aName) { 71 templateHeaderAttr = aName; 72 } 73 74 public void setTheme(String aName) { 75 themeAttr = aName; 76 } 77 78 public String getTheme() { 79 if ((theme == null) || (theme == "")) { 82 theme = (String ) pageContext.findAttribute("theme"); 83 } 84 85 if ((theme == null) || (theme == "")) { 87 theme = Configuration.getString("webwork.ui.theme"); 88 89 if (!theme.endsWith("/")) { 90 theme += "/"; 91 } 92 } 93 94 return theme; 95 } 96 97 public void addParameter(String name, Object value) { 98 addParameterInternal(name, value); 99 } 100 101 public int doAfterBody() throws JspException { 102 if (bodyContent != null) { 103 try { 104 JspWriter out = getPreviousOut(); 105 out.print(bodyContent.getString()); 106 bodyContent.clearBody(); 107 } catch (Exception ex) { 108 ex.printStackTrace(); 109 throw new JspTagException ("Exception:: " + toString(ex)); 110 } 111 112 return (EVAL_BODY_BUFFERED); 113 } 114 115 return (SKIP_BODY); 116 } 117 118 public int doEndTag() throws JspException { 119 if (themeAttr != null) { 120 theme = (String ) findValue(themeAttr); 121 } 122 123 getStack().push(this); 124 125 try { 126 String template = templateFooterAttr; 128 129 if (template == null) { 130 template = getFooterTemplate(); 131 } 132 133 Include.include(getTemplateDirectory() + template, pageContext.getOut(), 134 pageContext.getRequest(), (HttpServletResponse ) pageContext.getResponse()); 135 } catch (Exception ex) { 136 ex.printStackTrace(); 137 throw new JspTagException ("Exception including footer: " + toString(ex)); 138 } finally { 139 getStack().pop(); 140 params = new HashMap (); 141 } 142 143 return EVAL_PAGE; 144 } 145 146 public int doStartTag() { 148 if (themeAttr != null) { 149 theme = (String ) findValue(themeAttr); 150 } 151 152 initializeAttributes(); 153 154 getStack().push(this); 155 156 try { 157 String template = templateHeaderAttr; 159 160 if (template == null) { 161 template = getHeaderTemplate(); 162 } 163 164 Include.include(getTemplateDirectory() + template, pageContext.getOut(), 165 pageContext.getRequest(), (HttpServletResponse ) pageContext.getResponse()); 166 } catch (Exception ex) { 167 ex.printStackTrace(); 168 169 return SKIP_BODY; 170 } finally { 171 getStack().pop(); 172 } 173 174 return EVAL_BODY_INCLUDE; 175 } 176 177 public boolean memberOf(Object obj1, Object obj2) { 178 return ContainUtil.contains(obj1, obj2); 179 } 180 181 protected void getSetParameter(String a, String n) { 182 if (a != null) { 183 Object value = findValue(a); 184 185 if (value != null) { 186 addParameterInternal(n, value); 187 } 188 } 189 } 190 191 protected abstract void initializeAttributes(); 192 193 196 protected String getTemplateDirectory() { 197 String ua = getBrowserUserAgent(); 198 199 return BrowserSupport.getBrowserTemplateDirectory(ua); 200 } 201 202 private void addParameterInternal(String name, Object value) { 203 params.put(name, value); 204 } 205 } 206 | Popular Tags |