| 1 | /* |
| 2 | * @(#) $Id: SlugMgrTagHandler.java,v 1.1.1.1 2006/03/19 06:09:42 rossen Exp $ |
| 3 | * |
| 4 | * Copyright (c) 2006, WebHydra.com |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * * Neither the name of the WebHydra.com nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | package com.webhydra.slug.tld; |
| 33 | |
| 34 | import com.webhydra.slug.process.PidGenerator; |
| 35 | import java.io.BufferedOutputStream; |
| 36 | import java.io.StringWriter; |
| 37 | import javax.servlet.jsp.tagext.*; |
| 38 | import javax.servlet.jsp.JspWriter; |
| 39 | import javax.servlet.jsp.JspException; |
| 40 | |
| 41 | /** |
| 42 | * Generates SlugMgr JavaScript instance.<p> |
| 43 | * @author rossen |
| 44 | * @version 1.0 |
| 45 | */ |
| 46 | |
| 47 | public class SlugMgrTagHandler extends SimpleTagSupport { |
| 48 | |
| 49 | /** |
| 50 | * Initialization of pid property. |
| 51 | */ |
| 52 | private String pid; |
| 53 | |
| 54 | /** |
| 55 | * Initialization of updateFunction property. |
| 56 | */ |
| 57 | private String updateFunction; |
| 58 | |
| 59 | /** |
| 60 | * Initialization of url property. |
| 61 | */ |
| 62 | private String url; |
| 63 | |
| 64 | /** |
| 65 | * Initialization of translator property. |
| 66 | */ |
| 67 | private String translator; |
| 68 | |
| 69 | /** |
| 70 | * Initialization of runner property. |
| 71 | */ |
| 72 | private String runner; |
| 73 | |
| 74 | /** |
| 75 | * Initialization of refresh property. |
| 76 | */ |
| 77 | private int refresh; |
| 78 | |
| 79 | /** |
| 80 | * Initialization of errorFunction property. |
| 81 | */ |
| 82 | private String errorFunction; |
| 83 | |
| 84 | /** |
| 85 | * Holds value of property name. |
| 86 | */ |
| 87 | private String name; |
| 88 | |
| 89 | /** |
| 90 | * Checks if provided parameter is defined and not empty string. |
| 91 | * @param s string to check. |
| 92 | * @return true if <code>s</code> is not null and not blank. |
| 93 | * @see #isEmpty(String) |
| 94 | */ |
| 95 | boolean isNotEmpty(String s) |
| 96 | { |
| 97 | return !isEmpty(s); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Checks if provided parameter is null or empty string. |
| 102 | * @param s string to check. |
| 103 | * @return true if <code>s</code> null or blank. |
| 104 | */ |
| 105 | boolean isEmpty(String s) |
| 106 | { |
| 107 | return ((s == null) || (s.trim().length() == 0)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Generate name based on <code>pid</code>. |
| 112 | * @return name generated from current pid. |
| 113 | */ |
| 114 | String mkName() |
| 115 | { |
| 116 | return "slug_pid_" + pid.replace(PidGenerator.SEPARATOR, '_'); |
| 117 | } |
| 118 | |
| 119 | /**Called by the container to invoke this tag. |
| 120 | * The implementation of this method is provided by the tag library developer, |
| 121 | * and handles all tag processing, body iteration, etc. |
| 122 | * |
| 123 | * @exception JspException if <code>"script"</code> tag definition is missing |
| 124 | * or in case of invalid parameters. |
| 125 | */ |
| 126 | public void doTag() throws JspException |
| 127 | { |
| 128 | Boolean xmlFormat = (Boolean) getJspContext().findAttribute(SlugTagCommon.JS_FORMAT_REQUEST_ATTRIBUTE); |
| 129 | if (xmlFormat == null) |
| 130 | { |
| 131 | throw new JspException("Required \"script\" tag is not declared!"); |
| 132 | } |
| 133 | |
| 134 | try |
| 135 | { |
| 136 | StringBuffer body = new StringBuffer(); |
| 137 | // TODO the js class implementation |
| 138 | JspFragment f = getJspBody(); |
| 139 | String bodyTag = ""; |
| 140 | // Evaluate tag's body if present |
| 141 | if (f != null) |
| 142 | { |
| 143 | StringWriter out = new StringWriter(); |
| 144 | // include provided body |
| 145 | f.invoke(out); |
| 146 | bodyTag = out.toString(); |
| 147 | } |
| 148 | boolean hasBody = isNotEmpty(bodyTag); |
| 149 | boolean hasUpdateFunction = isNotEmpty(updateFunction); |
| 150 | if (!hasUpdateFunction && !hasBody) |
| 151 | { |
| 152 | throw new JspException("Please provide \"updateFunction\" attribute or define its implementation as a body for the tag!"); |
| 153 | } |
| 154 | if (isEmpty(pid)) |
| 155 | { |
| 156 | throw new JspException("Attribute \"pid\" is required!"); |
| 157 | } |
| 158 | if (isEmpty(url)) |
| 159 | { |
| 160 | throw new JspException("Attribute \"url\" is required!"); |
| 161 | } |
| 162 | // define SlugMgr object instance |
| 163 | String nameToUse = (isEmpty(name)) ? mkName() : name; |
| 164 | String updateFuncToUse = (hasUpdateFunction) ? updateFunction : "slug_update_" + nameToUse; |
| 165 | body.append("var ").append(nameToUse).append(" = new SlugMgr(").append(updateFuncToUse); |
| 166 | body.append(", '").append(pid).append("', '").append(url).append("'"); |
| 167 | if (isNotEmpty(translator)) |
| 168 | { |
| 169 | body.append(", '").append(translator).append("'"); |
| 170 | } |
| 171 | if (isNotEmpty(runner)) |
| 172 | { |
| 173 | body.append(", '").append(runner).append("'"); |
| 174 | } |
| 175 | body.append(");\n"); |
| 176 | // process tag's bpdy if present |
| 177 | if (hasBody) |
| 178 | { |
| 179 | if (!hasUpdateFunction) |
| 180 | { |
| 181 | // update function not provided. Declare it! |
| 182 | body.append("function ").append(updateFuncToUse).append("(smgr, ps) {\n"); |
| 183 | } |
| 184 | // use it as a initialization block if updateFunction is already defined. |
| 185 | body.append(bodyTag); |
| 186 | if (!hasUpdateFunction) |
| 187 | { |
| 188 | // Close function's declaration. |
| 189 | body.append("\n}\n"); |
| 190 | } |
| 191 | } |
| 192 | if (refresh > 0) |
| 193 | { |
| 194 | // optianlly set the refresh time. |
| 195 | body.append(nameToUse).append(".refresh = ").append(refresh).append(";\n"); |
| 196 | } |
| 197 | if (isNotEmpty(errorFunction)) |
| 198 | { |
| 199 | // optianlly set the error function. |
| 200 | body.append(nameToUse).append(".appError = ").append(errorFunction).append(";\n"); |
| 201 | } |
| 202 | // start the object |
| 203 | body.append("setTimeout('").append(nameToUse).append(".start()', ").append(nameToUse).append(".refresh);\n"); |
| 204 | |
| 205 | // print the generated script |
| 206 | getJspContext().getOut().print( |
| 207 | SlugTagCommon.internalScript(body.toString(), xmlFormat.booleanValue())); |
| 208 | } |
| 209 | catch (java.io.IOException ex) |
| 210 | { |
| 211 | throw new JspException(ex.getMessage()); |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Setter for the pid attribute. |
| 218 | */ |
| 219 | public void setPid(String value) { |
| 220 | this.pid = value; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Setter for the updateFunction attribute. |
| 225 | */ |
| 226 | public void setUpdateFunction(String value) { |
| 227 | this.updateFunction = value; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Setter for the url attribute. |
| 232 | */ |
| 233 | public void setUrl(String value) { |
| 234 | this.url = value; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Setter for the translator attribute. |
| 239 | */ |
| 240 | public void setTranslator(String value) { |
| 241 | this.translator = value; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Setter for the runner attribute. |
| 246 | */ |
| 247 | public void setRunner(String value) { |
| 248 | this.runner = value; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Setter for the refresh attribute. |
| 253 | */ |
| 254 | public void setRefresh(int value) { |
| 255 | this.refresh = value; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Setter for the errorFunction attribute. |
| 260 | */ |
| 261 | public void setErrorFunction(String value) { |
| 262 | this.errorFunction = value; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Setter for property name. |
| 267 | * @param name New value of property name. |
| 268 | */ |
| 269 | public void setName(String name) { |
| 270 | this.name = name; |
| 271 | } |
| 272 | } |