View Javadoc

1   /**************************************************************************
2   Copyright 2005 Webstersmalley
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  
17  
18  package com.webstersmalley.picweb.utils;
19  
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * @author Matthew Smalley
29   */
30  public class StringMapper
31  {
32  	/*** Logger for the class. */
33  	private static Log log = LogFactory.getLog(StringMapper.class);
34  	
35  	private Map map = new HashMap();
36  	
37  	public StringMapper()
38  	{
39  		
40  	}
41  	
42  	public void addMapping(String key, String value)
43  	{
44  		map.put("@@" + key + "@@", value);
45  	}
46  	
47  	public String map(String input)
48  	{
49  		String output = input;
50  		Iterator it = map.keySet().iterator();
51  		while (it.hasNext())
52  		{
53  			String key = (String)it.next();
54  			String value = (String)map.get(key);
55  			output = output.replaceAll(key, value);
56  		}
57  		return output;
58  	}
59  	
60  	public static void main(String[] args)
61  	{
62  		StringMapper mapper = new StringMapper();
63  		mapper.addMapping("key1", "value1");
64  		String input = "I should see the val here: @@key1@@!";
65  		log.info("input : " + input);
66  		log.info("output: " + mapper.map("I should see the val here: @@key1@@!"));
67  		log.info("other : " + input.replaceAll("@@key1@@", "val1"));
68  	}
69  }