View Javadoc

1   /**
2    * Copyright 2008 Bryan Ray
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0 
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package uk.org.bryanray.testtoys.util;
17  
18  import java.util.Iterator;
19  import java.util.Map;
20  import java.util.Properties;
21  
22  /**
23   * <DL>
24   * <DT><B>Project:</B></DT>
25   * <DD>TestDataLoader</DD>
26   * <DT><B>Filename:</B></DT>
27   * <DD>TemplateUtil.java</DD>
28   * <DT><B>Creation Date:</B></DT>
29   * <DD>24 May 2008</DD>
30   * </DL>
31   * 
32   * A class that substitutes placeholders of the form ${...} in a template with
33   * values.
34   * 
35   * @author Bryan Ray
36   */
37  public class TemplateUtil {
38  	/**
39  	 * @param template
40  	 *            The template which the place holders have been substituted
41  	 *            into.
42  	 * @param placeholderValues
43  	 *            The values to be substituted into the place holders.
44  	 * @return The template with the supplied place holders substituted into it.
45  	 */
46  	public static String substituteTemplatePlaceholders(String template,
47  			Map<String, String> placeholderValues) {
48  		for (Iterator<Map.Entry<String, String>> i = placeholderValues
49  				.entrySet().iterator(); i.hasNext();) {
50  			Map.Entry<String, String> entry = i.next();
51  			template = template.replaceAll("\\$\\{" + entry.getKey() + "\\}",
52  					entry.getValue());
53  		}
54  		return template;
55  	}
56  
57  	/**
58  	 * @param template
59  	 *            The template which the place holders have been substituted
60  	 *            into.
61  	 * @param placeholderValues
62  	 *            The values to be substituted into the place holders.
63  	 * @return The template with the supplied place holders substituted into it.
64  	 */
65  	public static String substituteTemplatePlaceholders(String template,
66  			Properties placeholderValues) {
67  		for (Iterator<Map.Entry<Object, Object>> i = placeholderValues
68  				.entrySet().iterator(); i.hasNext();) {
69  			Map.Entry<Object, Object> entry = i.next();
70  			template = template.replaceAll("\\$\\{" + entry.getKey().toString()
71  					+ "\\}", entry.getValue().toString());
72  		}
73  		return template;
74  	}
75  }