View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   */
21  package com.ebay.carad.os.vitalsigns.util;
22  
23  import java.text.MessageFormat;
24  
25  /***
26   * Provides basic <code>MessageFormat</code> utility methods.
27   * 
28   * @author Jeremy Kraybill
29   * @version $Id$
30   */
31  public abstract class MessageFormatUtil {
32  	
33  	/***
34  	 * Escape single-quotes for MessageFormat. Single quotes get special treatment
35  	 * by MessageFormat, but we almost never want that behavior.
36  	 * 
37  	 * @param inStr the string to escape
38  	 * @return an escaped string
39  	 */
40  	private static String escapeSingleQuotes(String inStr) {
41  		return inStr.replaceAll("'", "''");
42  	}
43  
44  	/***
45  	 * Reformats the passed-in message format-ready string with the passed-in parameters.
46  	 * Single quotes in the passed-in string are escaped and therefore ignored by MessageFormat.
47  	 * 
48  	 * @param inStr the string to format
49  	 * @param params the parameters to substitute
50  	 * @return a formatted string
51  	 */
52  	public static String format(String inStr, Object[] params) {
53  		return MessageFormat.format(escapeSingleQuotes(inStr), params);
54  	}
55  
56  }