1 52 53 package freemarker.ext.servlet; 54 55 import freemarker.template.*; 56 import java.util.*; 57 import javax.servlet.http.*; 58 59 64 65 public class HttpRequestParametersHashModel 66 implements 67 TemplateHashModelEx 68 { 69 private final HttpServletRequest request; 70 private List keys; 71 72 public HttpRequestParametersHashModel(HttpServletRequest request) 73 { 74 this.request = request; 75 } 76 77 public TemplateModel get(String key) 78 { 79 String value = request.getParameter(key); 80 return value == null ? null : new SimpleScalar(value); 81 } 82 83 public boolean isEmpty() 84 { 85 return !request.getParameterNames().hasMoreElements(); 86 } 87 88 public int size() { 89 return getKeys().size(); 90 } 91 92 public TemplateCollectionModel keys() { 93 return new SimpleCollection(getKeys().iterator()); 94 } 95 96 public TemplateCollectionModel values() { 97 final Iterator iter = getKeys().iterator(); 98 return new SimpleCollection( 99 new Iterator() { 100 public boolean hasNext() { 101 return iter.hasNext(); 102 } 103 public Object next() { 104 return request.getParameter((String )iter.next()); 105 } 106 public void remove() { 107 throw new UnsupportedOperationException (); 108 } 109 }); 110 } 111 112 protected String transcode(String string) 113 { 114 return string; 115 } 116 117 private synchronized List getKeys() { 118 if(keys == null) { 119 keys = new ArrayList(); 120 for (Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements();) { 121 keys.add(enumeration.nextElement()); 122 } 123 } 124 return keys; 125 } 126 } 127 | Popular Tags |