View Javadoc
1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.servlet.view;
18  
19  import java.util.List;
20  import java.util.Map;
21  import javax.servlet.http.HttpServletRequest;
22  
23  import org.springframework.web.servlet.support.BindStatus;
24  import org.springframework.web.servlet.support.RequestContext;
25  import org.springframework.web.util.UriTemplate;
26  
27  /**
28   * Dummy request context used for VTL and FTL macro tests.
29   *
30   * @author Darren Davison
31   * @author Juergen Hoeller
32   * @since 25.01.2005
33   * @see org.springframework.web.servlet.support.RequestContext
34   */
35  public class DummyMacroRequestContext {
36  
37  	private HttpServletRequest request;
38  
39  	private Map messageMap;
40  
41  	private Map themeMessageMap;
42  
43  	private String contextPath;
44  
45  
46  	public DummyMacroRequestContext(HttpServletRequest request) {
47  		this.request = request;
48  	}
49  
50  
51  	public void setMessageMap(Map messageMap) {
52  		this.messageMap = messageMap;
53  	}
54  
55  	public void setThemeMessageMap(Map themeMessageMap) {
56  		this.themeMessageMap = themeMessageMap;
57  	}
58  
59  
60  	/**
61  	 * @see org.springframework.web.servlet.support.RequestContext#getMessage(String)
62  	 */
63  	public String getMessage(String code) {
64  		return (String) this.messageMap.get(code);
65  	}
66  
67  	/**
68  	 * @see org.springframework.web.servlet.support.RequestContext#getMessage(String, String)
69  	 */
70  	public String getMessage(String code, String defaultMsg) {
71  		String msg = (String) this.messageMap.get(code);
72  		return (msg != null ? msg : defaultMsg);
73  	}
74  
75  	/**
76  	 * @see org.springframework.web.servlet.support.RequestContext#getMessage(String, List)
77  	 */
78  	public String getMessage(String code, List args) {
79  		return ((String) this.messageMap.get(code)) + args.toString();
80  	}
81  
82  	/**
83  	 * @see org.springframework.web.servlet.support.RequestContext#getMessage(String, List, String)
84  	 */
85  	public String getMessage(String code, List args, String defaultMsg) {
86  		String msg = (String) this.messageMap.get(code);
87  		return (msg != null ? msg  + args.toString(): defaultMsg);
88  	}
89  
90  	/**
91  	 * @see org.springframework.web.servlet.support.RequestContext#getThemeMessage(String)
92  	 */
93  	public String getThemeMessage(String code) {
94  		return (String) this.themeMessageMap.get(code);
95  	}
96  
97  	/**
98  	 * @see org.springframework.web.servlet.support.RequestContext#getThemeMessage(String, String)
99  	 */
100 	public String getThemeMessage(String code, String defaultMsg) {
101 		String msg = (String) this.themeMessageMap.get(code);
102 		return (msg != null ? msg : defaultMsg);
103 	}
104 
105 	/**
106 	 * @see org.springframework.web.servlet.support.RequestContext#getThemeMessage(String, List)
107 	 */
108 	public String getThemeMessage(String code, List args) {
109 		return ((String) this.themeMessageMap.get(code)) + args.toString();
110 	}
111 
112 	/**
113 	 * @see org.springframework.web.servlet.support.RequestContext#getThemeMessage(String, List, String)
114 	 */
115 	public String getThemeMessage(String code, List args, String defaultMsg) {
116 		String msg = (String) this.themeMessageMap.get(code);
117 		return (msg != null ? msg  + args.toString(): defaultMsg);
118 	}
119 
120 	public void setContextPath(String contextPath) {
121 		this.contextPath = contextPath;
122 	}
123 
124 	/**
125 	 * @see org.springframework.web.servlet.support.RequestContext#getContextPath()
126 	 */
127 	public String getContextPath() {
128 		return this.contextPath;
129 	}
130 
131 	/**
132 	 * @see org.springframework.web.servlet.support.RequestContext#getContextUrl(String)
133 	 */
134 	public String getContextUrl(String relativeUrl) {
135 		return getContextPath() + relativeUrl;
136 	}
137 
138 	/**
139 	 * @see org.springframework.web.servlet.support.RequestContext#getContextUrl(String, Map)
140 	 */
141 	public String getContextUrl(String relativeUrl, Map<String,String> params) {
142 		UriTemplate template = new UriTemplate(relativeUrl);
143 		return getContextPath() + template.expand(params).toASCIIString();
144 	}
145 
146 	/**
147 	 * @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String)
148 	 */
149 	public BindStatus getBindStatus(String path) throws IllegalStateException {
150 		return new BindStatus(new RequestContext(this.request), path, false);
151 	}
152 
153 	/**
154 	 * @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String, boolean)
155 	 */
156 	public BindStatus getBindStatus(String path, boolean htmlEscape) throws IllegalStateException {
157 		return new BindStatus(new RequestContext(this.request), path, true);
158 	}
159 
160 }