1 24 package org.ofbiz.widget.html; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.IOException ; 28 import java.io.Writer ; 29 import java.sql.Timestamp ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import javax.servlet.ServletContext ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import javax.servlet.http.HttpSession ; 37 38 import org.ofbiz.base.util.Debug; 39 import org.ofbiz.base.util.GeneralException; 40 import org.ofbiz.base.util.UtilDateTime; 41 import org.ofbiz.base.util.UtilFormatOut; 42 import org.ofbiz.base.util.UtilHttp; 43 import org.ofbiz.base.util.UtilMisc; 44 import org.ofbiz.base.util.UtilValidate; 45 import org.ofbiz.entity.GenericDelegator; 46 import org.ofbiz.entity.GenericEntityException; 47 import org.ofbiz.entity.GenericValue; 48 import org.ofbiz.webapp.control.RequestHandler; 49 import org.ofbiz.webapp.taglib.ContentUrlTag; 50 import org.ofbiz.webapp.view.ViewHandlerException; 51 import org.ofbiz.widget.WidgetContentWorker; 52 import org.ofbiz.widget.screen.ModelScreenWidget; 53 import org.ofbiz.widget.screen.ScreenStringRenderer; 54 55 62 public class HtmlScreenRenderer implements ScreenStringRenderer { 63 64 public static final String module = HtmlScreenRenderer.class.getName(); 65 66 public HtmlScreenRenderer() {} 67 68 public void renderSectionBegin(Writer writer, Map context, ModelScreenWidget.Section section) throws IOException { 69 } 71 public void renderSectionEnd(Writer writer, Map context, ModelScreenWidget.Section section) throws IOException { 72 } 74 75 public void renderContainerBegin(Writer writer, Map context, ModelScreenWidget.Container container) throws IOException { 76 writer.write("<div"); 77 78 String id = container.getId(context); 79 if (UtilValidate.isNotEmpty(id)) { 80 writer.write(" id=\""); 81 writer.write(id); 82 writer.write("\""); 83 } 84 85 String style = container.getStyle(context); 86 if (UtilValidate.isNotEmpty(style)) { 87 writer.write(" class=\""); 88 writer.write(style); 89 writer.write("\""); 90 } 91 92 writer.write(">"); 93 appendWhitespace(writer); 94 } 95 public void renderContainerEnd(Writer writer, Map context, ModelScreenWidget.Container container) throws IOException { 96 writer.write("</div>"); 97 appendWhitespace(writer); 98 } 99 100 public void renderLabel(Writer writer, Map context, ModelScreenWidget.Label label) throws IOException { 101 String style = label.getStyle(context); 103 String id = label.getId(context); 104 if (UtilValidate.isNotEmpty(style) || UtilValidate.isNotEmpty(id) ) { 105 writer.write("<span"); 106 107 if (UtilValidate.isNotEmpty(id)) { 108 writer.write(" id=\""); 109 writer.write(id); 110 writer.write("\""); 111 } 112 if (UtilValidate.isNotEmpty(style)) { 113 writer.write(" class=\""); 114 writer.write(style); 115 writer.write("\""); 116 } 117 writer.write(">"); 118 119 writer.write(label.getText(context)); 121 122 writer.write("</span>"); 124 125 } else { 126 writer.write(label.getText(context)); 127 } 128 129 appendWhitespace(writer); 130 } 131 132 public void renderLink(Writer writer, Map context, ModelScreenWidget.Link link) throws IOException { 133 writer.write("<a"); 135 String id = link.getId(context); 136 if (UtilValidate.isNotEmpty(id)) { 137 writer.write(" id=\""); 138 writer.write(id); 139 writer.write("\""); 140 } 141 String style = link.getStyle(context); 142 if (UtilValidate.isNotEmpty(style)) { 143 writer.write(" class=\""); 144 writer.write(style); 145 writer.write("\""); 146 } 147 String name = link.getName(context); 148 if (UtilValidate.isNotEmpty(name)) { 149 writer.write(" name=\""); 150 writer.write(name); 151 writer.write("\""); 152 } 153 String targetWindow = link.getTargetWindow(context); 154 if (UtilValidate.isNotEmpty(targetWindow)) { 155 writer.write(" target=\""); 156 writer.write(targetWindow); 157 writer.write("\""); 158 } 159 String target = link.getTarget(context); 160 if (UtilValidate.isNotEmpty(target)) { 161 writer.write(" HREF=\""); 162 String urlMode = link.getUrlMode(); 163 String prefix = link.getPrefix(context); 164 boolean fullPath = link.getFullPath(); 165 boolean secure = link.getSecure(); 166 boolean encode = link.getEncode(); 167 HttpServletResponse response = (HttpServletResponse ) context.get("response"); 168 HttpServletRequest request = (HttpServletRequest ) context.get("request"); 169 if (urlMode != null && urlMode.equalsIgnoreCase("intra-app")) { 170 if (request != null && response != null) { 171 ServletContext ctx = (ServletContext ) request.getAttribute("servletContext"); 172 RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); 173 String urlString = rh.makeLink(request, response, target, fullPath, secure, encode); 174 writer.write(urlString); 175 } else if (prefix != null) { 176 writer.write(prefix + target); 177 } else { 178 writer.write(target); 179 } 180 } else if (urlMode != null && urlMode.equalsIgnoreCase("content")) { 181 if (request != null && response != null) { 182 StringBuffer newURL = new StringBuffer (); 183 ContentUrlTag.appendContentPrefix(request, newURL); 184 newURL.append(target); 185 writer.write(newURL.toString()); 186 } 187 } else { 188 writer.write(target); 189 } 190 191 writer.write("\""); 192 } 193 writer.write(">"); 194 195 ModelScreenWidget.Image img = link.getImage(); 197 if (img == null) 198 writer.write(link.getText(context)); 199 else 200 renderImage(writer, context, img); 201 202 writer.write("</a>"); 204 205 appendWhitespace(writer); 206 } 207 208 public void renderImage(Writer writer, Map context, ModelScreenWidget.Image image) throws IOException { 209 writer.write("<img "); 211 String id = image.getId(context); 212 if (UtilValidate.isNotEmpty(id)) { 213 writer.write(" id=\""); 214 writer.write(id); 215 writer.write("\""); 216 } 217 String style = image.getStyle(context); 218 if (UtilValidate.isNotEmpty(style)) { 219 writer.write(" class=\""); 220 writer.write(style); 221 writer.write("\""); 222 } 223 String wid = image.getWidth(context); 224 if (UtilValidate.isNotEmpty(wid)) { 225 writer.write(" width=\""); 226 writer.write(wid); 227 writer.write("\""); 228 } 229 String hgt = image.getHeight(context); 230 if (UtilValidate.isNotEmpty(hgt)) { 231 writer.write(" height=\""); 232 writer.write(hgt); 233 writer.write("\""); 234 } 235 String border = image.getBorder(context); 236 if (UtilValidate.isNotEmpty(border)) { 237 writer.write(" border=\""); 238 writer.write(border); 239 writer.write("\""); 240 } 241 String src = image.getSrc(context); 242 if (UtilValidate.isNotEmpty(src)) { 243 writer.write(" SRC=\""); 244 String urlMode = image.getUrlMode(); 245 boolean fullPath = false; 246 boolean secure = false; 247 boolean encode = false; 248 HttpServletResponse response = (HttpServletResponse ) context.get("response"); 249 HttpServletRequest request = (HttpServletRequest ) context.get("request"); 250 if (urlMode != null && urlMode.equalsIgnoreCase("intra-app")) { 251 if (request != null && response != null) { 252 ServletContext ctx = (ServletContext ) request.getAttribute("servletContext"); 253 RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); 254 String urlString = rh.makeLink(request, response, src, fullPath, secure, encode); 255 writer.write(urlString); 256 } else { 257 writer.write(src); 258 } 259 } else if (urlMode != null && urlMode.equalsIgnoreCase("content")) { 260 if (request != null && response != null) { 261 StringBuffer newURL = new StringBuffer (); 262 ContentUrlTag.appendContentPrefix(request, newURL); 263 newURL.append(src); 264 writer.write(newURL.toString()); 265 } 266 } else { 267 writer.write(src); 268 } 269 270 writer.write("\""); 271 } 272 writer.write("/>"); 273 274 275 appendWhitespace(writer); 276 } 277 278 public void renderContentBegin(Writer writer, Map context, ModelScreenWidget.Content content) throws IOException { 279 String editRequest = content.getEditRequest(context); 280 String editContainerStyle = content.getEditContainerStyle(context); 281 String enableEditName = content.getEnableEditName(context); 282 String enableEditValue = (String )context.get(enableEditName); 283 284 if (Debug.verboseOn()) Debug.logVerbose("directEditRequest:" + editRequest, module); 285 286 if (UtilValidate.isNotEmpty(editRequest) && "true".equals(enableEditValue)) { 287 writer.write("<div"); 288 writer.write(" class=\"" + editContainerStyle + "\"> "); 289 appendWhitespace(writer); 290 } 291 } 292 293 public void renderContentBody(Writer writer, Map context, ModelScreenWidget.Content content) throws IOException { 294 Locale locale = Locale.getDefault(); 295 Boolean nullThruDatesOnly = new Boolean (false); 296 String mimeTypeId = "text/html"; 297 String expandedContentId = content.getContentId(context); 298 String renderedContent = null; 299 GenericDelegator delegator = (GenericDelegator) context.get("delegator"); 300 301 if (Debug.verboseOn()) Debug.logVerbose("expandedContentId:" + expandedContentId, module); 302 303 try { 304 if (UtilValidate.isNotEmpty(expandedContentId)) { 305 if (WidgetContentWorker.contentWorker != null) { 306 renderedContent = WidgetContentWorker.contentWorker.renderContentAsTextCacheExt(delegator, expandedContentId, context, null, locale, mimeTypeId); 307 } else { 308 Debug.logError("Not rendering content, not ContentWorker found.", module); 309 } 310 } 311 if (UtilValidate.isEmpty(renderedContent)) { 312 String editRequest = content.getEditRequest(context); 313 if (UtilValidate.isNotEmpty(editRequest)) { 314 if (WidgetContentWorker.contentWorker != null) { 315 WidgetContentWorker.contentWorker.renderContentAsTextCacheExt(delegator, "NOCONTENTFOUND", writer, context, null, locale, mimeTypeId); 316 } else { 317 Debug.logError("Not rendering content, not ContentWorker found.", module); 318 } 319 } 320 } else { 321 if (content.xmlEscape()) { 322 renderedContent = UtilFormatOut.encodeXmlValue(renderedContent); 323 } 324 325 writer.write(renderedContent); 326 } 327 328 } catch(GeneralException e) { 329 String errMsg = "Error rendering included content with id [" + expandedContentId + "] : " + e.toString(); 330 Debug.logError(e, errMsg, module); 331 } catch(IOException e2) { 333 String errMsg = "Error rendering included content with id [" + expandedContentId + "] : " + e2.toString(); 334 Debug.logError(e2, errMsg, module); 335 } 337 } 338 339 public void renderContentEnd(Writer writer, Map context, ModelScreenWidget.Content content) throws IOException { 340 341 String expandedContentId = content.getContentId(context); 343 String editMode = "Edit"; 344 String editRequest = content.getEditRequest(context); 345 String editContainerStyle = content.getEditContainerStyle(context); 346 String enableEditName = content.getEnableEditName(context); 347 String enableEditValue = (String )context.get(enableEditName); 348 if (editRequest != null && editRequest.toUpperCase().indexOf("IMAGE") > 0) { 349 editMode += " Image"; 350 } 351 Map params = (Map )context.get("parameters"); 352 354 if (UtilValidate.isNotEmpty(editRequest) && "true".equals(enableEditValue)) { 355 String contentId = content.getContentId(context); 356 HttpServletResponse response = (HttpServletResponse ) context.get("response"); 357 HttpServletRequest request = (HttpServletRequest ) context.get("request"); 358 if (request != null && response != null) { 359 if (editRequest.indexOf("?") < 0) editRequest += "?"; 360 else editRequest += "&"; 361 editRequest += "contentId=" + expandedContentId; 362 ServletContext ctx = (ServletContext ) request.getAttribute("servletContext"); 363 RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); 364 String urlString = rh.makeLink(request, response, editRequest, false, false, false); 365 String linkString = "<a HREF=\"" + urlString + "\">" + editMode + "</a>"; 366 writer.write(linkString); 367 } 368 if (UtilValidate.isNotEmpty(editContainerStyle)) { 369 writer.write("</div>"); 370 } 371 appendWhitespace(writer); 372 } 373 } 374 375 public void renderContentFrame(Writer writer, Map context, ModelScreenWidget.Content content) throws IOException { 376 377 String dataResourceId = content.getDataResourceId(context); 378 String urlString = "ViewSimpleContent?dataResourceId=" + dataResourceId; 379 380 String width = content.getWidth(); 381 String widthString=" width=\"" + width + "\""; 382 String height = content.getHeight(); 383 String heightString=" height=\"" + height + "\""; 384 String border = content.getBorder(); 385 String borderString = (UtilValidate.isNotEmpty(border)) ? " border=\"" + border + "\"" : ""; 386 387 HttpServletRequest request = (HttpServletRequest ) context.get("request"); 388 HttpServletResponse response = (HttpServletResponse ) context.get("response"); 389 if (request != null && response != null) { 390 ServletContext ctx = (ServletContext ) request.getAttribute("servletContext"); 391 RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); 392 String fullUrlString = rh.makeLink(request, response, urlString, true, false, false); 393 String linkString = "<iframe SRC=\"" + fullUrlString + "\" " + widthString + heightString + borderString + " />"; 394 writer.write(linkString); 395 } 396 397 } 398 399 400 public void renderSubContentBegin(Writer writer, Map context, ModelScreenWidget.SubContent content) throws IOException { 401 402 String editRequest = content.getEditRequest(context); 403 String editContainerStyle = content.getEditContainerStyle(context); 404 String enableEditName = content.getEnableEditName(context); 405 String enableEditValue = (String )context.get(enableEditName); 406 if (UtilValidate.isNotEmpty(editRequest) && "true".equals(enableEditValue)) { 407 writer.write("<div"); 408 writer.write(" class=\"" + editContainerStyle + "\"> "); 409 410 appendWhitespace(writer); 411 } 412 } 413 414 public void renderSubContentBody(Writer writer, Map context, ModelScreenWidget.SubContent content) throws IOException { 415 Locale locale = Locale.getDefault(); 416 Boolean nullThruDatesOnly = new Boolean (false); 417 String mimeTypeId = "text/html"; 418 String expandedContentId = content.getContentId(context); 419 String expandedAssocName = content.getAssocName(context); 420 String renderedContent = null; 421 GenericDelegator delegator = (GenericDelegator) context.get("delegator"); 422 Timestamp fromDate = UtilDateTime.nowTimestamp(); 423 HttpServletRequest request = (HttpServletRequest ) context.get("request"); 424 GenericValue userLogin = null; 425 if (request != null) { 426 HttpSession session = request.getSession(); 427 userLogin = (GenericValue)session.getAttribute("userLogin"); 428 } 429 Debug.logInfo("expandedContentId:" + expandedContentId, module); 430 try { 431 if (WidgetContentWorker.contentWorker != null) { 432 renderedContent = WidgetContentWorker.contentWorker.renderSubContentAsTextCacheExt(delegator, expandedContentId, expandedAssocName, null, context, locale, mimeTypeId, userLogin, fromDate); 433 } else { 434 Debug.logError("Not rendering content, not ContentWorker found.", module); 435 } 436 if (UtilValidate.isEmpty(renderedContent)) { 437 String editRequest = content.getEditRequest(context); 438 if (UtilValidate.isNotEmpty(editRequest)) { 439 if (WidgetContentWorker.contentWorker != null) { 440 WidgetContentWorker.contentWorker.renderContentAsTextCacheExt(delegator, "NOCONTENTFOUND", writer, context, null, locale, mimeTypeId); 441 } else { 442 Debug.logError("Not rendering content, not ContentWorker found.", module); 443 } 444 } 445 } else { 446 if (content.xmlEscape()) { 447 renderedContent = UtilFormatOut.encodeXmlValue(renderedContent); 448 } 449 450 writer.write(renderedContent); 451 } 452 453 } catch(GeneralException e) { 454 String errMsg = "Error rendering included content with id [" + expandedContentId + "] : " + e.toString(); 455 Debug.logError(e, errMsg, module); 456 } catch(IOException e2) { 458 String errMsg = "Error rendering included content with id [" + expandedContentId + "] : " + e2.toString(); 459 Debug.logError(e2, errMsg, module); 460 } 462 } 463 464 public void renderSubContentEnd(Writer writer, Map context, ModelScreenWidget.SubContent content) throws IOException { 465 466 String editMode = "Edit"; 467 String editRequest = content.getEditRequest(context); 468 String editContainerStyle = content.getEditContainerStyle(context); 469 String enableEditName = content.getEnableEditName(context); 470 String enableEditValue = (String )context.get(enableEditName); 471 String expandedContentId = content.getContentId(context); 472 String expandedAssocName = content.getAssocName(context); 473 Map params = (Map )context.get("parameters"); 474 if (editRequest != null && editRequest.toUpperCase().indexOf("IMAGE") > 0) { 475 editMode += " Image"; 476 } 477 if (UtilValidate.isNotEmpty(editRequest) && "true".equals(enableEditValue)) { 478 HttpServletResponse response = (HttpServletResponse ) context.get("response"); 479 HttpServletRequest request = (HttpServletRequest ) context.get("request"); 480 if (request != null && response != null) { 481 if (editRequest.indexOf("?") < 0) editRequest += "?"; 482 else editRequest += "&"; 483 editRequest += "contentId=" + expandedContentId; 484 if (UtilValidate.isNotEmpty(expandedAssocName)) { 485 editRequest += "&assocName=" + expandedAssocName; 486 } 487 HttpSession session = request.getSession(); 488 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 489 500 ServletContext ctx = (ServletContext ) request.getAttribute("servletContext"); 501 RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); 502 String urlString = rh.makeLink(request, response, editRequest, false, false, false); 503 String linkString = "<a HREF=\"" + urlString + "\">" + editMode + "</a>"; 504 writer.write(linkString); 505 } 506 if (UtilValidate.isNotEmpty(editContainerStyle)) { 507 writer.write("</div>"); 508 } 509 appendWhitespace(writer); 510 } 511 } 512 513 514 public void appendWhitespace(Writer writer) throws IOException { 515 writer.write("\r\n"); 517 } 519 } 520 | Popular Tags |