123 default: 124 throw new IllegalArgumentException("Invalid scope value."); 125 } 126 } 127 128 129 /** 130 * Retrieves the value of the attribute with the given name in 131 * the scope occurring earliest in the search order. The order 132 * is determined by the numeric value of the scope parameter (lowest 133 * scope values first.) 134 * 135 * @param name The name of the attribute to retrieve. 136 * @return The value of the attribute in the lowest scope for 137 * which an attribute with the given name is defined. Returns 138 * null if no attribute with the name exists in any scope. 139 * @throws NullPointerException if the name is null. 140 * @throws IllegalArgumentException if the name is empty. 141 */ 142 public Object getAttribute(String name) { 143 if (engineScope.containsKey(name)) { 144 return getAttribute(name, ENGINE_SCOPE); 145 } else if (globalScope != null && globalScope.containsKey(name)) { 146 return getAttribute(name, GLOBAL_SCOPE); 147 } 148 149 return null; 150 } 151 152 /** 153 * Gets the value of an attribute in a given scope. 154 * 155 * @param name The name of the attribute to retrieve. 156 * @param scope The scope in which to retrieve the attribute. 157 * @return The value of the attribute. Returns <code>null</code> is the name 158 * does not exist in the given scope. 159 * 160 * @throws IllegalArgumentException 161 * if the name is empty or if the value of scope is invalid. 162 * @throws NullPointerException if the name is null. 163 */ 164 public Object getAttribute(String name, int scope) { 165 166 switch (scope) { 167 168 case ENGINE_SCOPE: 169 return engineScope.get(name); 170 171 case GLOBAL_SCOPE: 172 if (globalScope != null) { 173 return globalScope.get(name); 174 } 175 return null; 176 177 default: 178 throw new IllegalArgumentException("Illegal scope value."); 179 } 180 } 181 182 /** 183 * Remove an attribute in a given scope. 184 * 185 * @param name The name of the attribute to remove 186 * @param scope The scope in which to remove the attribute 187 * 188 * @return The removed value. 189 * @throws IllegalArgumentException 190 * if the name is empty or if the scope is invalid. 191 * @throws NullPointerException if the name is null. 192 */ 193 public Object removeAttribute(String name, int scope) { 194 195 switch (scope) { 196 197 case ENGINE_SCOPE: 198 if (getBindings(ENGINE_SCOPE) != null) { 199 return getBindings(ENGINE_SCOPE).remove(name); 200 } 201 return null; 202 203 case GLOBAL_SCOPE: 204 if (getBindings(GLOBAL_SCOPE) != null) { 205 return getBindings(GLOBAL_SCOPE).remove(name); 206 } 207 return null; 208 209 default: 210 throw new IllegalArgumentException("Illegal scope value."); 211 } 212 } 213 214 /** 215 * Sets the value of an attribute in a given scope. 216 * 217 * @param name The name of the attribute to set 218 * @param value The value of the attribute 219 * @param scope The scope in which to set the attribute 220 * 221 * @throws IllegalArgumentException 222 * if the name is empty or if the scope is invalid. 223 * @throws NullPointerException if the name is null. 224 */ 225 public void setAttribute(String name, Object value, int scope) { 226 227 switch (scope) { 228 229 case ENGINE_SCOPE: 230 engineScope.put(name, value); 231 return; 232 233 case GLOBAL_SCOPE: 234 if (globalScope != null) { 235 globalScope.put(name, value); 236 } 237 return; 238 239 default: 240 throw new IllegalArgumentException("Illegal scope value."); 241 } 242 } 243 244 /** {@inheritDoc} */ 245 public Writer getWriter() { 246 return writer; 264 /** {@inheritDoc} */ 265 public Writer getErrorWriter() { 266 return errorWriter; 267 } 268 269 /** {@inheritDoc} */ 270 public void setErrorWriter(Writer writer) { 271 this.errorWriter = writer; 272 } 273 274 /** 275 * Get the lowest scope in which an attribute is defined. 276 * @param name Name of the attribute 277 * . 278 * @return The lowest scope. Returns -1 if no attribute with the given 279 * name is defined in any scope. 280 * @throws NullPointerException if name is null. 281 * @throws IllegalArgumentException if name is empty. 282 */ 283 public int getAttributesScope(String name) { 284 if (engineScope.containsKey(name)) { 285 return ENGINE_SCOPE; 286 } else if (globalScope != null && globalScope.containsKey(name)) { 287 return GLOBAL_SCOPE; 288 } else { 289 return -1; 290 } 291 } 292 293 /** 294 * Returns the value of the <code>engineScope</code> field if specified scope is 295 * <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is 296 * <code>GLOBAL_SCOPE</code>. 297 * 298 * @param scope The specified scope 299 * @return The value of either the <code>engineScope</code> or <code>globalScope</code> field. 300 * @throws IllegalArgumentException if the value of scope is invalid. 301 */ 302 public Bindings getBindings(int scope) { 303 if (scope == ENGINE_SCOPE) { 304 return engineScope; 305 } else if (scope == GLOBAL_SCOPE) { 306 return globalScope; 307 } else { 308 throw new IllegalArgumentException("Illegal scope value."); 309 } 310 } 311 312 /** {@inheritDoc} */ 313 public List<Integer> getScopes() { 314 return scopes; 315 } 316 317 private static List<Integer> scopes; 318 static { 319 scopes = new ArrayList<Integer>(2); 320 scopes.add(ENGINE_SCOPE); 321 scopes.add(GLOBAL_SCOPE); 322 scopes = Collections.unmodifiableList(scopes); 323 } 324 } | 123 default: 124 throw new IllegalArgumentException("Invalid scope value."); 125 } 126 } 127 128 129 /** 130 * Retrieves the value of the attribute with the given name in 131 * the scope occurring earliest in the search order. The order 132 * is determined by the numeric value of the scope parameter (lowest 133 * scope values first.) 134 * 135 * @param name The name of the attribute to retrieve. 136 * @return The value of the attribute in the lowest scope for 137 * which an attribute with the given name is defined. Returns 138 * null if no attribute with the name exists in any scope. 139 * @throws NullPointerException if the name is null. 140 * @throws IllegalArgumentException if the name is empty. 141 */ 142 public Object getAttribute(String name) { 143 checkName(name); 144 if (engineScope.containsKey(name)) { 145 return getAttribute(name, ENGINE_SCOPE); 146 } else if (globalScope != null && globalScope.containsKey(name)) { 147 return getAttribute(name, GLOBAL_SCOPE); 148 } 149 150 return null; 151 } 152 153 /** 154 * Gets the value of an attribute in a given scope. 155 * 156 * @param name The name of the attribute to retrieve. 157 * @param scope The scope in which to retrieve the attribute. 158 * @return The value of the attribute. Returns <code>null</code> is the name 159 * does not exist in the given scope. 160 * 161 * @throws IllegalArgumentException 162 * if the name is empty or if the value of scope is invalid. 163 * @throws NullPointerException if the name is null. 164 */ 165 public Object getAttribute(String name, int scope) { 166 checkName(name); 167 switch (scope) { 168 169 case ENGINE_SCOPE: 170 return engineScope.get(name); 171 172 case GLOBAL_SCOPE: 173 if (globalScope != null) { 174 return globalScope.get(name); 175 } 176 return null; 177 178 default: 179 throw new IllegalArgumentException("Illegal scope value."); 180 } 181 } 182 183 /** 184 * Remove an attribute in a given scope. 185 * 186 * @param name The name of the attribute to remove 187 * @param scope The scope in which to remove the attribute 188 * 189 * @return The removed value. 190 * @throws IllegalArgumentException 191 * if the name is empty or if the scope is invalid. 192 * @throws NullPointerException if the name is null. 193 */ 194 public Object removeAttribute(String name, int scope) { 195 checkName(name); 196 switch (scope) { 197 198 case ENGINE_SCOPE: 199 if (getBindings(ENGINE_SCOPE) != null) { 200 return getBindings(ENGINE_SCOPE).remove(name); 201 } 202 return null; 203 204 case GLOBAL_SCOPE: 205 if (getBindings(GLOBAL_SCOPE) != null) { 206 return getBindings(GLOBAL_SCOPE).remove(name); 207 } 208 return null; 209 210 default: 211 throw new IllegalArgumentException("Illegal scope value."); 212 } 213 } 214 215 /** 216 * Sets the value of an attribute in a given scope. 217 * 218 * @param name The name of the attribute to set 219 * @param value The value of the attribute 220 * @param scope The scope in which to set the attribute 221 * 222 * @throws IllegalArgumentException 223 * if the name is empty or if the scope is invalid. 224 * @throws NullPointerException if the name is null. 225 */ 226 public void setAttribute(String name, Object value, int scope) { 227 checkName(name); 228 switch (scope) { 229 230 case ENGINE_SCOPE: 231 engineScope.put(name, value); 232 return; 233 234 case GLOBAL_SCOPE: 235 if (globalScope != null) { 236 globalScope.put(name, value); 237 } 238 return; 239 240 default: 241 throw new IllegalArgumentException("Illegal scope value."); 242 } 243 } 244 245 /** {@inheritDoc} */ 246 public Writer getWriter() { 247 return writer; 265 /** {@inheritDoc} */ 266 public Writer getErrorWriter() { 267 return errorWriter; 268 } 269 270 /** {@inheritDoc} */ 271 public void setErrorWriter(Writer writer) { 272 this.errorWriter = writer; 273 } 274 275 /** 276 * Get the lowest scope in which an attribute is defined. 277 * @param name Name of the attribute 278 * . 279 * @return The lowest scope. Returns -1 if no attribute with the given 280 * name is defined in any scope. 281 * @throws NullPointerException if name is null. 282 * @throws IllegalArgumentException if name is empty. 283 */ 284 public int getAttributesScope(String name) { 285 checkName(name); 286 if (engineScope.containsKey(name)) { 287 return ENGINE_SCOPE; 288 } else if (globalScope != null && globalScope.containsKey(name)) { 289 return GLOBAL_SCOPE; 290 } else { 291 return -1; 292 } 293 } 294 295 /** 296 * Returns the value of the <code>engineScope</code> field if specified scope is 297 * <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is 298 * <code>GLOBAL_SCOPE</code>. 299 * 300 * @param scope The specified scope 301 * @return The value of either the <code>engineScope</code> or <code>globalScope</code> field. 302 * @throws IllegalArgumentException if the value of scope is invalid. 303 */ 304 public Bindings getBindings(int scope) { 305 if (scope == ENGINE_SCOPE) { 306 return engineScope; 307 } else if (scope == GLOBAL_SCOPE) { 308 return globalScope; 309 } else { 310 throw new IllegalArgumentException("Illegal scope value."); 311 } 312 } 313 314 /** {@inheritDoc} */ 315 public List<Integer> getScopes() { 316 return scopes; 317 } 318 319 private void checkName(String name) { 320 Objects.requireNonNull(name); 321 if (name.isEmpty()) { 322 throw new IllegalArgumentException("name cannot be empty"); 323 } 324 } 325 326 private static List<Integer> scopes; 327 static { 328 scopes = new ArrayList<Integer>(2); 329 scopes.add(ENGINE_SCOPE); 330 scopes.add(GLOBAL_SCOPE); 331 scopes = Collections.unmodifiableList(scopes); 332 } 333 } |