Problem Statement
JavaServer Pages(JSP) provide a way to generate dynamic web pages for clients. In its simplest incarnation, a JSP allows the developer to mix static HTML with dynamic Java code which is run on the server. In this problem, your task is to build a simplified parser that will split a JSP into two parts - HTML and Java. Your method should then return the HTML part only.
For the purposes of this problem, we will only consider one type of embedded Java code. This code will be set off from the HTML portions of the JSP by <% and %>. The former will indicate the opening of a block of code, and the latter will indicate the closing of a block of code.
Your task is to find all of the blocks of code within a JSP, which will be given to you as a String[], each element of which represents a single line. You should remove all of the code sections, starting with <% and ending with %>. If this generates any empty lines, you should remove those, but you should never concatenate lines (see example 1). A line containing only spaces is not considered empty. Your method should return the result as a String[], with the code removed.
For the purposes of this problem, we will only consider one type of embedded Java code. This code will be set off from the HTML portions of the JSP by <% and %>. The former will indicate the opening of a block of code, and the latter will indicate the closing of a block of code.
Your task is to find all of the blocks of code within a JSP, which will be given to you as a String[], each element of which represents a single line. You should remove all of the code sections, starting with <% and ending with %>. If this generates any empty lines, you should remove those, but you should never concatenate lines (see example 1). A line containing only spaces is not considered empty. Your method should return the result as a String[], with the code removed.
Definition
- Class:
- CodeRemover
- Method:
- remove
- Parameters:
- String[]
- Returns:
- String[]
- Method signature:
- String[] remove(String[] jsp)
- (be sure your method is public)
Notes
- Sections of code may span multiple lines.
- In order for <% to open a block of code, both characters must be on the same line. Hence, if < is at the end of one line, and % is at the beginning of the next line, that does not open a block of code. The same thing goes for %>
Constraints
- jsp will contain between 1 and 50 elements, inclusive.
- Each element of jsp will contain between 0 and 50 characters, inclusive.
- Each character of jsp will have ASCII value between 32 and 126, inclusive.
- jsp will be well formed, meaning that every <% has a corresponding %>.
Examples
{"<HTML><HEAD><TITLE> <%= title %> </TITLE></HEAD>", "<IMG SRC='img'>", "<BODY> <%", " for(int i = 0; i<100; i++)", " System.out.print(abc);", " for(int i = 0; i<100; i++)", " System.out.print(def);", "%></BODY></HTML>"}Returns: { "<HTML><HEAD><TITLE> </TITLE></HEAD>", "<IMG SRC='img'>", "<BODY> ", "</BODY></HTML>" }There are two chunks of code in this example that get removed. The first is opened and closed on the first line, between the TITLE tags. The second is opened on the third line, and doesn't end until the last line. After removing the code, we end up with 4 blank lines, which we remove.{"''<%''%>'<%'", "<% 'abc'", "'%>'%>xyz"}Returns: { "'''", "'%>xyz" }Here is a tricky example{"<","% yes %>","ok"}Returns: { "<", "% yes %>", "ok" }{"<%","% yes %%>","ok"}Returns: { "ok" }{"<","%","%",">"}Returns: { "<", "%", "%", ">" }{"<%<%%> '<%%>' %> <% %> '<% %>'"}Returns: { " '' %> ''" }{"","","",""}Returns: { }{" "}Returns: { " " }{"'<%%>'"}Returns: { "''" }{"<><%%><%%>%>"}Returns: { "<>%>" }