1 21 package net.mlw.vlh.adapter.util; 22 23 import java.util.Collections ; 24 import java.util.Map ; 25 26 30 public class TokenReplaceTextManipulator implements TextManipulator 31 { 32 private String startToken = "["; 33 private String endToken = "]"; 34 private String nullValue = ""; 35 36 39 public StringBuffer manipulate(StringBuffer text, Map model) 40 { 41 if (model == null) 42 { 43 model = Collections.EMPTY_MAP; 44 } 45 46 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 63 public void setEndToken(String endToken) 64 { 65 this.endToken = endToken; 66 } 67 68 71 public void setStartToken(String startToken) 72 { 73 this.startToken = startToken; 74 } 75 76 79 public void setNullValue(String nullValue) 80 { 81 this.nullValue = nullValue; 82 } 83 } | Popular Tags |