Author: jacopoc
Date: Sun Sep 25 17:20:58 2016 New Revision: 1762227 URL: http://svn.apache.org/viewvc?rev=1762227&view=rev Log: Improved: cleanups and enhancements in the FreeMarkerWorker class, and client code using it, that wraps most of the OFBiz integration with FreeMarker. This is the list of the main modifications: * simplified and cleaned up the public methods of FreeMarkerWorker, used to retrieve and render Freemarker templates and changed client code accordingly to use them * removed unused methods in FreeMarkerWorker and made some others private * improved the integration code in FreeMarkerWorker to better use the Freemarker API and specifically to leverage the various TemplateLoaders and the Freemarker caching mechanism; it is now possible to switch the OFBiz legacy template caching mechanism to use the Freemarker one instead * improved the implementation of Freemarker template rendering from strings (used by DataResourceWorker): it now leverages the Freemarker's StringTemplateLoader that provides the ability to cache the strings, retrieved from DataResources records, based on the timestamp of the last modification * moved freemarkerImports.properties from "widget" to "base" component, and changed its content (and the content of the associated templates AutoImportTemplate.ftl and HtmlTemplate.ftl) to remove the dependency from base to widget&common; some resources of "widget" and "common" are still referenced from AutoImportTemplate.ftl (that is in "base") but even if they are soft dependencies: if they are missing the system will load properly without any error or warning; before this change it was impossible to use, or unit test, FreeMarkerWorker before the "widget" and "common" components were loaded by the system, now it is possible * created a new class for unit tests for FreeMarkerWorker, named FreeMarkerWorkerTests: at the moment it contains just one simple unit test but more should be implemented * refactored WebToolsServices.entityImport(...) to leverage the FreeMarkerWorker.renderTemplate(...) method to run the Freemarker template, rather than dealing with the Freemarker API directly; this is now possible thanks to the cleanups and improvements done in the FreeMarkerWorker class; this same approach should be implemented for a few other similar integration points (mostly in the "content" component); this is a TODO item * moved the encodeDoubleQuotes(...) method from FreeMarkerWorker to MacroFormRenderer and made it private since this is the only calss using it and its logic is not related to FreeMarker Added: ofbiz/trunk/framework/base/config/AutoImportTemplate.ftl (with props) ofbiz/trunk/framework/base/config/freemarkerImports.properties - copied, changed from r1762109, ofbiz/trunk/framework/widget/config/freemarkerImports.properties ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/ ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/FreeMarkerWorkerTests.java (with props) Removed: ofbiz/trunk/framework/widget/config/freemarkerImports.properties Modified: ofbiz/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java ofbiz/trunk/applications/product/src/main/java/org/apache/ofbiz/shipment/thirdparty/fedex/FedexServices.java ofbiz/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java ofbiz/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java ofbiz/trunk/framework/common/template/includes/HtmlTemplate.ftl ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/FreeMarkerViewHandler.java ofbiz/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/WebToolsServices.java ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java Modified: ofbiz/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java (original) +++ ofbiz/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java Sun Sep 25 17:20:58 2016 @@ -29,6 +29,7 @@ import java.io.Writer; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; +import java.sql.Timestamp; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedList; @@ -578,7 +579,7 @@ public class DataResourceWorker impleme if (dataResource != null) { String dataTemplateTypeId = dataResource.getString("dataTemplateTypeId"); if ("FTL".equals(dataTemplateTypeId)) { - FreeMarkerWorker.clearTemplateFromCache(delegator.getDelegatorName() + ":DataResource:" + dataResourceId); + FreeMarkerWorker.clearTemplateFromCache("delegator:" + delegator.getDelegatorName() + ":DataResource:" + dataResourceId); } } } @@ -656,7 +657,8 @@ public class DataResourceWorker impleme // render the FTL template boolean useTemplateCache = cache && !UtilProperties.getPropertyAsBoolean("content", "disable.ftl.template.cache", false); - FreeMarkerWorker.renderTemplate(delegator.getDelegatorName() + ":DataResource:" + dataResourceId, templateText, templateContext, out, useTemplateCache); + Timestamp lastUpdatedStamp = dataResource.getTimestamp("lastUpdatedStamp"); + FreeMarkerWorker.renderTemplateFromString("delegator:" + delegator.getDelegatorName() + ":DataResource:" + dataResourceId, templateText, templateContext, out, lastUpdatedStamp.getTime(), useTemplateCache); } catch (TemplateException e) { throw new GeneralException("Error rendering FTL template", e); } @@ -899,7 +901,7 @@ public class DataResourceWorker impleme String location = mimeTypeTemplate.getString("templateLocation"); StringWriter writer = new StringWriter(); try { - FreeMarkerWorker.renderTemplateAtLocation(location, context, writer); + FreeMarkerWorker.renderTemplate(location, context, writer); } catch (TemplateException e) { throw new GeneralException(e.getMessage(), e); } Modified: ofbiz/trunk/applications/product/src/main/java/org/apache/ofbiz/shipment/thirdparty/fedex/FedexServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/main/java/org/apache/ofbiz/shipment/thirdparty/fedex/FedexServices.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/main/java/org/apache/ofbiz/shipment/thirdparty/fedex/FedexServices.java (original) +++ ofbiz/trunk/applications/product/src/main/java/org/apache/ofbiz/shipment/thirdparty/fedex/FedexServices.java Sun Sep 25 17:20:58 2016 @@ -334,7 +334,7 @@ public class FedexServices { StringWriter outWriter = new StringWriter(); try { - FreeMarkerWorker.renderTemplateAtLocation(templateLocation, subscriptionRequestContext, outWriter); + FreeMarkerWorker.renderTemplate(templateLocation, subscriptionRequestContext, outWriter); } catch (Exception e) { String errorMessage = "Cannot send Fedex subscription request: Failed to render Fedex XML Subscription Request Template [" + templateLocation + "]."; Debug.logError(e, errorMessage, module); @@ -925,7 +925,7 @@ public class FedexServices { StringWriter outWriter = new StringWriter(); try { - FreeMarkerWorker.renderTemplateAtLocation(templateLocation, shipRequestContext, outWriter); + FreeMarkerWorker.renderTemplate(templateLocation, shipRequestContext, outWriter); } catch (Exception e) { String errorMessage = "Cannot confirm Fedex shipment: Failed to render Fedex XML Ship Request Template [" + templateLocation + "]."; Debug.logError(e, errorMessage, module); Added: ofbiz/trunk/framework/base/config/AutoImportTemplate.ftl URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/config/AutoImportTemplate.ftl?rev=1762227&view=auto ============================================================================== --- ofbiz/trunk/framework/base/config/AutoImportTemplate.ftl (added) +++ ofbiz/trunk/framework/base/config/AutoImportTemplate.ftl Sun Sep 25 17:20:58 2016 @@ -0,0 +1,21 @@ +<#-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<#include "component://widget/templates/HtmlFormMacroLibrary.ftl" ignore_missing=true /> +<#include "component://common/template/includes/HtmlTemplate.ftl" ignore_missing=true /> Propchange: ofbiz/trunk/framework/base/config/AutoImportTemplate.ftl ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/base/config/AutoImportTemplate.ftl ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/trunk/framework/base/config/AutoImportTemplate.ftl ------------------------------------------------------------------------------ svn:mime-type = text/plain Copied: ofbiz/trunk/framework/base/config/freemarkerImports.properties (from r1762109, ofbiz/trunk/framework/widget/config/freemarkerImports.properties) URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/config/freemarkerImports.properties?p2=ofbiz/trunk/framework/base/config/freemarkerImports.properties&p1=ofbiz/trunk/framework/widget/config/freemarkerImports.properties&r1=1762109&r2=1762227&rev=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/config/freemarkerImports.properties (original) +++ ofbiz/trunk/framework/base/config/freemarkerImports.properties Sun Sep 25 17:20:58 2016 @@ -17,4 +17,4 @@ # under the License. ############################################################################### -htmlTemplate=component://common/template/includes/HtmlTemplate.ftl +htmlTemplate=ofbizhome://framework/base/config/AutoImportTemplate.ftl Modified: ofbiz/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java (original) +++ ofbiz/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java Sun Sep 25 17:20:58 2016 @@ -20,10 +20,7 @@ package org.apache.ofbiz.base.util.templ import java.io.File; import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; import java.io.Writer; -import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; @@ -39,6 +36,9 @@ import java.util.TimeZone; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; +import freemarker.cache.MultiTemplateLoader; +import freemarker.cache.StringTemplateLoader; +import freemarker.cache.TemplateLoader; import org.apache.ofbiz.base.location.FlexibleLocation; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.StringUtil; @@ -101,8 +101,13 @@ public final class FreeMarkerWorker { } newConfig.setLocalizedLookup(false); newConfig.setSharedVariable("StringUtil", new BeanModel(StringUtil.INSTANCE, wrapper)); - newConfig.setTemplateLoader(new FlexibleTemplateLoader()); - newConfig.setAutoImports(UtilProperties.getProperties("freemarkerImports")); + TemplateLoader[] templateLoaders = {new FlexibleTemplateLoader(), new StringTemplateLoader()}; + MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader(templateLoaders); + newConfig.setTemplateLoader(multiTemplateLoader); + Map freemarkerImports = UtilProperties.getProperties("freemarkerImports"); + if (freemarkerImports != null) { + newConfig.setAutoImports(freemarkerImports); + } newConfig.setLogTemplateExceptions(false); newConfig.setTemplateExceptionHandler(new FreeMarkerWorker.OFBizTemplateExceptionHandler()); try { @@ -115,7 +120,7 @@ public final class FreeMarkerWorker { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Enumeration<URL> resources; try { - resources = loader.getResources("freemarkerTransforms.properties"); + resources = loader.getResources("freemarkerTransforms.properties"); } catch (IOException e) { Debug.logError(e, "Could not load list of freemarkerTransforms.properties", module); throw UtilMisc.initCause(new InternalError(e.getMessage()), e); @@ -134,10 +139,7 @@ public final class FreeMarkerWorker { return newConfig; } - /** - * Public helper method. - */ - public static void loadTransforms(ClassLoader loader, Properties props, Configuration config) { + private static void loadTransforms(ClassLoader loader, Properties props, Configuration config) { for (Iterator<Object> i = props.keySet().iterator(); i.hasNext();) { String key = (String) i.next(); String className = props.getProperty(key); @@ -153,43 +155,6 @@ public final class FreeMarkerWorker { } /** - * Renders a template at the specified location. - * @param templateLocation Location of the template - file path or URL - * @param context The context Map - * @param outWriter The Writer to render to - */ - public static void renderTemplateAtLocation(String templateLocation, Map<String, Object> context, Appendable outWriter) throws MalformedURLException, TemplateException, IOException { - renderTemplate(templateLocation, context, outWriter); - } - - /** - * Renders a template contained in a String. - * @param templateLocation A unique ID for this template - used for caching - * @param templateString The String containing the template - * @param context The context Map - * @param outWriter The Writer to render to - */ - public static void renderTemplate(String templateLocation, String templateString, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException { - renderTemplate(templateLocation, templateString, context, outWriter, true); - } - - /** - * Renders a template contained in a String. - * @param templateLocation A unique ID for this template - used for caching - * @param templateString The String containing the template - * @param context The context Map - * @param outWriter The Writer to render to - * @param useCache try to get template from cache - */ - public static void renderTemplate(String templateLocation, String templateString, Map<String, Object> context, Appendable outWriter, boolean useCache) throws TemplateException, IOException { - if (templateString == null) { - renderTemplate(templateLocation, context, outWriter); - } else { - renderTemplateFromString(templateString, templateLocation, context, outWriter, useCache); - } - } - - /** * Renders a template from a Reader. * @param templateLocation A unique ID for this template - used for caching * @param context The context Map @@ -200,26 +165,30 @@ public final class FreeMarkerWorker { renderTemplate(template, context, outWriter); } - public static Environment renderTemplateFromString(String templateString, String templateLocation, Map<String, Object> context, Appendable outWriter, boolean useCache) throws TemplateException, IOException { + public static void renderTemplateFromString(String templateName, String templateString, Map<String, Object> context, Appendable outWriter, long lastModificationTime, boolean useCache) throws TemplateException, IOException { Template template = null; if (useCache) { - template = cachedTemplates.get(templateLocation); - if (template == null) { - Reader templateReader = new StringReader(templateString); - template = new Template(templateLocation, templateReader, defaultOfbizConfig); - templateReader.close(); - template = cachedTemplates.putIfAbsentAndGet(templateLocation, template); - } - } else { - Reader templateReader = new StringReader(templateString); - template = new Template(templateLocation, templateReader, defaultOfbizConfig); - templateReader.close(); + template = cachedTemplates.get(templateName); } - return renderTemplate(template, context, outWriter); + if (template == null) { + StringTemplateLoader stringTemplateLoader = (StringTemplateLoader)((MultiTemplateLoader)defaultOfbizConfig.getTemplateLoader()).getTemplateLoader(1); + Object templateSource = stringTemplateLoader.findTemplateSource(templateName); + if (templateSource == null || stringTemplateLoader.getLastModified(templateSource) < lastModificationTime) { + stringTemplateLoader.putTemplate(templateName, templateString, lastModificationTime); + } + } + + template = getTemplate(templateName); + renderTemplate(template, context, outWriter); } public static void clearTemplateFromCache(String templateLocation) { cachedTemplates.remove(templateLocation); + try { + defaultOfbizConfig.removeTemplateFromCache(templateLocation); + } catch(Exception e) { + Debug.logInfo("Template not found in Fremarker cache with name: " + templateLocation, module); + } } /** @@ -249,7 +218,7 @@ public final class FreeMarkerWorker { * @param env An Environment instance * @param context The context Map containing the user settings */ - public static void applyUserSettings(Environment env, Map<String, Object> context) throws TemplateException { + private static void applyUserSettings(Environment env, Map<String, Object> context) throws TemplateException { Locale locale = (Locale) context.get("locale"); if (locale == null) { locale = Locale.getDefault(); @@ -509,6 +478,7 @@ public final class FreeMarkerWorker { static class FlexibleTemplateLoader extends URLTemplateLoader { @Override protected URL getURL(String name) { + if (name != null && name.startsWith("delegator:")) return null; // this is a template stored in the database URL locationUrl = null; try { locationUrl = FlexibleLocation.resolveLocation(name); @@ -520,8 +490,7 @@ public final class FreeMarkerWorker { } /** - * OFBiz specific TemplateExceptionHandler. Sanitizes any error messages present in - * the stack trace prior to printing to the output writer. + * OFBiz specific TemplateExceptionHandler. */ static class OFBizTemplateExceptionHandler implements TemplateExceptionHandler { public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException { @@ -533,8 +502,4 @@ public final class FreeMarkerWorker { } } } - - public static String encodeDoubleQuotes(String htmlString) { - return htmlString.replaceAll("\"", "\\\\\""); - } } Added: ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/FreeMarkerWorkerTests.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/FreeMarkerWorkerTests.java?rev=1762227&view=auto ============================================================================== --- ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/FreeMarkerWorkerTests.java (added) +++ ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/FreeMarkerWorkerTests.java Sun Sep 25 17:20:58 2016 @@ -0,0 +1,43 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.apache.ofbiz.base.util.template; + +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +public class FreeMarkerWorkerTests { + @Before + public void initialize() { + System.setProperty("ofbiz.home", System.getProperty("user.dir")); + } + + @Test + public void renderTemplateFromString() throws Exception { + StringWriter out = new StringWriter(); + Map<String, Object> context = new HashMap<>(); + context.put("name", "World!"); + FreeMarkerWorker.renderTemplateFromString("template1", "Hello ${name}", context, out, 0, false); + assertEquals("Hello World!", out.toString()); + } +} Propchange: ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/FreeMarkerWorkerTests.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/FreeMarkerWorkerTests.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/template/FreeMarkerWorkerTests.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ofbiz/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java (original) +++ ofbiz/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java Sun Sep 25 17:20:58 2016 @@ -190,7 +190,7 @@ public class JsLanguageFileMappingCreato // some magic to create a new java file: render it as FTL Writer writer = new StringWriter(); try { - FreeMarkerWorker.renderTemplateAtLocation(template, mapWrapper, writer); + FreeMarkerWorker.renderTemplate(template, mapWrapper, writer); // write it as a Java file File file = new File(output); FileUtils.writeStringToFile(file, writer.toString(), encoding); Modified: ofbiz/trunk/framework/common/template/includes/HtmlTemplate.ftl URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/template/includes/HtmlTemplate.ftl?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/common/template/includes/HtmlTemplate.ftl (original) +++ ofbiz/trunk/framework/common/template/includes/HtmlTemplate.ftl Sun Sep 25 17:20:58 2016 @@ -17,8 +17,6 @@ specific language governing permissions under the License. --> -<#include "component://widget/templates/HtmlFormMacroLibrary.ftl"/> - <#macro lookupField className="" alert="" name="" value="" size="20" maxlength="20" id="" event="" action="" readonly="" autocomplete="" descriptionFieldName="" formName="" fieldFormName="" targetParameterIter="" imgSrc="" ajaxUrl="" ajaxEnabled="" presentation="layer" width="" height="" position="topleft" fadeBackground="true" clearText="" showDescription="" initiallyCollapsed="" tabindex=""> <#if (!ajaxEnabled?has_content)> <#assign javascriptEnabled = Static["org.apache.ofbiz.base.util.UtilHttp"].isJavaScriptEnabled(request) /> Modified: ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java (original) +++ ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java Sun Sep 25 17:20:58 2016 @@ -32,7 +32,7 @@ public class DelegatorUnitTests { @Before public void initialize() { - System.setProperty("ofbiz.home", "."); + System.setProperty("ofbiz.home", System.getProperty("user.dir")); System.setProperty("derby.system.home", "./runtime/data/derby"); } Modified: ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/FreeMarkerViewHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/FreeMarkerViewHandler.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/FreeMarkerViewHandler.java (original) +++ ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/FreeMarkerViewHandler.java Sun Sep 25 17:20:58 2016 @@ -64,7 +64,7 @@ public class FreeMarkerViewHandler exten // process the template & flush the output try { if (page.startsWith("component://")) { - FreeMarkerWorker.renderTemplateAtLocation(page, context, response.getWriter()); + FreeMarkerWorker.renderTemplate(page, context, response.getWriter()); } else { // backwards compatibility Template template = config.getTemplate(page); Modified: ofbiz/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/WebToolsServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/WebToolsServices.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/WebToolsServices.java (original) +++ ofbiz/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/WebToolsServices.java Sun Sep 25 17:20:58 2016 @@ -22,7 +22,6 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -45,6 +44,8 @@ import java.util.ResourceBundle; import java.util.Set; import java.util.TreeSet; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.ofbiz.base.location.FlexibleLocation; @@ -88,14 +89,10 @@ import org.apache.ofbiz.service.LocalDis import org.apache.ofbiz.service.ServiceUtil; import org.apache.ofbiz.webtools.artifactinfo.ArtifactInfoFactory; import org.apache.ofbiz.webtools.artifactinfo.ServiceArtifactInfo; +import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import freemarker.ext.dom.NodeModel; -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateHashModel; - /** * WebTools Services */ @@ -144,25 +141,16 @@ public class WebToolsServices { // FM Template // ############################# if (UtilValidate.isNotEmpty(fmfilename) && (UtilValidate.isNotEmpty(fulltext) || url != null)) { - FileReader templateReader = null; - try { - templateReader = new FileReader(fmfilename); - } catch (FileNotFoundException e) { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, "WebtoolsErrorReadingTemplateFile", UtilMisc.toMap("filename", fmfilename, "errorString", e.getMessage()), locale)); + File fmFile = new File(fmfilename); + if (!fmFile.exists()) { + return ServiceUtil.returnError(UtilProperties.getMessage(resource, "WebtoolsErrorReadingTemplateFile", UtilMisc.toMap("filename", fmfilename, "errorString", "Template file not found."), locale)); } - - StringWriter outWriter = new StringWriter(); - - Template template = null; try { - Configuration conf = org.apache.ofbiz.base.util.template.FreeMarkerWorker.getDefaultOfbizConfig(); - template = new Template("FMImportFilter", templateReader, conf); - Map<String, Object> fmcontext = new HashMap<String, Object>(); - + DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource ins = url != null ? new InputSource(url.openStream()) : new InputSource(new StringReader(fulltext)); - NodeModel nodeModel; + Document doc; try { - nodeModel = NodeModel.parse(ins); + doc = documentBuilder.parse(ins); } finally { if (ins.getByteStream() != null) { ins.getByteStream().close(); @@ -171,11 +159,10 @@ public class WebToolsServices { ins.getCharacterStream().close(); } } - fmcontext.put("doc", nodeModel); - TemplateHashModel staticModels = FreeMarkerWorker.getDefaultOfbizWrapper().getStaticModels(); - fmcontext.put("Static", staticModels); - - template.process(fmcontext, outWriter); + StringWriter outWriter = new StringWriter(); + Map<String, Object> fmcontext = new HashMap<>(); + fmcontext.put("doc", doc); + FreeMarkerWorker.renderTemplate(fmFile.toURI().toURL().toString(), fmcontext, outWriter); fulltext = outWriter.toString(); } catch (Exception ex) { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "WebtoolsErrorProcessingTemplateFile", UtilMisc.toMap("filename", fmfilename, "errorString", ex.getMessage()), locale)); Modified: ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java (original) +++ ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java Sun Sep 25 17:20:58 2016 @@ -157,7 +157,6 @@ public class HtmlWidget extends ModelScr writer.append(HtmlWidgetRenderer.formatBoundaryComment("Begin", "Template", location)); } - //FreeMarkerWorker.renderTemplateAtLocation(location, context, writer); Template template = null; if (location.endsWith(".fo.ftl")) { // FOP can't render correctly escaped characters template = FreeMarkerWorker.getTemplate(location); Modified: ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java?rev=1762227&r1=1762226&r2=1762227&view=diff ============================================================================== --- ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java (original) +++ ofbiz/trunk/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java Sun Sep 25 17:20:58 2016 @@ -173,6 +173,10 @@ public final class MacroFormRenderer imp return value; } + private static String encodeDoubleQuotes(String htmlString) { + return htmlString.replaceAll("\"", "\\\\\""); + } + public void renderLabel(Appendable writer, Map<String, Object> context, ModelScreenWidget.Label label) throws IOException { String labelText = label.getText(context); if (UtilValidate.isEmpty(labelText)) { @@ -218,7 +222,7 @@ public final class MacroFormRenderer imp sr.append("\" idName=\""); sr.append(idName); sr.append("\" description=\""); - sr.append(FreeMarkerWorker.encodeDoubleQuotes(description)); + sr.append(encodeDoubleQuotes(description)); sr.append("\" title=\""); sr.append(title); sr.append("\" class=\""); @@ -1276,7 +1280,7 @@ public final class MacroFormRenderer imp sr.append(" name=\""); sr.append(modelFormField.getModelForm().getName()); sr.append("\" title=\""); - sr.append(FreeMarkerWorker.encodeDoubleQuotes(title)); + sr.append(encodeDoubleQuotes(title)); sr.append("\" />"); executeMacro(writer, sr.toString()); } else if (modelFormField.isSortField()) { @@ -1316,7 +1320,7 @@ public final class MacroFormRenderer imp String helpText = UtilHelpText.getEntityFieldDescription(entityName, fieldName, delegator, locale); sr.append("\" fieldHelpText=\""); - sr.append(FreeMarkerWorker.encodeDoubleQuotes(helpText)); + sr.append(encodeDoubleQuotes(helpText)); } sr.append("\" title=\""); sr.append(sb.toString()); @@ -3005,7 +3009,7 @@ public final class MacroFormRenderer imp StringWriter sr = new StringWriter(); sr.append("<@renderTooltip "); sr.append("tooltip=\""); - sr.append(FreeMarkerWorker.encodeDoubleQuotes(tooltip)); + sr.append(encodeDoubleQuotes(tooltip)); sr.append("\" tooltipStyle=\""); sr.append(modelFormField.getTooltipStyle()); sr.append("\" />"); |
Free forum by Nabble | Edit this page |