Parsing JSP-like texts.
I have such a problem: there are text with JSP-like special tags in it:
<%! <some declaration> %>
<some text goes here><%= <some expression> %><another text goes here>
<% <some code> %>
<%! <declaration again> %>
<all the remaining text>
Declarations and code are written in special formal language, it's not Java. I want to construct program in it using algorithm similar with using by JSP container, here's the needed output:
<some declaration>
<declaration again>
out.print("<some text goes here>" + <some expression> +"<another text goes here>");
<some code>
out.print("<all the remaining text>");
I parse the text using finite automata, it has states indicating which tag we have met. The question is: is there any way to collect all declarations in the top of output file without using some temporary storage? With it the algorithm looks like not very effecient, because if we had two declaration areas separated with a little text it will be something like copying almost all file content into temporary storage (like StringBuffer) and then flushing it all to the output.

