Revision: 558
http://svn.sourceforge.net/stripes/?rev=558&view=rev
Author: bengunter
Date: 2007-05-26 21:07:57 -0700 (Sat, 26 May 2007)
Log Message:
-----------
Resolved STS-374: stripes:param should use formatters when possible. Adding
formatting to stripes:param would have been complex. Instead a new
stripes:format tag can be nested inside stripes:param to achieve the same
result. The stripes:format tag is also very useful in other situations.
Modified Paths:
--------------
trunk/stripes/resources/stripes.tld
Added Paths:
-----------
trunk/stripes/src/net/sourceforge/stripes/tag/FormatTag.java
trunk/stripes/src/net/sourceforge/stripes/tag/VarTagSupport.java
Modified: trunk/stripes/resources/stripes.tld
===================================================================
--- trunk/stripes/resources/stripes.tld 2007-05-27 01:07:31 UTC (rev 557)
+++ trunk/stripes/resources/stripes.tld 2007-05-27 04:07:57 UTC (rev 558)
@@ -1970,7 +1970,44 @@
<rtexprvalue>true</rtexprvalue>
</attribute>
<dynamic-attributes>true</dynamic-attributes>
- </tag>
+ </tag>
+
+ <tag>
+ <description><![CDATA[
+ Formats an object using Stripes' formatting facilities. If no
formatter is available
+ for the object, then toString() is called. Null values are
formatted as an empty
+ string. If a 'var' attribute is specified, the generated URL will
be written to that
+ variable, otherwise it will be written into the page.
+ ]]></description>
+ <display-name>format</display-name>
+ <name>format</name>
+ <tag-class>net.sourceforge.stripes.tag.FormatTag</tag-class>
+ <body-content>empty</body-content>
+ <attribute>
+ <description>
+ The object to be formatted.
+ </description>
+
<name>value</name><required>true</required><rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <description>
+ The (optional) name of a scoped variable to store the
formatted value in. If no
+ name is specified then the value will be written directly to
the page instead. See
+ also the 'scope' attribute to control which scope the variable
gets set in.
+ </description>
+
<name>var</name><required>false</required><rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <description>
+ The (optional) name of the JSP scope into which to set the
variable specified by
+ the var attribute. Valid values are 'page', 'request',
'session' and 'application'.
+ Defaults to 'page'.
+ </description>
+
<name>scope</name><required>false</required><rtexprvalue>true</rtexprvalue>
+ </attribute>
+
<attribute><description>@formatType@</description><name>formatType</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute>
+
<attribute><description>@formatPattern@</description><name>formatPattern</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute>
+ </tag>
<function>
<description>
Added: trunk/stripes/src/net/sourceforge/stripes/tag/FormatTag.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/tag/FormatTag.java
(rev 0)
+++ trunk/stripes/src/net/sourceforge/stripes/tag/FormatTag.java
2007-05-27 04:07:57 UTC (rev 558)
@@ -0,0 +1,134 @@
+/* Copyright 2007 Ben Gunter
+ *
+ * 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 net.sourceforge.stripes.tag;
+
+import java.io.IOException;
+
+import javax.servlet.jsp.JspException;
+
+import net.sourceforge.stripes.controller.StripesFilter;
+import net.sourceforge.stripes.format.Formatter;
+import net.sourceforge.stripes.format.FormatterFactory;
+import net.sourceforge.stripes.util.Log;
+
+/**
+ * This tag accepts an object and formats it using an appropriate
+ * [EMAIL PROTECTED] Formatter}. The resulting [EMAIL PROTECTED] String} can
be assigned in the page,
+ * request, session or application scopes by using "var" and "scope" or it can
+ * be written directly to the JSP output.
+ *
+ * @author Ben Gunter
+ * @since Stripes 1.5
+ */
+public class FormatTag extends VarTagSupport {
+ private static final Log log = Log.getInstance(FormatTag.class);
+ private Object value;
+ private String formatType;
+ private String formatPattern;
+
+ /** Get the format pattern */
+ public String getFormatPattern() {
+ return formatPattern;
+ }
+
+ /** Set the format pattern */
+ public void setFormatPattern(String formatPattern) {
+ this.formatPattern = formatPattern;
+ }
+
+ /** Get the format type */
+ public String getFormatType() {
+ return formatType;
+ }
+
+ /** Set the format type */
+ public void setFormatType(String formatType) {
+ this.formatType = formatType;
+ }
+
+ /** Get the object to be formatted */
+ public Object getValue() {
+ return value;
+ }
+
+ /** Set the object to be formatted */
+ public void setValue(Object value) {
+ this.value = value;
+ }
+
+ /**
+ * Attempts to format an object using an appropriate [EMAIL PROTECTED]
Formatter}. If
+ * no formatter is available for the object, then this method will call
+ * <code>toString()</code> on the object. A null <code>value</code> will
+ * be formatted as an empty string.
+ *
+ * @param value
+ * the object to be formatted
+ * @return the formatted value
+ */
+ @SuppressWarnings("unchecked")
+ protected String format(Object value) {
+ if (value == null)
+ return "";
+
+ FormatterFactory factory =
StripesFilter.getConfiguration().getFormatterFactory();
+ Formatter formatter = factory.getFormatter(value.getClass(),
+
getPageContext().getRequest().getLocale(),
+ this.formatType,
+ this.formatPattern);
+ if (formatter == null)
+ return String.valueOf(value);
+ else
+ return formatter.format(value);
+ }
+
+ /**
+ * Calls [EMAIL PROTECTED] #format(Object)} and writes the resulting
[EMAIL PROTECTED] String} to
+ * the JSP output.
+ *
+ * @param value
+ * the object to be formatted and written
+ * @throws JspException
+ */
+ protected void writeOut(Object value) throws JspException {
+ String formatted = format(value);
+ try {
+ pageContext.getOut().print(formatted);
+ }
+ catch (IOException e) {
+ JspException jspe = new JspException(
+ "IOException encountered while writing formatted value '"
+ + formatted + " to the JspWriter.", e);
+ log.warn(jspe);
+ throw jspe;
+ }
+ }
+
+ @Override
+ public int doStartTag() throws JspException {
+ return SKIP_BODY;
+ }
+
+ @Override
+ public int doEndTag() throws JspException {
+ if (var == null) {
+ writeOut(value);
+ }
+ else {
+ export(format(value));
+ }
+ return EVAL_PAGE;
+ }
+}
Added: trunk/stripes/src/net/sourceforge/stripes/tag/VarTagSupport.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/tag/VarTagSupport.java
(rev 0)
+++ trunk/stripes/src/net/sourceforge/stripes/tag/VarTagSupport.java
2007-05-27 04:07:57 UTC (rev 558)
@@ -0,0 +1,72 @@
+/* Copyright 2007 Ben Gunter
+ *
+ * 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 net.sourceforge.stripes.tag;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * Provides support for tags that allow assigning a value in a named scope. The
+ * "var" and "scope" properties are provided, as is an [EMAIL PROTECTED]
#export(Object)}
+ * method that assigns a value to the given name in the given scope.
+ *
+ * @author Ben Gunter
+ */
+public abstract class VarTagSupport extends StripesTagSupport {
+ protected String var;
+ protected String scope;
+
+ /** Get the scope in which the value will be stored */
+ public String getScope() {
+ return scope;
+ }
+
+ /** Set the scope in which the value will be stored */
+ public void setScope(String scope) {
+ this.scope = scope;
+ }
+
+ /** Get the name of the variable to which the value will be assigned */
+ public String getVar() {
+ return var;
+ }
+
+ /** Set the name of the variable to which the value will be assigned */
+ public void setVar(String var) {
+ this.var = var;
+ }
+
+ /**
+ * Assigns the <code>value</code> to an attribute named by
+ * <code>var</code> in the named <code>scope</code>.
+ *
+ * @param value
+ * the object to be exported
+ */
+ protected void export(Object value) {
+ if ("request".equals(scope)) {
+ pageContext.getRequest().setAttribute(var, value);
+ }
+ else if ("session".equals(scope)) {
+ ((HttpServletRequest) pageContext.getRequest()).getSession()
+ .setAttribute(var, value);
+ }
+ else if ("application".equals(scope)) {
+ pageContext.getServletContext().setAttribute(var, value);
+ }
+ else {
+ pageContext.setAttribute(var, value);
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development