33import java .io .BufferedReader ;
44import java .io .IOException ;
55import java .io .Reader ;
6- import java .util .LinkedList ;
76import java .util .List ;
87import java .util .concurrent .atomic .AtomicInteger ;
98
@@ -39,11 +38,10 @@ public Mustache compile(Reader reader, String file) {
3938 }
4039
4140 public Mustache compile (Reader reader , String file , String sm , String em ) {
42- Code [] codes = compile (reader , null , new AtomicInteger (0 ), file , sm , em ).toArray (new Code [0 ]);
43- return new DefaultMustache (cf , codes , file , sm , em );
41+ return compile (reader , null , new AtomicInteger (0 ), file , sm , em );
4442 }
4543
46- public List < Code > compile (final Reader reader , String tag , final AtomicInteger currentLine , String file , String sm , String em ) throws MustacheException {
44+ public Mustache compile (final Reader reader , String tag , final AtomicInteger currentLine , String file , String sm , String em ) throws MustacheException {
4745 if (reader == null ) {
4846 throw new MustacheException ("Reader is null" );
4947 }
@@ -53,7 +51,7 @@ public List<Code> compile(final Reader reader, String tag, final AtomicInteger c
5351 } else {
5452 br = new BufferedReader (reader );
5553 }
56- final List < Code > list = new LinkedList < Code > ();
54+ MustacheVisitor mv = cf . createMustacheVisitor ();
5755 // Now we grab the mustache template
5856 boolean onlywhitespace = true ;
5957 boolean iterable = currentLine .get () != 0 ;
@@ -71,7 +69,7 @@ public List<Code> compile(final Reader reader, String tag, final AtomicInteger c
7169 if (!iterable || (iterable && !onlywhitespace )) {
7270 out .append ("\n " );
7371 }
74- out = write (list , out , currentLine .intValue ());
72+ out = write (mv , out , currentLine .intValue ());
7573
7674 iterable = false ;
7775 onlywhitespace = true ;
@@ -107,24 +105,24 @@ public List<Code> compile(final Reader reader, String tag, final AtomicInteger c
107105 case '<' :
108106 case '$' : {
109107 int start = currentLine .get ();
110- final List < Code > codes = compile (br , variable , currentLine , file , sm , em );
108+ final Mustache mustache = compile (br , variable , currentLine , file , sm , em );
111109 int lines = currentLine .get () - start ;
112110 if (!onlywhitespace || lines == 0 ) {
113- write (list , out , currentLine .intValue ());
111+ write (mv , out , currentLine .intValue ());
114112 }
115113 out = new StringBuilder ();
116114 switch (ch ) {
117115 case '#' :
118- list . add ( cf . iterable (variable , codes , file , start , sm , em ) );
116+ mv . iterable (variable , mustache , file , start , sm , em );
119117 break ;
120118 case '^' :
121- list . add ( cf . notIterable (variable , codes , file , start , sm , em ) );
119+ mv . notIterable (variable , mustache , file , start , sm , em );
122120 break ;
123121 case '<' :
124- list . add ( cf . extend (variable , codes , file , start , sm , em ) );
122+ mv . extend (variable , mustache , file , start , sm , em );
125123 break ;
126124 case '$' :
127- list . add ( cf . name (variable , codes , file , start , sm , em ) );
125+ mv . name (variable , mustache , file , start , sm , em );
128126 break ;
129127 }
130128 iterable = lines != 0 ;
@@ -133,22 +131,22 @@ public List<Code> compile(final Reader reader, String tag, final AtomicInteger c
133131 case '/' : {
134132 // Tag end
135133 if (!onlywhitespace ) {
136- write (list , out , currentLine .intValue ());
134+ write (mv , out , currentLine .intValue ());
137135 }
138136 if (!variable .equals (tag )) {
139137 throw new MustacheException (
140138 "Mismatched start/end tags: " + tag + " != " + variable + " in " + file + ":" + currentLine );
141139 }
142140
143- return list ;
141+ return mv . mustache ( file , sm , em ) ;
144142 }
145143 case '>' : {
146- out = write (list , out , currentLine .intValue ());
147- list . add ( cf . partial (variable , file , currentLine .get (), sm , em ) );
144+ out = write (mv , out , currentLine .intValue ());
145+ mv . partial (variable , file , currentLine .get (), sm , em );
148146 break ;
149147 }
150148 case '{' : {
151- out = write (list , out , currentLine .intValue ());
149+ out = write (mv , out , currentLine .intValue ());
152150 // Not escaped
153151 String name = variable ;
154152 if (em .charAt (1 ) != '}' ) {
@@ -160,26 +158,26 @@ public List<Code> compile(final Reader reader, String tag, final AtomicInteger c
160158 }
161159 }
162160 final String finalName = name ;
163- list . add ( cf . value (finalName , false , currentLine .intValue (), sm , em ) );
161+ mv . value (finalName , false , currentLine .intValue (), sm , em );
164162 break ;
165163 }
166164 case '&' : {
167165 // Not escaped
168- out = write (list , out , currentLine .intValue ());
169- list . add ( cf . value (variable , false , currentLine .intValue (), sm , em ) );
166+ out = write (mv , out , currentLine .intValue ());
167+ mv . value (variable , false , currentLine .intValue (), sm , em );
170168 break ;
171169 }
172170 case '%' :
173171 // Pragmas
174- out = write (list , out , currentLine .intValue ());
172+ out = write (mv , out , currentLine .intValue ());
175173 break ;
176174 case '!' :
177175 // Comment
178- out = write (list , out , currentLine .intValue ());
176+ out = write (mv , out , currentLine .intValue ());
179177 break ;
180178 case '=' :
181179 // Change delimiters
182- out = write (list , out , currentLine .intValue ());
180+ out = write (mv , out , currentLine .intValue ());
183181 String delimiters = command .replaceAll ("\\ s+" , "" );
184182 int length = delimiters .length ();
185183 if (length > 6 || length / 2 * 2 != length ) {
@@ -194,8 +192,8 @@ public List<Code> compile(final Reader reader, String tag, final AtomicInteger c
194192 "Improperly closed variable in " + file + ":" + currentLine );
195193 }
196194 // Reference
197- out = write (list , out , currentLine .intValue ());
198- list . add ( cf . value (command .trim (), true , currentLine .intValue (), sm , em ) );
195+ out = write (mv , out , currentLine .intValue ());
196+ mv . value (command .trim (), true , currentLine .intValue (), sm , em );
199197 break ;
200198 }
201199 }
@@ -208,29 +206,21 @@ public List<Code> compile(final Reader reader, String tag, final AtomicInteger c
208206 onlywhitespace = onlywhitespace && (c == ' ' || c == '\t' || c == '\r' );
209207 out .append ((char ) c );
210208 }
211- write (list , out , currentLine .intValue ());
209+ write (mv , out , currentLine .intValue ());
212210 br .close ();
213211 } catch (IOException e ) {
214212 throw new MustacheException ("Failed to read" , e );
215213 }
216- list . add ( cf . eof (currentLine .intValue () ));
217- return list ;
214+ mv . eof (currentLine .intValue ());
215+ return mv . mustache ( file , sm , em ) ;
218216 }
219217
220218 /**
221219 * Ignore empty strings and append to the previous code if it was also a write.
222220 */
223- private StringBuilder write (List < Code > list , StringBuilder out , int line ) {
221+ private StringBuilder write (MustacheVisitor mv , StringBuilder out , int line ) {
224222 String text = out .toString ();
225- if (text .length () > 0 ) {
226- int size = list .size ();
227- if (size > 0 ) {
228- Code code = list .get (size - 1 );
229- code .append (text );
230- } else {
231- list .add (cf .write (text , line , null , null ));
232- }
233- }
223+ mv .write (text , line , null , null );
234224 return new StringBuilder ();
235225 }
236226
0 commit comments