You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2008/01/24 02:01:01 UTC

svn commit: r614761 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry/corelib/components/Output.java test/java/org/apache/tapestry/corelib/components/OutputTest.java

Author: hlship
Date: Wed Jan 23 17:00:59 2008
New Revision: 614761

URL: http://svn.apache.org/viewvc?rev=614761&view=rev
Log:
TAPESTRY-2036: Add a parameter to the Output component to all control over whether output is filtered or unfiltered

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Output.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Output.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Output.java?rev=614761&r1=614760&r2=614761&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Output.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Output.java Wed Jan 23 17:00:59 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -26,16 +26,14 @@
 import java.text.Format;
 
 /**
- * A component for formatting output. If the component is represented in the template using an
- * element, then the element (plus any informal parameters) will be output around the formatted
- * value.
+ * A component for formatting output. If the component is represented in the template using an element, then the element
+ * (plus any informal parameters) will be output around the formatted value.
  */
 @SupportsInformalParameters
 public class Output
 {
     /**
-     * The value to be output (before formatting). If the formatted value is blank, no output is
-     * produced.
+     * The value to be output (before formatting). If the formatted value is blank, no output is produced.
      */
     @Parameter(required = true)
     private Object _value;
@@ -47,8 +45,15 @@
     private Format _format;
 
     /**
-     * The element name, derived from the component template. This can even be overridden manually
-     * if desired (for example, to sometimes render a surrounding element and other times not).
+     * If true, the default, then output is filtered, escaping any reserved characters. If false, the output is written
+     * raw.
+     */
+    @Parameter
+    private boolean _filter = true;
+
+    /**
+     * The element name, derived from the component template. This can even be overridden manually if desired (for
+     * example, to sometimes render a surrounding element and other times not).
      */
     @Parameter("componentResources.elementName")
     private String _elementName;
@@ -79,7 +84,8 @@
                 _resources.renderInformalParameters(writer);
             }
 
-            writer.write(formatted);
+            if (_filter) writer.write(formatted);
+            else writer.writeRaw(formatted);
 
             if (_elementName != null) writer.end();
         }
@@ -89,10 +95,11 @@
 
     // For testing.
 
-    void setup(Object value, Format format, String elementName, ComponentResources resources)
+    void setup(Object value, Format format, boolean filter, String elementName, ComponentResources resources)
     {
         _value = value;
         _format = format;
+        _filter = filter;
         _elementName = elementName;
         _resources = resources;
     }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java?rev=614761&r1=614760&r2=614761&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java Wed Jan 23 17:00:59 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -20,19 +20,25 @@
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
-import java.text.*;
+import java.text.DecimalFormat;
+import java.text.FieldPosition;
+import java.text.Format;
+import java.text.ParsePosition;
 import java.util.Locale;
 
 public class OutputTest extends TapestryTestCase
 {
     private final Number _value = 22.7d;
 
-    private final NumberFormat _format = DecimalFormat.getInstance(Locale.US);
+    private final DecimalFormat _format = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
+
+    private final DecimalFormat _filterFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
 
     @BeforeClass
     public void setup()
     {
-        ((DecimalFormat) _format).applyPattern("0.00");
+        _format.applyPattern("0.00");
+        _filterFormat.applyPattern("<0.00>");
     }
 
     @Test
@@ -45,7 +51,7 @@
 
         Output component = new Output();
 
-        component.setup(_value, _format, null, resources);
+        component.setup(_value, _format, true, null, resources);
 
         writer.element("root");
         assertFalse(component.beginRender(writer));
@@ -57,6 +63,50 @@
     }
 
     @Test
+    public void simple_output_with_filter()
+    {
+        MarkupWriter writer = createMarkupWriter();
+        ComponentResources resources = mockComponentResources();
+
+        replay();
+
+        Output component = new Output();
+
+        component.setup(_value, _filterFormat, true, null, resources);
+
+        writer.element("root");
+        assertFalse(component.beginRender(writer));
+        writer.end();
+
+        verify();
+
+        assertEquals(writer.toString(), "<root>&lt;22.70&gt;</root>");
+    }
+
+    @Test
+    public void simple_output_with_filter_disabled()
+    {
+        MarkupWriter writer = createMarkupWriter();
+        ComponentResources resources = mockComponentResources();
+
+        replay();
+
+        Output component = new Output();
+
+        component.setup(_value, _filterFormat, false, null, resources);
+
+        writer.element("root");
+        assertFalse(component.beginRender(writer));
+        writer.end();
+
+        verify();
+
+        // It's not valid XML output, but that's why it's called programmer error :-)
+
+        assertEquals(writer.toString(), "<root><22.70></root>");
+    }
+
+    @Test
     public void null_output()
     {
         MarkupWriter writer = createMarkupWriter();
@@ -66,7 +116,7 @@
 
         Output component = new Output();
 
-        component.setup(null, _format, null, resources);
+        component.setup(null, _format, true, null, resources);
 
         writer.element("root");
         assertFalse(component.beginRender(writer));
@@ -92,7 +142,7 @@
 
         Output component = new Output();
 
-        component.setup(_value, _format, elementName, resources);
+        component.setup(_value, _format, true, elementName, resources);
 
         assertFalse(component.beginRender(writer));
 
@@ -131,7 +181,7 @@
 
         Output component = new Output();
 
-        component.setup(_value, format, elementName, resources);
+        component.setup(_value, format, true, elementName, resources);
 
         writer.element("root");
         assertFalse(component.beginRender(writer));