Mercurial Hosting > luan
comparison src/luan/lib/HtmlLib.java @ 49:8ede219cd111
add WebShell
git-svn-id: https://luan-java.googlecode.com/svn/trunk@50 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 28 Dec 2012 19:35:04 +0000 |
parents | |
children | f86e4f77ef32 |
comparison
equal
deleted
inserted
replaced
48:64ecb7a3aad7 | 49:8ede219cd111 |
---|---|
1 package luan.lib; | |
2 | |
3 import luan.LuanState; | |
4 import luan.LuanTable; | |
5 import luan.LuanJavaFunction; | |
6 | |
7 | |
8 public final class HtmlLib { | |
9 | |
10 public static void register(LuanState luan) { | |
11 LuanTable module = new LuanTable(); | |
12 LuanTable global = luan.global(); | |
13 global.put("html",module); | |
14 try { | |
15 add( module, "encode", String.class ); | |
16 } catch(NoSuchMethodException e) { | |
17 throw new RuntimeException(e); | |
18 } | |
19 } | |
20 | |
21 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | |
22 t.put( method, new LuanJavaFunction(HtmlLib.class.getMethod(method,parameterTypes),null) ); | |
23 } | |
24 | |
25 public static String encode(String s) { | |
26 char[] a = s.toCharArray(); | |
27 StringBuilder buf = new StringBuilder(); | |
28 for( int i=0; i<a.length; i++ ) { | |
29 char c = a[i]; | |
30 switch(c) { | |
31 case '&': | |
32 buf.append("&"); | |
33 break; | |
34 case '<': | |
35 buf.append("<"); | |
36 break; | |
37 case '>': | |
38 buf.append(">"); | |
39 break; | |
40 case '"': | |
41 buf.append("""); | |
42 break; | |
43 default: | |
44 buf.append(c); | |
45 } | |
46 } | |
47 return buf.toString(); | |
48 } | |
49 | |
50 } |