1 21 package net.mlw.vlh.adapter.util; 22 23 import java.util.Collections ; 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 27 41 public class FilterTextManipulator implements TextManipulator 42 { 43 private String startToken = "/~"; 44 45 private String endToken = "~/"; 46 47 50 public StringBuffer manipulate(StringBuffer text, Map model) 51 { 52 if (model == null) 53 { 54 model = Collections.EMPTY_MAP; 55 } 56 57 boolean inverse = false; 58 59 for (int start = 0, end = text.indexOf(endToken); ((end = text.indexOf(endToken)) >= 0);) 60 { 61 start = text.lastIndexOf(startToken, end); 62 String key = text.substring(start + 2, text.indexOf(":", start)); 63 64 if (key.indexOf(',') != -1) 66 { 67 for (StringTokenizer st = new StringTokenizer (key, ",");st.hasMoreElements();) 68 { 69 Object modelValue = model.get((key = st.nextToken())); 70 if (modelValue instanceof String && (((String ) modelValue).length() == 0)) 71 { 72 continue; 73 } 74 else if (modelValue != null) 75 { 76 break; 77 } 78 } 79 } 80 else if (key.indexOf('|') != -1) 81 { 82 for (StringTokenizer st = new StringTokenizer (key, "|");st.hasMoreElements();) 83 { 84 Object modelValue = model.get((key = st.nextToken())); 85 if (modelValue instanceof String && (((String ) modelValue).length() == 0)) 86 { 87 continue; 88 } 89 else if (modelValue != null) 90 { 91 break; 92 } 93 } 94 } 95 else if (key.indexOf('&') != -1) 96 { 97 for (StringTokenizer st = new StringTokenizer (key, "&");st.hasMoreElements();) 98 { 99 Object modelValue = model.get((key = st.nextToken())); 100 if (modelValue == null || modelValue instanceof String && (((String ) modelValue).length() == 0)) 101 { 102 break; 103 } 104 } 105 } 106 107 if ((inverse = key.startsWith("!"))) 108 { 109 key = key.substring(1, key.length()); 110 } 111 112 Object modelValue = model.get(key); 113 if (modelValue instanceof String && (((String ) modelValue).length() == 0)) 115 { 116 modelValue = null; 117 } 118 119 if (inverse) 120 { 121 if ((modelValue == null)) 122 { 123 text.replace(start, end + 2, text.substring(text.indexOf(":", start) + 1, end)); 124 } 125 else 126 { 127 text.replace(start, end + 2, ""); 128 } 129 } 130 else 131 { 132 if ((modelValue != null)) 133 { 134 text.replace(start, end + 2, text.substring(text.indexOf(":", start) + 1, end)); 135 } 136 else 137 { 138 text.replace(start, end + 2, ""); 139 } 140 } 141 142 } 143 144 return text; 145 } 146 147 150 public void setEndToken(String endToken) 151 { 152 this.endToken = endToken; 153 } 154 155 158 public void setStartToken(String startToken) 159 { 160 this.startToken = startToken; 161 } 162 } | Popular Tags |