View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package com.isenshi.util.extlib;
17  
18  import java.io.IOException;
19  import java.io.StringWriter;
20  import java.io.Writer;
21  import java.util.Enumeration;
22  
23  import javax.servlet.Servlet;
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.ServletResponse;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpSession;
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.JspWriter;
33  import javax.servlet.jsp.PageContext;
34  import javax.servlet.jsp.el.ExpressionEvaluator;
35  import javax.servlet.jsp.el.VariableResolver;
36  
37  import org.apache.struts.taglib.html.JavascriptValidatorTag;
38  
39  /***
40   * @author nishioka
41   */
42  public class JavascriptValidatorUtils {
43  
44  
45  
46  	public static String getScript(Servlet servlet, ServletRequest request,
47  			ServletResponse response, String formName)
48  			throws IllegalStateException, IllegalArgumentException,
49  			IOException, JspException {
50  
51  		StringWriter writer = new StringWriter();
52  
53  		PageContext pageContext = new PageContextImpl(writer);
54  		pageContext.initialize(servlet, request, response, "", false, 0, false);
55  		// pageContext.setContext(context);
56  
57  		JavascriptValidatorTag tag = new JavascriptValidatorTag();
58  		tag.setFormName(formName);
59  		tag.setPageContext(pageContext);
60  
61  		tag.doStartTag();
62  
63  		String ret = writer.toString();
64  
65  		return ret;
66  	}
67  
68  	public static class JspWriterImpl extends JspWriter {
69  
70  		private static final String SEP = System.getProperty("line.separator");
71  
72  		private Writer writer;
73  
74  		public JspWriterImpl(Writer writer, int buffSize, boolean autoFlash) {
75  			super(buffSize, autoFlash);
76  			this.writer = writer;
77  		}
78  
79  		public void newLine() throws IOException {
80  			writer.write(SEP);
81  		}
82  
83  		public void print(boolean b) throws IOException {
84  			write(Boolean.toString(b));
85  		}
86  
87  		public void print(char c) throws IOException {
88  			write(Character.toString(c));
89  		}
90  
91  		public void print(int i) throws IOException {
92  			write(Integer.toString(i));
93  		}
94  
95  		public void print(long l) throws IOException {
96  			write(Long.toString(l));
97  		}
98  
99  		public void print(float f) throws IOException {
100 			write(java.lang.Float.toString(f));
101 		}
102 
103 		public void print(double d) throws IOException {
104 			write(Double.toString(d));
105 		}
106 
107 		public void print(char[] s) throws IOException {
108 			write(s);
109 		}
110 
111 		public void print(String s) throws IOException {
112 			write(s);
113 		}
114 
115 		public void print(Object obj) throws IOException {
116 			write(String.valueOf(obj));
117 		}
118 
119 		public void println() throws IOException {
120 			newLine();
121 		}
122 
123 		public void println(boolean x) throws IOException {
124 			print(x);
125 			newLine();
126 		}
127 
128 		public void println(char x) throws IOException {
129 			print(x);
130 			newLine();
131 		}
132 
133 		public void println(int x) throws IOException {
134 			print(x);
135 			newLine();
136 		}
137 
138 		public void println(long x) throws IOException {
139 			print(x);
140 			newLine();
141 		}
142 
143 		public void println(float x) throws IOException {
144 			print(x);
145 			newLine();
146 		}
147 
148 		public void println(double x) throws IOException {
149 			print(x);
150 			newLine();
151 		}
152 
153 		public void println(char[] x) throws IOException {
154 			print(x);
155 			newLine();
156 		}
157 
158 		public void println(String x) throws IOException {
159 			print(x);
160 			newLine();
161 		}
162 
163 		public void println(Object x) throws IOException {
164 			print(x);
165 			newLine();
166 		}
167 
168 		public void clear() throws IOException {
169 			// TODO Auto-generated method stub
170 
171 		}
172 
173 		public void clearBuffer() throws IOException {
174 			// TODO Auto-generated method stub
175 
176 		}
177 
178 		public void flush() throws IOException {
179 			// TODO Auto-generated method stub
180 
181 		}
182 
183 		public void close() throws IOException {
184 			// TODO Auto-generated method stub
185 
186 		}
187 
188 		public int getRemaining() {
189 			// TODO Auto-generated method stub
190 			return 0;
191 		}
192 
193 		public void write(char[] cbuf, int off, int len) throws IOException {
194 			writer.write(cbuf, off, len);
195 		}
196 	}
197 
198 	public static class PageContextImpl extends PageContext {
199 
200 		private ServletContext context_;
201 
202 		private ServletRequest request;
203 
204 		private ServletResponse response;
205 
206 		private Writer writer;
207 
208 		private JspWriter jspWriter;
209 
210 		public PageContextImpl(Writer writer) {
211 			this.writer = writer;
212 		}
213 
214 		public void initialize(Servlet servlet, ServletRequest arg1,
215 				ServletResponse arg2, String arg3, boolean arg4, int buffSize,
216 				boolean autoFlash) throws IOException, IllegalStateException,
217 				IllegalArgumentException {
218 
219 			this.context_ = servlet.getServletConfig().getServletContext();
220 
221 			// this.context_ = context;
222 			this.request = arg1;
223 			this.response = arg2;
224 
225 			this.jspWriter = new JspWriterImpl(writer, buffSize, autoFlash);
226 
227 			response.getClass();
228 		}
229 
230 		public void release() {
231 		}
232 
233 		public HttpSession getSession() {
234 			return null;
235 		}
236 
237 		public Object getPage() {
238 			return null;
239 		}
240 
241 		public ServletRequest getRequest() {
242 			return this.request;
243 		}
244 
245 		public ServletResponse getResponse() {
246 			return null;
247 		}
248 
249 		public Exception getException() {
250 			return null;
251 		}
252 
253 		public ServletConfig getServletConfig() {
254 			return null;
255 		}
256 
257 		public ServletContext getServletContext() {
258 			return null;
259 		}
260 
261 		public void forward(String arg0) throws ServletException, IOException {
262 		}
263 
264 		public void include(String arg0) throws ServletException, IOException {
265 		}
266 
267 		public void include(String arg0, boolean arg1) throws ServletException,
268 				IOException {
269 		}
270 
271 		public void handlePageException(Exception arg0)
272 				throws ServletException, IOException {
273 		}
274 
275 		public void handlePageException(Throwable arg0)
276 				throws ServletException, IOException {
277 		}
278 
279 		public void setAttribute(String arg0, Object arg1) {
280 		}
281 
282 		public void setAttribute(String arg0, Object arg1, int arg2) {
283 		}
284 
285 		public Object getAttribute(String arg0) {
286 			return null;
287 		}
288 
289 		public Object getAttribute(String name, int scope) {
290 
291 			switch (scope) {
292 			case PAGE_SCOPE:
293 				return null;
294 
295 			case REQUEST_SCOPE:
296 				return this.request.getAttribute(name);
297 
298 			case SESSION_SCOPE:
299 				return ((HttpServletRequest) request).getSession()
300 						.getAttribute(name);
301 
302 			case APPLICATION_SCOPE:
303 				return context_.getAttribute(name);
304 
305 			default:
306 				throw new IllegalArgumentException("Invalid scope");
307 			}
308 
309 		}
310 
311 		public Object findAttribute(String arg0) {
312 			return null;
313 		}
314 
315 		public void removeAttribute(String arg0) {
316 		}
317 
318 		public void removeAttribute(String arg0, int arg1) {
319 		}
320 
321 		public int getAttributesScope(String arg0) {
322 			return 0;
323 		}
324 
325 		public Enumeration getAttributeNamesInScope(int arg0) {
326 			return null;
327 		}
328 
329 		public JspWriter getOut() {
330 			return jspWriter;
331 		}
332 
333 		public ExpressionEvaluator getExpressionEvaluator() {
334 			return null;
335 		}
336 
337 		public VariableResolver getVariableResolver() {
338 			return null;
339 		}
340 	}
341 
342 }