KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > formview > displayer > BaseElement


1 package net.sourceforge.formview.displayer;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import net.sourceforge.formview.FieldView;
10 import net.sourceforge.formview.LabelValue;
11 import net.sourceforge.formview.util.FormulaUtil;
12
13 /**
14  * Description : Base element of Property and Behaviour.
15  * loaded with displayers-config.xml
16  * XML file. This class describe attribute of HTML element
17  * (eg : maxlength="50" into input HTML).
18  *
19  * @version 1.0.0
20  * @author <a HREF="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
21  *
22  */

23 public class BaseElement {
24
25     private String JavaDoc name;
26     
27     private String JavaDoc insertBeforeElement;
28     private String JavaDoc insertAfterElement;
29     private String JavaDoc replaceElement;
30     
31     
32     private List JavaDoc lInsertAttribute = null;
33     
34     private List JavaDoc lForEach = null;
35     private List JavaDoc lIf = null;
36
37     public String JavaDoc getName() {
38         return name;
39     }
40     public void setName(String JavaDoc name) {
41         this.name = name;
42     }
43     public String JavaDoc getInsertAfterElement() {
44         return insertAfterElement;
45     }
46     public void setInsertAfterElement(String JavaDoc insertAfterElement) {
47         this.insertAfterElement = insertAfterElement;
48     }
49     public String JavaDoc getInsertBeforeElement() {
50         return insertBeforeElement;
51     }
52     public void setInsertBeforeElement(String JavaDoc insertBeforeElement) {
53         this.insertBeforeElement = insertBeforeElement;
54     }
55     public String JavaDoc getReplaceElement() {
56         return replaceElement;
57     }
58     public void setReplaceElement(String JavaDoc replaceElement) {
59         this.replaceElement = replaceElement;
60     }
61     
62     public void addInsertAttribute(Attribute attribute) {
63         if (lInsertAttribute == null)
64             lInsertAttribute = new ArrayList JavaDoc();
65         lInsertAttribute.add(attribute);
66     }
67     
68     
69     
70     public List JavaDoc getInsertAttributeList() {
71         return lInsertAttribute;
72     }
73     
74     public void addIf(If testIf) {
75         if (lIf == null)
76             lIf = new ArrayList JavaDoc();
77         lIf.add(testIf);
78     }
79     
80     public void addForEach(ForEach forEach) {
81         if (lForEach == null)
82             lForEach = new ArrayList JavaDoc();
83         lForEach.add(forEach);
84     }
85     
86     public List JavaDoc getForEachList() {
87         return lForEach;
88     }
89     
90     public String JavaDoc toString() {
91         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc defaultBehaviour, Map JavaDoc contextValuesMap, StringBuffer JavaDoc htmlContentToInsertBefore, StringBuffer JavaDoc htmlContentToInsertAfter, StringBuffer JavaDoc htmlContentToReplace, Map JavaDoc attributeMap) {
118         if (lIf != null) {
119             // Loop for test of If
120
If elementToExecute = null;
121             for (Iterator JavaDoc 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                 // Test if element has foreach
130
List JavaDoc forEachList = elementToExecute.getForEachList();
131                 if (forEachList != null) {
132                     for (Iterator JavaDoc iter = forEachList.iterator(); iter.hasNext();) {
133                         ForEach elementForEach = (ForEach) iter.next();
134                         
135                         String JavaDoc items = elementForEach.getItems();
136                         String JavaDoc var = elementForEach.getVar();
137                         
138                         List JavaDoc itemsList = (List JavaDoc)contextValuesMap.get(items);
139                         if (itemsList != null) {
140                             for (Iterator JavaDoc iterator = itemsList.iterator(); iterator
141                                     .hasNext();) {
142                                 LabelValue elementItem = (LabelValue) iterator.next();
143                                 String JavaDoc value = elementItem.getValue();
144                                 String JavaDoc label = elementItem.getLabel();
145                                 
146                                 String JavaDoc optionContextValue = "${" + var + ".value}";
147                                 String JavaDoc optionContextLabel = "${" + var + ".label}";
148                                 
149                                 Map JavaDoc itemContextValuesMap = new HashMap JavaDoc(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 JavaDoc contextValuesMap, StringBuffer JavaDoc htmlContentToInsertBefore,
175             StringBuffer JavaDoc htmlContentToInsertAfter, StringBuffer JavaDoc htmlContentToReplace, Map JavaDoc attributeMap,
176             BaseElement element) {
177         // Update Elements
178
String JavaDoc insertBeforeElement = element.getInsertBeforeElement();
179         String JavaDoc insertAfterElement = element.getInsertAfterElement();
180         String JavaDoc replaceElement =element.getReplaceElement();
181         List JavaDoc lInsertAttribute = element.getInsertAttributeList();
182         if (insertBeforeElement != null) {
183             String JavaDoc htmlContent = replaceKeyWithContextValue(insertBeforeElement, contextValuesMap);
184             htmlContentToInsertBefore.append(htmlContent);
185         }
186         if (insertAfterElement != null) {
187             String JavaDoc htmlContent = replaceKeyWithContextValue(insertAfterElement, contextValuesMap);
188             htmlContentToInsertAfter.append(htmlContent);
189         }
190         if (replaceElement != null) {
191             String JavaDoc htmlContent = replaceKeyWithContextValue(replaceElement, contextValuesMap);
192             htmlContentToReplace.append(htmlContent);
193         }
194         // Update Attributes
195
if (lInsertAttribute != null) {
196             for (Iterator JavaDoc iter = lInsertAttribute.iterator(); iter.hasNext();) {
197                 Attribute attribute = (Attribute) iter.next();
198                 String JavaDoc attributeName = attribute.getName();
199                 if (attributeMap.get(attributeName) == null) {
200                     String JavaDoc attributeValue = replaceKeyWithContextValue(attribute.getValue(), contextValuesMap);
201                     // insert attribute to element
202
attributeMap.put(attributeName, attributeValue);
203                 }
204                 
205             }
206         }
207     }
208
209     
210     private String JavaDoc replaceKeyWithContextValue(String JavaDoc htmlContent, Map JavaDoc contextValuesMap) {
211         // Test if html content contains ${
212
if (htmlContent.indexOf("${") == -1) {
213             // No value to replace, return html content
214
return htmlContent;
215         }
216         // Loop for parameters of Map
217
for (Iterator JavaDoc iter = contextValuesMap.keySet().iterator(); iter.hasNext();) {
218             String JavaDoc key = (String JavaDoc) iter.next();
219             Object JavaDoc o = contextValuesMap.get(key);
220             if (o instanceof String JavaDoc) {
221                 String JavaDoc value = (String JavaDoc)contextValuesMap.get(key);
222                 htmlContent = replace(htmlContent, key, value);
223             }
224         }
225         return htmlContent;
226     }
227     
228     private String JavaDoc replace(
229             String JavaDoc value,
230             String JavaDoc key,
231             String JavaDoc 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