KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanesha > replacer > SimplePrintf


1 /*
2  * libreplacer, Java library to support C-sprintf alike text formatting
3  * Copyright (C) 2002 Tanesha FTPD Project, www.tanesha.net
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 /*
20  * $Id: SimplePrintf.java,v 1.5 2002/12/12 13:06:48 flower Exp $
21  */

22 package org.tanesha.replacer;
23
24 /**
25  * A C-sprintf alike string formatting class.
26  * <p>
27  * This is the factory for most of the operations.
28  *
29  * @author Soren, www.tanesha.net
30  */

31 public class SimplePrintf {
32
33     /**
34      * The simplest way of getting something formatted.
35      *
36      * @param inputFormat the string that needs formatted.
37      * @param objs the array of Object to use for formatting
38      * @return the new formatted string
39      * @throws FormatterException if an exception occured while formatting.
40      */

41     public static String JavaDoc jprintf(String JavaDoc inputFormat, Object JavaDoc [] objs) throws FormatterException {
42         ReplacerFormat format = ReplacerFormat.createFormat(inputFormat);
43
44         return jprintf(format, objs);
45     }
46
47     public static String JavaDoc jprintf(String JavaDoc inputFormat, ReplacerEnvironment env) throws FormatterException {
48         ReplacerFormat format = ReplacerFormat.createFormat(inputFormat);
49
50         return jprintf(format, env);
51     }
52
53     /**
54      * Apply an array of Object to a replacer format.
55      *
56      * @param objs the array of Object to use for formatting
57      * @param format the replacer format
58      * @return the new formatted string
59      * @throws FormatterException if an exception occured while formatting.
60      */

61     public static String JavaDoc jprintf(ReplacerFormat format, Object JavaDoc[] objs) throws FormatterException {
62
63         // create a replacerenvironment
64
ReplacerEnvironment env = new ReplacerEnvironment();
65         for (int i = 0; i < objs.length; i++)
66             env.add(String.valueOf(i), objs[i]);
67
68         return jprintf(format, env);
69     }
70
71     /**
72      * Apply a replacerenvironment to a replacer format
73      *
74      * @param env The environment with variables to use for formatting
75      * @param format the replacer format
76      * @return the new formatted string
77      * @throws FormatterException if an exception occured while formatting.
78      */

79     public static String JavaDoc jprintf(ReplacerFormat format, ReplacerEnvironment env) throws FormatterException {
80         return format.format(env);
81     }
82
83 }
84
Popular Tags