You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by "Ezra Epstein (JIRA)" <de...@velocity.apache.org> on 2008/11/21 01:36:44 UTC

[jira] Created: (VELTOOLS-109) A PrinftTool comes in handy. I've one I wish to donate

A PrinftTool comes in handy.  I've one I wish to donate
-------------------------------------------------------

                 Key: VELTOOLS-109
                 URL: https://issues.apache.org/jira/browse/VELTOOLS-109
             Project: Velocity Tools
          Issue Type: Improvement
          Components: GenericTools
    Affects Versions: 1.4, 2.0
         Environment: JDK 1.5, 6
            Reporter: Ezra Epstein
            Priority: Minor
             Fix For: 2.0, 1.4


import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

public class PrintfTool extends org.apache.velocity.tools.generic.LocaleConfig {

		StringWriter output;
		PrintWriter printer;

		public PrintfTool() {
			output = new StringWriter();
			printer = new PrintWriter(output);
		}

		// Velocity doesn't support invoking with varargs
		private String printf(String format, Object... args) {
			printer.printf(format, args);
			printer.flush();
			output.flush();
			String result = output.toString();
			output.getBuffer().setLength(0);
			return result;
		}

		// Velocity does support invoking with a List of values.
		public String printf(String format, List args) {
			return printf(format, args.toArray());
		}

		// Since value convenience method
		public String printf(String format, Object arg) {
			return printf(format, new Object[] { arg });
		}

}


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


[jira] Resolved: (VELTOOLS-109) A PrinftTool comes in handy. I've one I wish to donate

