11package com .hindbiswas .jhp ;
22
3- import org .antlr .v4 .runtime .*;
4- import org .antlr .v4 .runtime .tree .*;
5-
6- import com .hindbiswas .jhp .ast .AstBuilder ;
7- import com .hindbiswas .jhp .ast .AstPrettyPrinter ;
8- import com .hindbiswas .jhp .ast .AstRendererOld ;
9- import com .hindbiswas .jhp .ast .TemplateNode ;
103import com .hindbiswas .jhp .engine .FunctionLibrary ;
114import com .hindbiswas .jhp .engine .JhpEngine ;
125import com .hindbiswas .jhp .engine .Settings ;
136
14- import java .nio .file .Files ;
15- import java .nio .file .Path ;
16- import java .nio .file .Paths ;
17- import java .util .HashMap ;
18- import java .util .Map ;
7+ class User {
8+ public String name ;
9+ public int age ;
10+ public String gender ;
11+
12+ User (String name , int age , String gender ) {
13+ this .name = name ;
14+ this .age = age ;
15+ this .gender = gender ;
16+ }
17+ }
1918
2019public class App {
2120 public static void main (String [] args ) throws Exception {
@@ -25,58 +24,11 @@ public static void main(String[] args) throws Exception {
2524 FunctionLibrary lib = new FunctionLibrary ();
2625 JhpEngine engine = new JhpEngine (settings , lib );
2726
28- Map <String , Object > ctx = new HashMap <>();
29- Map <String , Object > user = new HashMap <>();
30- user .put ("name" , "Alice" );
31- user .put ("age" , 150 );
32- user .put ("gender" , "f" );
33- ctx .put ("user" , user );
34- ctx .put ("title" , "Test JHP" );
27+ Context ctx = new Context ();
28+ ctx .add ("user" , new User ("Alice" , 150 , "f" ));
29+ ctx .add ("title" , "Test JHP" );
3530
3631 String out = engine .render (file , ctx );
3732 System .out .println (out );
3833 }
39-
40- public static void oldReneder (String file ) throws Exception {
41- ParseTree tree = generateTree (file );
42- AstBuilder builder = new AstBuilder ();
43- TemplateNode ast = (TemplateNode ) builder .visit (tree );
44- AstPrettyPrinter .print (ast );
45-
46- // Context
47- Map <String , Object > ctx = new HashMap <>();
48- Map <String , Object > user = new HashMap <>();
49- user .put ("name" , "Alice" );
50- user .put ("age" , 150 );
51- user .put ("gender" , "F" );
52- ctx .put ("user" , user );
53- ctx .put ("title" , "Test JHP" );
54-
55- // render
56- AstRendererOld renderer = new AstRendererOld (Path .of ("examples" )); // optional base dir
57- String out = renderer .render (ast , ctx );
58- System .out .println (out );
59- }
60-
61- public static ParseTree generateTree (String file ) throws Exception {
62- // Read template file
63- String text = Files .readString (Paths .get (file ));
64-
65- // Create CharStream
66- CharStream cs = CharStreams .fromString (text );
67-
68- // Lexer & token stream
69- JhpTemplateLexer lexer = new JhpTemplateLexer (cs );
70- CommonTokenStream tokens = new CommonTokenStream (lexer );
71-
72- // Parser
73- JhpTemplateParser parser = new JhpTemplateParser (tokens );
74- parser .removeErrorListeners ();
75- parser .addErrorListener (new DiagnosticErrorListener ());
76-
77- // Parse template
78- ParseTree tree = parser .template ();
79-
80- return tree ;
81- }
8234}
0 commit comments