| 1 | /* |
| 2 | * @(#) $Id: ClassDefTagHandler.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.Slug; |
| 35 | import com.webhydra.slug.process.javascript.JavaScriptProcessStatusTranslator; |
| 36 | import javax.servlet.jsp.JspContext; |
| 37 | import javax.servlet.jsp.PageContext; |
| 38 | import javax.servlet.jsp.tagext.*; |
| 39 | import javax.servlet.jsp.JspWriter; |
| 40 | import javax.servlet.jsp.JspException; |
| 41 | import java.io.IOException; |
| 42 | |
| 43 | /** |
| 44 | * Genrates all Slug related JavaScript Classes.<p> |
| 45 | * Inline and externl format are supported based on the <code>src</code>. If |
| 46 | * <code>src</code> parameter is ommited classes are defined inline. When |
| 47 | * <code>src</code> is specified, then external javascript definition is generated.<br> |
| 48 | * Two different formationgs - HTML 4.1 and XHTML 1.0 are supported and determined |
| 49 | * based on the <code>xmlFormating</code> property. When it is defined and set to |
| 50 | * <code>true</code> XHTML 1.0 script is generated. In all other cases HTML 4.1 |
| 51 | * compatible script is produced.<br> |
| 52 | * <ul> |
| 53 | * Example: |
| 54 | * <li> External script definition in XHTML 1.0 format |
| 55 | * <pre> |
| 56 | * <%@taglib uri="http://slug.webhydra.com/jstl/slug" prefix="slug" %> |
| 57 | * <slug:script src="/myapp/slugJs.jsp" xmlFprmat="true" /> |
| 58 | * </pre> |
| 59 | * <li> Internal script definition in HTML 4.1 format |
| 60 | * <pre> |
| 61 | * <%@taglib uri="http://slug.webhydra.com/jstl/slug" prefix="slug" %> |
| 62 | * <slug:script /> |
| 63 | * </pre> |
| 64 | * </ul> |
| 65 | * @author rossen |
| 66 | * @version 1.0 |
| 67 | */ |
| 68 | |
| 69 | public class ClassDefTagHandler extends SimpleTagSupport |
| 70 | { |
| 71 | |
| 72 | /** |
| 73 | * Constant holding the Slug JavaScript definitions. |
| 74 | */ |
| 75 | public static final String JAVA_SCRIPT_SRC; |
| 76 | |
| 77 | // Initialize the JAVA_SCRIPT_SRC constant |
| 78 | static { |
| 79 | JavaScriptProcessStatusTranslator jts = new JavaScriptProcessStatusTranslator(); |
| 80 | JAVA_SCRIPT_SRC = jts.getClassDef() + "\n" + |
| 81 | "/**\n" + |
| 82 | " * SlugMgr class definition\n" + |
| 83 | " * @param updateFunc application update function name.\n" + |
| 84 | " * @param pid process ID.\n" + |
| 85 | " * @param slugUrl URL to the SlugServlet mapping.\n" + |
| 86 | " * @param translator translator name.\n" + |
| 87 | " * @param runner optional runner name. Detault is used if this parameer is not provided.\n" + |
| 88 | " * @return new SlugMgr instance.\n" + |
| 89 | " */\n" + |
| 90 | "function SlugMgr(updateFunc, pid, slugUrl, translator, runner) {\n" + |
| 91 | " // default refresh time of 1/2 second.\n" + |
| 92 | " this.refresh = 500;\n" + |
| 93 | " // set update call bask\n" + |
| 94 | " this.appUpdate = function (ps) { updateFunc(this, ps); };\n" + |
| 95 | " // set the default error call back\n" + |
| 96 | " this.appError = function (status) { alert ('Error server status: ' + status); };\n" + |
| 97 | "\n" + |
| 98 | " // calculate urls\n" + |
| 99 | " this.statusUrl = mkSlugURL(slugUrl, pid, '" + Slug.PATH_STATUS + "', translator, runner);\n" + |
| 100 | " this.cancelUrl = mkSlugURL(slugUrl, pid, '" + Slug.PATH_CANCEL + "', translator, runner);\n" + |
| 101 | " this.consumeUrl = mkSlugURL(slugUrl, pid, '" + Slug.PATH_CONSUME + "', translator, runner);\n" + |
| 102 | "\n" + |
| 103 | " // set default URL\n" + |
| 104 | " this.reqUrl = this.statusUrl;\n" + |
| 105 | " // set running status to not in use\n" + |
| 106 | " this.inProcess = false;\n" + |
| 107 | "\n" + |
| 108 | " // Process manaagement functions\n" + |
| 109 | " // cancel process call\n" + |
| 110 | " this.cancel = function () {this.reqUrl = this.cancelUrl;};\n" + |
| 111 | " // consume process call\n" + |
| 112 | " this.consume = function () {this.reqUrl = this.consumeUrl;};\n" + |
| 113 | " // initial startup call\n" + |
| 114 | " this.start = function() { doRequest(this); };\n" + |
| 115 | "\n" + |
| 116 | " // internal functions\n" + |
| 117 | " /**\n" + |
| 118 | " * Send request\n" + |
| 119 | " * @param mgr SlugMgr to act on\n" + |
| 120 | " */\n" + |
| 121 | " function doRequest(mgr) {\n" + |
| 122 | " // do not reenter if request is pending\n" + |
| 123 | " if (mgr.inProcess) {\n" + |
| 124 | " return;\n" + |
| 125 | " }\n" + |
| 126 | " mgr.inProcess = true;\n" + |
| 127 | "\n" + |
| 128 | " // obtain a request object\n" + |
| 129 | " var req = false;\n" + |
| 130 | " if (window.XMLHttpRequest) { // Mozilla/Safari\n" + |
| 131 | " req = new XMLHttpRequest();\n" + |
| 132 | " } else if (window.ActiveXObject) { // IE\n" + |
| 133 | " req = new ActiveXObject('Microsoft.XMLHTTP');\n" + |
| 134 | " }\n" + |
| 135 | " req.open('GET', mgr.reqUrl, true);\n" + |
| 136 | " req.onreadystatechange = function() {\n" + |
| 137 | " process(mgr, req);\n" + |
| 138 | " };\n" + |
| 139 | " req.send(null);\n" + |
| 140 | " }\n" + |
| 141 | "\n" + |
| 142 | " /**\n" + |
| 143 | " * Request processor\n" + |
| 144 | " * @param mgr SlugMgr to act on\n" + |
| 145 | " * @param req current XMLHttpRequest\n" + |
| 146 | " */\n" + |
| 147 | " function process(mgr, req) {\n" + |
| 148 | " mgr.inProcess = false;\n" + |
| 149 | " if (req.readyState == 4) {\n" + |
| 150 | " if (req.status != 200) {\n" + |
| 151 | " // TODO retry?!\n" + |
| 152 | " mgr.appError(req.status);\n" + |
| 153 | " return;\n" + |
| 154 | " }\n" + |
| 155 | " var prStatus;\n" + |
| 156 | " if (req.responseText.indexOf('<?xml') == 0) { // XML input\n" + |
| 157 | " prStatus = dom2ps(req.responseXML);\n" + |
| 158 | " } else { // raw JavaScrip input\n" + |
| 159 | " prStatus = eval(req.responseText);\n" + |
| 160 | " }\n" + |
| 161 | " mgr.appUpdate(prStatus);\n" + |
| 162 | " if (!prStatus.isCompleted()) { // call itself recursively\n" + |
| 163 | " setTimeout(function () { doRequest(mgr); }, mgr.refresh);\n" + |
| 164 | " }\n" + |
| 165 | " }\n" + |
| 166 | " }\n" + |
| 167 | "\n" + |
| 168 | " /**\n" + |
| 169 | " * Create slug URL\n" + |
| 170 | " * @param slugUrl path to Slug servlet's mapping.\n" + |
| 171 | " * @oaram pid Process ID.\n" + |
| 172 | " * @param action Slug action to call - one of \"status\", \"cancel\" or \"consume\".\n" + |
| 173 | " * @param runner optioanl runner name.\n" + |
| 174 | " * @return SlugUrl URL for provided parameters.\n" + |
| 175 | " */\n" + |
| 176 | " function mkSlugURL(slugUrl, pid, action, translator, runner) {\n" + |
| 177 | " var aURL = slugUrl + '/' + action;\n" + |
| 178 | " if (translator != undefined) {\n" + |
| 179 | " aURL = aURL + '/' + translator;\n" + |
| 180 | " }\n" + |
| 181 | " aURL = aURL + '/?" + Slug.PARAM_PID + "=' + pid;\n" + |
| 182 | " if (runner != undefined) {\n" + |
| 183 | " aURL = aURL + '&" + Slug.PARAM_RUNNER_NAME + "=' + runner;\n" + |
| 184 | " }\n" + |
| 185 | " return aURL;\n" + |
| 186 | " }\n" + |
| 187 | "\n" + |
| 188 | " /**\n" + |
| 189 | " * Obtain value of the first node.\n" + |
| 190 | " * @param dom XML DOM.\n" + |
| 191 | " * @parma nn node name.\n" + |
| 192 | " * @return the value of the first chiled of the first node with provided \n" + |
| 193 | " * name or undefined if no node is found.\n" + |
| 194 | " */\n" + |
| 195 | " function getNodeValue(dom, nn) {\n" + |
| 196 | " var s;\n" + |
| 197 | " var node = dom.getElementsByTagName(nn);\n" + |
| 198 | " if (node.length > 0) {\n" + |
| 199 | " s = node.item(0).firstChild.nodeValue;\n" + |
| 200 | " }\n" + |
| 201 | " return s;\n" + |
| 202 | " }\n" + |
| 203 | "\n" + |
| 204 | " /**\n" + |
| 205 | " * Converts XML DOM ProcessStatus to a SlugProcessStatus object.\n" + |
| 206 | " * @param dom XML Slug response.\n" + |
| 207 | " * @return SlugProcessStatus instance.\n" + |
| 208 | " */\n" + |
| 209 | " function dom2ps(dom) {\n" + |
| 210 | " var status = getNodeValue(dom, 'status');\n" + |
| 211 | " var message = getNodeValue(dom, 'message');\n" + |
| 212 | " var progress = getNodeValue(dom, 'progress');\n" + |
| 213 | " var ps = new " + jts.JAVASCRIPT_OBJECT_NAME + "(status, message, progress);\n" + |
| 214 | " return ps;\n" + |
| 215 | " }\n" + |
| 216 | "}\n" + |
| 217 | "\n"; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Optional JavaScript URL. |
| 222 | */ |
| 223 | private String src; |
| 224 | |
| 225 | /** |
| 226 | * Optional JavaScript formating. |
| 227 | */ |
| 228 | private boolean xmlFormat = false; |
| 229 | |
| 230 | /** |
| 231 | * Holds value of property rawScript. |
| 232 | */ |
| 233 | private boolean rawScript = false; |
| 234 | |
| 235 | /** |
| 236 | * Generates Slug related JavaScript opbjects. |
| 237 | */ |
| 238 | public void doTag() throws JspException |
| 239 | { |
| 240 | if (getJspContext().findAttribute(SlugTagCommon.JS_FORMAT_REQUEST_ATTRIBUTE) == null) |
| 241 | { |
| 242 | |
| 243 | try |
| 244 | { |
| 245 | String tag = (xmlFormat) ? SlugTagCommon.XHTML_SCRIPT_TAG : SlugTagCommon.HTML_SCRIPT_TAG; |
| 246 | String js; |
| 247 | if (src != null) |
| 248 | { |
| 249 | js = SlugTagCommon.externalScript(src, xmlFormat); |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | if (rawScript) |
| 254 | { |
| 255 | js = JAVA_SCRIPT_SRC; |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | js = SlugTagCommon.internalScript(JAVA_SCRIPT_SRC, xmlFormat); |
| 260 | } |
| 261 | } |
| 262 | getJspContext().getOut().println(js); |
| 263 | } |
| 264 | catch (java.io.IOException ex) |
| 265 | { |
| 266 | throw new JspException(ex.getMessage()); |
| 267 | } |
| 268 | // flag that Class definitions are already declared. |
| 269 | getJspContext().setAttribute(SlugTagCommon.JS_FORMAT_REQUEST_ATTRIBUTE, |
| 270 | new Boolean(xmlFormat), PageContext.REQUEST_SCOPE); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Setter for the src attribute. |
| 276 | * @param value new value for the <code>src</code> property. |
| 277 | */ |
| 278 | public void setSrc(String value) |
| 279 | { |
| 280 | this.src = value; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Setter for the xml attribute. |
| 285 | * @param value new value for the <code>xml</code> property. |
| 286 | */ |
| 287 | public void setXmlFormat(boolean value) |
| 288 | { |
| 289 | this.xmlFormat = value; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Setter for property rawScript. |
| 294 | * @param rawScript New value of property rawScript. |
| 295 | */ |
| 296 | public void setRawScript(boolean rawScript) { |
| 297 | this.rawScript = rawScript; |
| 298 | } |
| 299 | } |