1 24 package org.ofbiz.base.util.collections; 25 26 import java.io.Serializable ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.servlet.ServletRequest ; 31 import javax.servlet.http.HttpSession ; 32 33 import org.ofbiz.base.util.string.FlexibleStringExpander; 34 35 44 public class FlexibleServletAccessor implements Serializable { 45 46 protected String name; 47 protected String attributeName; 48 protected FlexibleMapAccessor fma; 49 protected boolean needsExpand; 50 protected boolean empty; 51 52 public FlexibleServletAccessor(String name) { 53 init(name); 54 } 55 56 public FlexibleServletAccessor(String name, String defaultName) { 57 if (name == null || name.length() == 0) { 58 init(defaultName); 59 } else { 60 init(name); 61 } 62 } 63 64 protected void init(String 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 102 public Object get(ServletRequest request, Map expandContext) { 103 AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); 104 return aa.get(request); 105 } 106 107 112 public Object get(HttpSession session, Map expandContext) { 113 AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); 114 return aa.get(session); 115 } 116 117 126 public void put(ServletRequest request, Object value, Map expandContext) { 127 AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); 128 aa.put(request, value); 129 } 130 131 140 public void put(HttpSession session, Object value, Map expandContext) { 141 AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); 142 aa.put(session, value); 143 } 144 145 150 public Object remove(ServletRequest request, Map expandContext) { 151 AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); 152 return aa.remove(request); 153 } 154 155 160 public Object remove(HttpSession session, Map expandContext) { 161 AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); 162 return aa.remove(session); 163 } 164 165 168 public int hashCode() { 169 return this.name.hashCode(); 170 } 171 172 176 public boolean equals(Object 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 str = (String ) obj; 185 if (this.name == null) { 186 return str == null; 187 } 188 return this.name.equals(str); 189 } 190 } 191 192 195 public String toString() { 196 return this.name; 197 } 198 199 protected static class AttributeAccessor implements Serializable { 200 protected Map expandContext; 201 protected String 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 origName, Map expandContext, String defAttributeName, FlexibleMapAccessor defFma, boolean needsExpand) { 211 attributeName = defAttributeName; 212 fma = defFma; 213 214 if (needsExpand) { 215 String 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 liStr = attributeName.substring(openBrace+1, closeBrace); 234 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 get(ServletRequest request) { 253 Object theValue = null; 254 if (isListReference) { 255 List lst = (List ) 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 ) theValue); 263 } else { 264 return theValue; 265 } 266 } 267 268 public Object get(HttpSession session) { 269 Object theValue = null; 270 if (isListReference) { 271 List lst = (List ) 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 ) theValue); 279 } else { 280 return theValue; 281 } 282 } 283 284 protected void putInList(List lst, Object value) { 285 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 request, Object value) { 298 if (fma == null) { 299 if (isListReference) { 300 List lst = (List ) request.getAttribute(attributeName); 301 putInList(lst, value); 302 } else { 303 request.setAttribute(attributeName, value); 304 } 305 } else { 306 Object theObj = request.getAttribute(attributeName); 307 if (isListReference) { 308 List lst = (List ) theObj; 309 fma.put((Map ) lst.get(listIndex), value); 310 } else { 311 fma.put((Map ) theObj, value); 312 } 313 } 314 } 315 316 public void put(HttpSession session, Object value) { 317 if (fma == null) { 318 if (isListReference) { 319 List lst = (List ) session.getAttribute(attributeName); 320 putInList(lst, value); 321 } else { 322 session.setAttribute(attributeName, value); 323 } 324 } else { 325 Object theObj = session.getAttribute(attributeName); 326 if (isListReference) { 327 List lst = (List ) theObj; 328 fma.put((Map ) lst.get(listIndex), value); 329 } else { 330 fma.put((Map ) theObj, value); 331 } 332 } 333 } 334 335 public Object remove(ServletRequest request) { 336 if (fma != null) { 337 Object theObj = request.getAttribute(attributeName); 338 if (isListReference) { 339 List lst = (List ) theObj; 340 return fma.remove((Map ) lst.get(listIndex)); 341 } else { 342 return fma.remove((Map ) theObj); 343 } 344 } else { 345 if (isListReference) { 346 List lst = (List ) request.getAttribute(attributeName); 347 return lst.remove(listIndex); 348 } else { 349 Object theValue = request.getAttribute(attributeName); 350 request.removeAttribute(attributeName); 351 return theValue; 352 } 353 } 354 } 355 356 public Object remove(HttpSession session) { 357 if (fma != null) { 358 Object theObj = session.getAttribute(attributeName); 359 if (isListReference) { 360 List lst = (List ) theObj; 361 return fma.remove((Map ) lst.get(listIndex)); 362 } else { 363 return fma.remove((Map ) theObj); 364 } 365 } else { 366 if (isListReference) { 367 List lst = (List ) session.getAttribute(attributeName); 368 return lst.remove(listIndex); 369 } else { 370 Object theValue = session.getAttribute(attributeName); 371 session.removeAttribute(attributeName); 372 return theValue; 373 } 374 } 375 } 376 } 377 } 378 | Popular Tags |