KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > collections > FlexibleServletAccessor


1 /*
2  * $Id: FlexibleServletAccessor.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util.collections;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.ServletRequest JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
32
33 import org.ofbiz.base.util.string.FlexibleStringExpander;
34
35 /**
36  * Used to flexibly access Map values, supporting the "." (dot) syntax for
37  * accessing sub-map values and the "[]" (square bracket) syntax for accessing
38  * list elements. See individual Map operations for more information.
39  *
40  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
41  * @version $Rev: 5720 $
42  * @since 2.1
43  */

44 public class FlexibleServletAccessor implements Serializable JavaDoc {
45
46     protected String JavaDoc name;
47     protected String JavaDoc attributeName;
48     protected FlexibleMapAccessor fma;
49     protected boolean needsExpand;
50     protected boolean empty;
51
52     public FlexibleServletAccessor(String JavaDoc name) {
53         init(name);
54     }
55     
56     public FlexibleServletAccessor(String JavaDoc name, String JavaDoc defaultName) {
57         if (name == null || name.length() == 0) {
58             init(defaultName);
59         } else {
60             init(name);
61         }
62     }
63     
64     protected void init(String JavaDoc name) {
65         this.name = name;
66         if (name == null || name.length() == 0) {
67             empty = true;
68             needsExpand = false;
69             fma = new FlexibleMapAccessor(name);
70             attributeName = name;
71         } else {
72             empty = false;
73             int openPos = name.indexOf("${");
74             if (openPos != -1 && name.indexOf("}", openPos) != -1) {
75                 fma = null;
76                 attributeName = null;
77                 needsExpand = true;
78             } else {
79                 int dotIndex = name.indexOf('.');
80                 if (dotIndex != -1) {
81                     attributeName = name.substring(0, dotIndex);
82                     fma = new FlexibleMapAccessor(name.substring(dotIndex+1));
83                 } else {
84                     attributeName = name;
85                     fma = null;
86                 }
87                 
88                 needsExpand = false;
89             }
90         }
91     }
92     
93     public boolean isEmpty() {
94         return this.empty;
95     }
96
97     /** Based on name get from ServletRequest or from List in ServletRequest
98      * @param request request to get the value from
99      * @param expandContext the context to use for name expansion
100      * @return the object corresponding to this getter class
101      */

102     public Object JavaDoc get(ServletRequest JavaDoc request, Map JavaDoc expandContext) {
103         AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
104         return aa.get(request);
105     }
106
107     /** Based on name get from HttpSession or from List in HttpSession
108      * @param session
109      * @param expandContext
110      * @return
111      */

112     public Object JavaDoc get(HttpSession JavaDoc session, Map JavaDoc expandContext) {
113         AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
114         return aa.get(session);
115     }
116
117     /** Based on name put in ServletRequest or from List in ServletRequest;
118      * If the brackets for a list are empty the value will be appended to the list,
119      * otherwise the value will be set in the position of the number in the brackets.
120      * If a "+" (plus sign) is included inside the square brackets before the index
121      * number the value will inserted/added at that point instead of set at the point.
122      * @param request
123      * @param value
124      * @param expandContext
125      */

126     public void put(ServletRequest JavaDoc request, Object JavaDoc value, Map JavaDoc expandContext) {
127         AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
128         aa.put(request, value);
129     }
130     
131     /** Based on name put in HttpSession or from List in HttpSession;
132      * If the brackets for a list are empty the value will be appended to the list,
133      * otherwise the value will be set in the position of the number in the brackets.
134      * If a "+" (plus sign) is included inside the square brackets before the index
135      * number the value will inserted/added at that point instead of set at the point.
136      * @param session
137      * @param value
138      * @param expandContext
139      */

140     public void put(HttpSession JavaDoc session, Object JavaDoc value, Map JavaDoc expandContext) {
141         AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
142         aa.put(session, value);
143     }
144     
145     /** Based on name remove from ServletRequest or from List in ServletRequest
146      * @param request
147      * @param expandContext
148      * @return
149      */

150     public Object JavaDoc remove(ServletRequest JavaDoc request, Map JavaDoc expandContext) {
151         AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
152         return aa.remove(request);
153     }
154     
155     /** Based on name remove from HttpSession or from List in HttpSession
156      * @param session
157      * @param expandContext
158      * @return
159      */

160     public Object JavaDoc remove(HttpSession JavaDoc session, Map JavaDoc expandContext) {
161         AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
162         return aa.remove(session);
163     }
164     
165     /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key *
166      * @return
167      */

168     public int hashCode() {
169         return this.name.hashCode();
170     }
171
172     /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key
173      * @param obj
174      * @return
175      */

176     public boolean equals(Object JavaDoc obj) {
177         if (obj instanceof FlexibleServletAccessor) {
178             FlexibleServletAccessor flexibleServletAccessor = (FlexibleServletAccessor) obj;
179             if (this.name == null) {
180                 return flexibleServletAccessor.name == null;
181             }
182             return this.name.equals(flexibleServletAccessor.name);
183         } else {
184             String JavaDoc str = (String JavaDoc) obj;
185             if (this.name == null) {
186                 return str == null;
187             }
188             return this.name.equals(str);
189         }
190     }
191
192     /** To be used for a string representation of the accessor, returns the original name.
193      * @return
194      */

195     public String JavaDoc toString() {
196         return this.name;
197     }
198     
199     protected static class AttributeAccessor implements Serializable JavaDoc {
200         protected Map JavaDoc expandContext;
201         protected String JavaDoc attributeName;
202         protected FlexibleMapAccessor fma;
203         protected boolean isListReference;
204         protected boolean isAddAtIndex;
205         protected boolean isAddAtEnd;
206         protected int listIndex;
207         protected int openBrace;
208         protected int closeBrace;
209         
210         public AttributeAccessor(String JavaDoc origName, Map JavaDoc expandContext, String JavaDoc defAttributeName, FlexibleMapAccessor defFma, boolean needsExpand) {
211             attributeName = defAttributeName;
212             fma = defFma;
213             
214             if (needsExpand) {
215                 String JavaDoc name = FlexibleStringExpander.expandString(origName, expandContext);
216                 int dotIndex = name.indexOf('.');
217                 if (dotIndex != -1) {
218                     attributeName = name.substring(0, dotIndex);
219                     fma = new FlexibleMapAccessor(name.substring(dotIndex+1));
220                 } else {
221                     attributeName = name;
222                     fma = null;
223                 }
224             }
225
226             isListReference = false;
227             isAddAtIndex = false;
228             isAddAtEnd = false;
229             listIndex = -1;
230             openBrace = attributeName.indexOf('[');
231             closeBrace = (openBrace == -1 ? -1 : attributeName.indexOf(']', openBrace));
232             if (openBrace != -1 && closeBrace != -1) {
233                 String JavaDoc liStr = attributeName.substring(openBrace+1, closeBrace);
234                 //if brackets are empty, append to list
235
if (liStr.length() == 0) {
236                     isAddAtEnd = true;
237                 } else {
238                     if (liStr.charAt(0) == '+') {
239                         liStr = liStr.substring(1);
240                         listIndex = Integer.parseInt(liStr);
241                         isAddAtIndex = true;
242                     } else {
243                         listIndex = Integer.parseInt(liStr);
244                     }
245                 }
246                 attributeName = attributeName.substring(0, openBrace);
247                 isListReference = true;
248             }
249         
250         }
251
252         public Object JavaDoc get(ServletRequest JavaDoc request) {
253             Object JavaDoc theValue = null;
254             if (isListReference) {
255                 List JavaDoc lst = (List JavaDoc) request.getAttribute(attributeName);
256                 theValue = lst.get(listIndex);
257             } else {
258                 theValue = request.getAttribute(attributeName);
259             }
260
261             if (fma != null) {
262                 return fma.get((Map JavaDoc) theValue);
263             } else {
264                 return theValue;
265             }
266         }
267
268         public Object JavaDoc get(HttpSession JavaDoc session) {
269             Object JavaDoc theValue = null;
270             if (isListReference) {
271                 List JavaDoc lst = (List JavaDoc) session.getAttribute(attributeName);
272                 theValue = lst.get(listIndex);
273             } else {
274                 theValue = session.getAttribute(attributeName);
275             }
276
277             if (fma != null) {
278                 return fma.get((Map JavaDoc) theValue);
279             } else {
280                 return theValue;
281             }
282         }
283
284         protected void putInList(List JavaDoc lst, Object JavaDoc value) {
285             //if brackets are empty, append to list
286
if (isAddAtEnd) {
287                 lst.add(value);
288             } else {
289                 if (isAddAtIndex) {
290                     lst.add(listIndex, value);
291                 } else {
292                     lst.set(listIndex, value);
293                 }
294             }
295         }
296         
297         public void put(ServletRequest JavaDoc request, Object JavaDoc value) {
298             if (fma == null) {
299                 if (isListReference) {
300                     List JavaDoc lst = (List JavaDoc) request.getAttribute(attributeName);
301                     putInList(lst, value);
302                 } else {
303                     request.setAttribute(attributeName, value);
304                 }
305             } else {
306                 Object JavaDoc theObj = request.getAttribute(attributeName);
307                 if (isListReference) {
308                     List JavaDoc lst = (List JavaDoc) theObj;
309                     fma.put((Map JavaDoc) lst.get(listIndex), value);
310                 } else {
311                     fma.put((Map JavaDoc) theObj, value);
312                 }
313             }
314         }
315         
316         public void put(HttpSession JavaDoc session, Object JavaDoc value) {
317             if (fma == null) {
318                 if (isListReference) {
319                     List JavaDoc lst = (List JavaDoc) session.getAttribute(attributeName);
320                     putInList(lst, value);
321                 } else {
322                     session.setAttribute(attributeName, value);
323                 }
324             } else {
325                 Object JavaDoc theObj = session.getAttribute(attributeName);
326                 if (isListReference) {
327                     List JavaDoc lst = (List JavaDoc) theObj;
328                     fma.put((Map JavaDoc) lst.get(listIndex), value);
329                 } else {
330                     fma.put((Map JavaDoc) theObj, value);
331                 }
332             }
333         }
334
335         public Object JavaDoc remove(ServletRequest JavaDoc request) {
336             if (fma != null) {
337                 Object JavaDoc theObj = request.getAttribute(attributeName);
338                 if (isListReference) {
339                     List JavaDoc lst = (List JavaDoc) theObj;
340                     return fma.remove((Map JavaDoc) lst.get(listIndex));
341                 } else {
342                     return fma.remove((Map JavaDoc) theObj);
343                 }
344             } else {
345                 if (isListReference) {
346                     List JavaDoc lst = (List JavaDoc) request.getAttribute(attributeName);
347                     return lst.remove(listIndex);
348                 } else {
349                     Object JavaDoc theValue = request.getAttribute(attributeName);
350                     request.removeAttribute(attributeName);
351                     return theValue;
352                 }
353             }
354         }
355
356         public Object JavaDoc remove(HttpSession JavaDoc session) {
357             if (fma != null) {
358                 Object JavaDoc theObj = session.getAttribute(attributeName);
359                 if (isListReference) {
360                     List JavaDoc lst = (List JavaDoc) theObj;
361                     return fma.remove((Map JavaDoc) lst.get(listIndex));
362                 } else {
363                     return fma.remove((Map JavaDoc) theObj);
364                 }
365             } else {
366                 if (isListReference) {
367                     List JavaDoc lst = (List JavaDoc) session.getAttribute(attributeName);
368                     return lst.remove(listIndex);
369                 } else {
370                     Object JavaDoc theValue = session.getAttribute(attributeName);
371                     session.removeAttribute(attributeName);
372                     return theValue;
373                 }
374             }
375         }
376     }
377 }
378
Popular Tags