comparison src/luan/modules/HtmlLib.java @ 167:4c0131c2b650

merge luan/lib into modules git-svn-id: https://luan-java.googlecode.com/svn/trunk@168 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:28:32 +0000
parents src/luan/lib/HtmlLib.java@05f8c21160ef
children
comparison
equal deleted inserted replaced
166:4eaee12f6c65 167:4c0131c2b650
1 package luan.modules;
2
3 import luan.LuanState;
4 import luan.LuanTable;
5 import luan.LuanFunction;
6 import luan.LuanJavaFunction;
7 import luan.LuanException;
8
9
10 public final class HtmlLib {
11
12 public static final LuanFunction LOADER = new LuanFunction() {
13 @Override public Object call(LuanState luan,Object[] args) {
14 LuanTable module = new LuanTable();
15 try {
16 add( module, "encode", String.class );
17 } catch(NoSuchMethodException e) {
18 throw new RuntimeException(e);
19 }
20 return module;
21 }
22 };
23
24 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
25 t.put( method, new LuanJavaFunction(HtmlLib.class.getMethod(method,parameterTypes),null) );
26 }
27
28 public static String encode(String s) {
29 char[] a = s.toCharArray();
30 StringBuilder buf = new StringBuilder();
31 for( int i=0; i<a.length; i++ ) {
32 char c = a[i];
33 switch(c) {
34 case '&':
35 buf.append("&amp;");
36 break;
37 case '<':
38 buf.append("&lt;");
39 break;
40 case '>':
41 buf.append("&gt;");
42 break;
43 case '"':
44 buf.append("&quot;");
45 break;
46 default:
47 buf.append(c);
48 }
49 }
50 return buf.toString();
51 }
52
53 }