Question: Write a program called HTMLSummarizer.java that reads in an HTML file and outputs the corresponding outline representation. Your HTMLSummarizer class must implement the provided Tester

Write a program called HTMLSummarizer.java that reads in an HTML file and outputs the corresponding outline representation. Your HTMLSummarizer class must implement the provided Tester interface and also contain the main() method where your program starts running. This is because your program will be tested via this interface. The interface contains a single method:

public ArrayList compute( Scanner input );

This method must perform the required computation.

Input

The method takes a Scanner object, which contains a valid HTML file. You may assume that every start tag is properly matched with an end tag, and that there are no errors in the input.

Hint: Use the provided HTMLScanner object to easily parse the input by using its two methods: hasNextTag() and nextTag().

Semantics

The HTML file will contain 0 or more tags.

The depth of a void tag or an element is the number of elements that surround it. For example, the depth of element b in Figure 1 is 3.

An element or void tag is a child of another element, if it is contained in that element and is of depth one greater than the element. For example, in Figure 1, b is a child of p, but is not a child of body. Whereas, p, hr, p, and h1 are all children of body. All tags whose names begins with a ! are to be ignored. For example, tags of the form

and " +

"

Bold Example: bold

" +

"" +

"";

private static String [] testOutput6 = { "html 1",

" body 4",

" h1 0",

" p 2",

" hr",

" hr",

" br",

" p 1",

" b 0" };

private static Scanner mkTest( String input ) {

return new Scanner( input );

}

private static ArrayList mkOutput( String [] output ) {

ArrayList al = new ArrayList();

for( String s : output ) {

al.add( s );

}

return al;

}

private static boolean doTest( String input, String [] output ) {

Tester t = new HTMLSummarizer();

ArrayList al = t.compute( mkTest( input ) );

System.out.println( "Input: " );

System.out.println( input );

System.out.println( "Generated output" );

for( String s : al ) {

System.out.println( s );

}

System.out.println( "Expected output" );

for( String s : output ) {

System.out.println( s );

}

System.out.println( "---------------------------------------------------" );

return al != null && al.equals( mkOutput( output ) );

}

@Test

void testEmpty() {

assertTrue( doTest( testInput0, testOutput0 ), "Empty Test" );

}

@Test

void test1() {

assertTrue( doTest( testInput1, testOutput1 ), "Single element tag" );

}

@Test

void test2() {

assertTrue( doTest( testInput2, testOutput2 ), "Single void tag" );

}

@Test

void test3() {

assertTrue( doTest( testInput3, testOutput3 ), "Single nesting test" );

}

@Test

void test4() {

assertTrue( doTest( testInput4, testOutput4 ), "Multiple nestings" );

}

@Test

void test5() {

assertTrue( doTest( testInput5, testOutput5 ),

"Multiple nestings and levels" );

}

@Test

void test6() {

assertTrue( doTest( testInput6, testOutput6 ), "Assignment example" );

}

}

Sample Input Sample Output htm> html 1 tml> body> hl>My First Heading p My first paragraph body 4 h1 0 hr hr Another ruler ... o more rulers b 0 LI should be ignored --> p>Bold Example: bold

k/body> html> Sample Input Sample Output htm> html 1 tml> body> hl>My First Heading p My first paragraph body 4 h1 0 hr hr Another ruler ... o more rulers b 0 LI should be ignored --> p>Bold Example: bold

k/body> html>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!