1 /* 2 * $Id: VelocityScopeExtractor.java 1066512 2011-02-02 16:13:31Z apetrelli $ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 package org.apache.tiles.request.velocity.extractor; 22 23 import java.util.Enumeration; 24 25 import org.apache.tiles.request.attribute.AttributeExtractor; 26 import org.apache.velocity.context.Context; 27 28 /** 29 * Extracts attributes from Velocity context.. 30 * 31 * @version $Rev: 1066512 $ $Date: 2011-02-03 03:13:31 +1100 (Thu, 03 Feb 2011) $ 32 */ 33 public class VelocityScopeExtractor implements AttributeExtractor { 34 35 /** 36 * The Velocity context. 37 */ 38 private Context context; 39 40 /** 41 * Constructor. 42 * 43 * @param context The Velocity context. 44 */ 45 public VelocityScopeExtractor(Context context) { 46 this.context = context; 47 } 48 49 @Override 50 public void removeValue(String name) { 51 context.remove(name); 52 } 53 54 @Override 55 public Enumeration<String> getKeys() { 56 return new KeyEnumeration(context.getKeys()); 57 } 58 59 @Override 60 public Object getValue(String key) { 61 return context.get(key); 62 } 63 64 @Override 65 public void setValue(String key, Object value) { 66 context.put(key, value); 67 } 68 69 /** 70 * Enumerates an array. 71 */ 72 private static class KeyEnumeration implements Enumeration<String> { 73 74 /** 75 * The current index. 76 */ 77 private int index = 0; 78 79 /** 80 * The array to enumerate. 81 */ 82 private Object[] keys; 83 84 /** 85 * Constructor. 86 * 87 * @param keys The array to enumerate. 88 */ 89 public KeyEnumeration(Object[] keys) { 90 this.keys = keys; 91 } 92 93 @Override 94 public boolean hasMoreElements() { 95 return index < keys.length; 96 } 97 98 @Override 99 public String nextElement() { 100 return (String) keys[index++]; 101 } 102 } 103 }