1 24 package org.ofbiz.minilang.method; 25 26 import java.util.Map ; 27 28 import org.ofbiz.base.util.collections.FlexibleMapAccessor; 29 30 39 public class ContextAccessor { 40 41 protected String name; 42 protected FlexibleMapAccessor fma; 43 protected boolean needsExpand; 44 protected boolean empty; 45 46 public ContextAccessor(String name) { 47 init(name); 48 } 49 50 public ContextAccessor(String name, String defaultName) { 51 if (name == null || name.length() == 0) { 52 init(defaultName); 53 } else { 54 init(name); 55 } 56 } 57 58 protected void init(String name) { 59 this.name = name; 60 if (name == null || name.length() == 0) { 61 empty = true; 62 needsExpand = false; 63 fma = new FlexibleMapAccessor(name); 64 } else { 65 empty = false; 66 int openPos = name.indexOf("${"); 67 if (openPos != -1 && name.indexOf("}", openPos) != -1) { 68 fma = null; 69 needsExpand = true; 70 } else { 71 fma = new FlexibleMapAccessor(name); 72 needsExpand = false; 73 } 74 } 75 } 76 77 public boolean isEmpty() { 78 return this.empty; 79 } 80 81 82 public Object get(MethodContext methodContext) { 83 if (this.needsExpand) { 84 return methodContext.getEnv(name); 85 } else { 86 return methodContext.getEnv(fma); 87 } 88 } 89 90 96 public void put(MethodContext methodContext, Object value) { 97 if (this.needsExpand) { 98 methodContext.putEnv(name, value); 99 } else { 100 methodContext.putEnv(fma, value); 101 } 102 } 103 104 105 public Object remove(MethodContext methodContext) { 106 if (this.needsExpand) { 107 return methodContext.removeEnv(name); 108 } else { 109 return methodContext.removeEnv(fma); 110 } 111 } 112 113 114 public Object get(Map theMap, MethodContext methodContext) { 115 if (this.needsExpand) { 116 FlexibleMapAccessor fma = new FlexibleMapAccessor(methodContext.expandString(name)); 117 return fma.get(theMap); 118 } else { 119 return fma.get(theMap); 120 } 121 } 122 123 129 public void put(Map theMap, Object value, MethodContext methodContext) { 130 if (this.needsExpand) { 131 FlexibleMapAccessor fma = new FlexibleMapAccessor(methodContext.expandString(name)); 132 fma.put(theMap, value); 133 } else { 134 fma.put(theMap, value); 135 } 136 } 137 138 139 public Object remove(Map theMap, MethodContext methodContext) { 140 if (this.needsExpand) { 141 FlexibleMapAccessor fma = new FlexibleMapAccessor(methodContext.expandString(name)); 142 return fma.remove(theMap); 143 } else { 144 return fma.remove(theMap); 145 } 146 } 147 148 149 public int hashCode() { 150 return this.name.hashCode(); 151 } 152 153 154 public boolean equals(Object obj) { 155 if (obj instanceof ContextAccessor) { 156 ContextAccessor contextAccessor = (ContextAccessor) obj; 157 if (this.name == null) { 158 return contextAccessor.name == null; 159 } 160 return this.name.equals(contextAccessor.name); 161 } else { 162 String str = (String ) obj; 163 if (this.name == null) { 164 return str == null; 165 } 166 return this.name.equals(str); 167 } 168 } 169 170 171 public String toString() { 172 return this.name; 173 } 174 } 175 | Popular Tags |