Mercurial Hosting > luan
comparison src/luan/tools/WebRun.java @ 52:0c0f2b107099
add WebRun
git-svn-id: https://luan-java.googlecode.com/svn/trunk@53 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 31 Dec 2012 03:15:20 +0000 |
parents | |
children | 5c9cadd0acce |
comparison
equal
deleted
inserted
replaced
51:28dfd91a816c | 52:0c0f2b107099 |
---|---|
1 package luan.tools; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.PrintStream; | |
5 import javax.servlet.ServletException; | |
6 import javax.servlet.ServletOutputStream; | |
7 import javax.servlet.http.HttpServlet; | |
8 import javax.servlet.http.HttpServletRequest; | |
9 import javax.servlet.http.HttpServletResponse; | |
10 import org.slf4j.Logger; | |
11 import org.slf4j.LoggerFactory; | |
12 import luan.LuanState; | |
13 import luan.LuanException; | |
14 import luan.lib.HtmlLib; | |
15 | |
16 | |
17 public class WebRun extends HttpServlet { | |
18 private static final Logger logger = LoggerFactory.getLogger(WebRun.class); | |
19 | |
20 protected LuanState newLuanState() throws LuanException { | |
21 return CmdLine.newStandardLuan(); | |
22 } | |
23 | |
24 protected void service(HttpServletRequest request,HttpServletResponse response) | |
25 throws ServletException, IOException | |
26 { | |
27 ServletOutputStream sout = response.getOutputStream(); | |
28 PrintStream out = new PrintStream(sout); | |
29 String contentType = request.getParameter("content_type"); | |
30 if( contentType != null ) | |
31 response.setContentType("text/plain"); | |
32 String code = request.getParameter("code"); | |
33 try { | |
34 LuanState luan = newLuanState(); | |
35 luan.out = out; | |
36 luan.global().put("request",request); | |
37 luan.global().put("response",response); | |
38 CmdLine.eval(luan,code,"WebRun"); | |
39 } catch(LuanException e) { | |
40 logger.error(null,e); | |
41 response.reset(); | |
42 response.setHeader("Content-Type","text/html"); | |
43 out.println( "<html>" ); | |
44 out.println( "<body>" ); | |
45 out.println( "<pre>" ); | |
46 out.println( e ); | |
47 out.println(); | |
48 out.println( addLineNumbers(HtmlLib.encode(code)) ); | |
49 out.println( "</pre>" ); | |
50 out.println( "</body>" ); | |
51 out.println( "</html>" ); | |
52 } | |
53 } | |
54 | |
55 public static String addLineNumbers(String s) { | |
56 StringBuilder buf = new StringBuilder(); | |
57 int line = 1; | |
58 int i = 0; | |
59 while(true) { | |
60 buf.append( fmt(line++,3) ); | |
61 buf.append(" "); | |
62 int i2 = s.indexOf('\n',i); | |
63 if( i2 == -1 ) { | |
64 buf.append( s.substring(i) ); | |
65 break; | |
66 } | |
67 buf.append( s.substring(i,i2+1) ); | |
68 i = i2 + 1; | |
69 } | |
70 return buf.toString(); | |
71 } | |
72 | |
73 private static String fmt(int i,int w) { | |
74 StringBuilder buf = new StringBuilder(); | |
75 buf.append(i); | |
76 while( buf.length() < w ) { | |
77 buf.insert(0,' '); | |
78 } | |
79 return buf.toString(); | |
80 } | |
81 | |
82 } |