View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.            
3    *                                                                          
4    * This library is free software; you can redistribute it and/or modify it    
5    * under the terms of the GNU Lesser General Public License as published      
6    * by the Free Software Foundation; either version 2.1 of the License, or 
7    * (at your option) any later version.                                            
8    *                                                                            
9    * This library is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; with out even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12   * GNU Lesser General Public License for more details.                                                  
13   *                                                                           
14   * You should have received a copy of the GNU Lesser General Public License   
15   * along with this library;  if not, write to the Free Software Foundation,   
16   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
17   *                                                                            
18   * > http://www.gnu.org/copyleft/lesser.html                                  
19   * > http://www.opensource.org/licenses/lgpl-license.php
20   */
21  package net.mlw.vlh.adapter.util;
22  
23  import java.util.Collections;
24  import java.util.Map;
25  
26  /*** Replaces "This is a [test]" with "This is a <%=map.get("test")%>"
27   * 
28   * @author mwilson
29   */
30  public class TokenReplaceTextManipulator implements TextManipulator
31  {
32  	private String startToken = "[";
33  	private String endToken = "]";
34  	private String nullValue = "";
35  
36  	/***
37  	 * @see net.mlw.vlh.adapter.util.TextManipulator#manipulate(java.lang.String, java.lang.Object)
38  	 */
39  	public StringBuffer manipulate(StringBuffer text, Map model)
40  	{
41  		if (model == null)
42  		{
43  			model = Collections.EMPTY_MAP;
44  		}
45  
46  		//Replace any [key] with the value in the whereClause Map.
47  		for (int i = 0, end = 0, start; ((start = text.toString().indexOf(startToken, end)) >= 0); i++)
48  		{
49  			end = text.toString().indexOf(endToken, start);
50  			String key = text.substring(start + 1, end);
51  
52  			Object modelValue = model.get(key);
53  			text.replace(start, end + 1, (modelValue == null) ? nullValue : modelValue.toString());
54  			end -= (key.length() + 2);
55  		}
56  
57  		return text;
58  	}
59  
60  	/***
61  	 * @param endToken The endToken to set.
62  	 */
63  	public void setEndToken(String endToken)
64  	{
65  		this.endToken = endToken;
66  	}
67  
68  	/***
69  	 * @param startToken The startToken to set.
70  	 */
71  	public void setStartToken(String startToken)
72  	{
73  		this.startToken = startToken;
74  	}
75  	
76  	/***
77  	 * @param nullValue The nullValue to set.
78  	 */
79  	public void setNullValue(String nullValue)
80  	{
81  		this.nullValue = nullValue;
82  	}
83  }