Posted by "Nathan Bubna (JIRA)" <de...@velocity.apache.org>.
     [ https://issues.apache.org/jira/browse/VELTOOLS-109?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Nathan Bubna resolved VELTOOLS-109.
-----------------------------------

       Resolution: Fixed
    Fix Version/s:     (was: 1.4)

Ok, DisplayTool (in 2.0) now has a printf(String,Object...) method and extends LocaleConfig.  Thanks for the idea.  If we support MessageFormat style, we might as well support printf.

> A PrinftTool comes in handy.  I've one I wish to donate
> -------------------------------------------------------
>
>                 Key: VELTOOLS-109
>                 URL: https://issues.apache.org/jira/browse/VELTOOLS-109
>             Project: Velocity Tools
>          Issue Type: Improvement
>          Components: GenericTools
>    Affects Versions: 1.4, 2.0
>         Environment: JDK 1.5, 6
>            Reporter: Ezra Epstein
>            Priority: Minor
>             Fix For: 2.0
>
>   Original Estimate: 0.5h
>  Remaining Estimate: 0.5h
>
> import java.io.ByteArrayOutputStream;
> import java.io.PrintStream;
> import java.io.PrintWriter;
> import java.io.StringWriter;
> import java.util.List;
> public class PrintfTool extends org.apache.velocity.tools.generic.LocaleConfig {
> 		StringWriter output;
> 		PrintWriter printer;
> 		public PrintfTool() {
> 			output = new StringWriter();
> 			printer = new PrintWriter(output);
> 		}
> 		// Velocity doesn't support invoking with varargs
> 		private String printf(String format, Object... args) {
> 			printer.printf(format, args);
> 			printer.flush();
> 			output.flush();
> 			String result = output.toString();
> 			output.getBuffer().setLength(0);
> 			return result;
> 		}
> 		// Velocity does support invoking with a List of values.
> 		public String printf(String format, List args) {
> 			return printf(format, args.toArray());
> 		}
> 		// Since value convenience method
> 		public String printf(String format, Object arg) {
> 			return printf(format, new Object[] { arg });
> 		}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


[jira] Commented: (VELTOOLS-109) A PrinftTool comes in handy. I've one I wish to donate

Posted by "Nathan Bubna (JIRA)" <de...@velocity.apache.org>.
    [ https://issues.apache.org/jira/browse/VELTOOLS-109?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12649596#action_12649596 ] 

Nathan Bubna commented on VELTOOLS-109:
---------------------------------------

of course, you could just do one of these:

context.put("String", ""); || <tool key="String" class="java.lang.String"/>  || #set( $String = '' )

and then do $String.format("the arg is %s", $arg)

Still... i suppose it wouldn't be a trivial thing to add a printf(String,Object...) method to DisplayTool, just so people don't need to do one of the first steps.

p.s. the upcoming Velocity 1.6 does support varargs.  :)

> A PrinftTool comes in handy.  I've one I wish to donate
> -------------------------------------------------------
>
>                 Key: VELTOOLS-109
>                 URL: https://issues.apache.org/jira/browse/VELTOOLS-109
>             Project: Velocity Tools
>          Issue Type: Improvement
>          Components: GenericTools
>    Affects Versions: 1.4, 2.0
>         Environment: JDK 1.5, 6
>            Reporter: Ezra Epstein
>            Priority: Minor
>             Fix For: 1.4, 2.0
>
>   Original Estimate: 0.5h
>  Remaining Estimate: 0.5h
>
> import java.io.ByteArrayOutputStream;
> import java.io.PrintStream;
> import java.io.PrintWriter;
> import java.io.StringWriter;
> import java.util.List;
> public class PrintfTool extends org.apache.velocity.tools.generic.LocaleConfig {
> 		StringWriter output;
> 		PrintWriter printer;
> 		public PrintfTool() {
> 			output = new StringWriter();
> 			printer = new PrintWriter(output);
> 		}
> 		// Velocity doesn't support invoking with varargs
> 		private String printf(String format, Object... args) {
> 			printer.printf(format, args);
> 			printer.flush();
> 			output.flush();
> 			String result = output.toString();
> 			output.getBuffer().setLength(0);
> 			return result;
> 		}
> 		// Velocity does support invoking with a List of values.
> 		public String printf(String format, List args) {
> 			return printf(format, args.toArray());
> 		}
> 		// Since value convenience method
> 		public String printf(String format, Object arg) {
> 			return printf(format, new Object[] { arg });
> 		}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


[jira] Commented: (VELTOOLS-109) A PrinftTool comes in handy. I've one I wish to donate

Posted by "Ezra Epstein (JIRA)" <de...@velocity.apache.org>.
    [ https://issues.apache.org/jira/browse/VELTOOLS-109?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12649557#action_12649557 ] 

Ezra Epstein commented on VELTOOLS-109:
---------------------------------------

One more time with feeling... it ain't so fast, but it's Thread Safe ...  

import java.util.List;
import java.util.Formatter;

public class PrintfTool extends org.apache.velocity.tools.generic.LocaleConfig {

		// Velocity doesn't support invoking with varargs
		private String printf(String format, Object... args) {
			StringBuffer sb = new StringBuffer();
			Formatter formatter = new Formatter(sb, getLocale());
			formatter.format(format, args);
			formatter.flush();
			return sb.toString();
		}

		// Velocity does support invoking with a List of values.
		public String printf(String format, List args) {
			return printf(format, args.toArray());
		}

		// Since value convenience method
		public String printf(String format, Object arg) {
			return printf(format, new Object[] { arg });
		}

}


> A PrinftTool comes in handy.  I've one I wish to donate
> -------------------------------------------------------
>
>                 Key: VELTOOLS-109
>                 URL: https://issues.apache.org/jira/browse/VELTOOLS-109
>             Project: Velocity Tools
>          Issue Type: Improvement
>          Components: GenericTools
>    Affects Versions: 1.4, 2.0
>         Environment: JDK 1.5, 6
>            Reporter: Ezra Epstein
>            Priority: Minor
>             Fix For: 1.4, 2.0
>
>   Original Estimate: 0.5h
>  Remaining Estimate: 0.5h
>
> import java.io.ByteArrayOutputStream;
> import java.io.PrintStream;
> import java.io.PrintWriter;
> import java.io.StringWriter;
> import java.util.List;
> public class PrintfTool extends org.apache.velocity.tools.generic.LocaleConfig {
> 		StringWriter output;
> 		PrintWriter printer;
> 		public PrintfTool() {
> 			output = new StringWriter();
> 			printer = new PrintWriter(output);
> 		}
> 		// Velocity doesn't support invoking with varargs
> 		private String printf(String format, Object... args) {
> 			printer.printf(format, args);
> 			printer.flush();
> 			output.flush();
> 			String result = output.toString();
> 			output.getBuffer().setLength(0);
> 			return result;
> 		}
> 		// Velocity does support invoking with a List of values.
> 		public String printf(String format, List args) {
> 			return printf(format, args.toArray());
> 		}
> 		// Since value convenience method
> 		public String printf(String format, Object arg) {
> 			return printf(format, new Object[] { arg });
> 		}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


[jira] Commented: (VELTOOLS-109) A PrinftTool comes in handy. I've one I wish to donate

Posted by "Ezra Epstein (JIRA)" <de...@velocity.apache.org>.
    [ https://issues.apache.org/jira/browse/VELTOOLS-109?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12649554#action_12649554 ] 

Ezra Epstein commented on VELTOOLS-109:
---------------------------------------

Whoops, the line that reads:

printer.printf(format, args); 

should instead read:

printer.printf(getLocale(), format, args); 

> A PrinftTool comes in handy.  I've one I wish to donate
> -------------------------------------------------------
>
>                 Key: VELTOOLS-109
>                 URL: https://issues.apache.org/jira/browse/VELTOOLS-109
>             Project: Velocity Tools
>          Issue Type: Improvement
>          Components: GenericTools
>    Affects Versions: 1.4, 2.0
>         Environment: JDK 1.5, 6
>            Reporter: Ezra Epstein
>            Priority: Minor
>             Fix For: 1.4, 2.0
>
>   Original Estimate: 0.5h
>  Remaining Estimate: 0.5h
>
> import java.io.ByteArrayOutputStream;
> import java.io.PrintStream;
> import java.io.PrintWriter;
> import java.io.StringWriter;
> import java.util.List;
> public class PrintfTool extends org.apache.velocity.tools.generic.LocaleConfig {
> 		StringWriter output;
> 		PrintWriter printer;
> 		public PrintfTool() {
> 			output = new StringWriter();
> 			printer = new PrintWriter(output);
> 		}
> 		// Velocity doesn't support invoking with varargs
> 		private String printf(String format, Object... args) {
> 			printer.printf(format, args);
> 			printer.flush();
> 			output.flush();
> 			String result = output.toString();
> 			output.getBuffer().setLength(0);
> 			return result;
> 		}
> 		// Velocity does support invoking with a List of values.
> 		public String printf(String format, List args) {
> 			return printf(format, args.toArray());
> 		}
> 		// Since value convenience method
> 		public String printf(String format, Object arg) {
> 			return printf(format, new Object[] { arg });
> 		}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org