Author: lektran
Date: Thu Apr 8 23:14:17 2010 New Revision: 932172 URL: http://svn.apache.org/viewvc?rev=932172&view=rev Log: Fixed a bug in the macro form renderer where the writer being passed in to the render* methods was being ignored and the writer supplied in the constructor was being used instead, this causes problems when multiple writers are being used to render a page. Solved by ignoring the writer passed into the constructor (I'll deprecate that constructor and put in a new one shortly) and instead maintaining a separate freemarker environment for each appendable passed into the renderer using a WeakHashMap. Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java?rev=932172&r1=932171&r2=932172&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java (original) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java Thu Apr 8 23:14:17 2010 @@ -29,6 +29,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.WeakHashMap; import java.util.Map.Entry; import javax.servlet.ServletContext; @@ -89,7 +90,7 @@ public class MacroFormRenderer implement public static final String module = MacroFormRenderer.class.getName(); private Template macroLibrary; - private Environment environment; + private WeakHashMap<Appendable, Environment> environments = new WeakHashMap<Appendable, Environment>(); private StringUtil.SimpleEncoder internalEncoder; protected RequestHandler rh; protected HttpServletRequest request; @@ -99,8 +100,6 @@ public class MacroFormRenderer implement public MacroFormRenderer(String macroLibraryPath, Appendable writer, HttpServletRequest request, HttpServletResponse response) throws TemplateException, IOException { macroLibrary = FreeMarkerWorker.getTemplate(macroLibraryPath); - Map<String, Object> input = UtilMisc.toMap("key", null); - environment = FreeMarkerWorker.renderTemplate(macroLibrary, input, writer); this.request = request; this.response = response; ServletContext ctx = (ServletContext) request.getAttribute("servletContext"); @@ -119,25 +118,27 @@ public class MacroFormRenderer implement private void executeMacro(Appendable writer, String macro) throws IOException { try { + Environment environment = getEnvironment(writer); Reader templateReader = new StringReader(macro); // FIXME: I am using a Date as an hack to provide a unique name for the template... Template template = new Template((new java.util.Date()).toString(), templateReader, FreeMarkerWorker.getDefaultOfbizConfig()); templateReader.close(); - if (writer != null) { - Map<String, Object> input = UtilMisc.toMap("key", null); - Environment tmpEnvironment = FreeMarkerWorker.renderTemplate(macroLibrary, input, writer); - tmpEnvironment.include(template); - } else { - environment.include(template); - } + environment.include(template); } catch (TemplateException e) { Debug.logError(e, "Error rendering screen thru ftl", module); } catch (IOException e) { Debug.logError(e, "Error rendering screen thru ftl", module); } } - private void executeMacro(String macro) throws IOException { - executeMacro(null, macro); + + private Environment getEnvironment(Appendable writer) throws TemplateException, IOException { + Environment environment = environments.get(writer); + if (environment == null) { + Map<String, Object> input = UtilMisc.toMap("key", null); + environment = FreeMarkerWorker.renderTemplate(macroLibrary, input, writer); + environments.put(writer, environment); + } + return environment; } private void appendWhitespace(Appendable writer) throws IOException { @@ -170,7 +171,7 @@ public class MacroFormRenderer implement sr.append(labelText); sr.append("\""); sr.append(" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderDisplayField(Appendable writer, Map<String, Object> context, DisplayField displayField) throws IOException { @@ -322,7 +323,7 @@ public class MacroFormRenderer implement } sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); if (displayField instanceof DisplayEntityField) { makeHyperlinkString(writer,((DisplayEntityField) displayField).getSubHyperlink(),context); } @@ -410,7 +411,7 @@ public class MacroFormRenderer implement sr.append("\" ajaxEnabled="); sr.append(Boolean.toString(ajaxEnabled)); sr.append(" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); ModelFormField.SubHyperlink subHyperlink = textField.getSubHyperlink(); if (subHyperlink != null && subHyperlink.shouldUse(context)) { @@ -471,7 +472,7 @@ public class MacroFormRenderer implement sr.append("\" buttons=\""); sr.append(buttons); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.addAsterisks(writer, context, modelFormField); this.appendTooltip(writer, context, modelFormField); } @@ -656,7 +657,7 @@ public class MacroFormRenderer implement sr.append("\" formName=\""); sr.append(formName); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.addAsterisks(writer, context, modelFormField); this.appendTooltip(writer, context, modelFormField); } @@ -868,7 +869,7 @@ public class MacroFormRenderer implement sr.append("\" fullSearch=\""); sr.append(fullSearch); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); ModelFormField.SubHyperlink subHyperlink = dropDownField .getSubHyperlink(); if (subHyperlink != null && subHyperlink.shouldUse(context)) { @@ -931,7 +932,7 @@ public class MacroFormRenderer implement sr.append(action); } sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.appendTooltip(writer, context, modelFormField); } @@ -990,7 +991,7 @@ public class MacroFormRenderer implement sr.append(action); } sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.appendTooltip(writer, context, modelFormField); } @@ -1067,7 +1068,7 @@ public class MacroFormRenderer implement sr.append(ajaxUrl); } sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.appendTooltip(writer, context, modelFormField); } @@ -1095,7 +1096,7 @@ public class MacroFormRenderer implement sr.append(" title=\""); sr.append(title); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.appendTooltip(writer, context, modelFormField); } @@ -1129,7 +1130,7 @@ public class MacroFormRenderer implement sr.append(action); } sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderIgnoredField(Appendable writer, Map<String, Object> context, IgnoredField ignoredField) { @@ -1169,7 +1170,7 @@ public class MacroFormRenderer implement sr.append("\" title=\""); sr.append(FreeMarkerWorker.encodeDoubleQuotes(title)); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } else if (modelFormField.isSortField()) { renderSortField(writer, context, modelFormField, titleText); } else if (modelFormField.isRowSubmit()) { @@ -1180,7 +1181,7 @@ public class MacroFormRenderer implement sr.append("\" title=\""); sr.append(titleText); sr.append("\" showSelectAll=\"Y\"/>"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } else { sb.append(titleText); } @@ -1202,7 +1203,7 @@ public class MacroFormRenderer implement sr.append("\" title=\""); sr.append(sb.toString()); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } } @@ -1248,7 +1249,7 @@ public class MacroFormRenderer implement sr.append("\" useRowSubmit="); sr.append(Boolean.toString(useRowSubmit)); sr.append(" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { @@ -1261,7 +1262,7 @@ public class MacroFormRenderer implement sr.append("\" formName=\""); sr.append(formName); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); renderEndingBoundaryComment(writer, "Form Widget - Form Element", modelForm); } @@ -1354,7 +1355,7 @@ public class MacroFormRenderer implement sr.append("\" columnStyles=["); sr.append(columnStyleListString); sr.append("] />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } @@ -1364,7 +1365,7 @@ public class MacroFormRenderer implement sr.append(" formName=\""); sr.append(modelForm.getName()); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); if (this.renderPagination) { this.renderNextPrev(writer, context, modelForm); } @@ -1378,13 +1379,13 @@ public class MacroFormRenderer implement sr.append(" style=\""); sr.append(headerStyle); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatHeaderRowClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatHeaderRowClose />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatHeaderRowCellOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm, ModelFormField modelFormField, int positionSpan) throws IOException { @@ -1396,13 +1397,13 @@ public class MacroFormRenderer implement sr.append("\" positionSpan="); sr.append(Integer.toString(positionSpan)); sr.append(" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatHeaderRowCellClose(Appendable writer, Map<String, Object> context, ModelForm modelForm, ModelFormField modelFormField) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatHeaderRowCellClose />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatHeaderRowFormCellOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { @@ -1412,13 +1413,13 @@ public class MacroFormRenderer implement sr.append(" style=\""); sr.append(areaStyle); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatHeaderRowFormCellClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatHeaderRowFormCellClose />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatHeaderRowFormCellTitleSeparator(Appendable writer, Map<String, Object> context, ModelForm modelForm, ModelFormField modelFormField, boolean isLast) throws IOException { @@ -1430,7 +1431,7 @@ public class MacroFormRenderer implement sr.append("\" isLast="); sr.append(Boolean.toString(isLast)); sr.append(" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatItemRowOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { @@ -1459,7 +1460,7 @@ public class MacroFormRenderer implement sr.append("\" oddRowStyle=\""); sr.append(oddRowStyle); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatItemRowClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { @@ -1468,7 +1469,7 @@ public class MacroFormRenderer implement sr.append(" formName=\""); sr.append(modelForm.getName()); sr.append("\"/>"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatItemRowCellOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm, ModelFormField modelFormField, int positionSpan) throws IOException { @@ -1482,7 +1483,7 @@ public class MacroFormRenderer implement sr.append("\" positionSpan="); sr.append(Integer.toString(positionSpan)); sr.append(" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatItemRowCellClose(Appendable writer, Map<String, Object> context, ModelForm modelForm, ModelFormField modelFormField) throws IOException { @@ -1491,7 +1492,7 @@ public class MacroFormRenderer implement sr.append(" fieldName=\""); sr.append(modelFormField.getName()); sr.append("\"/>"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatItemRowFormCellOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { @@ -1501,13 +1502,13 @@ public class MacroFormRenderer implement sr.append(" style=\""); sr.append(areaStyle); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatItemRowFormCellClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatItemRowFormCellClose />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatSingleWrapperOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { @@ -1519,7 +1520,7 @@ public class MacroFormRenderer implement sr.append("\" style=\""); sr.append(style); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatSingleWrapperClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { @@ -1528,19 +1529,19 @@ public class MacroFormRenderer implement sr.append(" formName=\""); sr.append(modelForm.getName()); sr.append("\"/>"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatFieldRowOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatFieldRowOpen />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatFieldRowClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatFieldRowClose />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatFieldRowTitleCellOpen(Appendable writer, Map<String, Object> context, ModelFormField modelFormField) throws IOException { @@ -1550,13 +1551,13 @@ public class MacroFormRenderer implement sr.append(" style=\""); sr.append(style); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatFieldRowTitleCellClose(Appendable writer, Map<String, Object> context, ModelFormField modelFormField) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatFieldRowTitleCellClose />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatFieldRowSpacerCell(Appendable writer, Map<String, Object> context, ModelFormField modelFormField) throws IOException { @@ -1571,20 +1572,20 @@ public class MacroFormRenderer implement sr.append(" style=\""); sr.append(areaStyle); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatFieldRowWidgetCellClose(Appendable writer, Map<String, Object> context, ModelFormField modelFormField, int positions, int positionSpan, Integer nextPositionInRow) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatFieldRowWidgetCellClose />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFormatEmptySpace(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException { StringWriter sr = new StringWriter(); sr.append("<@renderFormatEmptySpace />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderTextFindField(Appendable writer, Map<String, Object> context, TextFindField textFindField) throws IOException { @@ -1674,7 +1675,7 @@ public class MacroFormRenderer implement sr.append(" ignoreCase=\""); sr.append(ignoreCase); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.appendTooltip(writer, context, modelFormField); } @@ -1760,7 +1761,7 @@ public class MacroFormRenderer implement sr.append("\" defaultOptionThru=\""); sr.append(defaultOptionThru); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.appendTooltip(writer, context, modelFormField); } @@ -1895,7 +1896,7 @@ public class MacroFormRenderer implement sr.append("\" opIsEmpty=\""); sr.append(opIsEmpty); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.appendTooltip(writer, context, modelFormField); } @@ -2060,7 +2061,7 @@ public class MacroFormRenderer implement sr.append("\" clearText=\""); sr.append(clearText); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.addAsterisks(writer, context, modelFormField); @@ -2318,7 +2319,7 @@ public class MacroFormRenderer implement sr.append("\" paginateViewSizeLabel=\""); sr.append(paginateViewSizeLabel); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFileField(Appendable writer, Map<String, Object> context, FileField textField) throws IOException { @@ -2365,7 +2366,7 @@ public class MacroFormRenderer implement sr.append("\" autocomplete=\""); sr.append(autocomplete); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.makeHyperlinkString(writer, textField.getSubHyperlink(), context); @@ -2424,7 +2425,7 @@ public class MacroFormRenderer implement sr.append("\" autocomplete=\""); sr.append(autocomplete); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.addAsterisks(writer, context, modelFormField); @@ -2488,7 +2489,7 @@ public class MacroFormRenderer implement sr.append("\" action=\""); sr.append(action==null?"":action); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); this.makeHyperlinkString(writer, imageField.getSubHyperlink(), context); this.appendTooltip(writer, context, modelFormField); @@ -2536,7 +2537,7 @@ public class MacroFormRenderer implement sr.append("\" collapseToolTip=\""); sr.append(collapseToolTip); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderFieldGroupClose(Appendable writer, Map<String, Object> context, ModelForm.FieldGroup fieldGroup) throws IOException { @@ -2559,7 +2560,7 @@ public class MacroFormRenderer implement sr.append(title); } sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderBanner(Appendable writer, Map<String, Object> context, ModelForm.Banner banner) throws IOException { @@ -2599,7 +2600,7 @@ public class MacroFormRenderer implement sr.append("\" rightText=\""); sr.append(rightText); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } @@ -2620,7 +2621,7 @@ public class MacroFormRenderer implement sr.append("\" widgetName=\""); sr.append(modelWidget.getBoundaryCommentName()); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } } @@ -2641,7 +2642,7 @@ public class MacroFormRenderer implement sr.append("\" widgetName=\""); sr.append(modelWidget.getBoundaryCommentName()); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } } @@ -2730,7 +2731,7 @@ public class MacroFormRenderer implement sr.append("\" ajaxEnabled="); sr.append(Boolean.toString(ajaxEnabled)); sr.append(" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } /** Create an ajaxXxxx JavaScript CSV string from a list of UpdateArea objects. See @@ -2789,7 +2790,7 @@ public class MacroFormRenderer implement sr.append("\" tooltipStyle=\""); sr.append(modelFormField.getTooltipStyle()); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void makeHyperlinkString(Appendable writer, ModelFormField.SubHyperlink subHyperlink, Map<String, Object> context) throws IOException { if (subHyperlink == null) { @@ -2816,7 +2817,7 @@ public class MacroFormRenderer implement sr.append("\" requiredStyle=\""); sr.append(requiredStyle); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void appendContentUrl(Appendable writer, String location) throws IOException { StringBuilder buffer = new StringBuilder(); @@ -2912,7 +2913,7 @@ public class MacroFormRenderer implement sr.append("\" confirmation =\""); sr.append(confirmation ); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } } @@ -2949,7 +2950,7 @@ public class MacroFormRenderer implement sr.append("\" confirmation =\""); sr.append(confirmation ); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } } @@ -2983,7 +2984,7 @@ public class MacroFormRenderer implement sr.append(" targetWindow=\""); sr.append(targetWindow); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } public void renderContainerFindField(Appendable writer, @@ -2999,6 +3000,6 @@ public class MacroFormRenderer implement sr.append("\" class=\""); sr.append(className); sr.append("\" />"); - executeMacro(sr.toString()); + executeMacro(writer, sr.toString()); } } |
Free forum by Nabble | Edit this page |