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 org.seasar.tuigwaa.view;
17  
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  import java.io.StringWriter;
21  
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpServletResponseWrapper;
26  
27  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
28  
29  public class JspEngine {
30  
31  	public static String parse(String path) {
32  
33  		StringWriter swriter = new StringWriter();
34  		PrintWriter writer = new PrintWriter(swriter);
35  
36  		HttpServletRequest req = SingletonS2ContainerFactory.getContainer()
37  				.getRequest();
38  		HttpServletResponse res = SingletonS2ContainerFactory.getContainer()
39  				.getResponse();
40  
41  		try {
42  			req.getRequestDispatcher(path).include(req,
43  					new Response(res, writer));
44  		} catch (ServletException e) {
45  			e.printStackTrace();
46  		} catch (IOException e) {
47  			e.printStackTrace();
48  		}
49  
50  		String ret = swriter.toString();
51  		return ret;
52  	}
53  
54  	static class Response extends HttpServletResponseWrapper {
55  
56  		private PrintWriter writer;
57  
58  		public Response(HttpServletResponse response, PrintWriter writer) {
59  			super(response);
60  			this.writer = writer;
61  		}
62  
63  		public PrintWriter getWriter() throws IOException {
64  			return writer;
65  		}
66  	}
67  }