On 5/8/06, Eelco Hillenius <[EMAIL PROTECTED]
> wrote:
Could you please provide a patch? You already have it on your computer
right? I'll apply it right away.
Thanks,
Eelco
On 5/8/06, karthik Guru <[EMAIL PROTECTED]> wrote:
> I'm not sure if there is any interest in
> wicket.contrib.markup.html.velocity.VelocityPanel. But just
> for the sake of completeness may be it can be fixed.
>
> There is a small issue that i noticed today:
>
> This method is called everytime to read the template from a
> IStringResourceStream (templateResource) that is stored as an instance
> variable . But templateResource.asString() after reading in the content does
> not 'reset' the reader. So all subsequent calls to
> templateResource.asString() results in a blank string.
>
> private Reader getTemplateReader()
> {
> final String template = templateResource.asString();
> if (template != null)
> {
> return new StringReader(template);
> }
> return null;
> }
>
>
> I fixed this on my side by storing the String representation as an instance
> variable in VelocityPanel.
>
> Actually wicket.util.io.Streams.readString(final Reader in)
> does not seem to reset the Reader.
>
> Also ,
>
> VelocityPanel:
>
> protected void onComponentTagBody(final MarkupStream markupStream,
> final ComponentTag openTag) -
>
>
> modified result = Strings.escapeMarkup(result);
> to
> result = Strings.escapeMarkup(result).toString();
>
> -- karthik --
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmdlnk&kid0709&bid&3057&dat1642
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user
--
-- karthik --
/* * $Id: VelocityPanel.java,v 1.3 2005/03/31 20:50:42 joco01 Exp $ $Revision: * 1.1 $ $Date: 2005/03/31 20:50:42 $ * * ============================================================================== * Licensed 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 wicket.contrib.markup.html.velocity;
import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.util.Map; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.exception.MethodInvocationException; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; import wicket.WicketRuntimeException; import wicket.markup.ComponentTag; import wicket.markup.MarkupStream; import wicket.markup.html.WebComponent; import wicket.model.Model; import wicket.util.resource.IStringResourceStream; import wicket.util.string.Strings; /** * Panel that displays the result of rendering a Velocity template. The template * itself can be any IStringResourceStream implementation, of which there are a number * of convenient implementations in wicket.util. The model can be any normal * Wicket MapModel. * * @author Eelco Hillenius * @author Jonathan Locke */ public final class VelocityPanel extends WebComponent { /** Whether to escape HTML characters. The default value is false. */ private boolean escapeHtml = false; /** Velocity template content */ private String templateContent; /** * Whether any velocity exception should be trapped and displayed on the * panel (false) or thrown up to be handled by the exception mechanism of * Wicket (true). The default is false, which traps and displays any * exception without having consequences for the other components on the * page. * <p> * Trapping these exceptions without disturbing the other components is * especially usefull in CMS like applications, where 'normal' users are * allowed to do basic scripting. On errors, you want them to be able to * have them correct them while the rest of the application keeps on * working. */ private boolean throwVelocityExceptions = false; /** * Construct. * * @param name * See Component * @param templateResource * The velocity template as a string resource * @param model * MapModel with variables that can be substituted by Velocity */ public VelocityPanel(final String name, final IStringResourceStream templateResource, final Model model) { super(name, model); if(templateResource != null){ final String template = templateResource.asString(); if (template != null){ templateContent = template; } } } /** * Gets whether to escape HTML characters. * * @return whether to escape HTML characters */ public final boolean getEscapeHtml() { return escapeHtml; } /** * Gets whether any velocity exception should be trapped and displayed on * the panel (false) or thrown up to be handled by the exception mechanism * of Wicket (true). The default is true, which traps and displays any * exception without having consequences for the other components on the * page. * * @return Whether any velocity exceptions should be thrown or trapped. */ public final boolean getThrowVelocityExceptions() { return throwVelocityExceptions; } /** * Sets whether to escape HTML characters. The default value is false. * * @param escapeHtml * whether to escape HTML characters * @return This */ public final VelocityPanel setEscapeHtml(boolean escapeHtml) { this.escapeHtml = escapeHtml; return this; } /** * Gets whether any velocity exception should be trapped and displayed on * the panel (true) or thrown up to be handled by the exception mechanism of * Wicket (false). * * @param throwVelocityExceptions * whether any exception should be trapped or rethrown * @return This */ public final VelocityPanel setThrowVelocityExceptions(boolean throwVelocityExceptions) { this.throwVelocityExceptions = throwVelocityExceptions; return this; } /** * @see wicket.Component#onComponentTagBody(wicket.markup.MarkupStream, * wicket.markup.ComponentTag) */ protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { final Reader templateReader = getTemplateReader(); if (templateReader != null) { // Get model as a map final Map map = (Map)getModelObject(); // create a Velocity context object using the model if set final VelocityContext ctx = new VelocityContext(map); // create a writer for capturing the Velocity output StringWriter writer = new StringWriter(); // string to be used as the template name for log messages in case // of error final String logTag = getId(); try { Velocity.init(); // execute the velocity script and capture the output in writer Velocity.evaluate(ctx, writer, logTag, templateReader); // replace the tag's body the Velocity output String result = writer.toString(); if (escapeHtml) { // encode the result in order to get valid html output that // does not break the rest of the page result = Strings.escapeMarkup(result).toString(); } // now replace the body of the tag with the velocity merge // result replaceComponentTagBody(markupStream, openTag, result); } catch (ParseErrorException e) { onException(e, markupStream, openTag); } catch (MethodInvocationException e) { onException(e, markupStream, openTag); } catch (ResourceNotFoundException e) { onException(e, markupStream, openTag); } catch (IOException e) { onException(e, markupStream, openTag); } catch (Exception e) { // TODO Auto-generated catch block onException(e, markupStream, openTag); } } else { replaceComponentTagBody(markupStream, openTag, ""); // just empty it } } /** * Gets a reader for the velocity template. * * @return reader for the velocity template */ private Reader getTemplateReader() { return templateContent == null? null:new StringReader(templateContent); } /** * Either print or rethrow the throwable. * * @param exception * the cause * @param markupStream * the markup stream * @param openTag * the open tag */ private void onException(final Exception exception, final MarkupStream markupStream, final ComponentTag openTag) { if (!throwVelocityExceptions) { // print the exception on the panel String stackTraceAsString = Strings.toString(exception); replaceComponentTagBody(markupStream, openTag, stackTraceAsString); } else { // rethrow the exception throw new WicketRuntimeException(exception); } } }
/* * $Id: FreeMarkerPanel.java,v 1.2 2005/09/01 22:46:12 eelco12 Exp $ * $Revision: 1.2 $ * $Date: 2005/09/01 22:46:12 $ * * ============================================================================== * Licensed 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 wicket.contrib.markup.html.freemarker; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.util.Map; import freemarker.template.Template; import freemarker.template.TemplateException; import wicket.WicketRuntimeException; import wicket.markup.ComponentTag; import wicket.markup.MarkupStream; import wicket.markup.html.WebComponent; import wicket.model.Model; import wicket.util.resource.IStringResourceStream; import wicket.util.string.Strings; /** * Panel that displays the result of rendering a FreeMarker template. The * template itself can be any IStringResourceStream implementation, of which * there are a number of convenient implementations in wicket.util. The model * can be any normal Wicket MapModel. * * @author Jonas Klingstedt */ public final class FreeMarkerPanel extends WebComponent { /** Whether to escape HTML characters. The default value is false. */ private boolean escapeHtml = false; /*Freemarker template content as String */ private String templateContent; /** * Whether any FreeMarker exception should be trapped and displayed on the * panel (false) or thrown up to be handled by the exception mechanism of * Wicket (true). The default is false, which traps and displays any * exception without having consequences for the other components on the * page. * <p> * Trapping these exceptions without disturbing the other components is * especially usefull in CMS like applications, where 'normal' users are * allowed to do basic scripting. On errors, you want them to be able to * have them correct them while the rest of the application keeps on * working. */ private boolean throwFreeMarkerExceptions = false; /** FreeMarker configuration */ private SerializableFreeMarkerConfiguration config; /** * Constructor. * * @param id the component id. * @param templateResource the FreeMarker template as a String resource. * @param model a MapModel with variables that can be substituted by * FreeMarker. * * @see Component */ public FreeMarkerPanel(final String id, final IStringResourceStream templateResource, final Model model) { super(id, model); if(templateResource != null){ final String template = templateResource.asString(); if (template != null){ templateContent = template; } } // Create FreeMarker configuration config = new SerializableFreeMarkerConfiguration(); } /** * Gets whether to escape HTML characters. * * @return whether to escape HTML characters. */ public final boolean getEscapeHtml() { return escapeHtml; } /** * Sets whether to escape HTML characters. The default value is false. * * @param escapeHtml whether to escape HTML characters. * @return this */ public final FreeMarkerPanel setEscapeHtml(boolean escapeHtml) { this.escapeHtml = escapeHtml; return this; } /** * Gets whether any FreeMarker exception should be trapped and displayed on * the panel (false) or thrown up to be handled by the exception mechanism * of Wicket (true). The default is true, which traps and displays any * exception without having consequences for the other components on the * page. * * @return whether any FreeMarker exceptions should be thrown or trapped. */ public final boolean getThrowFreeMarkerExceptions() { return throwFreeMarkerExceptions; } /** * Sets whether any FreeMarker exception should be trapped and displayed on * the panel (true) or thrown up to be handled by the exception mechanism of * Wicket (false). * * @param throwFreeMarkerExceptions whether any exception should be trapped * * or rethrown * @return this */ public final FreeMarkerPanel setThrowFreeMarkerExceptions( boolean throwFreeMarkerExceptions) { this.throwFreeMarkerExceptions = throwFreeMarkerExceptions; return this; } /** * @see wicket.Component#onComponentTagbody(wicket.markup.MarkupStream, * wicket.markup.ComponentTag) */ protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { final Reader templateReader = getTemplateReader(); if (null == templateReader) { // Just empty it replaceComponentTagBody(markupStream, openTag, ""); } else { // Get model as a map final Map map = (Map) getModelObject(); // Create a writer for capturing the FreeMarker output StringWriter writer = new StringWriter(); try { // Create a new FreeMarker template Template template = new Template( getId(), templateReader, config); // Process the FreeMarker template and capture the output in // the writer template.process(map, writer); // Replace the tag's body with the FreeMarker output String result = writer.toString(); // Escape the HTML ? if (escapeHtml) { // Encode the result in order to get valid HTML output that // does not break the rest of the page result = Strings.escapeMarkup(result).toString(); } // Finally, actually replace the tag body replaceComponentTagBody(markupStream, openTag, result); } catch (TemplateException e) { onException(e, markupStream, openTag); } catch (IOException e) { onException(e, markupStream, openTag); } } } /** * Either print or rethrow the throwable. * * @param exception the cause. * @param markupStream the markup stream. * @param openTag the open tag. */ private void onException(final Exception exception, final MarkupStream markupStream, final ComponentTag openTag) { if (!throwFreeMarkerExceptions) { // Print the exception on the panel String stackTraceAsString = Strings.toString(exception); replaceComponentTagBody(markupStream, openTag, stackTraceAsString); } else { // Rethrow the exception throw new WicketRuntimeException(exception); } } /** * Gets a reader for the FreeMarker template. * * @return a reader for the FreeMarker template. */ private Reader getTemplateReader() { return templateContent == null? null: new StringReader(templateContent); } }