1
1
package org .javalite .templator ;
2
2
3
- import org .javalite .templator .tags .IfTag ;
4
- import org .javalite .templator .tags .ListTag ;
5
-
6
3
import java .io .Writer ;
7
- import java .util .*;
8
-
9
- import static org .javalite .common .Util .split ;
4
+ import java .util .ArrayList ;
5
+ import java .util .List ;
6
+ import java .util .Map ;
7
+ import java .util .Stack ;
10
8
11
9
/**
12
10
* @author Igor Polevoy on 1/10/15.
@@ -16,11 +14,11 @@ public class Template {
16
14
private final List <TemplateToken > templateTokens = new ArrayList <TemplateToken >();
17
15
18
16
public Template (String template ) {
19
- try {
17
+ try {
20
18
parse (template );
21
- }catch (ParseException e ){
19
+ } catch (ParseException e ) {
22
20
throw e ;
23
- }catch (Exception e ){
21
+ } catch (Exception e ) {
24
22
throw new TemplateException (e );
25
23
}
26
24
@@ -31,9 +29,11 @@ List<TemplateToken> templateTokens() {
31
29
return templateTokens ;
32
30
}
33
31
32
+ Stack <AbstractTag > stack = new Stack <AbstractTag >();
33
+
34
34
private void parse (String template ) throws IllegalAccessException , InstantiationException {
35
35
Map <String , Class > tags = TemplatorConfig .instance ().getTags ();
36
- Stack < AbstractTag > stack = new Stack < AbstractTag >();
36
+
37
37
List <AbstractTag > tagsList = new ArrayList <AbstractTag >();
38
38
39
39
//TODO: separate iterations through all tags, and focus on stack
@@ -45,7 +45,7 @@ private void parse(String template) throws IllegalAccessException, Instantiation
45
45
tag = (AbstractTag ) tagClass .newInstance ();
46
46
47
47
//match tag start, such as "<#list"
48
- if (stack . isEmpty () && tag .matchStartAtIndex (template , templateIndex )) {
48
+ if (tag .matchStartAtIndex (template , templateIndex )) {
49
49
tag .setTagStartIndex (templateIndex - tag .getTagStart ().length ());
50
50
stack .push (tag );
51
51
continue ;
@@ -66,28 +66,33 @@ private void parse(String template) throws IllegalAccessException, Instantiation
66
66
67
67
//match tag end, such as: <#list people as person > body </#list>
68
68
// ---^
69
- if (!stack .isEmpty () && stack .peek ().matchEndTag (template , templateIndex )) {
70
- AbstractTag currentTag = stack .pop ();
71
- tagsList .add (currentTag );
69
+ if (!stack .isEmpty ()
70
+ && stack .peek ().matchEndTag (template , templateIndex )) {
72
71
73
- String arguments ;
74
- if (currentTag .getArgumentsEndIndex () != -1 ) { // we have body. case like this: <#list people as person > body </#list>
75
- arguments = template .substring (currentTag .getTagStartIndex () + currentTag .getTagStart ().length (), currentTag .getArgumentsEndIndex ());
76
- String body = template .substring (currentTag .getArgumentsEndIndex () + 1 , currentTag .getTagEndIndex ());
77
- currentTag .setBody (body );
72
+ AbstractTag currentTag = stack .pop ();
73
+ if (stack .size () == 0 ) {
74
+ tagsList .add (currentTag );
75
+ String arguments ;
76
+ if (currentTag .getArgumentsEndIndex () != -1 ) { // we have body. case like this: <#list people as person > body </#list>
77
+ arguments = template .substring (currentTag .getTagStartIndex () + currentTag .getTagStart ().length (), currentTag .getArgumentsEndIndex ());
78
+ String body = template .substring (currentTag .getArgumentsEndIndex () + 1 , currentTag .getTagEndIndex ());
79
+ currentTag .setBody (body );
80
+ } else {
81
+ // this is what is between the start and end tag in case there is no body:
82
+ ///<@blah arguments for blah />
83
+ arguments = template .substring (currentTag .getTagStartIndex () + currentTag .getTagStart ().length (), currentTag .getTagEndIndex ());
84
+ }
85
+ currentTag .setArguments (arguments );
78
86
} else {
79
- // this is what is between the start and end tag in case there is no body:
80
- ///<@blah arguments for blah />
81
- arguments = template .substring (currentTag .getTagStartIndex () + currentTag .getTagStart ().length (), currentTag .getTagEndIndex ());
87
+ break ;
82
88
}
83
- currentTag .setArguments (arguments );
84
89
}
85
90
}
86
91
}
87
92
88
- if (tagsList .size () == 0 ){ // assuming that this is just text, no funny business
93
+ if (tagsList .size () == 0 ) { // assuming that this is just text, no funny business
89
94
templateTokens .add (new StringToken (template ));
90
- }else {
95
+ } else {
91
96
//now we need to collect string chunks in between
92
97
for (int i = 0 ; i < tagsList .size (); i ++) {
93
98
AbstractTag currentTag = tagsList .get (i );
@@ -98,17 +103,17 @@ private void parse(String template) throws IllegalAccessException, Instantiation
98
103
}
99
104
100
105
// if there is a gap between current and previous
101
- if (tagsList .size () > 1 && i != 0 ){
102
- int startIndex = tagsList .get (i - 1 ).getTagEndIndex () + 1 ;
106
+ if (tagsList .size () > 1 && i != 0 ) {
107
+ int startIndex = tagsList .get (i - 1 ).getTagEndIndex () + tagsList . get ( i - 1 ). getMatchingEnd (). length () ;
103
108
int endIndex = currentTag .getTagStartIndex ();
104
- StringToken st = new StringToken (template .substring (startIndex , endIndex ));
109
+ StringToken st = new StringToken (template .substring (startIndex , endIndex ));
105
110
templateTokens .add (st );
106
111
}
107
112
108
113
templateTokens .add (currentTag );
109
114
110
115
// if this is the last tag and there is some text after
111
- if (i == (tagsList .size () - 1 ) && template .length () > currentTag .getTagEndIndex ()) {
116
+ if (i == (tagsList .size () - 1 ) && template .length () > currentTag .getTagEndIndex ()) {
112
117
StringToken st = new StringToken (template .substring (currentTag .getTagEndIndex () + currentTag .getMatchingEnd ().length ()));
113
118
templateTokens .add (st );
114
119
}
@@ -120,23 +125,13 @@ private void parse(String template) throws IllegalAccessException, Instantiation
120
125
}
121
126
}
122
127
123
- private static class Token {
124
- int index ;
125
- String token ;
126
-
127
- private Token (int index , String token ) {
128
- this .index = index ;
129
- this .token = token ;
130
- }
131
- }
132
-
133
-
134
128
public void process (Map values , Writer writer ) {
135
-
136
129
try {
137
130
for (TemplateToken token : templateTokens ) {
138
131
token .process (values , writer );
139
132
}
133
+ } catch (TemplateException e ) {
134
+ throw e ;
140
135
} catch (Exception e ) {
141
136
throw new TemplateException (e );
142
137
}
0 commit comments