001: /*
002: * $Id: FlexibleServletAccessor.java,v 1.2 2003/11/25 07:48:14 jonesde Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.base.util;
025:
026: import java.util.List;
027: import java.util.Map;
028:
029: import javax.servlet.ServletRequest;
030: import javax.servlet.http.HttpSession;
031:
032: /**
033: * Used to flexibly access Map values, supporting the "." (dot) syntax for
034: * accessing sub-map values and the "[]" (square bracket) syntax for accessing
035: * list elements. See individual Map operations for more information.
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.2 $
039: * @since 2.1
040: */
041: public class FlexibleServletAccessor {
042:
043: protected String name;
044: protected String attributeName;
045: protected FlexibleMapAccessor fma;
046: protected boolean needsExpand;
047: protected boolean empty;
048:
049: public FlexibleServletAccessor(String name) {
050: init(name);
051: }
052:
053: public FlexibleServletAccessor(String name, String defaultName) {
054: if (name == null || name.length() == 0) {
055: init(defaultName);
056: } else {
057: init(name);
058: }
059: }
060:
061: protected void init(String name) {
062: this .name = name;
063: if (name == null || name.length() == 0) {
064: empty = true;
065: needsExpand = false;
066: fma = new FlexibleMapAccessor(name);
067: attributeName = name;
068: } else {
069: empty = false;
070: int openPos = name.indexOf("${");
071: if (openPos != -1 && name.indexOf("}", openPos) != -1) {
072: fma = null;
073: attributeName = null;
074: needsExpand = true;
075: } else {
076: int dotIndex = name.indexOf('.');
077: if (dotIndex != -1) {
078: attributeName = name.substring(0, dotIndex);
079: fma = new FlexibleMapAccessor(name
080: .substring(dotIndex + 1));
081: } else {
082: attributeName = name;
083: fma = null;
084: }
085:
086: needsExpand = false;
087: }
088: }
089: }
090:
091: public boolean isEmpty() {
092: return this .empty;
093: }
094:
095: /** Based on name get from ServletRequest or from List in ServletRequest
096: * @param request request to get the value from
097: * @param expandContext the context to use for name expansion
098: * @return the object corresponding to this getter class
099: */
100: public Object get(ServletRequest request, Map expandContext) {
101: AttributeAccessor aa = new AttributeAccessor(name,
102: expandContext, this .attributeName, this .fma,
103: 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 get(HttpSession session, Map expandContext) {
113: AttributeAccessor aa = new AttributeAccessor(name,
114: expandContext, this .attributeName, this .fma,
115: this .needsExpand);
116: return aa.get(session);
117: }
118:
119: /** Based on name put in ServletRequest or from List in ServletRequest;
120: * If the brackets for a list are empty the value will be appended to the list,
121: * otherwise the value will be set in the position of the number in the brackets.
122: * If a "+" (plus sign) is included inside the square brackets before the index
123: * number the value will inserted/added at that point instead of set at the point.
124: * @param request
125: * @param value
126: * @param expandContext
127: */
128: public void put(ServletRequest request, Object value,
129: Map expandContext) {
130: AttributeAccessor aa = new AttributeAccessor(name,
131: expandContext, this .attributeName, this .fma,
132: this .needsExpand);
133: aa.put(request, value);
134: }
135:
136: /** Based on name put in HttpSession or from List in HttpSession;
137: * If the brackets for a list are empty the value will be appended to the list,
138: * otherwise the value will be set in the position of the number in the brackets.
139: * If a "+" (plus sign) is included inside the square brackets before the index
140: * number the value will inserted/added at that point instead of set at the point.
141: * @param session
142: * @param value
143: * @param expandContext
144: */
145: public void put(HttpSession session, Object value, Map expandContext) {
146: AttributeAccessor aa = new AttributeAccessor(name,
147: expandContext, this .attributeName, this .fma,
148: this .needsExpand);
149: aa.put(session, value);
150: }
151:
152: /** Based on name remove from ServletRequest or from List in ServletRequest
153: * @param request
154: * @param expandContext
155: * @return
156: */
157: public Object remove(ServletRequest request, Map expandContext) {
158: AttributeAccessor aa = new AttributeAccessor(name,
159: expandContext, this .attributeName, this .fma,
160: this .needsExpand);
161: return aa.remove(request);
162: }
163:
164: /** Based on name remove from HttpSession or from List in HttpSession
165: * @param session
166: * @param expandContext
167: * @return
168: */
169: public Object remove(HttpSession session, Map expandContext) {
170: AttributeAccessor aa = new AttributeAccessor(name,
171: expandContext, this .attributeName, this .fma,
172: this .needsExpand);
173: return aa.remove(session);
174: }
175:
176: /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key *
177: * @return
178: */
179: public int hashCode() {
180: return this .name.hashCode();
181: }
182:
183: /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key
184: * @param obj
185: * @return
186: */
187: public boolean equals(Object obj) {
188: if (obj instanceof FlexibleServletAccessor) {
189: FlexibleServletAccessor flexibleServletAccessor = (FlexibleServletAccessor) obj;
190: if (this .name == null) {
191: return flexibleServletAccessor.name == null;
192: }
193: return this .name.equals(flexibleServletAccessor.name);
194: } else {
195: String str = (String) obj;
196: if (this .name == null) {
197: return str == null;
198: }
199: return this .name.equals(str);
200: }
201: }
202:
203: /** To be used for a string representation of the accessor, returns the original name.
204: * @return
205: */
206: public String toString() {
207: return this .name;
208: }
209:
210: protected static class AttributeAccessor {
211: protected Map expandContext;
212: protected String attributeName;
213: protected FlexibleMapAccessor fma;
214: protected boolean isListReference;
215: protected boolean isAddAtIndex;
216: protected boolean isAddAtEnd;
217: protected int listIndex;
218: protected int openBrace;
219: protected int closeBrace;
220:
221: public AttributeAccessor(String origName, Map expandContext,
222: String defAttributeName, FlexibleMapAccessor defFma,
223: boolean needsExpand) {
224: attributeName = defAttributeName;
225: fma = defFma;
226:
227: if (needsExpand) {
228: String name = FlexibleStringExpander.expandString(
229: origName, expandContext);
230: int dotIndex = name.indexOf('.');
231: if (dotIndex != -1) {
232: attributeName = name.substring(0, dotIndex);
233: fma = new FlexibleMapAccessor(name
234: .substring(dotIndex + 1));
235: } else {
236: attributeName = name;
237: fma = null;
238: }
239: }
240:
241: isListReference = false;
242: isAddAtIndex = false;
243: isAddAtEnd = false;
244: listIndex = -1;
245: openBrace = attributeName.indexOf('[');
246: closeBrace = (openBrace == -1 ? -1 : attributeName.indexOf(
247: ']', openBrace));
248: if (openBrace != -1 && closeBrace != -1) {
249: String liStr = attributeName.substring(openBrace + 1,
250: closeBrace);
251: //if brackets are empty, append to list
252: if (liStr.length() == 0) {
253: isAddAtEnd = true;
254: } else {
255: if (liStr.charAt(0) == '+') {
256: liStr = liStr.substring(1);
257: listIndex = Integer.parseInt(liStr);
258: isAddAtIndex = true;
259: } else {
260: listIndex = Integer.parseInt(liStr);
261: }
262: }
263: attributeName = attributeName.substring(0, openBrace);
264: isListReference = true;
265: }
266:
267: }
268:
269: public Object get(ServletRequest request) {
270: Object theValue = null;
271: if (isListReference) {
272: List lst = (List) request.getAttribute(attributeName);
273: theValue = lst.get(listIndex);
274: } else {
275: theValue = request.getAttribute(attributeName);
276: }
277:
278: if (fma != null) {
279: return fma.get((Map) theValue);
280: } else {
281: return theValue;
282: }
283: }
284:
285: public Object get(HttpSession session) {
286: Object theValue = null;
287: if (isListReference) {
288: List lst = (List) session.getAttribute(attributeName);
289: theValue = lst.get(listIndex);
290: } else {
291: theValue = session.getAttribute(attributeName);
292: }
293:
294: if (fma != null) {
295: return fma.get((Map) theValue);
296: } else {
297: return theValue;
298: }
299: }
300:
301: protected void putInList(List lst, Object value) {
302: //if brackets are empty, append to list
303: if (isAddAtEnd) {
304: lst.add(value);
305: } else {
306: if (isAddAtIndex) {
307: lst.add(listIndex, value);
308: } else {
309: lst.set(listIndex, value);
310: }
311: }
312: }
313:
314: public void put(ServletRequest request, Object value) {
315: if (fma == null) {
316: if (isListReference) {
317: List lst = (List) request
318: .getAttribute(attributeName);
319: putInList(lst, value);
320: } else {
321: request.setAttribute(attributeName, value);
322: }
323: } else {
324: Object theObj = request.getAttribute(attributeName);
325: if (isListReference) {
326: List lst = (List) theObj;
327: fma.put((Map) lst.get(listIndex), value);
328: } else {
329: fma.put((Map) theObj, value);
330: }
331: }
332: }
333:
334: public void put(HttpSession session, Object value) {
335: if (fma == null) {
336: if (isListReference) {
337: List lst = (List) session
338: .getAttribute(attributeName);
339: putInList(lst, value);
340: } else {
341: session.setAttribute(attributeName, value);
342: }
343: } else {
344: Object theObj = session.getAttribute(attributeName);
345: if (isListReference) {
346: List lst = (List) theObj;
347: fma.put((Map) lst.get(listIndex), value);
348: } else {
349: fma.put((Map) theObj, value);
350: }
351: }
352: }
353:
354: public Object remove(ServletRequest request) {
355: if (fma != null) {
356: Object theObj = request.getAttribute(attributeName);
357: if (isListReference) {
358: List lst = (List) theObj;
359: return fma.remove((Map) lst.get(listIndex));
360: } else {
361: return fma.remove((Map) theObj);
362: }
363: } else {
364: if (isListReference) {
365: List lst = (List) request
366: .getAttribute(attributeName);
367: return lst.remove(listIndex);
368: } else {
369: Object theValue = request
370: .getAttribute(attributeName);
371: request.removeAttribute(attributeName);
372: return theValue;
373: }
374: }
375: }
376:
377: public Object remove(HttpSession session) {
378: if (fma != null) {
379: Object theObj = session.getAttribute(attributeName);
380: if (isListReference) {
381: List lst = (List) theObj;
382: return fma.remove((Map) lst.get(listIndex));
383: } else {
384: return fma.remove((Map) theObj);
385: }
386: } else {
387: if (isListReference) {
388: List lst = (List) session
389: .getAttribute(attributeName);
390: return lst.remove(listIndex);
391: } else {
392: Object theValue = session
393: .getAttribute(attributeName);
394: session.removeAttribute(attributeName);
395: return theValue;
396: }
397: }
398: }
399: }
400: }
|