Velocity Tools
  1. Velocity Tools
  2. VELTOOLS-109

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

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.4, 2.0
    • Fix Version/s: 2.0
    • Component/s: GenericTools
    • Labels:
      None
    • Environment:
      JDK 1.5, 6

      Description

      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 }

      );
      }

      }

        Activity

        Hide
        Ezra Epstein added a comment -

        Whoops, the line that reads:

        printer.printf(format, args);

        should instead read:

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

        Show
        Ezra Epstein added a comment - Whoops, the line that reads: printer.printf(format, args); should instead read: printer.printf(getLocale(), format, args);
        Hide
        Ezra Epstein added a comment -

        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 }

        );
        }

        }

        Show
        Ezra Epstein added a comment - 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 } ); } }
        Hide
        Nathan Bubna added a comment -

        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.

        Show
        Nathan Bubna added a comment - 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.
        Hide
        Nathan Bubna added a comment -

        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.

        Show
        Nathan Bubna added a comment - 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.

          People

          • Assignee:
            Unassigned
            Reporter:
            Ezra Epstein
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - 0.5h
              0.5h
              Remaining:
              Remaining Estimate - 0.5h
              0.5h
              Logged:
              Time Spent - Not Specified
              Not Specified

                Development