1 package net.sourceforge.formview.displayer; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import net.sourceforge.formview.FieldView; 10 import net.sourceforge.formview.LabelValue; 11 import net.sourceforge.formview.util.FormulaUtil; 12 13 23 public class BaseElement { 24 25 private String name; 26 27 private String insertBeforeElement; 28 private String insertAfterElement; 29 private String replaceElement; 30 31 32 private List lInsertAttribute = null; 33 34 private List lForEach = null; 35 private List lIf = null; 36 37 public String getName() { 38 return name; 39 } 40 public void setName(String name) { 41 this.name = name; 42 } 43 public String getInsertAfterElement() { 44 return insertAfterElement; 45 } 46 public void setInsertAfterElement(String insertAfterElement) { 47 this.insertAfterElement = insertAfterElement; 48 } 49 public String getInsertBeforeElement() { 50 return insertBeforeElement; 51 } 52 public void setInsertBeforeElement(String insertBeforeElement) { 53 this.insertBeforeElement = insertBeforeElement; 54 } 55 public String getReplaceElement() { 56 return replaceElement; 57 } 58 public void setReplaceElement(String replaceElement) { 59 this.replaceElement = replaceElement; 60 } 61 62 public void addInsertAttribute(Attribute attribute) { 63 if (lInsertAttribute == null) 64 lInsertAttribute = new ArrayList (); 65 lInsertAttribute.add(attribute); 66 } 67 68 69 70 public List getInsertAttributeList() { 71 return lInsertAttribute; 72 } 73 74 public void addIf(If testIf) { 75 if (lIf == null) 76 lIf = new ArrayList (); 77 lIf.add(testIf); 78 } 79 80 public void addForEach(ForEach forEach) { 81 if (lForEach == null) 82 lForEach = new ArrayList (); 83 lForEach.add(forEach); 84 } 85 86 public List getForEachList() { 87 return lForEach; 88 } 89 90 public String toString() { 91 StringBuffer results = new StringBuffer (); 92 results.append("\t\tname = " + name + "\n"); 93 results.append("\t\tinsertBeforeElement = " + insertBeforeElement + "\n"); 94 results.append("\t\tinsertAfterElement = " + insertAfterElement + "\n"); 95 results.append("\t\treplaceElement = " + replaceElement + "\n"); 96 if (lInsertAttribute != null) { 97 for (Iterator iter = lInsertAttribute.iterator(); iter.hasNext();) { 98 results.append("\t\tinsertAttribute : \n"); 99 results.append(iter.next()); 100 } 101 } 102 if (lIf != null) { 103 for (Iterator iter = lIf.iterator(); iter.hasNext();) { 104 results.append("\t\tif : \n"); 105 results.append(iter.next()); 106 } 107 } 108 if (lForEach != null) { 109 for (Iterator iter = lForEach.iterator(); iter.hasNext();) { 110 results.append("\t\tforEach : \n"); 111 results.append(iter.next()); 112 } 113 } 114 return results.toString(); 115 } 116 117 public void processHTML(FieldView field, String defaultBehaviour, Map contextValuesMap, StringBuffer htmlContentToInsertBefore, StringBuffer htmlContentToInsertAfter, StringBuffer htmlContentToReplace, Map attributeMap) { 118 if (lIf != null) { 119 If elementToExecute = null; 121 for (Iterator iter = lIf.iterator(); iter.hasNext();) { 122 If elementIf = (If) iter.next(); 123 if (FormulaUtil.executeCondition(elementIf.getTest() ,attributeMap)) { 124 elementToExecute = elementIf; 125 break; 126 } 127 } 128 if (elementToExecute != null) { 129 List forEachList = elementToExecute.getForEachList(); 131 if (forEachList != null) { 132 for (Iterator iter = forEachList.iterator(); iter.hasNext();) { 133 ForEach elementForEach = (ForEach) iter.next(); 134 135 String items = elementForEach.getItems(); 136 String var = elementForEach.getVar(); 137 138 List itemsList = (List )contextValuesMap.get(items); 139 if (itemsList != null) { 140 for (Iterator iterator = itemsList.iterator(); iterator 141 .hasNext();) { 142 LabelValue elementItem = (LabelValue) iterator.next(); 143 String value = elementItem.getValue(); 144 String label = elementItem.getLabel(); 145 146 String optionContextValue = "${" + var + ".value}"; 147 String optionContextLabel = "${" + var + ".label}"; 148 149 Map itemContextValuesMap = new HashMap (contextValuesMap); 150 itemContextValuesMap.put(optionContextValue, value); 151 itemContextValuesMap.put(optionContextLabel, label); 152 153 processHTML(itemContextValuesMap, htmlContentToInsertBefore, htmlContentToInsertAfter, htmlContentToReplace, attributeMap, 154 elementForEach); 155 } 156 157 } 158 159 160 } 161 } 162 163 164 processHTML(contextValuesMap, htmlContentToInsertBefore, htmlContentToInsertAfter, htmlContentToReplace, attributeMap, 165 elementToExecute); 166 } 167 } 168 else {processHTML(contextValuesMap, htmlContentToInsertBefore, htmlContentToInsertAfter, htmlContentToReplace, attributeMap, 169 this); 170 } 171 } 172 173 174 private void processHTML(Map contextValuesMap, StringBuffer htmlContentToInsertBefore, 175 StringBuffer htmlContentToInsertAfter, StringBuffer htmlContentToReplace, Map attributeMap, 176 BaseElement element) { 177 String insertBeforeElement = element.getInsertBeforeElement(); 179 String insertAfterElement = element.getInsertAfterElement(); 180 String replaceElement =element.getReplaceElement(); 181 List lInsertAttribute = element.getInsertAttributeList(); 182 if (insertBeforeElement != null) { 183 String htmlContent = replaceKeyWithContextValue(insertBeforeElement, contextValuesMap); 184 htmlContentToInsertBefore.append(htmlContent); 185 } 186 if (insertAfterElement != null) { 187 String htmlContent = replaceKeyWithContextValue(insertAfterElement, contextValuesMap); 188 htmlContentToInsertAfter.append(htmlContent); 189 } 190 if (replaceElement != null) { 191 String htmlContent = replaceKeyWithContextValue(replaceElement, contextValuesMap); 192 htmlContentToReplace.append(htmlContent); 193 } 194 if (lInsertAttribute != null) { 196 for (Iterator iter = lInsertAttribute.iterator(); iter.hasNext();) { 197 Attribute attribute = (Attribute) iter.next(); 198 String attributeName = attribute.getName(); 199 if (attributeMap.get(attributeName) == null) { 200 String attributeValue = replaceKeyWithContextValue(attribute.getValue(), contextValuesMap); 201 attributeMap.put(attributeName, attributeValue); 203 } 204 205 } 206 } 207 } 208 209 210 private String replaceKeyWithContextValue(String htmlContent, Map contextValuesMap) { 211 if (htmlContent.indexOf("${") == -1) { 213 return htmlContent; 215 } 216 for (Iterator iter = contextValuesMap.keySet().iterator(); iter.hasNext();) { 218 String key = (String ) iter.next(); 219 Object o = contextValuesMap.get(key); 220 if (o instanceof String ) { 221 String value = (String )contextValuesMap.get(key); 222 htmlContent = replace(htmlContent, key, value); 223 } 224 } 225 return htmlContent; 226 } 227 228 private String replace( 229 String value, 230 String key, 231 String replaceValue) { 232 233 if (value == null || key == null || replaceValue == null) { 234 return value; 235 } 236 237 int pos = value.indexOf(key); 238 239 if (pos < 0) { 240 return value; 241 } 242 243 int length = value.length(); 244 int start = pos; 245 int end = pos + key.length(); 246 247 if (length == key.length()) { 248 value = replaceValue; 249 250 } else if (end == length) { 251 value = value.substring(0, start) + replaceValue; 252 253 } else { 254 value = 255 value.substring(0, start) 256 + replaceValue 257 + replace(value.substring(end), key, replaceValue); 258 } 259 260 return value; 261 } 262 263 264 265 } 266 | Popular Tags |